Skip to content

Commit

Permalink
add liveness probe to nginx (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverMKing authored Apr 1, 2024
1 parent ef0b85a commit ed8800d
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pkg/manifests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func withTypicalReadinessProbe(port int, contain *corev1.Container) *corev1.Cont
return copy
}

func withLivenessProbeMatchingReadinessNewFailureThresh(contain *corev1.Container, failureThresh int32) *corev1.Container {
copy := withLivenessProbeMatchingReadiness(contain)

if copy != nil && copy.LivenessProbe != nil {
copy.LivenessProbe.FailureThreshold = failureThresh
}

return copy
}

func withLivenessProbeMatchingReadiness(contain *corev1.Container) *corev1.Container {
copy := contain.DeepCopy()
copy.LivenessProbe = copy.ReadinessProbe.DeepCopy()
Expand Down
92 changes: 92 additions & 0 deletions pkg/manifests/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,98 @@ func TestHasTopLevelLabels(t *testing.T) {
}
}

func TestWithLivenessProbeMatchingReadinessNewFailureThresh(t *testing.T) {
cases := []struct {
name string
inputContainer *corev1.Container
failureThresh int32
expected corev1.Container
}{
{
name: "empty readiness",
inputContainer: &corev1.Container{
Name: "name",
},
failureThresh: 2,
expected: corev1.Container{
Name: "name",
},
},
{
name: "new failure thresh",
inputContainer: &corev1.Container{
Name: "name",
ReadinessProbe: &corev1.Probe{
FailureThreshold: 2,
},
},
failureThresh: 10,
expected: corev1.Container{
Name: "name",
ReadinessProbe: &corev1.Probe{
FailureThreshold: 2,
},
LivenessProbe: &corev1.Probe{
FailureThreshold: 10,
},
},
},
{
name: "new failure thresh with other fields",
inputContainer: &corev1.Container{
Name: "name",
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
},
},
FailureThreshold: 2,
SuccessThreshold: 1,
TimeoutSeconds: 2,
PeriodSeconds: 12,
InitialDelaySeconds: 3,
},
},
failureThresh: 200,
expected: corev1.Container{
Name: "name",
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
},
},
FailureThreshold: 2,
SuccessThreshold: 1,
TimeoutSeconds: 2,
PeriodSeconds: 12,
InitialDelaySeconds: 3,
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
},
},
FailureThreshold: 200,
SuccessThreshold: 1,
TimeoutSeconds: 2,
PeriodSeconds: 12,
InitialDelaySeconds: 3,
},
},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := withLivenessProbeMatchingReadinessNewFailureThresh(c.inputContainer, c.failureThresh)
require.Equal(t, c.expected, *got)
})
}
}

func TestGetOwnerRefs(t *testing.T) {
cases := []struct {
Name string
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/full-with-replicas.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/full-with-target-cpu.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/internal-with-ssl-cert.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/kube-system.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/no-ownership.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
12 changes: 12 additions & 0 deletions pkg/manifests/fixtures/nginx/optional-features-disabled.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,18 @@
"memory": "127Mi"
}
},
"livenessProbe": {
"httpGet": {
"path": "/healthz",
"port": 10254,
"scheme": "HTTP"
},
"initialDelaySeconds": 10,
"timeoutSeconds": 1,
"periodSeconds": 5,
"successThreshold": 1,
"failureThreshold": 6
},
"readinessProbe": {
"httpGet": {
"path": "/healthz",
Expand Down
4 changes: 2 additions & 2 deletions pkg/manifests/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func newNginxIngressControllerDeployment(conf *config.Config, ingressConfig *Ngi
},
},
ServiceAccountName: ingressConfig.ResourceName,
Containers: []corev1.Container{*withPodRefEnvVars(withTypicalReadinessProbe(10254, &corev1.Container{
Containers: []corev1.Container{*withPodRefEnvVars(withLivenessProbeMatchingReadinessNewFailureThresh(withTypicalReadinessProbe(10254, &corev1.Container{
Name: "controller",
Image: path.Join(conf.Registry, "/oss/kubernetes/ingress/nginx-ingress-controller:"+controllerImageTag),
Args: deploymentArgs,
Expand All @@ -499,7 +499,7 @@ func newNginxIngressControllerDeployment(conf *config.Config, ingressConfig *Ngi
corev1.ResourceMemory: resource.MustParse("127Mi"),
},
},
}))},
}), 6))},
}),
},
},
Expand Down

0 comments on commit ed8800d

Please sign in to comment.