Skip to content

Commit

Permalink
Use native sleep prestophook instead of exec-ing sleep 5 (#571)
Browse files Browse the repository at this point in the history
* Fixes #538
* Use native sleep PreStopHook instead of exec-ing `sleep 5`
* Upgrade test suite to Kubernetes 1.30
  • Loading branch information
kimtore authored Oct 22, 2024
1 parent 57b45f5 commit 51b703d
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME := naiserator
TAG := navikt/${NAME}
LATEST := ${TAG}:latest
ROOT_DIR := $(shell git rev-parse --show-toplevel)
K8S_VERSION := 1.27.1
K8S_VERSION := 1.30.0
arch := $(shell uname -m | sed s/aarch64/arm64/ | sed s/x86_64/amd64/)
os := $(shell uname -s | tr '[:upper:]' '[:lower:]')
testbin_dir := ./.testbin/
Expand Down Expand Up @@ -50,4 +50,4 @@ proto:

install-protobuf-go:
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
20 changes: 13 additions & 7 deletions pkg/resourcecreator/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,18 @@ func copyLinkerdAnnotations(src, dst map[string]string) {
}
}

// The default PreStopHook will wait for five seconds before killing pods.
// Earlier, this function was solved by "sleep 5" inside the container.
func defaultPreStopHook() *corev1.Lifecycle {
return &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Sleep: &corev1.SleepAction{
Seconds: 5,
},
},
}
}

// lifecycle creates lifecycle definitions, right now adding only PreStop handlers.
//
// preStopHookPath is the old, deprecated way of adding preStopHook definitions.
Expand All @@ -470,13 +482,7 @@ func lifecycle(preStopHookPath string, preStopHook *nais_io_v1.PreStopHook) (*co
}

if preStopHook == nil {
return &corev1.Lifecycle{
PreStop: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: []string{"sleep", "5"},
},
},
}, nil
return defaultPreStopHook(), nil
}

if preStopHook.Exec != nil && preStopHook.Http != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ tests:
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
name: myapplication
ports:
- containerPort: 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,8 @@ tests:
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
name: mynaisjob
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ tests:
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
name: mynaisjob
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ tests:
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
name: mynaisjob
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ tests:
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
name: mynaisjob
resources:
limits:
Expand Down
12 changes: 12 additions & 0 deletions pkg/resourcecreator/testdata/vanilla.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ tests:
kind: Deployment
name: myapplication
match:
- type: subset
name: "prestophook set"
resource:
spec:
template:
spec:
containers:
- name: myapplication
lifecycle:
preStop:
sleep:
seconds: 5
- type: subset
name: "replicas set to 2"
resource:
Expand Down
6 changes: 2 additions & 4 deletions pkg/resourcecreator/testdata/vanilla_on_premises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,8 @@ tests:
name: writable-tmp
lifecycle:
preStop:
exec:
command:
- sleep
- "5"
sleep:
seconds: 5
imagePullPolicy: IfNotPresent
name: myapplication
image: navikt/myapplication:1.2.3
Expand Down

0 comments on commit 51b703d

Please sign in to comment.