diff --git a/pkg/inject/config.go b/pkg/inject/config.go index d3146449..c5f113dd 100644 --- a/pkg/inject/config.go +++ b/pkg/inject/config.go @@ -14,6 +14,7 @@ const ( flagSidecarMemoryRequests = "sidecar-memory-requests" flagPreview = "preview" flagLogLevel = "sidecar-log-level" + flagPreStopDelay = "prestop-delay" flagInitImage = "init-image" flagIgnoredIPs = "ignored-ips" @@ -42,6 +43,7 @@ type Config struct { SidecarMemory string Preview bool LogLevel string + PreStopDelay string // Init container settings InitImage string @@ -83,6 +85,8 @@ func (cfg *Config) BindFlags(fs *pflag.FlagSet) { "Enable preview channel") fs.StringVar(&cfg.LogLevel, flagLogLevel, "info", "AWS App Mesh envoy log level") + fs.StringVar(&cfg.PreStopDelay, flagPreStopDelay, "20", + "AWS App Mesh envoy preStop hook sleep duration") fs.StringVar(&cfg.InitImage, flagInitImage, "111345817488.dkr.ecr.us-west-2.amazonaws.com/aws-appmesh-proxy-route-manager:v2", "Init container image.") fs.StringVar(&cfg.IgnoredIPs, flagIgnoredIPs, "169.254.169.254", diff --git a/pkg/inject/envoy.go b/pkg/inject/envoy.go index 434f0452..9d63791b 100644 --- a/pkg/inject/envoy.go +++ b/pkg/inject/envoy.go @@ -27,6 +27,17 @@ const envoyContainerTemplate = ` "protocol": "TCP" } ], + "lifecycle": { + "preStop": { + "exec": { + "command": [ + "sh", + "-c", + "sleep {{.PreStopDelay}}" + ] + } + } + }, "env": [ { "name": "APPMESH_VIRTUAL_NODE_NAME", @@ -82,6 +93,7 @@ type EnvoyTemplateVariables struct { VirtualNodeName string Preview string LogLevel string + PreStopDelay string SidecarImage string SidecarCPURequests string SidecarMemoryRequests string @@ -98,6 +110,7 @@ type envoyMutatorConfig struct { awsRegion string preview bool logLevel string + preStopDelay string sidecarImage string sidecarCPURequests string sidecarMemoryRequests string @@ -156,6 +169,7 @@ func (m *envoyMutator) buildTemplateVariables(pod *corev1.Pod) EnvoyTemplateVari VirtualNodeName: virtualNodeName, Preview: preview, LogLevel: m.mutatorConfig.logLevel, + PreStopDelay: m.mutatorConfig.preStopDelay, SidecarImage: m.mutatorConfig.sidecarImage, SidecarCPURequests: getSidecarCPURequest(m.mutatorConfig.sidecarCPURequests, pod), SidecarMemoryRequests: getSidecarMemoryRequest(m.mutatorConfig.sidecarMemoryRequests, pod), diff --git a/pkg/inject/envoy_test.go b/pkg/inject/envoy_test.go index 069d9874..3b3a294b 100644 --- a/pkg/inject/envoy_test.go +++ b/pkg/inject/envoy_test.go @@ -71,6 +71,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -103,6 +104,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -141,6 +150,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: true, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -173,6 +183,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -211,6 +229,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -244,6 +263,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -286,6 +313,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -319,6 +347,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -367,6 +403,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -400,6 +437,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -448,6 +493,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -481,6 +527,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -523,6 +577,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -556,6 +611,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -598,6 +661,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), @@ -649,6 +713,14 @@ func Test_envoyMutator_mutate(t *testing.T) { Protocol: "TCP", }, }, + Lifecycle: &corev1.Lifecycle{ + PostStart: nil, + PreStop: &corev1.Handler{ + Exec: &corev1.ExecAction{Command: []string{ + "sh", "-c", "sleep 20", + }}, + }, + }, Env: []corev1.EnvVar{ { Name: "APPMESH_VIRTUAL_NODE_NAME", @@ -717,6 +789,7 @@ func Test_envoyMutator_mutate(t *testing.T) { awsRegion: "us-west-2", preview: false, logLevel: "debug", + preStopDelay: "20", sidecarImage: "envoy:v2", sidecarCPURequests: cpuRequests.String(), sidecarMemoryRequests: memoryRequests.String(), diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go index 7b9409a8..74613170 100644 --- a/pkg/inject/inject.go +++ b/pkg/inject/inject.go @@ -109,6 +109,7 @@ func (m *SidecarInjector) injectAppMeshPatches(ms *appmesh.Mesh, vn *appmesh.Vir awsRegion: m.awsRegion, preview: m.config.Preview, logLevel: m.config.LogLevel, + preStopDelay: m.config.PreStopDelay, sidecarImage: m.config.SidecarImage, sidecarCPURequests: m.config.SidecarCpu, sidecarMemoryRequests: m.config.SidecarMemory,