|
| 1 | +/* |
| 2 | +Copyright 2021. |
| 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 v1alpha2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | +) |
| 25 | + |
| 26 | +const defaultWaitDuration = time.Second * 60 |
| 27 | + |
| 28 | +const ( |
| 29 | + BootstrappedAnnotation = "capi.weave.works/bootstrapped" |
| 30 | + BootstrapConfigsAnnotation = "capi.weave.works/bootstrap-configs" |
| 31 | +) |
| 32 | + |
| 33 | +// JobTemplate describes a job to create |
| 34 | +type JobTemplate struct { |
| 35 | + // DNS1032 compatible name used as a prefix when generating the Job from the |
| 36 | + // Spec. |
| 37 | + GenerateName string `json:"generateName"` |
| 38 | + // Specifies the number of retries before marking this job failed. |
| 39 | + // Defaults to 6 |
| 40 | + //+kubebuilder:validation:Optional |
| 41 | + //+kubebuilder:default:=6 |
| 42 | + BackoffLimit *int32 `json:"backoffLimit,optional"` |
| 43 | + |
| 44 | + // ttlSecondsAfterFinished limits the lifetime of a Job that has finished |
| 45 | + // execution (either Complete or Failed). If this field is set, |
| 46 | + // ttlSecondsAfterFinished after the Job finishes, it is eligible to be |
| 47 | + // automatically deleted. When the Job is being deleted, its lifecycle |
| 48 | + // guarantees (e.g. finalizers) will be honored. If this field is unset, |
| 49 | + // the Job won't be automatically deleted. If this field is set to zero, |
| 50 | + // the Job becomes eligible to be deleted immediately after it finishes. |
| 51 | + //+optional |
| 52 | + TTLSecondsAfterFinished *int32 `json:"ttlSecondsAfterFinished,omitempty"` |
| 53 | + |
| 54 | + // A batch/v1 Job is created with the Spec as the PodSpec. |
| 55 | + Spec corev1.PodSpec `json:"spec"` |
| 56 | +} |
| 57 | + |
| 58 | +// ClusterBootstrapConfigSpec defines the desired state of ClusterBootstrapConfig |
| 59 | +type ClusterBootstrapConfigSpec struct { |
| 60 | + ClusterSelector metav1.LabelSelector `json:"clusterSelector"` |
| 61 | + Template JobTemplate `json:"jobTemplate"` |
| 62 | + |
| 63 | + // Trigger the bootstrapping when the linked cluster has a True |
| 64 | + // "ClusterProvisioned" condition. |
| 65 | + // |
| 66 | + // A new job will not be triggered when the cluster is finally "Ready" |
| 67 | + // because it will already have the annotation that indicates the cluster |
| 68 | + // has been bootstrapped. |
| 69 | + // |
| 70 | + // Defaults to false. |
| 71 | + //+kubebuilder:default:false |
| 72 | + //+optional |
| 73 | + RequireClusterProvisioned bool `json:"requireClusterProvisioned"` |
| 74 | + |
| 75 | + // Wait for the remote cluster to be "ready" before creating the jobs. |
| 76 | + // Defaults to false. |
| 77 | + //+kubebuilder:default:false |
| 78 | + //+optional |
| 79 | + RequireClusterReady bool `json:"requireClusterReady"` |
| 80 | + // When checking for readiness, this is the time to wait before |
| 81 | + // checking again. |
| 82 | + //+kubebuilder:default:60s |
| 83 | + //+optional |
| 84 | + ClusterReadinessBackoff *metav1.Duration `json:"clusterReadinessBackoff,omitempty"` |
| 85 | +} |
| 86 | + |
| 87 | +// ClusterBootstrapConfigStatus defines the observed state of ClusterBootstrapConfig |
| 88 | +type ClusterBootstrapConfigStatus struct { |
| 89 | +} |
| 90 | + |
| 91 | +//+kubebuilder:object:root=true |
| 92 | +//+kubebuilder:subresource:status |
| 93 | +//+kubebuilder:storageversion |
| 94 | + |
| 95 | +// ClusterBootstrapConfig is the Schema for the clusterbootstrapconfigs API |
| 96 | +type ClusterBootstrapConfig struct { |
| 97 | + metav1.TypeMeta `json:",inline"` |
| 98 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 99 | + |
| 100 | + Spec ClusterBootstrapConfigSpec `json:"spec,omitempty"` |
| 101 | + Status ClusterBootstrapConfigStatus `json:"status,omitempty"` |
| 102 | +} |
| 103 | + |
| 104 | +// ClusterReadinessRequeue returns the configured ClusterReadinessBackoff or a default |
| 105 | +// value if not configured. |
| 106 | +func (c ClusterBootstrapConfig) ClusterReadinessRequeue() time.Duration { |
| 107 | + if v := c.Spec.ClusterReadinessBackoff; v != nil { |
| 108 | + return v.Duration |
| 109 | + } |
| 110 | + return defaultWaitDuration |
| 111 | +} |
| 112 | + |
| 113 | +//+kubebuilder:object:root=true |
| 114 | + |
| 115 | +// ClusterBootstrapConfigList contains a list of ClusterBootstrapConfig |
| 116 | +type ClusterBootstrapConfigList struct { |
| 117 | + metav1.TypeMeta `json:",inline"` |
| 118 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 119 | + Items []ClusterBootstrapConfig `json:"items"` |
| 120 | +} |
| 121 | + |
| 122 | +func init() { |
| 123 | + SchemeBuilder.Register(&ClusterBootstrapConfig{}, &ClusterBootstrapConfigList{}) |
| 124 | +} |
0 commit comments