Skip to content

Commit

Permalink
Probe use built-in, discarded healthcheck.sh
Browse files Browse the repository at this point in the history
Signed-off-by: muicoder <[email protected]>
https://github.com/redis/redis/blob/unstable/TLS.md
The probe uses env mode to load the TLS certificate.
  • Loading branch information
muicoder committed Dec 26, 2023
1 parent 8f789aa commit 7438363
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
50 changes: 43 additions & 7 deletions k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ func generateContainerDef(name string, containerParams containerParameters, clus
containerParams.EnvVars,
containerParams.Port,
),
ReadinessProbe: getProbeInfo(containerParams.ReadinessProbe),
LivenessProbe: getProbeInfo(containerParams.LivenessProbe),
ReadinessProbe: getProbeInfo(containerParams, "R"),
LivenessProbe: getProbeInfo(containerParams, "L"),

Check warning on line 366 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L365-L366

Added lines #L365 - L366 were not covered by tests
VolumeMounts: getVolumeMount(name, containerParams.PersistenceEnabled, clusterMode, nodeConfVolume, externalConfig, mountpath, containerParams.TLSConfig, containerParams.ACLConfig),
},
}
Expand Down Expand Up @@ -573,7 +573,35 @@ func getVolumeMount(name string, persistenceEnabled *bool, clusterMode bool, nod
}

// getProbeInfo generate probe for Redis StatefulSet
func getProbeInfo(probe *commonapi.Probe) *corev1.Probe {
func getProbeInfo(params containerParameters, probeType string) *corev1.Probe {
probePort := redisPort
if params.Role == "sentinel" {
probePort = sentinelPort
}

Check warning on line 580 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L576-L580

Added lines #L576 - L580 were not covered by tests

probeCommand := []string{
"redis-cli", "-p", strconv.Itoa(probePort),
}

if params.TLSConfig != nil {
probeCommand = append(probeCommand, "--tls")
if params.TLSConfig.CaKeyFile != "" {
probeCommand = append(probeCommand, "--cacert", "$(REDIS_TLS_CA_KEY)")
}
probeCommand = append(probeCommand, "--cert", "$(REDIS_TLS_CERT)", "--key", "$(REDIS_TLS_CERT_KEY)")

Check warning on line 591 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L582-L591

Added lines #L582 - L591 were not covered by tests
}
probeCommand = append(probeCommand, "ping")

var probe *commonapi.Probe
switch probeType {
case "R":
probe = params.ReadinessProbe
case "L":
probe = params.LivenessProbe
default:
probe = params.LivenessProbe

Check warning on line 602 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L593-L602

Added lines #L593 - L602 were not covered by tests
}

return &corev1.Probe{
InitialDelaySeconds: probe.InitialDelaySeconds,
PeriodSeconds: probe.PeriodSeconds,
Expand All @@ -582,10 +610,7 @@ func getProbeInfo(probe *commonapi.Probe) *corev1.Probe {
SuccessThreshold: probe.SuccessThreshold,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"bash",
"/usr/bin/healthcheck.sh",
},
Command: probeCommand,

Check warning on line 613 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L613

Added line #L613 was not covered by tests
},
},
}
Expand Down Expand Up @@ -645,6 +670,17 @@ func getEnvironmentVariables(role string, enabledPassword *bool, secretName *str
},
},
})
envVars = append(envVars, corev1.EnvVar{
Name: "REDISCLI_AUTH",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: *secretName,
},
Key: *secretKey,
},
},
})
}
if persistenceEnabled != nil && *persistenceEnabled {
envVars = append(envVars, corev1.EnvVar{Name: "PERSISTENCE_ENABLED", Value: "true"})
Expand Down
16 changes: 16 additions & 0 deletions k8sutils/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ func TestGetEnvironmentVariables(t *testing.T) {
Key: "test-key",
},
}},
{Name: "REDISCLI_AUTH", ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-secret",
},
Key: "test-key",
},
}},
{Name: "SERVER_MODE", Value: "sentinel"},
{Name: "SETUP_MODE", Value: "sentinel"},
{Name: "TEST_ENV", Value: "test-value"},
Expand Down Expand Up @@ -336,6 +344,14 @@ func TestGetEnvironmentVariables(t *testing.T) {
Key: "test-key",
},
}},
{Name: "REDISCLI_AUTH", ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-secret",
},
Key: "test-key",
},
}},
{Name: "SERVER_MODE", Value: "cluster"},
{Name: "SETUP_MODE", Value: "cluster"},
{Name: "TEST_ENV", Value: "test-value"},
Expand Down

0 comments on commit 7438363

Please sign in to comment.