From 5e5c543b1300ddf1e27bf1972283c86ace529da4 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Mon, 20 Feb 2023 11:25:27 +0800 Subject: [PATCH] Forcing the value of TerminationGracePeriodSeconds to 1 if it is negative --- pkg/apis/core/v1/conversion.go | 11 ++++ pkg/apis/core/v1/conversion_test.go | 80 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/pkg/apis/core/v1/conversion.go b/pkg/apis/core/v1/conversion.go index 7869f0389b9..dd92428cdf3 100644 --- a/pkg/apis/core/v1/conversion.go +++ b/pkg/apis/core/v1/conversion.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/kubernetes/pkg/apis/apps" "k8s.io/kubernetes/pkg/apis/core" + utilpointer "k8s.io/utils/pointer" ) func addConversionFuncs(scheme *runtime.Scheme) error { @@ -372,6 +373,11 @@ func Convert_v1_Pod_To_core_Pod(in *v1.Pod, out *core.Pod, s conversion.Scope) e // drop init container annotations so they don't show up as differences when receiving requests from old clients out.Annotations = dropInitContainerAnnotations(out.Annotations) + // Forcing the value of TerminationGracePeriodSeconds to 1 if it is negative. + // Just for Pod, not for PodSpec, because we don't want to change the behavior of the PodTemplate. + if in.Spec.TerminationGracePeriodSeconds != nil && *in.Spec.TerminationGracePeriodSeconds < 0 { + out.Spec.TerminationGracePeriodSeconds = utilpointer.Int64(1) + } return nil } @@ -384,6 +390,11 @@ func Convert_core_Pod_To_v1_Pod(in *core.Pod, out *v1.Pod, s conversion.Scope) e // remove this once the oldest supported kubelet no longer honors the annotations over the field. out.Annotations = dropInitContainerAnnotations(out.Annotations) + // Forcing the value of TerminationGracePeriodSeconds to 1 if it is negative. + // Just for Pod, not for PodSpec, because we don't want to change the behavior of the PodTemplate. + if in.Spec.TerminationGracePeriodSeconds != nil && *in.Spec.TerminationGracePeriodSeconds < 0 { + out.Spec.TerminationGracePeriodSeconds = utilpointer.Int64(1) + } return nil } diff --git a/pkg/apis/core/v1/conversion_test.go b/pkg/apis/core/v1/conversion_test.go index 68161256293..77e14f2711f 100644 --- a/pkg/apis/core/v1/conversion_test.go +++ b/pkg/apis/core/v1/conversion_test.go @@ -24,6 +24,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" @@ -704,3 +705,82 @@ func Test_v1_NodeSpec_to_core_NodeSpec(t *testing.T) { } } } + +func TestConvert_v1_Pod_To_core_Pod(t *testing.T) { + type args struct { + in *v1.Pod + out *core.Pod + } + tests := []struct { + name string + args args + wantErr bool + wantOut *core.Pod + }{ + { + args: args{ + in: &v1.Pod{ + Spec: v1.PodSpec{ + TerminationGracePeriodSeconds: utilpointer.Int64(-1), + }, + }, + out: &core.Pod{}, + }, + wantOut: &core.Pod{ + Spec: core.PodSpec{ + TerminationGracePeriodSeconds: utilpointer.Int64(1), + SecurityContext: &core.PodSecurityContext{}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := corev1.Convert_v1_Pod_To_core_Pod(tt.args.in, tt.args.out, nil); (err != nil) != tt.wantErr { + t.Errorf("Convert_v1_Pod_To_core_Pod() error = %v, wantErr %v", err, tt.wantErr) + } + if diff := cmp.Diff(tt.args.out, tt.wantOut); diff != "" { + t.Errorf("Convert_v1_Pod_To_core_Pod() mismatch (-want +got):\n%s", diff) + } + }) + } +} + +func TestConvert_core_Pod_To_v1_Pod(t *testing.T) { + type args struct { + in *core.Pod + out *v1.Pod + } + tests := []struct { + name string + args args + wantErr bool + wantOut *v1.Pod + }{ + { + args: args{ + in: &core.Pod{ + Spec: core.PodSpec{ + TerminationGracePeriodSeconds: utilpointer.Int64(-1), + }, + }, + out: &v1.Pod{}, + }, + wantOut: &v1.Pod{ + Spec: v1.PodSpec{ + TerminationGracePeriodSeconds: utilpointer.Int64(1), + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := corev1.Convert_core_Pod_To_v1_Pod(tt.args.in, tt.args.out, nil); (err != nil) != tt.wantErr { + t.Errorf("Convert_core_Pod_To_v1_Pod() error = %v, wantErr %v", err, tt.wantErr) + } + if diff := cmp.Diff(tt.args.out, tt.wantOut); diff != "" { + t.Errorf("Convert_core_Pod_To_v1_Pod() mismatch (-want +got):\n%s", diff) + } + }) + } +}