|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux CD contributors. |
| 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 v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// HelmChartSpec defines the desired state of HelmChart |
| 25 | +type HelmChartSpec struct { |
| 26 | + // The name of the Helm chart, as made available by the referenced |
| 27 | + // Helm repository. |
| 28 | + // +required |
| 29 | + Name string `json:"name"` |
| 30 | + |
| 31 | + // The chart version semver expression, defaults to latest when |
| 32 | + // omitted. |
| 33 | + // +optional |
| 34 | + Version string `json:"version,omitempty"` |
| 35 | + |
| 36 | + // The name of the HelmRepository the chart is available at. |
| 37 | + // +required |
| 38 | + HelmRepositoryRef corev1.LocalObjectReference `json:"helmRepositoryRef"` |
| 39 | + |
| 40 | + // The interval at which to check the Helm repository for updates. |
| 41 | + // Defaults to the interval of the Helm repository. |
| 42 | + // +optional |
| 43 | + Interval *metav1.Duration `json:"interval,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +// IntervalOrDefault returns the defined interval on the HelmChartSpec |
| 47 | +// or the given default. |
| 48 | +func (s HelmChartSpec) IntervalOrDefault(interval metav1.Duration) metav1.Duration { |
| 49 | + if s.Interval == nil { |
| 50 | + return interval |
| 51 | + } |
| 52 | + return *s.Interval |
| 53 | +} |
| 54 | + |
| 55 | +// HelmChartStatus defines the observed state of HelmChart |
| 56 | +type HelmChartStatus struct { |
| 57 | + // +optional |
| 58 | + Conditions []SourceCondition `json:"conditions,omitempty"` |
| 59 | + |
| 60 | + // URL is the download link for the last chart pulled. |
| 61 | + // +optional |
| 62 | + URL string `json:"url,omitempty"` |
| 63 | + |
| 64 | + // URI for the artifact of the latest successful chart pull. |
| 65 | + // +optional |
| 66 | + Artifact *Artifact `json:"artifact,omitempty"` |
| 67 | +} |
| 68 | + |
| 69 | +const ( |
| 70 | + // ChartPullFailedReason represents the fact that the pull of the |
| 71 | + // Helm chart failed. |
| 72 | + ChartPullFailedReason string = "ChartPullFailed" |
| 73 | + |
| 74 | + // ChartPulLSucceededReason represents the fact that the pull of |
| 75 | + // the Helm chart succeeded. |
| 76 | + ChartPullSucceededReason string = "ChartPullSucceeded" |
| 77 | +) |
| 78 | + |
| 79 | +func HelmChartReady(chart HelmChart, artifact Artifact, url, reason, message string) HelmChart { |
| 80 | + chart.Status.Conditions = []SourceCondition{ |
| 81 | + { |
| 82 | + Type: ReadyCondition, |
| 83 | + Status: corev1.ConditionTrue, |
| 84 | + LastTransitionTime: metav1.Now(), |
| 85 | + Reason: reason, |
| 86 | + Message: message, |
| 87 | + }, |
| 88 | + } |
| 89 | + chart.Status.URL = url |
| 90 | + |
| 91 | + if chart.Status.Artifact != nil { |
| 92 | + if chart.Status.Artifact.Path != artifact.Path { |
| 93 | + chart.Status.Artifact = &artifact |
| 94 | + } |
| 95 | + } else { |
| 96 | + chart.Status.Artifact = &artifact |
| 97 | + } |
| 98 | + |
| 99 | + return chart |
| 100 | +} |
| 101 | + |
| 102 | +func HelmChartNotReady(chart HelmChart, reason, message string) HelmChart { |
| 103 | + chart.Status.Conditions = []SourceCondition{ |
| 104 | + { |
| 105 | + Type: ReadyCondition, |
| 106 | + Status: corev1.ConditionFalse, |
| 107 | + LastTransitionTime: metav1.Now(), |
| 108 | + Reason: reason, |
| 109 | + Message: message, |
| 110 | + }, |
| 111 | + } |
| 112 | + return chart |
| 113 | +} |
| 114 | + |
| 115 | +func HelmChartReadyMessage(chart HelmChart) string { |
| 116 | + for _, condition := range chart.Status.Conditions { |
| 117 | + if condition.Type == ReadyCondition { |
| 118 | + return condition.Message |
| 119 | + } |
| 120 | + } |
| 121 | + return "" |
| 122 | +} |
| 123 | + |
| 124 | +// +kubebuilder:object:root=true |
| 125 | +// +kubebuilder:subresource:status |
| 126 | +// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name` |
| 127 | +// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.version` |
| 128 | +// +kubebuilder:printcolumn:name="Repository",type=string,JSONPath=`.spec.helmRepositoryRef.name` |
| 129 | +// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description="" |
| 130 | +// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description="" |
| 131 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="" |
| 132 | + |
| 133 | +// HelmChart is the Schema for the helmcharts API |
| 134 | +type HelmChart struct { |
| 135 | + metav1.TypeMeta `json:",inline"` |
| 136 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 137 | + |
| 138 | + Spec HelmChartSpec `json:"spec,omitempty"` |
| 139 | + Status HelmChartStatus `json:"status,omitempty"` |
| 140 | +} |
| 141 | + |
| 142 | +// +kubebuilder:object:root=true |
| 143 | + |
| 144 | +// HelmChartList contains a list of HelmChart |
| 145 | +type HelmChartList struct { |
| 146 | + metav1.TypeMeta `json:",inline"` |
| 147 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 148 | + Items []HelmChart `json:"items"` |
| 149 | +} |
| 150 | + |
| 151 | +func init() { |
| 152 | + SchemeBuilder.Register(&HelmChart{}, &HelmChartList{}) |
| 153 | +} |
0 commit comments