defaults.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. "time"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/intstr"
  19. "k8s.io/kubernetes/pkg/util/parsers"
  20. utilpointer "k8s.io/utils/pointer"
  21. )
  22. func addDefaultingFuncs(scheme *runtime.Scheme) error {
  23. return RegisterDefaults(scheme)
  24. }
  25. func SetDefaults_ResourceList(obj *v1.ResourceList) {
  26. for key, val := range *obj {
  27. // TODO(#18538): We round up resource values to milli scale to maintain API compatibility.
  28. // In the future, we should instead reject values that need rounding.
  29. const milliScale = -3
  30. val.RoundUp(milliScale)
  31. (*obj)[v1.ResourceName(key)] = val
  32. }
  33. }
  34. func SetDefaults_ReplicationController(obj *v1.ReplicationController) {
  35. var labels map[string]string
  36. if obj.Spec.Template != nil {
  37. labels = obj.Spec.Template.Labels
  38. }
  39. // TODO: support templates defined elsewhere when we support them in the API
  40. if labels != nil {
  41. if len(obj.Spec.Selector) == 0 {
  42. obj.Spec.Selector = labels
  43. }
  44. if len(obj.Labels) == 0 {
  45. obj.Labels = labels
  46. }
  47. }
  48. if obj.Spec.Replicas == nil {
  49. obj.Spec.Replicas = new(int32)
  50. *obj.Spec.Replicas = 1
  51. }
  52. }
  53. func SetDefaults_Volume(obj *v1.Volume) {
  54. if utilpointer.AllPtrFieldsNil(&obj.VolumeSource) {
  55. obj.VolumeSource = v1.VolumeSource{
  56. EmptyDir: &v1.EmptyDirVolumeSource{},
  57. }
  58. }
  59. }
  60. func SetDefaults_ContainerPort(obj *v1.ContainerPort) {
  61. if obj.Protocol == "" {
  62. obj.Protocol = v1.ProtocolTCP
  63. }
  64. }
  65. func SetDefaults_Container(obj *v1.Container) {
  66. if obj.ImagePullPolicy == "" {
  67. // Ignore error and assume it has been validated elsewhere
  68. _, tag, _, _ := parsers.ParseImageName(obj.Image)
  69. // Check image tag
  70. if tag == "latest" {
  71. obj.ImagePullPolicy = v1.PullAlways
  72. } else {
  73. obj.ImagePullPolicy = v1.PullIfNotPresent
  74. }
  75. }
  76. if obj.TerminationMessagePath == "" {
  77. obj.TerminationMessagePath = v1.TerminationMessagePathDefault
  78. }
  79. if obj.TerminationMessagePolicy == "" {
  80. obj.TerminationMessagePolicy = v1.TerminationMessageReadFile
  81. }
  82. }
  83. func SetDefaults_Service(obj *v1.Service) {
  84. if obj.Spec.SessionAffinity == "" {
  85. obj.Spec.SessionAffinity = v1.ServiceAffinityNone
  86. }
  87. if obj.Spec.SessionAffinity == v1.ServiceAffinityNone {
  88. obj.Spec.SessionAffinityConfig = nil
  89. }
  90. if obj.Spec.SessionAffinity == v1.ServiceAffinityClientIP {
  91. if obj.Spec.SessionAffinityConfig == nil || obj.Spec.SessionAffinityConfig.ClientIP == nil || obj.Spec.SessionAffinityConfig.ClientIP.TimeoutSeconds == nil {
  92. timeoutSeconds := v1.DefaultClientIPServiceAffinitySeconds
  93. obj.Spec.SessionAffinityConfig = &v1.SessionAffinityConfig{
  94. ClientIP: &v1.ClientIPConfig{
  95. TimeoutSeconds: &timeoutSeconds,
  96. },
  97. }
  98. }
  99. }
  100. if obj.Spec.Type == "" {
  101. obj.Spec.Type = v1.ServiceTypeClusterIP
  102. }
  103. for i := range obj.Spec.Ports {
  104. sp := &obj.Spec.Ports[i]
  105. if sp.Protocol == "" {
  106. sp.Protocol = v1.ProtocolTCP
  107. }
  108. if sp.TargetPort == intstr.FromInt(0) || sp.TargetPort == intstr.FromString("") {
  109. sp.TargetPort = intstr.FromInt(int(sp.Port))
  110. }
  111. }
  112. // Defaults ExternalTrafficPolicy field for NodePort / LoadBalancer service
  113. // to Global for consistency.
  114. if (obj.Spec.Type == v1.ServiceTypeNodePort ||
  115. obj.Spec.Type == v1.ServiceTypeLoadBalancer) &&
  116. obj.Spec.ExternalTrafficPolicy == "" {
  117. obj.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeCluster
  118. }
  119. }
  120. func SetDefaults_Pod(obj *v1.Pod) {
  121. // If limits are specified, but requests are not, default requests to limits
  122. // This is done here rather than a more specific defaulting pass on v1.ResourceRequirements
  123. // because we only want this defaulting semantic to take place on a v1.Pod and not a v1.PodTemplate
  124. for i := range obj.Spec.Containers {
  125. // set requests to limits if requests are not specified, but limits are
  126. if obj.Spec.Containers[i].Resources.Limits != nil {
  127. if obj.Spec.Containers[i].Resources.Requests == nil {
  128. obj.Spec.Containers[i].Resources.Requests = make(v1.ResourceList)
  129. }
  130. for key, value := range obj.Spec.Containers[i].Resources.Limits {
  131. if _, exists := obj.Spec.Containers[i].Resources.Requests[key]; !exists {
  132. obj.Spec.Containers[i].Resources.Requests[key] = *(value.Copy())
  133. }
  134. }
  135. }
  136. }
  137. for i := range obj.Spec.InitContainers {
  138. if obj.Spec.InitContainers[i].Resources.Limits != nil {
  139. if obj.Spec.InitContainers[i].Resources.Requests == nil {
  140. obj.Spec.InitContainers[i].Resources.Requests = make(v1.ResourceList)
  141. }
  142. for key, value := range obj.Spec.InitContainers[i].Resources.Limits {
  143. if _, exists := obj.Spec.InitContainers[i].Resources.Requests[key]; !exists {
  144. obj.Spec.InitContainers[i].Resources.Requests[key] = *(value.Copy())
  145. }
  146. }
  147. }
  148. }
  149. if obj.Spec.EnableServiceLinks == nil {
  150. enableServiceLinks := v1.DefaultEnableServiceLinks
  151. obj.Spec.EnableServiceLinks = &enableServiceLinks
  152. }
  153. }
  154. func SetDefaults_PodSpec(obj *v1.PodSpec) {
  155. // New fields added here will break upgrade tests:
  156. // https://github.com/kubernetes/kubernetes/issues/69445
  157. // In most cases the new defaulted field can added to SetDefaults_Pod instead of here, so
  158. // that it only materializes in the Pod object and not all objects with a PodSpec field.
  159. if obj.DNSPolicy == "" {
  160. obj.DNSPolicy = v1.DNSClusterFirst
  161. }
  162. if obj.RestartPolicy == "" {
  163. obj.RestartPolicy = v1.RestartPolicyAlways
  164. }
  165. if obj.HostNetwork {
  166. defaultHostNetworkPorts(&obj.Containers)
  167. defaultHostNetworkPorts(&obj.InitContainers)
  168. }
  169. if obj.SecurityContext == nil {
  170. obj.SecurityContext = &v1.PodSecurityContext{}
  171. }
  172. if obj.TerminationGracePeriodSeconds == nil {
  173. period := int64(v1.DefaultTerminationGracePeriodSeconds)
  174. obj.TerminationGracePeriodSeconds = &period
  175. }
  176. if obj.SchedulerName == "" {
  177. obj.SchedulerName = v1.DefaultSchedulerName
  178. }
  179. }
  180. func SetDefaults_Probe(obj *v1.Probe) {
  181. if obj.TimeoutSeconds == 0 {
  182. obj.TimeoutSeconds = 1
  183. }
  184. if obj.PeriodSeconds == 0 {
  185. obj.PeriodSeconds = 10
  186. }
  187. if obj.SuccessThreshold == 0 {
  188. obj.SuccessThreshold = 1
  189. }
  190. if obj.FailureThreshold == 0 {
  191. obj.FailureThreshold = 3
  192. }
  193. }
  194. func SetDefaults_SecretVolumeSource(obj *v1.SecretVolumeSource) {
  195. if obj.DefaultMode == nil {
  196. perm := int32(v1.SecretVolumeSourceDefaultMode)
  197. obj.DefaultMode = &perm
  198. }
  199. }
  200. func SetDefaults_ConfigMapVolumeSource(obj *v1.ConfigMapVolumeSource) {
  201. if obj.DefaultMode == nil {
  202. perm := int32(v1.ConfigMapVolumeSourceDefaultMode)
  203. obj.DefaultMode = &perm
  204. }
  205. }
  206. func SetDefaults_DownwardAPIVolumeSource(obj *v1.DownwardAPIVolumeSource) {
  207. if obj.DefaultMode == nil {
  208. perm := int32(v1.DownwardAPIVolumeSourceDefaultMode)
  209. obj.DefaultMode = &perm
  210. }
  211. }
  212. func SetDefaults_Secret(obj *v1.Secret) {
  213. if obj.Type == "" {
  214. obj.Type = v1.SecretTypeOpaque
  215. }
  216. }
  217. func SetDefaults_ProjectedVolumeSource(obj *v1.ProjectedVolumeSource) {
  218. if obj.DefaultMode == nil {
  219. perm := int32(v1.ProjectedVolumeSourceDefaultMode)
  220. obj.DefaultMode = &perm
  221. }
  222. }
  223. func SetDefaults_ServiceAccountTokenProjection(obj *v1.ServiceAccountTokenProjection) {
  224. hour := int64(time.Hour.Seconds())
  225. if obj.ExpirationSeconds == nil {
  226. obj.ExpirationSeconds = &hour
  227. }
  228. }
  229. func SetDefaults_PersistentVolume(obj *v1.PersistentVolume) {
  230. if obj.Status.Phase == "" {
  231. obj.Status.Phase = v1.VolumePending
  232. }
  233. if obj.Spec.PersistentVolumeReclaimPolicy == "" {
  234. obj.Spec.PersistentVolumeReclaimPolicy = v1.PersistentVolumeReclaimRetain
  235. }
  236. if obj.Spec.VolumeMode == nil {
  237. obj.Spec.VolumeMode = new(v1.PersistentVolumeMode)
  238. *obj.Spec.VolumeMode = v1.PersistentVolumeFilesystem
  239. }
  240. }
  241. func SetDefaults_PersistentVolumeClaim(obj *v1.PersistentVolumeClaim) {
  242. if obj.Status.Phase == "" {
  243. obj.Status.Phase = v1.ClaimPending
  244. }
  245. if obj.Spec.VolumeMode == nil {
  246. obj.Spec.VolumeMode = new(v1.PersistentVolumeMode)
  247. *obj.Spec.VolumeMode = v1.PersistentVolumeFilesystem
  248. }
  249. }
  250. func SetDefaults_ISCSIVolumeSource(obj *v1.ISCSIVolumeSource) {
  251. if obj.ISCSIInterface == "" {
  252. obj.ISCSIInterface = "default"
  253. }
  254. }
  255. func SetDefaults_ISCSIPersistentVolumeSource(obj *v1.ISCSIPersistentVolumeSource) {
  256. if obj.ISCSIInterface == "" {
  257. obj.ISCSIInterface = "default"
  258. }
  259. }
  260. func SetDefaults_AzureDiskVolumeSource(obj *v1.AzureDiskVolumeSource) {
  261. if obj.CachingMode == nil {
  262. obj.CachingMode = new(v1.AzureDataDiskCachingMode)
  263. *obj.CachingMode = v1.AzureDataDiskCachingReadWrite
  264. }
  265. if obj.Kind == nil {
  266. obj.Kind = new(v1.AzureDataDiskKind)
  267. *obj.Kind = v1.AzureSharedBlobDisk
  268. }
  269. if obj.FSType == nil {
  270. obj.FSType = new(string)
  271. *obj.FSType = "ext4"
  272. }
  273. if obj.ReadOnly == nil {
  274. obj.ReadOnly = new(bool)
  275. *obj.ReadOnly = false
  276. }
  277. }
  278. func SetDefaults_Endpoints(obj *v1.Endpoints) {
  279. for i := range obj.Subsets {
  280. ss := &obj.Subsets[i]
  281. for i := range ss.Ports {
  282. ep := &ss.Ports[i]
  283. if ep.Protocol == "" {
  284. ep.Protocol = v1.ProtocolTCP
  285. }
  286. }
  287. }
  288. }
  289. func SetDefaults_HTTPGetAction(obj *v1.HTTPGetAction) {
  290. if obj.Path == "" {
  291. obj.Path = "/"
  292. }
  293. if obj.Scheme == "" {
  294. obj.Scheme = v1.URISchemeHTTP
  295. }
  296. }
  297. func SetDefaults_NamespaceStatus(obj *v1.NamespaceStatus) {
  298. if obj.Phase == "" {
  299. obj.Phase = v1.NamespaceActive
  300. }
  301. }
  302. func SetDefaults_NodeStatus(obj *v1.NodeStatus) {
  303. if obj.Allocatable == nil && obj.Capacity != nil {
  304. obj.Allocatable = make(v1.ResourceList, len(obj.Capacity))
  305. for key, value := range obj.Capacity {
  306. obj.Allocatable[key] = *(value.Copy())
  307. }
  308. obj.Allocatable = obj.Capacity
  309. }
  310. }
  311. func SetDefaults_ObjectFieldSelector(obj *v1.ObjectFieldSelector) {
  312. if obj.APIVersion == "" {
  313. obj.APIVersion = "v1"
  314. }
  315. }
  316. func SetDefaults_LimitRangeItem(obj *v1.LimitRangeItem) {
  317. // for container limits, we apply default values
  318. if obj.Type == v1.LimitTypeContainer {
  319. if obj.Default == nil {
  320. obj.Default = make(v1.ResourceList)
  321. }
  322. if obj.DefaultRequest == nil {
  323. obj.DefaultRequest = make(v1.ResourceList)
  324. }
  325. // If a default limit is unspecified, but the max is specified, default the limit to the max
  326. for key, value := range obj.Max {
  327. if _, exists := obj.Default[key]; !exists {
  328. obj.Default[key] = *(value.Copy())
  329. }
  330. }
  331. // If a default limit is specified, but the default request is not, default request to limit
  332. for key, value := range obj.Default {
  333. if _, exists := obj.DefaultRequest[key]; !exists {
  334. obj.DefaultRequest[key] = *(value.Copy())
  335. }
  336. }
  337. // If a default request is not specified, but the min is provided, default request to the min
  338. for key, value := range obj.Min {
  339. if _, exists := obj.DefaultRequest[key]; !exists {
  340. obj.DefaultRequest[key] = *(value.Copy())
  341. }
  342. }
  343. }
  344. }
  345. func SetDefaults_ConfigMap(obj *v1.ConfigMap) {
  346. if obj.Data == nil {
  347. obj.Data = make(map[string]string)
  348. }
  349. }
  350. // With host networking default all container ports to host ports.
  351. func defaultHostNetworkPorts(containers *[]v1.Container) {
  352. for i := range *containers {
  353. for j := range (*containers)[i].Ports {
  354. if (*containers)[i].Ports[j].HostPort == 0 {
  355. (*containers)[i].Ports[j].HostPort = (*containers)[i].Ports[j].ContainerPort
  356. }
  357. }
  358. }
  359. }
  360. func SetDefaults_RBDVolumeSource(obj *v1.RBDVolumeSource) {
  361. if obj.RBDPool == "" {
  362. obj.RBDPool = "rbd"
  363. }
  364. if obj.RadosUser == "" {
  365. obj.RadosUser = "admin"
  366. }
  367. if obj.Keyring == "" {
  368. obj.Keyring = "/etc/ceph/keyring"
  369. }
  370. }
  371. func SetDefaults_RBDPersistentVolumeSource(obj *v1.RBDPersistentVolumeSource) {
  372. if obj.RBDPool == "" {
  373. obj.RBDPool = "rbd"
  374. }
  375. if obj.RadosUser == "" {
  376. obj.RadosUser = "admin"
  377. }
  378. if obj.Keyring == "" {
  379. obj.Keyring = "/etc/ceph/keyring"
  380. }
  381. }
  382. func SetDefaults_ScaleIOVolumeSource(obj *v1.ScaleIOVolumeSource) {
  383. if obj.StorageMode == "" {
  384. obj.StorageMode = "ThinProvisioned"
  385. }
  386. if obj.FSType == "" {
  387. obj.FSType = "xfs"
  388. }
  389. }
  390. func SetDefaults_ScaleIOPersistentVolumeSource(obj *v1.ScaleIOPersistentVolumeSource) {
  391. if obj.StorageMode == "" {
  392. obj.StorageMode = "ThinProvisioned"
  393. }
  394. if obj.FSType == "" {
  395. obj.FSType = "xfs"
  396. }
  397. }
  398. func SetDefaults_HostPathVolumeSource(obj *v1.HostPathVolumeSource) {
  399. typeVol := v1.HostPathUnset
  400. if obj.Type == nil {
  401. obj.Type = &typeVol
  402. }
  403. }