|
| 1 | +/* |
| 2 | +Copyright 2023 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 v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + |
| 24 | + "github.com/fluxcd/pkg/apis/meta" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // GitRepositoryKind is the string representation of a GitRepository. |
| 29 | + GitRepositoryKind = "GitRepository" |
| 30 | + |
| 31 | + // GoGitImplementation for performing Git operations using go-git. |
| 32 | + GoGitImplementation = "go-git" |
| 33 | + // LibGit2Implementation for performing Git operations using libgit2. |
| 34 | + LibGit2Implementation = "libgit2" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + // IncludeUnavailableCondition indicates one of the includes is not |
| 39 | + // available. For example, because it does not exist, or does not have an |
| 40 | + // Artifact. |
| 41 | + // This is a "negative polarity" or "abnormal-true" type, and is only |
| 42 | + // present on the resource if it is True. |
| 43 | + IncludeUnavailableCondition string = "IncludeUnavailable" |
| 44 | +) |
| 45 | + |
| 46 | +// GitRepositorySpec specifies the required configuration to produce an |
| 47 | +// Artifact for a Git repository. |
| 48 | +type GitRepositorySpec struct { |
| 49 | + // URL specifies the Git repository URL, it can be an HTTP/S or SSH address. |
| 50 | + // +kubebuilder:validation:Pattern="^(http|https|ssh)://.*$" |
| 51 | + // +required |
| 52 | + URL string `json:"url"` |
| 53 | + |
| 54 | + // SecretRef specifies the Secret containing authentication credentials for |
| 55 | + // the GitRepository. |
| 56 | + // For HTTPS repositories the Secret must contain 'username' and 'password' |
| 57 | + // fields for basic auth or 'bearerToken' field for token auth. |
| 58 | + // For SSH repositories the Secret must contain 'identity' |
| 59 | + // and 'known_hosts' fields. |
| 60 | + // +optional |
| 61 | + SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"` |
| 62 | + |
| 63 | + // Interval at which to check the GitRepository for updates. |
| 64 | + // +kubebuilder:validation:Type=string |
| 65 | + // +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$" |
| 66 | + // +required |
| 67 | + Interval metav1.Duration `json:"interval"` |
| 68 | + |
| 69 | + // Timeout for Git operations like cloning, defaults to 60s. |
| 70 | + // +kubebuilder:default="60s" |
| 71 | + // +kubebuilder:validation:Type=string |
| 72 | + // +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m))+$" |
| 73 | + // +optional |
| 74 | + Timeout *metav1.Duration `json:"timeout,omitempty"` |
| 75 | + |
| 76 | + // Reference specifies the Git reference to resolve and monitor for |
| 77 | + // changes, defaults to the 'master' branch. |
| 78 | + // +optional |
| 79 | + Reference *GitRepositoryRef `json:"ref,omitempty"` |
| 80 | + |
| 81 | + // Verification specifies the configuration to verify the Git commit |
| 82 | + // signature(s). |
| 83 | + // +optional |
| 84 | + Verification *GitRepositoryVerification `json:"verify,omitempty"` |
| 85 | + |
| 86 | + // Ignore overrides the set of excluded patterns in the .sourceignore format |
| 87 | + // (which is the same as .gitignore). If not provided, a default will be used, |
| 88 | + // consult the documentation for your version to find out what those are. |
| 89 | + // +optional |
| 90 | + Ignore *string `json:"ignore,omitempty"` |
| 91 | + |
| 92 | + // Suspend tells the controller to suspend the reconciliation of this |
| 93 | + // GitRepository. |
| 94 | + // +optional |
| 95 | + Suspend bool `json:"suspend,omitempty"` |
| 96 | + |
| 97 | + // GitImplementation specifies which Git client library implementation to |
| 98 | + // use. Defaults to 'go-git', valid values are ('go-git', 'libgit2'). |
| 99 | + // Deprecated: gitImplementation is deprecated now that 'go-git' is the |
| 100 | + // only supported implementation. |
| 101 | + // +kubebuilder:validation:Enum=go-git;libgit2 |
| 102 | + // +kubebuilder:default:=go-git |
| 103 | + // +optional |
| 104 | + GitImplementation string `json:"gitImplementation,omitempty"` |
| 105 | + |
| 106 | + // RecurseSubmodules enables the initialization of all submodules within |
| 107 | + // the GitRepository as cloned from the URL, using their default settings. |
| 108 | + // +optional |
| 109 | + RecurseSubmodules bool `json:"recurseSubmodules,omitempty"` |
| 110 | + |
| 111 | + // Include specifies a list of GitRepository resources which Artifacts |
| 112 | + // should be included in the Artifact produced for this GitRepository. |
| 113 | + Include []GitRepositoryInclude `json:"include,omitempty"` |
| 114 | +} |
| 115 | + |
| 116 | +// GitRepositoryInclude specifies a local reference to a GitRepository which |
| 117 | +// Artifact (sub-)contents must be included, and where they should be placed. |
| 118 | +type GitRepositoryInclude struct { |
| 119 | + // GitRepositoryRef specifies the GitRepository which Artifact contents |
| 120 | + // must be included. |
| 121 | + GitRepositoryRef meta.LocalObjectReference `json:"repository"` |
| 122 | + |
| 123 | + // FromPath specifies the path to copy contents from, defaults to the root |
| 124 | + // of the Artifact. |
| 125 | + // +optional |
| 126 | + FromPath string `json:"fromPath"` |
| 127 | + |
| 128 | + // ToPath specifies the path to copy contents to, defaults to the name of |
| 129 | + // the GitRepositoryRef. |
| 130 | + // +optional |
| 131 | + ToPath string `json:"toPath"` |
| 132 | +} |
| 133 | + |
| 134 | +// GetFromPath returns the specified FromPath. |
| 135 | +func (in *GitRepositoryInclude) GetFromPath() string { |
| 136 | + return in.FromPath |
| 137 | +} |
| 138 | + |
| 139 | +// GetToPath returns the specified ToPath, falling back to the name of the |
| 140 | +// GitRepositoryRef. |
| 141 | +func (in *GitRepositoryInclude) GetToPath() string { |
| 142 | + if in.ToPath == "" { |
| 143 | + return in.GitRepositoryRef.Name |
| 144 | + } |
| 145 | + return in.ToPath |
| 146 | +} |
| 147 | + |
| 148 | +// GitRepositoryRef specifies the Git reference to resolve and checkout. |
| 149 | +type GitRepositoryRef struct { |
| 150 | + // Branch to check out, defaults to 'master' if no other field is defined. |
| 151 | + // +optional |
| 152 | + Branch string `json:"branch,omitempty"` |
| 153 | + |
| 154 | + // Tag to check out, takes precedence over Branch. |
| 155 | + // +optional |
| 156 | + Tag string `json:"tag,omitempty"` |
| 157 | + |
| 158 | + // SemVer tag expression to check out, takes precedence over Tag. |
| 159 | + // +optional |
| 160 | + SemVer string `json:"semver,omitempty"` |
| 161 | + |
| 162 | + // Name of the reference to check out; takes precedence over Branch, Tag and SemVer. |
| 163 | + // |
| 164 | + // It must be a valid Git reference: https://git-scm.com/docs/git-check-ref-format#_description |
| 165 | + // Examples: "refs/heads/main", "refs/tags/v0.1.0", "refs/pull/420/head", "refs/merge-requests/1/head" |
| 166 | + // +optional |
| 167 | + Name string `json:"name,omitempty"` |
| 168 | + |
| 169 | + // Commit SHA to check out, takes precedence over all reference fields. |
| 170 | + // |
| 171 | + // This can be combined with Branch to shallow clone the branch, in which |
| 172 | + // the commit is expected to exist. |
| 173 | + // +optional |
| 174 | + Commit string `json:"commit,omitempty"` |
| 175 | +} |
| 176 | + |
| 177 | +// GitRepositoryVerification specifies the Git commit signature verification |
| 178 | +// strategy. |
| 179 | +type GitRepositoryVerification struct { |
| 180 | + // Mode specifies what Git object should be verified, currently ('head'). |
| 181 | + // +kubebuilder:validation:Enum=head |
| 182 | + Mode string `json:"mode"` |
| 183 | + |
| 184 | + // SecretRef specifies the Secret containing the public keys of trusted Git |
| 185 | + // authors. |
| 186 | + SecretRef meta.LocalObjectReference `json:"secretRef,omitempty"` |
| 187 | +} |
| 188 | + |
| 189 | +// GitRepositoryStatus records the observed state of a Git repository. |
| 190 | +type GitRepositoryStatus struct { |
| 191 | + // ObservedGeneration is the last observed generation of the GitRepository |
| 192 | + // object. |
| 193 | + // +optional |
| 194 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 195 | + |
| 196 | + // Conditions holds the conditions for the GitRepository. |
| 197 | + // +optional |
| 198 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 199 | + |
| 200 | + // URL is the dynamic fetch link for the latest Artifact. |
| 201 | + // It is provided on a "best effort" basis, and using the precise |
| 202 | + // GitRepositoryStatus.Artifact data is recommended. |
| 203 | + // +optional |
| 204 | + URL string `json:"url,omitempty"` |
| 205 | + |
| 206 | + // Artifact represents the last successful GitRepository reconciliation. |
| 207 | + // +optional |
| 208 | + Artifact *Artifact `json:"artifact,omitempty"` |
| 209 | + |
| 210 | + // IncludedArtifacts contains a list of the last successfully included |
| 211 | + // Artifacts as instructed by GitRepositorySpec.Include. |
| 212 | + // +optional |
| 213 | + IncludedArtifacts []*Artifact `json:"includedArtifacts,omitempty"` |
| 214 | + |
| 215 | + // ContentConfigChecksum is a checksum of all the configurations related to |
| 216 | + // the content of the source artifact: |
| 217 | + // - .spec.ignore |
| 218 | + // - .spec.recurseSubmodules |
| 219 | + // - .spec.included and the checksum of the included artifacts |
| 220 | + // observed in .status.observedGeneration version of the object. This can |
| 221 | + // be used to determine if the content of the included repository has |
| 222 | + // changed. |
| 223 | + // It has the format of `<algo>:<checksum>`, for example: `sha256:<checksum>`. |
| 224 | + // |
| 225 | + // Deprecated: Replaced with explicit fields for observed artifact content |
| 226 | + // config in the status. |
| 227 | + // +optional |
| 228 | + ContentConfigChecksum string `json:"contentConfigChecksum,omitempty"` |
| 229 | + |
| 230 | + // ObservedIgnore is the observed exclusion patterns used for constructing |
| 231 | + // the source artifact. |
| 232 | + // +optional |
| 233 | + ObservedIgnore *string `json:"observedIgnore,omitempty"` |
| 234 | + |
| 235 | + // ObservedRecurseSubmodules is the observed resource submodules |
| 236 | + // configuration used to produce the current Artifact. |
| 237 | + // +optional |
| 238 | + ObservedRecurseSubmodules bool `json:"observedRecurseSubmodules,omitempty"` |
| 239 | + |
| 240 | + // ObservedInclude is the observed list of GitRepository resources used to |
| 241 | + // to produce the current Artifact. |
| 242 | + // +optional |
| 243 | + ObservedInclude []GitRepositoryInclude `json:"observedInclude,omitempty"` |
| 244 | + |
| 245 | + meta.ReconcileRequestStatus `json:",inline"` |
| 246 | +} |
| 247 | + |
| 248 | +const ( |
| 249 | + // GitOperationSucceedReason signals that a Git operation (e.g. clone, |
| 250 | + // checkout, etc.) succeeded. |
| 251 | + GitOperationSucceedReason string = "GitOperationSucceeded" |
| 252 | + |
| 253 | + // GitOperationFailedReason signals that a Git operation (e.g. clone, |
| 254 | + // checkout, etc.) failed. |
| 255 | + GitOperationFailedReason string = "GitOperationFailed" |
| 256 | +) |
| 257 | + |
| 258 | +// GetConditions returns the status conditions of the object. |
| 259 | +func (in GitRepository) GetConditions() []metav1.Condition { |
| 260 | + return in.Status.Conditions |
| 261 | +} |
| 262 | + |
| 263 | +// SetConditions sets the status conditions on the object. |
| 264 | +func (in *GitRepository) SetConditions(conditions []metav1.Condition) { |
| 265 | + in.Status.Conditions = conditions |
| 266 | +} |
| 267 | + |
| 268 | +// GetRequeueAfter returns the duration after which the GitRepository must be |
| 269 | +// reconciled again. |
| 270 | +func (in GitRepository) GetRequeueAfter() time.Duration { |
| 271 | + return in.Spec.Interval.Duration |
| 272 | +} |
| 273 | + |
| 274 | +// GetArtifact returns the latest Artifact from the GitRepository if present in |
| 275 | +// the status sub-resource. |
| 276 | +func (in *GitRepository) GetArtifact() *Artifact { |
| 277 | + return in.Status.Artifact |
| 278 | +} |
| 279 | + |
| 280 | +// +genclient |
| 281 | +// +genclient:Namespaced |
| 282 | +// +kubebuilder:storageversion |
| 283 | +// +kubebuilder:object:root=true |
| 284 | +// +kubebuilder:resource:shortName=gitrepo |
| 285 | +// +kubebuilder:subresource:status |
| 286 | +// +kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.spec.url` |
| 287 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="" |
| 288 | +// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description="" |
| 289 | +// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description="" |
| 290 | + |
| 291 | +// GitRepository is the Schema for the gitrepositories API. |
| 292 | +type GitRepository struct { |
| 293 | + metav1.TypeMeta `json:",inline"` |
| 294 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 295 | + |
| 296 | + Spec GitRepositorySpec `json:"spec,omitempty"` |
| 297 | + // +kubebuilder:default={"observedGeneration":-1} |
| 298 | + Status GitRepositoryStatus `json:"status,omitempty"` |
| 299 | +} |
| 300 | + |
| 301 | +// GitRepositoryList contains a list of GitRepository objects. |
| 302 | +// +kubebuilder:object:root=true |
| 303 | +type GitRepositoryList struct { |
| 304 | + metav1.TypeMeta `json:",inline"` |
| 305 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 306 | + Items []GitRepository `json:"items"` |
| 307 | +} |
| 308 | + |
| 309 | +func init() { |
| 310 | + SchemeBuilder.Register(&GitRepository{}, &GitRepositoryList{}) |
| 311 | +} |
0 commit comments