Skip to content

Commit

Permalink
feat: update conversion helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
  • Loading branch information
knight42 committed Nov 7, 2024
1 parent d9687a8 commit 0793f65
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
14 changes: 7 additions & 7 deletions pkg/apis/core/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"fmt"
"reflect"

v1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
"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 {
Expand Down Expand Up @@ -380,7 +380,7 @@ func Convert_v1_Pod_To_core_Pod(in *v1.Pod, out *core.Pod, s conversion.Scope) e
// 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)
out.Spec.TerminationGracePeriodSeconds = ptr.To[int64](1)
}
return nil
}
Expand All @@ -397,7 +397,7 @@ func Convert_core_Pod_To_v1_Pod(in *core.Pod, out *v1.Pod, s conversion.Scope) e
// 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)
out.Spec.TerminationGracePeriodSeconds = ptr.To[int64](1)
}
return nil
}
Expand Down Expand Up @@ -555,12 +555,12 @@ func Convert_v1_PersistentVolumeSpec_To_core_PersistentVolumeSpec(in *v1.Persist
return autoConvert_v1_PersistentVolumeSpec_To_core_PersistentVolumeSpec(in, out, s)
}

// Convert_Slice_string_To_Pointer_v1_LogStreamType is needed because decoding URL parameters requires manual assistance.
func Convert_Slice_string_To_Pointer_v1_LogStreamType(in *[]string, out **v1.LogStreamType, s conversion.Scope) error {
// Convert_Slice_string_To_Pointer_string is needed because decoding URL parameters requires manual assistance.
func Convert_Slice_string_To_Pointer_string(in *[]string, out **string, s conversion.Scope) error {
if len(*in) == 0 {
return nil
}
temp := v1.LogStreamType((*in)[0])
temp := (*in)[0]
*out = &temp
return nil
}
4 changes: 2 additions & 2 deletions pkg/apis/core/v1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestPodLogOptions(t *testing.T) {
sinceTime := metav1.NewTime(time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC).Local())
tailLines := int64(2)
limitBytes := int64(3)
v1StreamStderr := v1.LogStreamTypeStderr
coreStreamStderr := core.LogStreamTypeStderr
v1StreamStderr := v1.LogStreamStderr
coreStreamStderr := core.LogStreamStderr

versionedLogOptions := &v1.PodLogOptions{
Container: "mycontainer",
Expand Down
15 changes: 12 additions & 3 deletions pkg/apis/core/v1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package v1
import (
"time"

"k8s.io/utils/ptr"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/kubernetes/pkg/api/v1/service"
"k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/util/parsers"
"k8s.io/utils/pointer"
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
Expand Down Expand Up @@ -64,7 +65,7 @@ func SetDefaults_ReplicationController(obj *v1.ReplicationController) {
}
}
func SetDefaults_Volume(obj *v1.Volume) {
if pointer.AllPtrFieldsNil(&obj.VolumeSource) {
if ptr.AllPtrFieldsNil(&obj.VolumeSource) {
obj.VolumeSource = v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
}
Expand Down Expand Up @@ -147,7 +148,7 @@ func SetDefaults_Service(obj *v1.Service) {

if obj.Spec.Type == v1.ServiceTypeLoadBalancer {
if obj.Spec.AllocateLoadBalancerNodePorts == nil {
obj.Spec.AllocateLoadBalancerNodePorts = pointer.Bool(true)
obj.Spec.AllocateLoadBalancerNodePorts = ptr.To(true)
}
}

Expand Down Expand Up @@ -429,3 +430,11 @@ func SetDefaults_HostPathVolumeSource(obj *v1.HostPathVolumeSource) {
obj.Type = &typeVol
}
}

func SetDefaults_PodLogOptions(obj *v1.PodLogOptions) {
if utilfeature.DefaultFeatureGate.Enabled(features.PodLogsQuerySplitStreams) {
if obj.Stream == nil {
obj.Stream = ptr.To(v1.LogStreamAll)
}
}
}
32 changes: 28 additions & 4 deletions pkg/apis/core/v1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/utils/ptr"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,7 +37,6 @@ import (
"k8s.io/kubernetes/pkg/api/legacyscheme"
corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
"k8s.io/kubernetes/pkg/features"
utilpointer "k8s.io/utils/pointer"

// ensure types are installed
_ "k8s.io/kubernetes/pkg/apis/core/install"
Expand Down Expand Up @@ -690,7 +691,7 @@ func TestSetDefaultReplicationControllerReplicas(t *testing.T) {
{
rc: v1.ReplicationController{
Spec: v1.ReplicationControllerSpec{
Replicas: utilpointer.Int32(0),
Replicas: ptr.To[int32](0),
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand All @@ -705,7 +706,7 @@ func TestSetDefaultReplicationControllerReplicas(t *testing.T) {
{
rc: v1.ReplicationController{
Spec: v1.ReplicationControllerSpec{
Replicas: utilpointer.Int32(3),
Replicas: ptr.To[int32](3),
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -1930,7 +1931,7 @@ func TestDefaultRequestIsNotSetForReplicationController(t *testing.T) {
}
rc := &v1.ReplicationController{
Spec: v1.ReplicationControllerSpec{
Replicas: utilpointer.Int32(3),
Replicas: ptr.To[int32](3),
Template: &v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down Expand Up @@ -2349,3 +2350,26 @@ func TestSetDefaults_Volume(t *testing.T) {
})
}
}

func TestSetDefaults_PodLogOptions(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodLogsQuerySplitStreams, true)
for desc, tc := range map[string]struct {
given, expected *v1.PodLogOptions
}{
"defaults to All": {
given: &v1.PodLogOptions{},
expected: &v1.PodLogOptions{Stream: ptr.To(v1.LogStreamAll)},
},
"the specified stream should not be overridden": {
given: &v1.PodLogOptions{Stream: ptr.To(v1.LogStreamStdout)},
expected: &v1.PodLogOptions{Stream: ptr.To(v1.LogStreamStdout)},
},
} {
t.Run(desc, func(t *testing.T) {
corev1.SetDefaults_PodLogOptions(tc.given)
if !cmp.Equal(tc.given, tc.expected) {
t.Errorf("expected volume %+v, but got %+v", tc.expected, tc.given)
}
})
}
}
5 changes: 5 additions & 0 deletions pkg/apis/core/v1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0793f65

Please sign in to comment.