Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix liveness and readiness probes #396

Merged
merged 5 commits into from
Aug 8, 2022
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
90 changes: 45 additions & 45 deletions controllers/etcd_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/util/retry"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -832,11 +833,6 @@ func validateEtcdWithDefaults(instance *druidv1alpha1.Etcd, s *appsv1.StatefulSe
// Validate TLS. Ensure that enableTLS flag is not triggered in the go-template
Expect(instance.Spec.Etcd.PeerUrlTLS).To(BeNil())

readinessProbeUrl := fmt.Sprintf("http://%s-local:%d/health", instance.Name, clientPort)
if int(instance.Spec.Replicas) == 1 {
readinessProbeUrl = fmt.Sprintf("http://%s-local:%d/healthz", instance.Name, backupPort)
}

Expect(config).To(MatchKeys(IgnoreExtras, Keys{
"name": Equal(fmt.Sprintf("etcd-%s", instance.UID[:6])),
"data-dir": Equal("/var/etcd/data/new.etcd"),
Expand Down Expand Up @@ -989,33 +985,42 @@ func validateEtcdWithDefaults(instance *druidv1alpha1.Etcd, s *appsv1.StatefulSe
}),
"ReadinessProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/usr/bin/curl": Equal("/usr/bin/curl"),
readinessProbeUrl: Equal(readinessProbeUrl),
}),
"HTTPGet": PointTo(MatchFields(IgnoreExtras, Fields{
"Path": Equal("/healthz"),
"Port": Equal(intstr.FromInt(int(backupPort))),
"Scheme": Equal(corev1.URISchemeHTTP),
})),
}),
"InitialDelaySeconds": Equal(int32(15)),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(5)),
})),
"LivenessProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
"ETCDCTL_API=3": Equal("ETCDCTL_API=3"),
"etcdctl": Equal("etcdctl"),
fmt.Sprintf("--endpoints=http://%s-local:%d", instance.Name, clientPort): Equal(fmt.Sprintf("--endpoints=http://%s-local:%d", instance.Name, clientPort)),
"get": Equal("get"),
"foo": Equal("foo"),
"--consistency=s": Equal("--consistency=s"),
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=http://%s-local:%d get foo --consistency=s", instance.Name, clientPort): Equal(fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=http://%s-local:%d get foo --consistency=s", instance.Name, clientPort)),
}),
})),
}),
"InitialDelaySeconds": Equal(int32(15)),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(5)),
})),
"StartupProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=http://%s-local:%d get foo --consistency=s", instance.Name, clientPort): Equal(fmt.Sprintf("ETCDCTL_API=3 etcdctl --endpoints=http://%s-local:%d get foo --consistency=s", instance.Name, clientPort)),
}),
})),
}),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(24)),
})),
"VolumeMounts": MatchAllElements(volumeMountIterator, Elements{
instance.Name: MatchFields(IgnoreExtras, Fields{
Expand Down Expand Up @@ -1170,11 +1175,6 @@ func validateEtcd(instance *druidv1alpha1.Etcd, s *appsv1.StatefulSet, cm *corev
store, err := utils.StorageProviderFromInfraProvider(instance.Spec.Backup.Store.Provider)
Expect(err).NotTo(HaveOccurred())

readinessProbeUrl := fmt.Sprintf("https://%s-local:%d/health", instance.Name, clientPort)
if int(instance.Spec.Replicas) == 1 {
readinessProbeUrl = fmt.Sprintf("https://%s-local:%d/healthz", instance.Name, backupPort)
}

Expect(*cm).To(MatchFields(IgnoreExtras, Fields{
"ObjectMeta": MatchFields(IgnoreExtras, Fields{
"Name": Equal(fmt.Sprintf("etcd-bootstrap-%s", string(instance.UID[:6]))),
Expand Down Expand Up @@ -1371,42 +1371,42 @@ func validateEtcd(instance *druidv1alpha1.Etcd, s *appsv1.StatefulSet, cm *corev
}),
"ReadinessProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/usr/bin/curl": Equal("/usr/bin/curl"),
"--cert": Equal("--cert"),
"/var/etcd/ssl/client/client/tls.crt": Equal("/var/etcd/ssl/client/client/tls.crt"),
"--key": Equal("--key"),
"/var/etcd/ssl/client/client/tls.key": Equal("/var/etcd/ssl/client/client/tls.key"),
"--cacert": Equal("--cacert"),
"/var/etcd/ssl/client/ca/ca.crt": Equal("/var/etcd/ssl/client/ca/ca.crt"),
readinessProbeUrl: Equal(readinessProbeUrl),
}),
"HTTPGet": PointTo(MatchFields(IgnoreExtras, Fields{
"Path": Equal("/healthz"),
"Port": Equal(intstr.FromInt(int(backupPort))),
"Scheme": Equal(corev1.URISchemeHTTPS),
})),
}),
"InitialDelaySeconds": Equal(int32(15)),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(5)),
})),
"LivenessProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
"ETCDCTL_API=3": Equal("ETCDCTL_API=3"),
"etcdctl": Equal("etcdctl"),
"--cert=/var/etcd/ssl/client/client/tls.crt": Equal("--cert=/var/etcd/ssl/client/client/tls.crt"),
"--key=/var/etcd/ssl/client/client/tls.key": Equal("--key=/var/etcd/ssl/client/client/tls.key"),
"--cacert=/var/etcd/ssl/client/ca/ca.crt": Equal("--cacert=/var/etcd/ssl/client/ca/ca.crt"),
fmt.Sprintf("--endpoints=https://%s-local:%d", instance.Name, clientPort): Equal(fmt.Sprintf("--endpoints=https://%s-local:%d", instance.Name, clientPort)),
"get": Equal("get"),
"foo": Equal("foo"),
"--consistency=s": Equal("--consistency=s"),
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
fmt.Sprintf("ETCDCTL_API=3 etcdctl --cacert=/var/etcd/ssl/client/ca/ca.crt --cert=/var/etcd/ssl/client/client/tls.crt --key=/var/etcd/ssl/client/client/tls.key --endpoints=https://%s-local:%d get foo --consistency=s", instance.Name, clientPort): Equal(fmt.Sprintf("ETCDCTL_API=3 etcdctl --cacert=/var/etcd/ssl/client/ca/ca.crt --cert=/var/etcd/ssl/client/client/tls.crt --key=/var/etcd/ssl/client/client/tls.key --endpoints=https://%s-local:%d get foo --consistency=s", instance.Name, clientPort)),
}),
})),
}),
"InitialDelaySeconds": Equal(int32(15)),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(5)),
})),
"StartupProbe": PointTo(MatchFields(IgnoreExtras, Fields{
"Handler": MatchFields(IgnoreExtras, Fields{
"Exec": PointTo(MatchFields(IgnoreExtras, Fields{
"Command": MatchAllElements(cmdIterator, Elements{
"/bin/sh": Equal("/bin/sh"),
"-ec": Equal("-ec"),
fmt.Sprintf("ETCDCTL_API=3 etcdctl --cacert=/var/etcd/ssl/client/ca/ca.crt --cert=/var/etcd/ssl/client/client/tls.crt --key=/var/etcd/ssl/client/client/tls.key --endpoints=https://%s-local:%d get foo --consistency=s", instance.Name, clientPort): Equal(fmt.Sprintf("ETCDCTL_API=3 etcdctl --cacert=/var/etcd/ssl/client/ca/ca.crt --cert=/var/etcd/ssl/client/client/tls.crt --key=/var/etcd/ssl/client/client/tls.key --endpoints=https://%s-local:%d get foo --consistency=s", instance.Name, clientPort)),
}),
})),
}),
"PeriodSeconds": Equal(int32(5)),
"FailureThreshold": Equal(int32(24)),
})),
"VolumeMounts": MatchAllElements(volumeMountIterator, Elements{
*instance.Spec.VolumeClaimTemplate: MatchFields(IgnoreExtras, Fields{
Expand Down
9 changes: 8 additions & 1 deletion hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ if ! command -v setup-envtest &> /dev/null ; then
exit 1
fi

ARCH=
# if using M1 macbook, use amd64 architecture build, as suggested in
# https://github.com/kubernetes-sigs/controller-runtime/issues/1657#issuecomment-988484517
if [[ $(uname) == 'Darwin' && $(uname -m) == 'arm64' ]]; then
ARCH='--arch=amd64'
fi

# --use-env allows overwriting the envtest tools path via the KUBEBUILDER_ASSETS env var just like it was before
export KUBEBUILDER_ASSETS="$(setup-envtest use --use-env -p path ${ENVTEST_K8S_VERSION})"
export KUBEBUILDER_ASSETS="$(setup-envtest ${ARCH} use --use-env -p path ${ENVTEST_K8S_VERSION})"
echo "using envtest tools installed at '$KUBEBUILDER_ASSETS'"

echo "> Tests"
Expand Down
85 changes: 61 additions & 24 deletions pkg/component/etcd/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -174,13 +174,13 @@ func (c *component) syncStatefulset(ctx context.Context, sts *appsv1.StatefulSet
Selector: &metav1.LabelSelector{
MatchLabels: getCommonLabels(&c.values),
},
Template: v1.PodTemplateSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: c.values.Annotations,
Labels: sts.GetLabels(),
},
Spec: v1.PodSpec{
HostAliases: []v1.HostAlias{
Spec: corev1.PodSpec{
HostAliases: []corev1.HostAlias{
{
IP: "127.0.0.1",
Hostnames: []string{c.values.Name + "-local"},
Expand All @@ -189,32 +189,37 @@ func (c *component) syncStatefulset(ctx context.Context, sts *appsv1.StatefulSet
ServiceAccountName: c.values.ServiceAccountName,
Affinity: c.values.Affinity,
TopologySpreadConstraints: c.values.TopologySpreadConstraints,
Containers: []v1.Container{
Containers: []corev1.Container{
{
Name: "etcd",
Image: c.values.EtcdImage,
ImagePullPolicy: v1.PullIfNotPresent,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: c.values.EtcdCommand,
ReadinessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: c.values.ReadinessProbeCommand,
},
},
ReadinessProbe: &corev1.Probe{
Handler: getReadinessHandler(c.values),
InitialDelaySeconds: 15,
PeriodSeconds: 5,
FailureThreshold: 5,
},
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: c.values.LivenessProbCommand,
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: c.values.LivenessProbeCommand,
},
},
InitialDelaySeconds: 15,
PeriodSeconds: 5,
FailureThreshold: 5,
},
StartupProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: c.values.LivenessProbeCommand,
},
},
PeriodSeconds: 5,
FailureThreshold: 24,
},
Ports: getEtcdPorts(c.values),
Resources: getEtcdResources(c.values),
Env: getEtcdEnvVars(c.values),
Expand All @@ -223,15 +228,15 @@ func (c *component) syncStatefulset(ctx context.Context, sts *appsv1.StatefulSet
{
Name: "backup-restore",
Image: c.values.BackupImage,
ImagePullPolicy: v1.PullIfNotPresent,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: c.values.EtcdBackupCommand,
Ports: getBackupPorts(c.values),
Resources: getBackupResources(c.values),
Env: getBackupRestoreEnvVars(c.values),
VolumeMounts: getBackupRestoreVolumeMounts(c.values),
SecurityContext: &v1.SecurityContext{
Capabilities: &v1.Capabilities{
Add: []v1.Capability{
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{
"SYS_PTRACE",
},
},
Expand All @@ -242,14 +247,14 @@ func (c *component) syncStatefulset(ctx context.Context, sts *appsv1.StatefulSet
Volumes: getVolumes(c.values),
},
},
VolumeClaimTemplates: []v1.PersistentVolumeClaim{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: c.values.VolumeClaimTemplateName,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteOnce,
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
},
StorageClassName: c.values.StorageClass,
Resources: getStorageReq(c.values),
Expand Down Expand Up @@ -713,3 +718,35 @@ func getEnvVarFromSecrets(name, secretName, secretKey string) corev1.EnvVar {
},
}
}

func getReadinessHandler(val Values) corev1.Handler {
if val.Replicas > 1 {
// TODO(timuthy): Special handling for multi-node etcd can be removed as soon as
// etcd-backup-restore supports `/healthz` for etcd followers, see https://github.com/gardener/etcd-backup-restore/pull/491.
return getReadinessHandlerForMultiNode(val)
}
return getReadinessHandlerForSingleNode(val)
}

func getReadinessHandlerForSingleNode(val Values) corev1.Handler {
scheme := corev1.URISchemeHTTPS
if val.BackupTLS == nil {
scheme = corev1.URISchemeHTTP
}

return corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.FromInt(int(pointer.Int32Deref(val.BackupPort, defaultBackupPort))),
Scheme: scheme,
},
}
}

func getReadinessHandlerForMultiNode(val Values) corev1.Handler {
return corev1.Handler{
Exec: &corev1.ExecAction{
Command: val.ReadinessProbeCommand,
},
}
}
Loading