github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/v1/defaults.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1 18 19 import ( 20 "k8s.io/kubernetes/pkg/runtime" 21 "k8s.io/kubernetes/pkg/util" 22 "k8s.io/kubernetes/pkg/util/intstr" 23 "k8s.io/kubernetes/pkg/util/parsers" 24 ) 25 26 func addDefaultingFuncs(scheme *runtime.Scheme) { 27 scheme.AddDefaultingFuncs( 28 SetDefaults_PodExecOptions, 29 SetDefaults_PodAttachOptions, 30 SetDefaults_ReplicationController, 31 SetDefaults_Volume, 32 SetDefaults_ContainerPort, 33 SetDefaults_Container, 34 SetDefaults_ServiceSpec, 35 SetDefaults_Pod, 36 SetDefaults_PodSpec, 37 SetDefaults_Probe, 38 SetDefaults_Secret, 39 SetDefaults_PersistentVolume, 40 SetDefaults_PersistentVolumeClaim, 41 SetDefaults_ISCSIVolumeSource, 42 SetDefaults_Endpoints, 43 SetDefaults_HTTPGetAction, 44 SetDefaults_NamespaceStatus, 45 SetDefaults_Node, 46 SetDefaults_NodeStatus, 47 SetDefaults_ObjectFieldSelector, 48 SetDefaults_LimitRangeItem, 49 SetDefaults_ConfigMap, 50 ) 51 } 52 53 func SetDefaults_PodExecOptions(obj *PodExecOptions) { 54 obj.Stdout = true 55 obj.Stderr = true 56 } 57 func SetDefaults_PodAttachOptions(obj *PodAttachOptions) { 58 obj.Stdout = true 59 obj.Stderr = true 60 } 61 func SetDefaults_ReplicationController(obj *ReplicationController) { 62 var labels map[string]string 63 if obj.Spec.Template != nil { 64 labels = obj.Spec.Template.Labels 65 } 66 // TODO: support templates defined elsewhere when we support them in the API 67 if labels != nil { 68 if len(obj.Spec.Selector) == 0 { 69 obj.Spec.Selector = labels 70 } 71 if len(obj.Labels) == 0 { 72 obj.Labels = labels 73 } 74 } 75 if obj.Spec.Replicas == nil { 76 obj.Spec.Replicas = new(int32) 77 *obj.Spec.Replicas = 1 78 } 79 } 80 func SetDefaults_Volume(obj *Volume) { 81 if util.AllPtrFieldsNil(&obj.VolumeSource) { 82 obj.VolumeSource = VolumeSource{ 83 EmptyDir: &EmptyDirVolumeSource{}, 84 } 85 } 86 } 87 func SetDefaults_ContainerPort(obj *ContainerPort) { 88 if obj.Protocol == "" { 89 obj.Protocol = ProtocolTCP 90 } 91 } 92 func SetDefaults_Container(obj *Container) { 93 if obj.ImagePullPolicy == "" { 94 // Ignore error and assume it has been validated elsewhere 95 _, tag, _, _ := parsers.ParseImageName(obj.Image) 96 97 // Check image tag 98 99 if tag == "latest" { 100 obj.ImagePullPolicy = PullAlways 101 } else { 102 obj.ImagePullPolicy = PullIfNotPresent 103 } 104 } 105 if obj.TerminationMessagePath == "" { 106 obj.TerminationMessagePath = TerminationMessagePathDefault 107 } 108 } 109 func SetDefaults_ServiceSpec(obj *ServiceSpec) { 110 if obj.SessionAffinity == "" { 111 obj.SessionAffinity = ServiceAffinityNone 112 } 113 if obj.Type == "" { 114 obj.Type = ServiceTypeClusterIP 115 } 116 for i := range obj.Ports { 117 sp := &obj.Ports[i] 118 if sp.Protocol == "" { 119 sp.Protocol = ProtocolTCP 120 } 121 if sp.TargetPort == intstr.FromInt(0) || sp.TargetPort == intstr.FromString("") { 122 sp.TargetPort = intstr.FromInt(int(sp.Port)) 123 } 124 } 125 } 126 func SetDefaults_Pod(obj *Pod) { 127 // If limits are specified, but requests are not, default requests to limits 128 // This is done here rather than a more specific defaulting pass on ResourceRequirements 129 // because we only want this defaulting semantic to take place on a Pod and not a PodTemplate 130 for i := range obj.Spec.Containers { 131 // set requests to limits if requests are not specified, but limits are 132 if obj.Spec.Containers[i].Resources.Limits != nil { 133 if obj.Spec.Containers[i].Resources.Requests == nil { 134 obj.Spec.Containers[i].Resources.Requests = make(ResourceList) 135 } 136 for key, value := range obj.Spec.Containers[i].Resources.Limits { 137 if _, exists := obj.Spec.Containers[i].Resources.Requests[key]; !exists { 138 obj.Spec.Containers[i].Resources.Requests[key] = *(value.Copy()) 139 } 140 } 141 } 142 } 143 } 144 func SetDefaults_PodSpec(obj *PodSpec) { 145 if obj.DNSPolicy == "" { 146 obj.DNSPolicy = DNSClusterFirst 147 } 148 if obj.RestartPolicy == "" { 149 obj.RestartPolicy = RestartPolicyAlways 150 } 151 if obj.HostNetwork { 152 defaultHostNetworkPorts(&obj.Containers) 153 } 154 if obj.SecurityContext == nil { 155 obj.SecurityContext = &PodSecurityContext{} 156 } 157 if obj.TerminationGracePeriodSeconds == nil { 158 period := int64(DefaultTerminationGracePeriodSeconds) 159 obj.TerminationGracePeriodSeconds = &period 160 } 161 } 162 func SetDefaults_Probe(obj *Probe) { 163 if obj.TimeoutSeconds == 0 { 164 obj.TimeoutSeconds = 1 165 } 166 if obj.PeriodSeconds == 0 { 167 obj.PeriodSeconds = 10 168 } 169 if obj.SuccessThreshold == 0 { 170 obj.SuccessThreshold = 1 171 } 172 if obj.FailureThreshold == 0 { 173 obj.FailureThreshold = 3 174 } 175 } 176 func SetDefaults_Secret(obj *Secret) { 177 if obj.Type == "" { 178 obj.Type = SecretTypeOpaque 179 } 180 } 181 func SetDefaults_PersistentVolume(obj *PersistentVolume) { 182 if obj.Status.Phase == "" { 183 obj.Status.Phase = VolumePending 184 } 185 if obj.Spec.PersistentVolumeReclaimPolicy == "" { 186 obj.Spec.PersistentVolumeReclaimPolicy = PersistentVolumeReclaimRetain 187 } 188 } 189 func SetDefaults_PersistentVolumeClaim(obj *PersistentVolumeClaim) { 190 if obj.Status.Phase == "" { 191 obj.Status.Phase = ClaimPending 192 } 193 } 194 func SetDefaults_ISCSIVolumeSource(obj *ISCSIVolumeSource) { 195 if obj.ISCSIInterface == "" { 196 obj.ISCSIInterface = "default" 197 } 198 } 199 func SetDefaults_Endpoints(obj *Endpoints) { 200 for i := range obj.Subsets { 201 ss := &obj.Subsets[i] 202 for i := range ss.Ports { 203 ep := &ss.Ports[i] 204 if ep.Protocol == "" { 205 ep.Protocol = ProtocolTCP 206 } 207 } 208 } 209 } 210 func SetDefaults_HTTPGetAction(obj *HTTPGetAction) { 211 if obj.Path == "" { 212 obj.Path = "/" 213 } 214 if obj.Scheme == "" { 215 obj.Scheme = URISchemeHTTP 216 } 217 } 218 func SetDefaults_NamespaceStatus(obj *NamespaceStatus) { 219 if obj.Phase == "" { 220 obj.Phase = NamespaceActive 221 } 222 } 223 func SetDefaults_Node(obj *Node) { 224 if obj.Spec.ExternalID == "" { 225 obj.Spec.ExternalID = obj.Name 226 } 227 } 228 func SetDefaults_NodeStatus(obj *NodeStatus) { 229 if obj.Allocatable == nil && obj.Capacity != nil { 230 obj.Allocatable = make(ResourceList, len(obj.Capacity)) 231 for key, value := range obj.Capacity { 232 obj.Allocatable[key] = *(value.Copy()) 233 } 234 obj.Allocatable = obj.Capacity 235 } 236 } 237 func SetDefaults_ObjectFieldSelector(obj *ObjectFieldSelector) { 238 if obj.APIVersion == "" { 239 obj.APIVersion = "v1" 240 } 241 } 242 func SetDefaults_LimitRangeItem(obj *LimitRangeItem) { 243 // for container limits, we apply default values 244 if obj.Type == LimitTypeContainer { 245 246 if obj.Default == nil { 247 obj.Default = make(ResourceList) 248 } 249 if obj.DefaultRequest == nil { 250 obj.DefaultRequest = make(ResourceList) 251 } 252 253 // If a default limit is unspecified, but the max is specified, default the limit to the max 254 for key, value := range obj.Max { 255 if _, exists := obj.Default[key]; !exists { 256 obj.Default[key] = *(value.Copy()) 257 } 258 } 259 // If a default limit is specified, but the default request is not, default request to limit 260 for key, value := range obj.Default { 261 if _, exists := obj.DefaultRequest[key]; !exists { 262 obj.DefaultRequest[key] = *(value.Copy()) 263 } 264 } 265 // If a default request is not specified, but the min is provided, default request to the min 266 for key, value := range obj.Min { 267 if _, exists := obj.DefaultRequest[key]; !exists { 268 obj.DefaultRequest[key] = *(value.Copy()) 269 } 270 } 271 } 272 } 273 func SetDefaults_ConfigMap(obj *ConfigMap) { 274 if obj.Data == nil { 275 obj.Data = make(map[string]string) 276 } 277 } 278 279 // With host networking default all container ports to host ports. 280 func defaultHostNetworkPorts(containers *[]Container) { 281 for i := range *containers { 282 for j := range (*containers)[i].Ports { 283 if (*containers)[i].Ports[j].HostPort == 0 { 284 (*containers)[i].Ports[j].HostPort = (*containers)[i].Ports[j].ContainerPort 285 } 286 } 287 } 288 }