func TestSetDefaultPodSpecHostNetwork(t *testing.T) { portNum := 8080 s := versioned.PodSpec{} s.HostNetwork = true s.Containers = []versioned.Container{ { Ports: []versioned.ContainerPort{ { ContainerPort: portNum, }, }, }, } pod := &versioned.Pod{ Spec: s, } obj2 := roundTrip(t, runtime.Object(pod)) pod2 := obj2.(*versioned.Pod) s2 := pod2.Spec hostPortNum := s2.Containers[0].Ports[0].HostPort if hostPortNum != portNum { t.Errorf("Expected container port to be defaulted, was made %d instead of %d", hostPortNum, portNum) } }
func TestDefaultRequestIsNotSetForReplicaSet(t *testing.T) { s := v1.PodSpec{} s.Containers = []v1.Container{ { Resources: v1.ResourceRequirements{ Limits: v1.ResourceList{ v1.ResourceCPU: resource.MustParse("100m"), }, }, }, } rs := &ReplicaSet{ Spec: ReplicaSetSpec{ Replicas: newInt32(3), Template: v1.PodTemplateSpec{ ObjectMeta: v1.ObjectMeta{ Labels: map[string]string{ "foo": "bar", }, }, Spec: s, }, }, } output := roundTrip(t, runtime.Object(rs)) rs2 := output.(*ReplicaSet) defaultRequest := rs2.Spec.Template.Spec.Containers[0].Resources.Requests requestValue := defaultRequest[v1.ResourceCPU] if requestValue.String() != "0" { t.Errorf("Expected 0 request value, got: %s", requestValue.String()) } }
func TestDefaultRequestIsNotSetForReplicationController(t *testing.T) { s := versioned.PodSpec{} s.Containers = []versioned.Container{ { Resources: versioned.ResourceRequirements{ Limits: versioned.ResourceList{ versioned.ResourceCPU: resource.MustParse("100m"), }, }, }, } rc := &versioned.ReplicationController{ Spec: versioned.ReplicationControllerSpec{ Replicas: newInt(3), Template: &versioned.PodTemplateSpec{ ObjectMeta: versioned.ObjectMeta{ Labels: map[string]string{ "foo": "bar", }, }, Spec: s, }, }, } output := roundTrip(t, runtime.Object(rc)) rc2 := output.(*versioned.ReplicationController) defaultRequest := rc2.Spec.Template.Spec.Containers[0].Resources.Requests requestValue := defaultRequest[versioned.ResourceCPU] if requestValue.String() != "0" { t.Errorf("Expected 0 request value, got: %s", requestValue.String()) } }
func TestSetDefaultRequestsPod(t *testing.T) { // verify we default if limits are specified (and that request=0 is preserved) s := versioned.PodSpec{} s.Containers = []versioned.Container{ { Resources: versioned.ResourceRequirements{ Requests: versioned.ResourceList{ versioned.ResourceMemory: resource.MustParse("0"), }, Limits: versioned.ResourceList{ versioned.ResourceCPU: resource.MustParse("100m"), versioned.ResourceMemory: resource.MustParse("1Gi"), }, }, }, } pod := &versioned.Pod{ Spec: s, } output := roundTrip(t, runtime.Object(pod)) pod2 := output.(*versioned.Pod) defaultRequest := pod2.Spec.Containers[0].Resources.Requests if requestValue := defaultRequest[versioned.ResourceCPU]; requestValue.String() != "100m" { t.Errorf("Expected request cpu: %s, got: %s", "100m", requestValue.String()) } if requestValue := defaultRequest[versioned.ResourceMemory]; requestValue.String() != "0" { t.Errorf("Expected request memory: %s, got: %s", "0", requestValue.String()) } // verify we do nothing if no limits are specified s = versioned.PodSpec{} s.Containers = []versioned.Container{{}} pod = &versioned.Pod{ Spec: s, } output = roundTrip(t, runtime.Object(pod)) pod2 = output.(*versioned.Pod) defaultRequest = pod2.Spec.Containers[0].Resources.Requests if requestValue := defaultRequest[versioned.ResourceCPU]; requestValue.String() != "0" { t.Errorf("Expected 0 request value, got: %s", requestValue.String()) } }
func TestSetDefaultDownwardAPIVolumeSource(t *testing.T) { s := versioned.PodSpec{} s.Volumes = []versioned.Volume{ { VolumeSource: versioned.VolumeSource{ DownwardAPI: &versioned.DownwardAPIVolumeSource{}, }, }, } pod := &versioned.Pod{ Spec: s, } output := roundTrip(t, runtime.Object(pod)) pod2 := output.(*versioned.Pod) defaultMode := pod2.Spec.Volumes[0].VolumeSource.DownwardAPI.DefaultMode expectedMode := versioned.DownwardAPIVolumeSourceDefaultMode if defaultMode == nil || *defaultMode != expectedMode { t.Errorf("Expected DownwardAPI DefaultMode %v, got %v", expectedMode, defaultMode) } }
func TestSetDefaultConfigMapVolumeSource(t *testing.T) { s := v1.PodSpec{} s.Volumes = []v1.Volume{ { VolumeSource: v1.VolumeSource{ ConfigMap: &v1.ConfigMapVolumeSource{}, }, }, } pod := &v1.Pod{ Spec: s, } output := roundTrip(t, runtime.Object(pod)) pod2 := output.(*v1.Pod) defaultMode := pod2.Spec.Volumes[0].VolumeSource.ConfigMap.DefaultMode expectedMode := v1.ConfigMapVolumeSourceDefaultMode if defaultMode == nil || *defaultMode != expectedMode { t.Errorf("Expected ConfigMap DefaultMode %v, got %v", expectedMode, defaultMode) } }
func TestSetMinimumScalePod(t *testing.T) { // verify we default if limits are specified (and that request=0 is preserved) s := versioned.PodSpec{} s.Containers = []versioned.Container{ { Resources: versioned.ResourceRequirements{ Requests: versioned.ResourceList{ versioned.ResourceMemory: resource.MustParse("1n"), }, Limits: versioned.ResourceList{ versioned.ResourceCPU: resource.MustParse("2n"), }, }, }, } pod := &versioned.Pod{ Spec: s, } versioned.SetObjectDefaults_Pod(pod) if expect := resource.MustParse("1m"); expect.Cmp(pod.Spec.Containers[0].Resources.Requests[versioned.ResourceMemory]) != 0 { t.Errorf("did not round resources: %#v", pod.Spec.Containers[0].Resources) } }
// The following two PodSpec conversions functions where copied from pkg/api/conversion.go // for the generated functions to work properly. // This should be fixed: https://github.com/kubernetes/kubernetes/issues/12977 func Convert_api_PodSpec_To_v1_PodSpec(in *api.PodSpec, out *v1.PodSpec, s conversion.Scope) error { if defaulting, found := s.DefaultingInterface(reflect.TypeOf(*in)); found { defaulting.(func(*api.PodSpec))(in) } if in.Volumes != nil { out.Volumes = make([]v1.Volume, len(in.Volumes)) for i := range in.Volumes { if err := Convert_api_Volume_To_v1_Volume(&in.Volumes[i], &out.Volumes[i], s); err != nil { return err } } } else { out.Volumes = nil } if in.Containers != nil { out.Containers = make([]v1.Container, len(in.Containers)) for i := range in.Containers { if err := Convert_api_Container_To_v1_Container(&in.Containers[i], &out.Containers[i], s); err != nil { return err } } } else { out.Containers = nil } out.RestartPolicy = v1.RestartPolicy(in.RestartPolicy) if in.TerminationGracePeriodSeconds != nil { out.TerminationGracePeriodSeconds = new(int64) *out.TerminationGracePeriodSeconds = *in.TerminationGracePeriodSeconds } else { out.TerminationGracePeriodSeconds = nil } if in.ActiveDeadlineSeconds != nil { out.ActiveDeadlineSeconds = new(int64) *out.ActiveDeadlineSeconds = *in.ActiveDeadlineSeconds } else { out.ActiveDeadlineSeconds = nil } out.DNSPolicy = v1.DNSPolicy(in.DNSPolicy) if in.NodeSelector != nil { out.NodeSelector = make(map[string]string) for key, val := range in.NodeSelector { out.NodeSelector[key] = val } } else { out.NodeSelector = nil } out.ServiceAccountName = in.ServiceAccountName // DeprecatedServiceAccount is an alias for ServiceAccountName. out.DeprecatedServiceAccount = in.ServiceAccountName out.NodeName = in.NodeName if in.SecurityContext != nil { out.SecurityContext = new(v1.PodSecurityContext) if err := Convert_api_PodSecurityContext_To_v1_PodSecurityContext(in.SecurityContext, out.SecurityContext, s); err != nil { return err } out.HostNetwork = in.SecurityContext.HostNetwork out.HostPID = in.SecurityContext.HostPID out.HostIPC = in.SecurityContext.HostIPC } if in.ImagePullSecrets != nil { out.ImagePullSecrets = make([]v1.LocalObjectReference, len(in.ImagePullSecrets)) for i := range in.ImagePullSecrets { if err := Convert_api_LocalObjectReference_To_v1_LocalObjectReference(&in.ImagePullSecrets[i], &out.ImagePullSecrets[i], s); err != nil { return err } } } else { out.ImagePullSecrets = nil } return nil }
func deepCopy_v1_PodSpec(in v1.PodSpec, out *v1.PodSpec, c *conversion.Cloner) error { if in.Volumes != nil { out.Volumes = make([]v1.Volume, len(in.Volumes)) for i := range in.Volumes { if err := deepCopy_v1_Volume(in.Volumes[i], &out.Volumes[i], c); err != nil { return err } } } else { out.Volumes = nil } if in.Containers != nil { out.Containers = make([]v1.Container, len(in.Containers)) for i := range in.Containers { if err := deepCopy_v1_Container(in.Containers[i], &out.Containers[i], c); err != nil { return err } } } else { out.Containers = nil } out.RestartPolicy = in.RestartPolicy if in.TerminationGracePeriodSeconds != nil { out.TerminationGracePeriodSeconds = new(int64) *out.TerminationGracePeriodSeconds = *in.TerminationGracePeriodSeconds } else { out.TerminationGracePeriodSeconds = nil } if in.ActiveDeadlineSeconds != nil { out.ActiveDeadlineSeconds = new(int64) *out.ActiveDeadlineSeconds = *in.ActiveDeadlineSeconds } else { out.ActiveDeadlineSeconds = nil } out.DNSPolicy = in.DNSPolicy if in.NodeSelector != nil { out.NodeSelector = make(map[string]string) for key, val := range in.NodeSelector { out.NodeSelector[key] = val } } else { out.NodeSelector = nil } out.ServiceAccountName = in.ServiceAccountName out.DeprecatedServiceAccount = in.DeprecatedServiceAccount out.NodeName = in.NodeName out.HostNetwork = in.HostNetwork out.HostPID = in.HostPID out.HostIPC = in.HostIPC if in.ImagePullSecrets != nil { out.ImagePullSecrets = make([]v1.LocalObjectReference, len(in.ImagePullSecrets)) for i := range in.ImagePullSecrets { if err := deepCopy_v1_LocalObjectReference(in.ImagePullSecrets[i], &out.ImagePullSecrets[i], c); err != nil { return err } } } else { out.ImagePullSecrets = nil } return nil }