From a0c9ce61100c0e41f7b576919c1c52b07f135a2c Mon Sep 17 00:00:00 2001 From: Natasha Tomattis Date: Tue, 20 Jun 2023 17:01:11 -0300 Subject: [PATCH] added extra annotation to connect inject --- .../constants/annotations_and_labels.go | 3 +++ .../webhook/consul_dataplane_sidecar.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/control-plane/connect-inject/constants/annotations_and_labels.go b/control-plane/connect-inject/constants/annotations_and_labels.go index 637e028202..14ccfb6d0b 100644 --- a/control-plane/connect-inject/constants/annotations_and_labels.go +++ b/control-plane/connect-inject/constants/annotations_and_labels.go @@ -128,6 +128,9 @@ const ( // passed via the -envoy-extra-args flag. AnnotationEnvoyExtraArgs = "consul.hashicorp.com/envoy-extra-args" + // annotationSidecarProxyPreStopDelay to add a pre-stop delay to the sidecar proxy. + AnnotationSidecarProxyPreStopDelay = "consul.hashicorp.com/sidecar-proxy-prestop-delay" + // AnnotationConsulNamespace is the Consul namespace the service is registered into. AnnotationConsulNamespace = "consul.hashicorp.com/consul-namespace" diff --git a/control-plane/connect-inject/webhook/consul_dataplane_sidecar.go b/control-plane/connect-inject/webhook/consul_dataplane_sidecar.go index 9d02a26fa4..52124db49e 100644 --- a/control-plane/connect-inject/webhook/consul_dataplane_sidecar.go +++ b/control-plane/connect-inject/webhook/consul_dataplane_sidecar.go @@ -110,6 +110,19 @@ func (w *MeshWebhook) consulDataplaneSidecar(namespace corev1.Namespace, pod cor container.VolumeMounts = append(container.VolumeMounts, saTokenVolumeMount) } + // Prestop hook + if _, ok := pod.Annotations[constants.AnnotationSidecarProxyPreStopDelay]; ok { + preStopHook := &corev1.Lifecycle{ + PreStop: &corev1.LifecycleHandler { + Exec: &corev1.ExecAction { + Command: []string{ + "/bin/sh", + "-c", + "sleep " + pod.Annotations[constants.AnnotationSidecarProxyPreStopDelay], + }}}} + container.Lifecycle = preStopHook + } + if useProxyHealthCheck(pod) { // Configure the Readiness Address for the proxy's health check to be the Pod IP. container.Env = append(container.Env, corev1.EnvVar{