defaults.go 9.0 KB

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