|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux 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 v1alpha2 |
| 18 | + |
| 19 | +import ( |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + |
| 22 | + "github.com/fluxcd/pkg/apis/meta" |
| 23 | +) |
| 24 | + |
| 25 | +const ImageUpdateAutomationKind = "ImageUpdateAutomation" |
| 26 | + |
| 27 | +// ImageUpdateAutomationSpec defines the desired state of ImageUpdateAutomation |
| 28 | +type ImageUpdateAutomationSpec struct { |
| 29 | + // SourceRef refers to the resource giving access details |
| 30 | + // to a git repository. |
| 31 | + // +required |
| 32 | + SourceRef SourceReference `json:"sourceRef"` |
| 33 | + // GitSpec contains all the git-specific definitions. This is |
| 34 | + // technically optional, but in practice mandatory until there are |
| 35 | + // other kinds of source allowed. |
| 36 | + // +optional |
| 37 | + GitSpec *GitSpec `json:"git,omitempty"` |
| 38 | + |
| 39 | + // Interval gives an lower bound for how often the automation |
| 40 | + // run should be attempted. |
| 41 | + // +required |
| 42 | + Interval metav1.Duration `json:"interval"` |
| 43 | + |
| 44 | + // Update gives the specification for how to update the files in |
| 45 | + // the repository. This can be left empty, to use the default |
| 46 | + // value. |
| 47 | + // +kubebuilder:default={"strategy":"Setters"} |
| 48 | + Update *UpdateStrategy `json:"update,omitempty"` |
| 49 | + |
| 50 | + // Suspend tells the controller to not run this automation, until |
| 51 | + // it is unset (or set to false). Defaults to false. |
| 52 | + // +optional |
| 53 | + Suspend bool `json:"suspend,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// UpdateStrategyName is the type for names that go in |
| 57 | +// .update.strategy. NB the value in the const immediately below. |
| 58 | +// +kubebuilder:validation:Enum=Setters |
| 59 | +type UpdateStrategyName string |
| 60 | + |
| 61 | +const ( |
| 62 | + // UpdateStrategySetters is the name of the update strategy that |
| 63 | + // uses kyaml setters. NB the value in the enum annotation for the |
| 64 | + // type, above. |
| 65 | + UpdateStrategySetters UpdateStrategyName = "Setters" |
| 66 | +) |
| 67 | + |
| 68 | +// UpdateStrategy is a union of the various strategies for updating |
| 69 | +// the Git repository. Parameters for each strategy (if any) can be |
| 70 | +// inlined here. |
| 71 | +type UpdateStrategy struct { |
| 72 | + // Strategy names the strategy to be used. |
| 73 | + // +required |
| 74 | + // +kubebuilder:default=Setters |
| 75 | + Strategy UpdateStrategyName `json:"strategy"` |
| 76 | + |
| 77 | + // Path to the directory containing the manifests to be updated. |
| 78 | + // Defaults to 'None', which translates to the root path |
| 79 | + // of the GitRepositoryRef. |
| 80 | + // +optional |
| 81 | + Path string `json:"path,omitempty"` |
| 82 | +} |
| 83 | + |
| 84 | +// ImageUpdateAutomationStatus defines the observed state of ImageUpdateAutomation |
| 85 | +type ImageUpdateAutomationStatus struct { |
| 86 | + // LastAutomationRunTime records the last time the controller ran |
| 87 | + // this automation through to completion (even if no updates were |
| 88 | + // made). |
| 89 | + // +optional |
| 90 | + LastAutomationRunTime *metav1.Time `json:"lastAutomationRunTime,omitempty"` |
| 91 | + // LastPushCommit records the SHA1 of the last commit made by the |
| 92 | + // controller, for this automation object |
| 93 | + // +optional |
| 94 | + LastPushCommit string `json:"lastPushCommit,omitempty"` |
| 95 | + // LastPushTime records the time of the last pushed change. |
| 96 | + // +optional |
| 97 | + LastPushTime *metav1.Time `json:"lastPushTime,omitempty"` |
| 98 | + // +optional |
| 99 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 100 | + // +optional |
| 101 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 102 | + meta.ReconcileRequestStatus `json:",inline"` |
| 103 | +} |
| 104 | + |
| 105 | +const ( |
| 106 | + // GitNotAvailableReason is used for ConditionReady when the |
| 107 | + // automation run cannot proceed because the git repository is |
| 108 | + // missing or cannot be cloned. |
| 109 | + GitNotAvailableReason = "GitRepositoryNotAvailable" |
| 110 | + // NoStrategyReason is used for ConditionReady when the automation |
| 111 | + // run cannot proceed because there is no update strategy given in |
| 112 | + // the spec. |
| 113 | + NoStrategyReason = "MissingUpdateStrategy" |
| 114 | +) |
| 115 | + |
| 116 | +// SetImageUpdateAutomationReadiness sets the ready condition with the given status, reason and message. |
| 117 | +func SetImageUpdateAutomationReadiness(auto *ImageUpdateAutomation, status metav1.ConditionStatus, reason, message string) { |
| 118 | + auto.Status.ObservedGeneration = auto.ObjectMeta.Generation |
| 119 | + meta.SetResourceCondition(auto, meta.ReadyCondition, status, reason, message) |
| 120 | +} |
| 121 | + |
| 122 | +//+kubebuilder:storageversion |
| 123 | +//+kubebuilder:object:root=true |
| 124 | +//+kubebuilder:subresource:status |
| 125 | +//+kubebuilder:printcolumn:name="Last run",type=string,JSONPath=`.status.lastAutomationRunTime` |
| 126 | + |
| 127 | +// ImageUpdateAutomation is the Schema for the imageupdateautomations API |
| 128 | +type ImageUpdateAutomation struct { |
| 129 | + metav1.TypeMeta `json:",inline"` |
| 130 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 131 | + |
| 132 | + Spec ImageUpdateAutomationSpec `json:"spec,omitempty"` |
| 133 | + Status ImageUpdateAutomationStatus `json:"status,omitempty"` |
| 134 | +} |
| 135 | + |
| 136 | +func (auto *ImageUpdateAutomation) GetStatusConditions() *[]metav1.Condition { |
| 137 | + return &auto.Status.Conditions |
| 138 | +} |
| 139 | + |
| 140 | +//+kubebuilder:object:root=true |
| 141 | + |
| 142 | +// ImageUpdateAutomationList contains a list of ImageUpdateAutomation |
| 143 | +type ImageUpdateAutomationList struct { |
| 144 | + metav1.TypeMeta `json:",inline"` |
| 145 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 146 | + Items []ImageUpdateAutomation `json:"items"` |
| 147 | +} |
| 148 | + |
| 149 | +func init() { |
| 150 | + SchemeBuilder.Register(&ImageUpdateAutomation{}, &ImageUpdateAutomationList{}) |
| 151 | +} |
0 commit comments