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

Add configurable preStop delay to Envoy sidecar #911

Closed
Closed
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
3 changes: 3 additions & 0 deletions control-plane/connect-inject/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions control-plane/connect-inject/envoy_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{},
Expand Down