github.com/enmand/kubernetes@v1.2.0-alpha.0/pkg/apis/experimental/v1alpha1/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 v1alpha1 18 19 import ( 20 "k8s.io/kubernetes/pkg/api" 21 "k8s.io/kubernetes/pkg/util" 22 ) 23 24 func addDefaultingFuncs() { 25 api.Scheme.AddDefaultingFuncs( 26 func(obj *APIVersion) { 27 if len(obj.APIGroup) == 0 { 28 obj.APIGroup = "experimental" 29 } 30 }, 31 func(obj *DaemonSet) { 32 var labels map[string]string 33 if obj.Spec.Template != nil { 34 labels = obj.Spec.Template.Labels 35 } 36 // TODO: support templates defined elsewhere when we support them in the API 37 if labels != nil { 38 if len(obj.Spec.Selector) == 0 { 39 obj.Spec.Selector = labels 40 } 41 if len(obj.Labels) == 0 { 42 obj.Labels = labels 43 } 44 } 45 }, 46 func(obj *Deployment) { 47 // Set DeploymentSpec.Replicas to 1 if it is not set. 48 if obj.Spec.Replicas == nil { 49 obj.Spec.Replicas = new(int) 50 *obj.Spec.Replicas = 1 51 } 52 strategy := &obj.Spec.Strategy 53 // Set default DeploymentStrategyType as RollingUpdate. 54 if strategy.Type == "" { 55 strategy.Type = RollingUpdateDeploymentStrategyType 56 } 57 if strategy.Type == RollingUpdateDeploymentStrategyType { 58 if strategy.RollingUpdate == nil { 59 rollingUpdate := RollingUpdateDeployment{} 60 strategy.RollingUpdate = &rollingUpdate 61 } 62 if strategy.RollingUpdate.MaxUnavailable == nil { 63 // Set default MaxUnavailable as 1 by default. 64 maxUnavailable := util.NewIntOrStringFromInt(1) 65 strategy.RollingUpdate.MaxUnavailable = &maxUnavailable 66 } 67 if strategy.RollingUpdate.MaxSurge == nil { 68 // Set default MaxSurge as 1 by default. 69 maxSurge := util.NewIntOrStringFromInt(1) 70 strategy.RollingUpdate.MaxSurge = &maxSurge 71 } 72 } 73 if obj.Spec.UniqueLabelKey == nil { 74 obj.Spec.UniqueLabelKey = new(string) 75 *obj.Spec.UniqueLabelKey = "deployment.kubernetes.io/podTemplateHash" 76 } 77 }, 78 func(obj *Job) { 79 var labels map[string]string 80 if obj.Spec.Template != nil { 81 labels = obj.Spec.Template.Labels 82 } 83 // TODO: support templates defined elsewhere when we support them in the API 84 if labels != nil { 85 if len(obj.Spec.Selector) == 0 { 86 obj.Spec.Selector = labels 87 } 88 if len(obj.Labels) == 0 { 89 obj.Labels = labels 90 } 91 } 92 if obj.Spec.Completions == nil { 93 completions := 1 94 obj.Spec.Completions = &completions 95 } 96 if obj.Spec.Parallelism == nil { 97 obj.Spec.Parallelism = obj.Spec.Completions 98 } 99 }, 100 ) 101 }