mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 15:05:27 +00:00
feat: update conversion helpers
Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
This commit is contained in:
parent
d9687a8c3a
commit
0793f6577f
@ -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 {
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -19,6 +19,8 @@ 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"
|
||||
@ -26,7 +28,6 @@ import (
|
||||
"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 {
|
||||
@ -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{},
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
@ -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"
|
||||
@ -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{
|
||||
@ -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{
|
||||
@ -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{
|
||||
@ -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
pkg/apis/core/v1/zz_generated.defaults.go
generated
5
pkg/apis/core/v1/zz_generated.defaults.go
generated
@ -48,6 +48,7 @@ func RegisterDefaults(scheme *runtime.Scheme) error {
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PersistentVolumeList{}, func(obj interface{}) { SetObjectDefaults_PersistentVolumeList(obj.(*corev1.PersistentVolumeList)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.Pod{}, func(obj interface{}) { SetObjectDefaults_Pod(obj.(*corev1.Pod)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PodList{}, func(obj interface{}) { SetObjectDefaults_PodList(obj.(*corev1.PodList)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PodLogOptions{}, func(obj interface{}) { SetObjectDefaults_PodLogOptions(obj.(*corev1.PodLogOptions)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PodStatusResult{}, func(obj interface{}) { SetObjectDefaults_PodStatusResult(obj.(*corev1.PodStatusResult)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PodTemplate{}, func(obj interface{}) { SetObjectDefaults_PodTemplate(obj.(*corev1.PodTemplate)) })
|
||||
scheme.AddTypeDefaultingFunc(&corev1.PodTemplateList{}, func(obj interface{}) { SetObjectDefaults_PodTemplateList(obj.(*corev1.PodTemplateList)) })
|
||||
@ -532,6 +533,10 @@ func SetObjectDefaults_PodList(in *corev1.PodList) {
|
||||
}
|
||||
}
|
||||
|
||||
func SetObjectDefaults_PodLogOptions(in *corev1.PodLogOptions) {
|
||||
SetDefaults_PodLogOptions(in)
|
||||
}
|
||||
|
||||
func SetObjectDefaults_PodStatusResult(in *corev1.PodStatusResult) {
|
||||
for i := range in.Status.InitContainerStatuses {
|
||||
a := &in.Status.InitContainerStatuses[i]
|
||||
|
Loading…
Reference in New Issue
Block a user