diff --git a/control-plane/connect-inject/annotations.go b/control-plane/connect-inject/annotations.go index c8bf650e08..3613a68b06 100644 --- a/control-plane/connect-inject/annotations.go +++ b/control-plane/connect-inject/annotations.go @@ -69,6 +69,9 @@ const ( annotationSidecarProxyMemoryLimit = "consul.hashicorp.com/sidecar-proxy-memory-limit" annotationSidecarProxyMemoryRequest = "consul.hashicorp.com/sidecar-proxy-memory-request" + // annotationSidecarProxyPreStopDelay is the number of seconds to delay Envoy Sidecar shutdown + annotationSidecarProxyPreStopDelay = "consul.hashicorp.com/sidecar-proxy-prestop-delay" + // annotations for metrics to configure where Prometheus scrapes // metrics from, whether to run a merged metrics endpoint on the consul // sidecar, and configure the connect service metrics. diff --git a/control-plane/connect-inject/envoy_sidecar.go b/control-plane/connect-inject/envoy_sidecar.go index b521297c54..6b63c1d0b3 100644 --- a/control-plane/connect-inject/envoy_sidecar.go +++ b/control-plane/connect-inject/envoy_sidecar.go @@ -42,6 +42,11 @@ func (h *Handler) envoySidecar(namespace corev1.Namespace, pod corev1.Pod) (core Command: cmd, } + lifecycle, err := h.envoySidecarLifecycle(pod) + if err == nil { + container.Lifecycle = lifecycle + } + tproxyEnabled, err := transparentProxyEnabled(namespace, pod, h.EnableTransparentProxy) if err != nil { return corev1.Container{}, err @@ -109,6 +114,29 @@ func (h *Handler) getContainerSidecarCommand(pod corev1.Pod) ([]string, error) { return cmd, nil } +func (h *Handler) envoySidecarLifecycle(pod corev1.Pod) (*corev1.Lifecycle, error) { + + delay, annotationSet := pod.Annotations[annotationSidecarProxyPreStopDelay] + + if !annotationSet { + return &corev1.Lifecycle{}, fmt.Errorf("Annotation not set") + } + + lifecycle := &corev1.Lifecycle{ + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{ + Command: []string{ + "/bin/sh", + "-c", + "sleep " + delay, + }, + }, + }, + } + + return lifecycle, nil +} + func (h *Handler) envoySidecarResources(pod corev1.Pod) (corev1.ResourceRequirements, error) { resources := corev1.ResourceRequirements{ Limits: corev1.ResourceList{},