Skip to content

Commit

Permalink
feat: use operator built in probe script
Browse files Browse the repository at this point in the history
Signed-off-by: drivebyer <[email protected]>
  • Loading branch information
drivebyer committed Apr 1, 2024
1 parent 41fd164 commit f405307
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions k8sutils/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ func generateContainerDef(name string, containerParams containerParameters, clus
containerParams.Port,
clusterVersion,
),
ReadinessProbe: getProbeInfo(containerParams.ReadinessProbe),
LivenessProbe: getProbeInfo(containerParams.LivenessProbe),
ReadinessProbe: getProbeInfo(containerParams.ReadinessProbe, containerParams.Role),
LivenessProbe: getProbeInfo(containerParams.LivenessProbe, containerParams.Role),

Check warning on line 367 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L366-L367

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

var healthCheckScript = `
#!/bin/bash
health_check() {
if [[ -n "${REDIS_PASSWORD}" ]]; then
export REDISCLI_AUTH="${REDIS_PASSWORD}"
fi
if [[ "${TLS_MODE}" == "true" ]]; then
redis-cli --tls --cert "${REDIS_TLS_CERT}" --key "${REDIS_TLS_CERT_KEY}" --cacert "${REDIS_TLS_CA_KEY}" -h "$(hostname)" -p "${REDIS_PORT}" ping
else
redis-cli -h "$(hostname)" -p "${REDIS_PORT}" ping
fi
}
health_check
`

var sentinelHealthCheckScript = `
#!/bin/bash
health_check() {
if [[ -n "${REDIS_PASSWORD}" ]]; then
export REDISCLI_AUTH="${REDIS_PASSWORD}"
fi
if [[ "${TLS_MODE}" == "true" ]]; then
redis-cli --tls --cert "${REDIS_TLS_CERT}" --key "${REDIS_TLS_CERT_KEY}" --cacert "${REDIS_TLS_CA_KEY}" -h "$(hostname)" -p "${SENTINEL_PORT}" ping
else
redis-cli -h "$(hostname)" -p "${SENTINEL_PORT}" ping
fi
}
health_check
`

// getProbeInfo generate probe for Redis StatefulSet
func getProbeInfo(probe *commonapi.Probe) *corev1.Probe {
return &corev1.Probe{
func getProbeInfo(probe *commonapi.Probe, role string) *corev1.Probe {
ret := &corev1.Probe{

Check warning on line 629 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L628-L629

Added lines #L628 - L629 were not covered by tests
InitialDelaySeconds: probe.InitialDelaySeconds,
PeriodSeconds: probe.PeriodSeconds,
FailureThreshold: probe.FailureThreshold,
Expand All @@ -602,11 +636,20 @@ func getProbeInfo(probe *commonapi.Probe) *corev1.Probe {
Exec: &corev1.ExecAction{
Command: []string{
"bash",
"/usr/bin/healthcheck.sh",
"-c",
healthCheckScript,

Check warning on line 640 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L639-L640

Added lines #L639 - L640 were not covered by tests
},
},
},
}
if role == "sentinel" {
ret.ProbeHandler.Exec.Command = []string{
"bash",
"-c",
sentinelHealthCheckScript,

Check warning on line 649 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L645-L649

Added lines #L645 - L649 were not covered by tests
}
}
return ret

Check warning on line 652 in k8sutils/statefulset.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/statefulset.go#L652

Added line #L652 was not covered by tests
}

// getEnvironmentVariables returns all the required Environment Variables
Expand Down

0 comments on commit f405307

Please sign in to comment.