k8s.io/kubernetes@v1.29.3/pkg/apis/batch/v1/defaults.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 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 "math" 21 22 batchv1 "k8s.io/api/batch/v1" 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 utilfeature "k8s.io/apiserver/pkg/util/feature" 26 "k8s.io/kubernetes/pkg/features" 27 utilpointer "k8s.io/utils/pointer" 28 ) 29 30 func addDefaultingFuncs(scheme *runtime.Scheme) error { 31 return RegisterDefaults(scheme) 32 } 33 34 func SetDefaults_Job(obj *batchv1.Job) { 35 // For a non-parallel job, you can leave both `.spec.completions` and 36 // `.spec.parallelism` unset. When both are unset, both are defaulted to 1. 37 if obj.Spec.Completions == nil && obj.Spec.Parallelism == nil { 38 obj.Spec.Completions = utilpointer.Int32(1) 39 obj.Spec.Parallelism = utilpointer.Int32(1) 40 } 41 if obj.Spec.Parallelism == nil { 42 obj.Spec.Parallelism = utilpointer.Int32(1) 43 } 44 if obj.Spec.BackoffLimit == nil { 45 if obj.Spec.BackoffLimitPerIndex != nil { 46 obj.Spec.BackoffLimit = utilpointer.Int32(math.MaxInt32) 47 } else { 48 obj.Spec.BackoffLimit = utilpointer.Int32(6) 49 } 50 } 51 labels := obj.Spec.Template.Labels 52 if labels != nil && len(obj.Labels) == 0 { 53 obj.Labels = labels 54 } 55 if obj.Spec.CompletionMode == nil { 56 mode := batchv1.NonIndexedCompletion 57 obj.Spec.CompletionMode = &mode 58 } 59 if obj.Spec.Suspend == nil { 60 obj.Spec.Suspend = utilpointer.Bool(false) 61 } 62 if obj.Spec.PodFailurePolicy != nil { 63 for _, rule := range obj.Spec.PodFailurePolicy.Rules { 64 if rule.OnPodConditions != nil { 65 for i, pattern := range rule.OnPodConditions { 66 if pattern.Status == "" { 67 rule.OnPodConditions[i].Status = corev1.ConditionTrue 68 } 69 } 70 } 71 } 72 } 73 if utilfeature.DefaultFeatureGate.Enabled(features.JobPodReplacementPolicy) { 74 if obj.Spec.PodReplacementPolicy == nil { 75 if obj.Spec.PodFailurePolicy != nil { 76 obj.Spec.PodReplacementPolicy = podReplacementPolicyPtr(batchv1.Failed) 77 } else { 78 obj.Spec.PodReplacementPolicy = podReplacementPolicyPtr(batchv1.TerminatingOrFailed) 79 } 80 } 81 } 82 if obj.Spec.ManualSelector == nil { 83 obj.Spec.ManualSelector = utilpointer.Bool(false) 84 } 85 } 86 87 func SetDefaults_CronJob(obj *batchv1.CronJob) { 88 if obj.Spec.ConcurrencyPolicy == "" { 89 obj.Spec.ConcurrencyPolicy = batchv1.AllowConcurrent 90 } 91 if obj.Spec.Suspend == nil { 92 obj.Spec.Suspend = utilpointer.Bool(false) 93 } 94 if obj.Spec.SuccessfulJobsHistoryLimit == nil { 95 obj.Spec.SuccessfulJobsHistoryLimit = utilpointer.Int32(3) 96 } 97 if obj.Spec.FailedJobsHistoryLimit == nil { 98 obj.Spec.FailedJobsHistoryLimit = utilpointer.Int32(1) 99 } 100 } 101 102 func podReplacementPolicyPtr(obj batchv1.PodReplacementPolicy) *batchv1.PodReplacementPolicy { 103 return &obj 104 }