Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Add option for initial probe delay, timeout, and failure threshholds #1246

Merged
merged 6 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1246.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
plugin/k8s: Add new probe configuration options
```
85 changes: 75 additions & 10 deletions builtin/k8s/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ func (p *Platform) Deploy(
// assume the first port defined is the 'main' port to use
defaultPort := int(containerPorts[0].ContainerPort)

initialDelaySeconds := int32(5)
timeoutSeconds := int32(5)
failureThreshold := int32(5)
if p.config.Probe != nil {
if p.config.Probe.InitialDelaySeconds != 0 {
initialDelaySeconds = int32(p.config.Probe.InitialDelaySeconds)
}
if p.config.Probe.TimeoutSeconds != 0 {
timeoutSeconds = int32(p.config.Probe.TimeoutSeconds)
}
if p.config.Probe.FailureThreshold != 0 {
failureThreshold = int32(p.config.Probe.FailureThreshold)
}
}

// Update the deployment with our spec
deployment.Spec.Template.Spec = corev1.PodSpec{
Containers: []corev1.Container{
Expand All @@ -262,18 +277,18 @@ func (p *Platform) Deploy(
Port: intstr.FromInt(defaultPort),
},
},
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
FailureThreshold: 5,
InitialDelaySeconds: initialDelaySeconds,
TimeoutSeconds: timeoutSeconds,
FailureThreshold: failureThreshold,
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(defaultPort),
},
},
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
InitialDelaySeconds: initialDelaySeconds,
TimeoutSeconds: timeoutSeconds,
},
Env: env,
Resources: resourceRequirements,
Expand All @@ -290,9 +305,9 @@ func (p *Platform) Deploy(
Port: intstr.FromInt(defaultPort),
},
},
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
FailureThreshold: 5,
InitialDelaySeconds: initialDelaySeconds,
TimeoutSeconds: timeoutSeconds,
FailureThreshold: failureThreshold,
}

deployment.Spec.Template.Spec.Containers[0].ReadinessProbe = &corev1.Probe{
Expand All @@ -302,8 +317,8 @@ func (p *Platform) Deploy(
Port: intstr.FromInt(defaultPort),
},
},
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
InitialDelaySeconds: initialDelaySeconds,
TimeoutSeconds: timeoutSeconds,
}
}

Expand Down Expand Up @@ -547,6 +562,9 @@ type Config struct {
// made to the port.
ProbePath string `hcl:"probe_path,optional"`

// Probe details for describing a health check to be performed against a container.
Probe *Probe `hcl:"probe,block"`

// Optionally define various resources limits for kubernetes pod containers
// such as memory and cpu.
Resources map[string]string `hcl:"resources,optional"`
Expand All @@ -571,6 +589,23 @@ type Config struct {
StaticEnvVars map[string]string `hcl:"static_environment,optional"`
}

// Probe describes a health check to be performed against a container to determine whether it is
// alive or ready to receive traffic.
type Probe struct {
// Time in seconds to wait before performing the initial liveness and readiness probes.
// Defaults to 5 seconds.
InitialDelaySeconds uint `hcl:"initial_delay,optional"`

// Time in seconds before the probe fails.
// Defaults to 5 seconds.
TimeoutSeconds uint `hcl:"timeout,optional"`

// Number of times a liveness probe can fail before the container is killed.
// FailureThreshold * TimeoutSeconds should be long enough to cover your worst
// case startup times. Defaults to 5 failures.
FailureThreshold uint `hcl:"failure_threshold,optional"`
}

func (p *Platform) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&Config{}), docs.FromFunc(p.DeployFunc()))
if err != nil {
Expand Down Expand Up @@ -636,6 +671,36 @@ deploy "kubernetes" {
),
)

doc.SetField(
"probe",
"configuration to control liveness and readiness probes",
docs.Summary("Probe describes a health check to be performed against a ",
"container to determine whether it is alive or ready to receive traffic."),
docs.SubFields(func(doc *docs.SubFieldDoc) {
doc.SetField(
"initial_delay",
"time in seconds to wait before performing the initial liveness and readiness probes",
docs.Default("5"),
)

doc.SetField(
"timeout",
"time in seconds before the probe fails",
docs.Default("5"),
)

doc.SetField(
"failure_threshold",
"number of times a liveness probe can fail before the container is killed",
docs.Summary(
"failureThreshold * TimeoutSeconds should be long enough to cover your worst case startup times",
),
docs.Default("5"),
)

}),
)

doc.SetField(
"scratch_path",
"a path for the service to store temporary data",
Expand Down