ingress-nginx-helm/vendor/k8s.io/kubernetes/pkg/api/v1/defaults.go

375 lines
10 KiB
Go
Raw Normal View History

2016-02-22 00:13:08 +00:00
/*
2016-07-12 03:42:47 +00:00
Copyright 2015 The Kubernetes Authors.
2016-02-22 00:13:08 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
2017-07-16 19:30:35 +00:00
"k8s.io/api/core/v1"
2017-04-01 14:42:02 +00:00
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
2016-02-22 00:13:08 +00:00
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/parsers"
)
2016-08-10 18:53:55 +00:00
func addDefaultingFuncs(scheme *runtime.Scheme) error {
2017-05-21 00:11:38 +00:00
return RegisterDefaults(scheme)
2016-05-09 18:53:58 +00:00
}
2016-02-22 00:13:08 +00:00
2017-07-16 19:30:35 +00:00
func SetDefaults_ResourceList(obj *v1.ResourceList) {
2016-11-10 22:57:28 +00:00
for key, val := range *obj {
// TODO(#18538): We round up resource values to milli scale to maintain API compatibility.
// In the future, we should instead reject values that need rounding.
const milliScale = -3
val.RoundUp(milliScale)
2017-07-16 19:30:35 +00:00
(*obj)[v1.ResourceName(key)] = val
2016-11-10 22:57:28 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_PodExecOptions(obj *v1.PodExecOptions) {
2016-05-09 18:53:58 +00:00
obj.Stdout = true
obj.Stderr = true
}
2017-07-16 19:30:35 +00:00
func SetDefaults_PodAttachOptions(obj *v1.PodAttachOptions) {
2016-05-09 18:53:58 +00:00
obj.Stdout = true
obj.Stderr = true
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ReplicationController(obj *v1.ReplicationController) {
2016-05-09 18:53:58 +00:00
var labels map[string]string
if obj.Spec.Template != nil {
labels = obj.Spec.Template.Labels
}
// TODO: support templates defined elsewhere when we support them in the API
if labels != nil {
if len(obj.Spec.Selector) == 0 {
obj.Spec.Selector = labels
}
if len(obj.Labels) == 0 {
obj.Labels = labels
}
}
if obj.Spec.Replicas == nil {
obj.Spec.Replicas = new(int32)
*obj.Spec.Replicas = 1
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Volume(obj *v1.Volume) {
2016-05-09 18:53:58 +00:00
if util.AllPtrFieldsNil(&obj.VolumeSource) {
2017-07-16 19:30:35 +00:00
obj.VolumeSource = v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
2016-05-09 18:53:58 +00:00
}
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ContainerPort(obj *v1.ContainerPort) {
2016-05-09 18:53:58 +00:00
if obj.Protocol == "" {
2017-07-16 19:30:35 +00:00
obj.Protocol = v1.ProtocolTCP
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Container(obj *v1.Container) {
2016-05-09 18:53:58 +00:00
if obj.ImagePullPolicy == "" {
// Ignore error and assume it has been validated elsewhere
_, tag, _, _ := parsers.ParseImageName(obj.Image)
// Check image tag
if tag == "latest" {
2017-07-16 19:30:35 +00:00
obj.ImagePullPolicy = v1.PullAlways
2016-05-09 18:53:58 +00:00
} else {
2017-07-16 19:30:35 +00:00
obj.ImagePullPolicy = v1.PullIfNotPresent
2016-05-09 18:53:58 +00:00
}
}
if obj.TerminationMessagePath == "" {
2017-07-16 19:30:35 +00:00
obj.TerminationMessagePath = v1.TerminationMessagePathDefault
2016-05-09 18:53:58 +00:00
}
2017-04-01 14:42:02 +00:00
if obj.TerminationMessagePolicy == "" {
2017-07-16 19:30:35 +00:00
obj.TerminationMessagePolicy = v1.TerminationMessageReadFile
2017-04-01 14:42:02 +00:00
}
2016-05-09 18:53:58 +00:00
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Service(obj *v1.Service) {
2017-05-21 00:11:38 +00:00
if obj.Spec.SessionAffinity == "" {
2017-07-16 19:30:35 +00:00
obj.Spec.SessionAffinity = v1.ServiceAffinityNone
2016-05-09 18:53:58 +00:00
}
2017-05-21 00:11:38 +00:00
if obj.Spec.Type == "" {
2017-07-16 19:30:35 +00:00
obj.Spec.Type = v1.ServiceTypeClusterIP
2016-05-09 18:53:58 +00:00
}
2017-05-21 00:11:38 +00:00
for i := range obj.Spec.Ports {
sp := &obj.Spec.Ports[i]
2016-05-09 18:53:58 +00:00
if sp.Protocol == "" {
2017-07-16 19:30:35 +00:00
sp.Protocol = v1.ProtocolTCP
2016-05-09 18:53:58 +00:00
}
if sp.TargetPort == intstr.FromInt(0) || sp.TargetPort == intstr.FromString("") {
sp.TargetPort = intstr.FromInt(int(sp.Port))
}
}
2017-05-21 00:11:38 +00:00
// Defaults ExternalTrafficPolicy field for NodePort / LoadBalancer service
// to Global for consistency.
2017-07-16 19:30:35 +00:00
if _, ok := obj.Annotations[v1.BetaAnnotationExternalTraffic]; ok {
2017-05-21 00:11:38 +00:00
// Don't default this field if beta annotation exists.
return
2017-07-16 19:30:35 +00:00
} else if (obj.Spec.Type == v1.ServiceTypeNodePort ||
obj.Spec.Type == v1.ServiceTypeLoadBalancer) &&
2017-05-21 00:11:38 +00:00
obj.Spec.ExternalTrafficPolicy == "" {
2017-07-16 19:30:35 +00:00
obj.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeCluster
2017-05-21 00:11:38 +00:00
}
2016-05-09 18:53:58 +00:00
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Pod(obj *v1.Pod) {
2016-05-09 18:53:58 +00:00
// If limits are specified, but requests are not, default requests to limits
2017-07-16 19:30:35 +00:00
// This is done here rather than a more specific defaulting pass on v1.ResourceRequirements
// because we only want this defaulting semantic to take place on a v1.Pod and not a v1.PodTemplate
2016-05-09 18:53:58 +00:00
for i := range obj.Spec.Containers {
// set requests to limits if requests are not specified, but limits are
if obj.Spec.Containers[i].Resources.Limits != nil {
if obj.Spec.Containers[i].Resources.Requests == nil {
2017-07-16 19:30:35 +00:00
obj.Spec.Containers[i].Resources.Requests = make(v1.ResourceList)
2016-05-09 18:53:58 +00:00
}
for key, value := range obj.Spec.Containers[i].Resources.Limits {
if _, exists := obj.Spec.Containers[i].Resources.Requests[key]; !exists {
obj.Spec.Containers[i].Resources.Requests[key] = *(value.Copy())
2016-02-22 00:13:08 +00:00
}
}
2016-05-09 18:53:58 +00:00
}
}
2017-04-01 14:42:02 +00:00
for i := range obj.Spec.InitContainers {
if obj.Spec.InitContainers[i].Resources.Limits != nil {
if obj.Spec.InitContainers[i].Resources.Requests == nil {
2017-07-16 19:30:35 +00:00
obj.Spec.InitContainers[i].Resources.Requests = make(v1.ResourceList)
2017-04-01 14:42:02 +00:00
}
for key, value := range obj.Spec.InitContainers[i].Resources.Limits {
if _, exists := obj.Spec.InitContainers[i].Resources.Requests[key]; !exists {
obj.Spec.InitContainers[i].Resources.Requests[key] = *(value.Copy())
}
}
}
}
2016-05-09 18:53:58 +00:00
}
2017-07-16 19:30:35 +00:00
func SetDefaults_PodSpec(obj *v1.PodSpec) {
2016-05-09 18:53:58 +00:00
if obj.DNSPolicy == "" {
2017-07-16 19:30:35 +00:00
obj.DNSPolicy = v1.DNSClusterFirst
2016-05-09 18:53:58 +00:00
}
if obj.RestartPolicy == "" {
2017-07-16 19:30:35 +00:00
obj.RestartPolicy = v1.RestartPolicyAlways
2016-05-09 18:53:58 +00:00
}
if obj.HostNetwork {
defaultHostNetworkPorts(&obj.Containers)
2017-04-01 14:42:02 +00:00
defaultHostNetworkPorts(&obj.InitContainers)
2016-05-09 18:53:58 +00:00
}
if obj.SecurityContext == nil {
2017-07-16 19:30:35 +00:00
obj.SecurityContext = &v1.PodSecurityContext{}
2016-05-09 18:53:58 +00:00
}
if obj.TerminationGracePeriodSeconds == nil {
2017-07-16 19:30:35 +00:00
period := int64(v1.DefaultTerminationGracePeriodSeconds)
2016-05-09 18:53:58 +00:00
obj.TerminationGracePeriodSeconds = &period
}
2017-04-01 14:42:02 +00:00
if obj.SchedulerName == "" {
2017-07-16 19:30:35 +00:00
obj.SchedulerName = v1.DefaultSchedulerName
2017-04-01 14:42:02 +00:00
}
2016-05-09 18:53:58 +00:00
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Probe(obj *v1.Probe) {
2016-05-09 18:53:58 +00:00
if obj.TimeoutSeconds == 0 {
obj.TimeoutSeconds = 1
}
if obj.PeriodSeconds == 0 {
obj.PeriodSeconds = 10
}
if obj.SuccessThreshold == 0 {
obj.SuccessThreshold = 1
}
if obj.FailureThreshold == 0 {
obj.FailureThreshold = 3
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_SecretVolumeSource(obj *v1.SecretVolumeSource) {
2016-08-10 18:53:55 +00:00
if obj.DefaultMode == nil {
2017-07-16 19:30:35 +00:00
perm := int32(v1.SecretVolumeSourceDefaultMode)
2016-08-10 18:53:55 +00:00
obj.DefaultMode = &perm
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ConfigMapVolumeSource(obj *v1.ConfigMapVolumeSource) {
2016-08-10 18:53:55 +00:00
if obj.DefaultMode == nil {
2017-07-16 19:30:35 +00:00
perm := int32(v1.ConfigMapVolumeSourceDefaultMode)
2016-08-10 18:53:55 +00:00
obj.DefaultMode = &perm
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_DownwardAPIVolumeSource(obj *v1.DownwardAPIVolumeSource) {
2016-08-10 18:53:55 +00:00
if obj.DefaultMode == nil {
2017-07-16 19:30:35 +00:00
perm := int32(v1.DownwardAPIVolumeSourceDefaultMode)
2016-08-10 18:53:55 +00:00
obj.DefaultMode = &perm
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Secret(obj *v1.Secret) {
2016-05-09 18:53:58 +00:00
if obj.Type == "" {
2017-07-16 19:30:35 +00:00
obj.Type = v1.SecretTypeOpaque
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ProjectedVolumeSource(obj *v1.ProjectedVolumeSource) {
2017-04-01 14:42:02 +00:00
if obj.DefaultMode == nil {
2017-07-16 19:30:35 +00:00
perm := int32(v1.ProjectedVolumeSourceDefaultMode)
2017-04-01 14:42:02 +00:00
obj.DefaultMode = &perm
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_PersistentVolume(obj *v1.PersistentVolume) {
2016-05-09 18:53:58 +00:00
if obj.Status.Phase == "" {
2017-07-16 19:30:35 +00:00
obj.Status.Phase = v1.VolumePending
2016-05-09 18:53:58 +00:00
}
if obj.Spec.PersistentVolumeReclaimPolicy == "" {
2017-07-16 19:30:35 +00:00
obj.Spec.PersistentVolumeReclaimPolicy = v1.PersistentVolumeReclaimRetain
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_PersistentVolumeClaim(obj *v1.PersistentVolumeClaim) {
2016-05-09 18:53:58 +00:00
if obj.Status.Phase == "" {
2017-07-16 19:30:35 +00:00
obj.Status.Phase = v1.ClaimPending
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ISCSIVolumeSource(obj *v1.ISCSIVolumeSource) {
2016-05-09 18:53:58 +00:00
if obj.ISCSIInterface == "" {
obj.ISCSIInterface = "default"
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_AzureDiskVolumeSource(obj *v1.AzureDiskVolumeSource) {
2016-08-10 18:53:55 +00:00
if obj.CachingMode == nil {
2017-07-16 19:30:35 +00:00
obj.CachingMode = new(v1.AzureDataDiskCachingMode)
*obj.CachingMode = v1.AzureDataDiskCachingReadWrite
2017-05-21 00:11:38 +00:00
}
if obj.Kind == nil {
2017-07-16 19:30:35 +00:00
obj.Kind = new(v1.AzureDataDiskKind)
*obj.Kind = v1.AzureSharedBlobDisk
2016-08-10 18:53:55 +00:00
}
if obj.FSType == nil {
obj.FSType = new(string)
*obj.FSType = "ext4"
}
if obj.ReadOnly == nil {
obj.ReadOnly = new(bool)
*obj.ReadOnly = false
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Endpoints(obj *v1.Endpoints) {
2016-05-09 18:53:58 +00:00
for i := range obj.Subsets {
ss := &obj.Subsets[i]
for i := range ss.Ports {
ep := &ss.Ports[i]
if ep.Protocol == "" {
2017-07-16 19:30:35 +00:00
ep.Protocol = v1.ProtocolTCP
2016-02-22 00:13:08 +00:00
}
2016-05-09 18:53:58 +00:00
}
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_HTTPGetAction(obj *v1.HTTPGetAction) {
2016-05-09 18:53:58 +00:00
if obj.Path == "" {
obj.Path = "/"
}
if obj.Scheme == "" {
2017-07-16 19:30:35 +00:00
obj.Scheme = v1.URISchemeHTTP
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_NamespaceStatus(obj *v1.NamespaceStatus) {
2016-05-09 18:53:58 +00:00
if obj.Phase == "" {
2017-07-16 19:30:35 +00:00
obj.Phase = v1.NamespaceActive
2016-05-09 18:53:58 +00:00
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_Node(obj *v1.Node) {
2016-05-09 18:53:58 +00:00
if obj.Spec.ExternalID == "" {
obj.Spec.ExternalID = obj.Name
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_NodeStatus(obj *v1.NodeStatus) {
2016-05-09 18:53:58 +00:00
if obj.Allocatable == nil && obj.Capacity != nil {
2017-07-16 19:30:35 +00:00
obj.Allocatable = make(v1.ResourceList, len(obj.Capacity))
2016-05-09 18:53:58 +00:00
for key, value := range obj.Capacity {
obj.Allocatable[key] = *(value.Copy())
}
obj.Allocatable = obj.Capacity
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ObjectFieldSelector(obj *v1.ObjectFieldSelector) {
2016-05-09 18:53:58 +00:00
if obj.APIVersion == "" {
obj.APIVersion = "v1"
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_LimitRangeItem(obj *v1.LimitRangeItem) {
2016-05-09 18:53:58 +00:00
// for container limits, we apply default values
2017-07-16 19:30:35 +00:00
if obj.Type == v1.LimitTypeContainer {
2016-02-22 00:13:08 +00:00
2016-05-09 18:53:58 +00:00
if obj.Default == nil {
2017-07-16 19:30:35 +00:00
obj.Default = make(v1.ResourceList)
2016-05-09 18:53:58 +00:00
}
if obj.DefaultRequest == nil {
2017-07-16 19:30:35 +00:00
obj.DefaultRequest = make(v1.ResourceList)
2016-05-09 18:53:58 +00:00
}
2016-02-22 00:13:08 +00:00
2016-05-09 18:53:58 +00:00
// If a default limit is unspecified, but the max is specified, default the limit to the max
for key, value := range obj.Max {
if _, exists := obj.Default[key]; !exists {
obj.Default[key] = *(value.Copy())
}
}
// If a default limit is specified, but the default request is not, default request to limit
for key, value := range obj.Default {
if _, exists := obj.DefaultRequest[key]; !exists {
obj.DefaultRequest[key] = *(value.Copy())
2016-02-22 00:13:08 +00:00
}
2016-05-09 18:53:58 +00:00
}
// If a default request is not specified, but the min is provided, default request to the min
for key, value := range obj.Min {
if _, exists := obj.DefaultRequest[key]; !exists {
obj.DefaultRequest[key] = *(value.Copy())
2016-02-22 00:13:08 +00:00
}
2016-05-09 18:53:58 +00:00
}
}
}
2017-07-16 19:30:35 +00:00
func SetDefaults_ConfigMap(obj *v1.ConfigMap) {
2016-05-09 18:53:58 +00:00
if obj.Data == nil {
obj.Data = make(map[string]string)
}
2016-02-22 00:13:08 +00:00
}
// With host networking default all container ports to host ports.
2017-07-16 19:30:35 +00:00
func defaultHostNetworkPorts(containers *[]v1.Container) {
2016-02-22 00:13:08 +00:00
for i := range *containers {
for j := range (*containers)[i].Ports {
if (*containers)[i].Ports[j].HostPort == 0 {
(*containers)[i].Ports[j].HostPort = (*containers)[i].Ports[j].ContainerPort
}
}
}
}
2016-06-21 18:58:43 +00:00
2017-07-16 19:30:35 +00:00
func SetDefaults_RBDVolumeSource(obj *v1.RBDVolumeSource) {
2016-06-21 18:58:43 +00:00
if obj.RBDPool == "" {
obj.RBDPool = "rbd"
}
if obj.RadosUser == "" {
obj.RadosUser = "admin"
}
if obj.Keyring == "" {
obj.Keyring = "/etc/ceph/keyring"
}
}
2017-04-01 14:42:02 +00:00
2017-07-16 19:30:35 +00:00
func SetDefaults_ScaleIOVolumeSource(obj *v1.ScaleIOVolumeSource) {
2017-04-01 14:42:02 +00:00
if obj.ProtectionDomain == "" {
obj.ProtectionDomain = "default"
}
if obj.StoragePool == "" {
obj.StoragePool = "default"
}
if obj.StorageMode == "" {
obj.StorageMode = "ThinProvisioned"
}
if obj.FSType == "" {
obj.FSType = "xfs"
}
}