sigs.k8s.io/kubebuilder/v3@v3.14.0/hack/docs/internal/cronjob-tutorial/api_design.go (about) 1 /* 2 Copyright 2023 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 cronjob 18 19 const CronjobSpecExplaination = ` 20 21 // +kubebuilder:docs-gen:collapse=Imports 22 23 /* 24 First, let's take a look at our spec. As we discussed before, spec holds 25 *desired state*, so any "inputs" to our controller go here. 26 27 Fundamentally a CronJob needs the following pieces: 28 29 - A schedule (the *cron* in CronJob) 30 - A template for the Job to run (the 31 *job* in CronJob) 32 33 We'll also want a few extras, which will make our users' lives easier: 34 35 - A deadline for starting jobs (if we miss this deadline, we'll just wait till 36 the next scheduled time) 37 - What to do if multiple jobs would run at once (do we wait? stop the old one? run both?) 38 - A way to pause the running of a CronJob, in case something's wrong with it 39 - Limits on old job history 40 41 Remember, since we never read our own status, we need to have some other way to 42 keep track of whether a job has run. We can use at least one old job to do 43 this. 44 45 We'll use several markers (` + "`" + `// +comment` + "`" + `) to specify additional metadata. These 46 will be used by [controller-tools](https://github.com/kubernetes-sigs/controller-tools) when generating our CRD manifest. 47 As we'll see in a bit, controller-tools will also use GoDoc to form descriptions for 48 the fields. 49 */ 50 ` 51 52 const CronjobSpecStruct = ` 53 //+kubebuilder:validation:MinLength=0 54 55 // The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron. 56 Schedule string` + " `" + `json:"schedule"` + "`" + ` 57 58 //+kubebuilder:validation:Minimum=0 59 60 // Optional deadline in seconds for starting the job if it misses scheduled 61 // time for any reason. Missed jobs executions will be counted as failed ones. 62 // +optional 63 StartingDeadlineSeconds *int64` + " `" + `json:"startingDeadlineSeconds,omitempty"` + "`" + ` 64 65 // Specifies how to treat concurrent executions of a Job. 66 // Valid values are: 67 // - "Allow" (default): allows CronJobs to run concurrently; 68 // - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet; 69 // - "Replace": cancels currently running job and replaces it with a new one 70 // +optional 71 ConcurrencyPolicy ConcurrencyPolicy` + " `" + `json:"concurrencyPolicy,omitempty"` + "`" + ` 72 73 // This flag tells the controller to suspend subsequent executions, it does 74 // not apply to already started executions. Defaults to false. 75 // +optional 76 Suspend *bool` + " `" + `json:"suspend,omitempty"` + "`" + ` 77 78 // Specifies the job that will be created when executing a CronJob. 79 JobTemplate batchv1.JobTemplateSpec` + " `" + `json:"jobTemplate"` + "`" + ` 80 81 //+kubebuilder:validation:Minimum=0 82 83 // The number of successful finished jobs to retain. 84 // This is a pointer to distinguish between explicit zero and not specified. 85 // +optional 86 SuccessfulJobsHistoryLimit *int32` + " `" + `json:"successfulJobsHistoryLimit,omitempty"` + "`" + ` 87 88 //+kubebuilder:validation:Minimum=0 89 90 // The number of failed finished jobs to retain. 91 // This is a pointer to distinguish between explicit zero and not specified. 92 // +optional 93 FailedJobsHistoryLimit *int32` + " `" + `json:"failedJobsHistoryLimit,omitempty"` + "`" + ` 94 } 95 96 /* 97 We define a custom type to hold our concurrency policy. It's actually 98 just a string under the hood, but the type gives extra documentation, 99 and allows us to attach validation on the type instead of the field, 100 making the validation more easily reusable. 101 */ 102 103 // ConcurrencyPolicy describes how the job will be handled. 104 // Only one of the following concurrent policies may be specified. 105 // If none of the following policies is specified, the default one 106 // is AllowConcurrent. 107 // +kubebuilder:validation:Enum=Allow;Forbid;Replace 108 type ConcurrencyPolicy string 109 110 const ( 111 // AllowConcurrent allows CronJobs to run concurrently. 112 AllowConcurrent ConcurrencyPolicy = "Allow" 113 114 // ForbidConcurrent forbids concurrent runs, skipping next run if previous 115 // hasn't finished yet. 116 ForbidConcurrent ConcurrencyPolicy = "Forbid" 117 118 // ReplaceConcurrent cancels currently running job and replaces it with a new one. 119 ReplaceConcurrent ConcurrencyPolicy = "Replace" 120 ) 121 122 /* 123 Next, let's design our status, which holds observed state. It contains any information 124 we want users or other controllers to be able to easily obtain. 125 126 We'll keep a list of actively running jobs, as well as the last time that we successfully 127 ran our job. Notice that we use` + " `" + `metav1.Time` + "`" + ` instead of` + " `" + `time.Time` + "`" + ` to get the stable 128 serialization, as mentioned above. 129 */` 130 131 const CronjobList = ` 132 133 // A list of pointers to currently running jobs. 134 // +optional 135 Active []corev1.ObjectReference` + " `" + `json:"active,omitempty"` + "`" + ` 136 137 // Information when was the last time the job was successfully scheduled. 138 // +optional 139 LastScheduleTime *metav1.Time` + " `" + `json:"lastScheduleTime,omitempty"` + "`" + ` 140 } 141 142 /* 143 Finally, we have the rest of the boilerplate that we've already discussed. 144 As previously noted, we don't need to change this, except to mark that 145 we want a status subresource, so that we behave like built-in kubernetes types. 146 */ 147 148 `