This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
/
types.go
299 lines (258 loc) · 8.87 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package v1beta1
import (
"fmt"
"strings"
"github.com/ghodss/yaml"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/helm/pkg/chartutil"
"github.com/weaveworks/flux/resource"
)
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// HelmRelease represents custom resource associated with a Helm Chart
type HelmRelease struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec HelmReleaseSpec `json:"spec"`
Status HelmReleaseStatus `json:"status"`
}
// ResourceID returns an ID made from the identifying parts of the
// resource, as a convenience for Flux, which uses them
// everywhere.
func (hr HelmRelease) ResourceID() resource.ID {
return resource.MakeID(hr.Namespace, "HelmRelease", hr.Name)
}
// ReleaseName returns the configured release name, or constructs and
// returns one based on the namespace and name of the HelmRelease.
// When the HelmRelease's metadata.namespace and spec.targetNamespace
// differ, both are used in the generated name.
// This name is used for naming and operating on the release in Helm.
func (hr HelmRelease) ReleaseName() string {
if hr.Spec.ReleaseName == "" {
namespace := hr.GetDefaultedNamespace()
targetNamespace := hr.GetTargetNamespace()
if namespace != targetNamespace {
// prefix the releaseName with the administering HelmRelease namespace as well
return fmt.Sprintf("%s-%s-%s", namespace, targetNamespace, hr.Name)
}
return fmt.Sprintf("%s-%s", targetNamespace, hr.Name)
}
return hr.Spec.ReleaseName
}
// GetDefaultedNamespace returns the HelmRelease's namespace
// defaulting to the "default" if not set.
func (hr HelmRelease) GetDefaultedNamespace() string {
if hr.GetNamespace() == "" {
return "default"
}
return hr.Namespace
}
// GetTargetNamespace returns the configured release targetNamespace
// defaulting to the namespace of the HelmRelease if not set.
func (hr HelmRelease) GetTargetNamespace() string {
if hr.Spec.TargetNamespace == "" {
return hr.GetDefaultedNamespace()
}
return hr.Spec.TargetNamespace
}
// ValuesFromSource represents a source of values.
// Only one of its fields may be set.
type ValuesFromSource struct {
// Selects a key of a ConfigMap.
// +optional
ConfigMapKeyRef *v1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
// Selects a key of a Secret.
// +optional
SecretKeyRef *v1.SecretKeySelector `json:"secretKeyRef,omitempty"`
// Selects an URL.
// +optional
ExternalSourceRef *ExternalSourceSelector `json:"externalSourceRef,omitempty"`
// Selects a file from git source helm chart.
// +optional
ChartFileRef *ChartFileSelector `json:"chartFileRef,omitempty"`
}
type ChartFileSelector struct {
Path string `json:"path"`
// Do not fail if chart file could not be retrieved
// +optional
Optional *bool `json:"optional,omitempty"`
}
type ExternalSourceSelector struct {
URL string `json:"url"`
// Do not fail if external source could not be retrieved
// +optional
Optional *bool `json:"optional,omitempty"`
}
type ChartSource struct {
// one of the following...
// +optional
*GitChartSource
// +optional
*RepoChartSource
}
type GitChartSource struct {
GitURL string `json:"git"`
Ref string `json:"ref"`
Path string `json:"path"`
// Do not run 'dep' update (assume requirements.yaml is already fulfilled)
// +optional
SkipDepUpdate bool `json:"skipDepUpdate,omitempty"`
}
// DefaultGitRef is the ref assumed if the Ref field is not given in
// a GitChartSource
const DefaultGitRef = "master"
func (s GitChartSource) RefOrDefault() string {
if s.Ref == "" {
return DefaultGitRef
}
return s.Ref
}
type RepoChartSource struct {
RepoURL string `json:"repository"`
Name string `json:"name"`
Version string `json:"version"`
// An authentication secret for accessing the chart repo
// +optional
ChartPullSecret *v1.LocalObjectReference `json:"chartPullSecret,omitempty"`
}
// CleanRepoURL returns the RepoURL but ensures it ends with a trailing slash
func (s RepoChartSource) CleanRepoURL() string {
cleanURL := strings.TrimRight(s.RepoURL, "/")
return cleanURL + "/"
}
type Rollback struct {
Enable bool `json:"enable,omitempty"`
Force bool `json:"force,omitempty"`
Recreate bool `json:"recreate,omitempty"`
DisableHooks bool `json:"disableHooks,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
Wait bool `json:"wait,omitempty"`
}
func (r Rollback) GetTimeout() int64 {
if r.Timeout == nil {
return 300
}
return *r.Timeout
}
// HelmReleaseSpec is the spec for a HelmRelease resource
type HelmReleaseSpec struct {
ChartSource `json:"chart"`
ReleaseName string `json:"releaseName,omitempty"`
ValueFileSecrets []v1.LocalObjectReference `json:"valueFileSecrets,omitempty"`
ValuesFrom []ValuesFromSource `json:"valuesFrom,omitempty"`
HelmValues `json:",inline"`
// Override the target namespace, defaults to metadata.namespace
// +optional
TargetNamespace string `json:"targetNamespace,omitempty"`
// Install or upgrade timeout in seconds
// +optional
Timeout *int64 `json:"timeout,omitempty"`
// Reset values on helm upgrade
// +optional
ResetValues bool `json:"resetValues,omitempty"`
// Force resource update through delete/recreate, allows recovery from a failed state
// +optional
ForceUpgrade bool `json:"forceUpgrade,omitempty"`
// Enable rollback and configure options
// +optional
Rollback Rollback `json:"rollback,omitempty"`
}
// GetTimeout returns the install or upgrade timeout (defaults to 300s)
func (r HelmRelease) GetTimeout() int64 {
if r.Spec.Timeout == nil {
return 300
}
return *r.Spec.Timeout
}
// GetValuesFromSources maintains backwards compatibility with
// ValueFileSecrets by merging them into the ValuesFrom array.
func (r HelmRelease) GetValuesFromSources() []ValuesFromSource {
valuesFrom := r.Spec.ValuesFrom
// Maintain backwards compatibility with ValueFileSecrets
if r.Spec.ValueFileSecrets != nil {
var secretKeyRefs []ValuesFromSource
for _, ref := range r.Spec.ValueFileSecrets {
s := &v1.SecretKeySelector{LocalObjectReference: ref}
secretKeyRefs = append(secretKeyRefs, ValuesFromSource{SecretKeyRef: s})
}
valuesFrom = append(secretKeyRefs, valuesFrom...)
}
return valuesFrom
}
type HelmReleaseStatus struct {
// ReleaseName is the name as either supplied or generated.
// +optional
ReleaseName string `json:"releaseName"`
// ReleaseStatus is the status as given by Helm for the release
// managed by this resource.
ReleaseStatus string `json:"releaseStatus"`
// ObservedGeneration is the most recent generation observed by
// the controller.
ObservedGeneration int64 `json:"observedGeneration"`
// ValuesChecksum holds the SHA256 checksum of the last applied
// values.
ValuesChecksum string `json:"valuesChecksum"`
// Revision would define what Git hash or Chart version has currently
// been deployed.
// +optional
Revision string `json:"revision,omitempty"`
// Conditions contains observations of the resource's state, e.g.,
// has the chart which it refers to been fetched.
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []HelmReleaseCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
}
type HelmReleaseCondition struct {
Type HelmReleaseConditionType `json:"type"`
Status v1.ConditionStatus `json:"status"`
// +optional
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
// +optional
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// +optional
Reason string `json:"reason,omitempty"`
// +optional
Message string `json:"message,omitempty"`
}
type HelmReleaseConditionType string
const (
// ChartFetched means the chart to which the HelmRelease refers
// has been fetched successfully
HelmReleaseChartFetched HelmReleaseConditionType = "ChartFetched"
// Released means the chart release, as specified in this
// HelmRelease, has been processed by Helm.
HelmReleaseReleased HelmReleaseConditionType = "Released"
// RolledBack means the chart to which the HelmRelease refers
// has been rolled back
HelmReleaseRolledBack HelmReleaseConditionType = "RolledBack"
)
// FluxHelmValues embeds chartutil.Values so we can implement deepcopy on map[string]interface{}
// +k8s:deepcopy-gen=false
type HelmValues struct {
chartutil.Values `json:"values,omitempty"`
}
// DeepCopyInto implements deepcopy-gen method for use in generated code
func (in *HelmValues) DeepCopyInto(out *HelmValues) {
if in == nil {
return
}
b, err := yaml.Marshal(in.Values)
if err != nil {
return
}
var values chartutil.Values
err = yaml.Unmarshal(b, &values)
if err != nil {
return
}
out.Values = values
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// HelmReleaseList is a list of HelmRelease resources
type HelmReleaseList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []HelmRelease `json:"items"`
}