Forcing the value of TerminationGracePeriodSeconds to 1 if it is negative

This commit is contained in:
Shiming Zhang 2023-02-20 11:25:27 +08:00
parent d6fe718e19
commit 5e5c543b13
2 changed files with 91 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}
})
}
}