/
helmrelease_types.go
193 lines (155 loc) · 6.36 KB
/
helmrelease_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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
//ChartsDir env variable name which contains the directory where the charts are installed
const ChartsDir = "CHARTS_DIR"
//SourceTypeEnum types of sources
type SourceTypeEnum string
const (
// HelmRepoSourceType helmrepo source type
HelmRepoSourceType SourceTypeEnum = "helmrepo"
// GitHubSourceType github source type
GitHubSourceType SourceTypeEnum = "github"
)
//GitHub provides the parameters to access the helm-chart located in a github repo
type GitHub struct {
Urls []string `json:"urls,omitempty"`
ChartPath string `json:"chartPath,omitempty"`
Branch string `json:"branch,omitempty"`
}
//HelmRepo provides the urls to retrieve the helm-chart
type HelmRepo struct {
Urls []string `json:"urls,omitempty"`
}
//Source holds the different types of repository
type Source struct {
SourceType SourceTypeEnum `json:"type,omitempty"`
GitHub *GitHub `json:"github,omitempty"`
HelmRepo *HelmRepo `json:"helmRepo,omitempty"`
}
func (s Source) String() string {
switch strings.ToLower(string(s.SourceType)) {
case string(HelmRepoSourceType):
return fmt.Sprintf("%v", s.HelmRepo.Urls)
case string(GitHubSourceType):
return fmt.Sprintf("%v|%s|%s", s.GitHub.Urls, s.GitHub.Branch, s.GitHub.ChartPath)
default:
return fmt.Sprintf("SourceType %s not supported", s.SourceType)
}
}
// HelmReleaseRepo defines the repository of HelmRelease
// +k8s:openapi-gen=true
type HelmReleaseRepo struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
// Source holds the url toward the helm-chart
Source *Source `json:"source,omitempty"`
// ChartName is the name of the chart within the repo
ChartName string `json:"chartName,omitempty"`
// Version is the chart version
Version string `json:"version,omitempty"`
// Secret to use to access the helm-repo defined in the CatalogSource.
SecretRef *corev1.ObjectReference `json:"secretRef,omitempty"`
// Configuration parameters to access the helm-repo defined in the CatalogSource
ConfigMapRef *corev1.ObjectReference `json:"configMapRef,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// HelmRelease is the Schema for the subscriptionreleases API
// +k8s:openapi-gen=true
// +kubebuilder:resource:scope=Namespaced
// +kubebuilder:subresource:status
type HelmRelease struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Repo HelmReleaseRepo `json:"repo,omitempty"`
Spec HelmAppSpec `json:"spec,omitempty"`
Status HelmAppStatus `json:"status,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// HelmReleaseList contains a list of HelmRelease
type HelmReleaseList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []HelmRelease `json:"items"`
}
func init() {
SchemeBuilder.Register(&HelmRelease{}, &HelmReleaseList{})
}
// Below are mostly copied from operator sdk's internal package
// https://github.com/operator-framework/operator-sdk/blob/v0.12.x/pkg/helm/internal/types/types.go
type HelmAppSpec interface{} // modified
type HelmAppConditionType string
type ConditionStatus string
type HelmAppConditionReason string
type HelmAppCondition struct {
Type HelmAppConditionType `json:"type"`
Status ConditionStatus `json:"status"`
Reason HelmAppConditionReason `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
}
type HelmAppRelease struct {
Name string `json:"name,omitempty"`
Manifest string `json:"manifest,omitempty"`
}
const (
ConditionInitialized HelmAppConditionType = "Initialized"
ConditionDeployed HelmAppConditionType = "Deployed"
ConditionReleaseFailed HelmAppConditionType = "ReleaseFailed"
ConditionIrreconcilable HelmAppConditionType = "Irreconcilable"
StatusTrue ConditionStatus = "True"
StatusFalse ConditionStatus = "False"
StatusUnknown ConditionStatus = "Unknown"
ReasonInstallSuccessful HelmAppConditionReason = "InstallSuccessful"
ReasonUpdateSuccessful HelmAppConditionReason = "UpdateSuccessful"
ReasonUninstallSuccessful HelmAppConditionReason = "UninstallSuccessful"
ReasonInstallError HelmAppConditionReason = "InstallError"
ReasonUpdateError HelmAppConditionReason = "UpdateError"
ReasonReconcileError HelmAppConditionReason = "ReconcileError"
ReasonUninstallError HelmAppConditionReason = "UninstallError"
)
type HelmAppStatus struct {
Conditions []HelmAppCondition `json:"conditions"`
DeployedRelease *HelmAppRelease `json:"deployedRelease,omitempty"`
}
// SetCondition sets a condition on the status object. If the condition already
// exists, it will be replaced. SetCondition does not update the resource in
// the cluster.
func (s *HelmAppStatus) SetCondition(condition HelmAppCondition) *HelmAppStatus {
now := metav1.Now()
for i := range s.Conditions {
if s.Conditions[i].Type == condition.Type {
if s.Conditions[i].Status != condition.Status {
condition.LastTransitionTime = now
} else {
condition.LastTransitionTime = s.Conditions[i].LastTransitionTime
}
s.Conditions[i] = condition
return s
}
}
// If the condition does not exist,
// initialize the lastTransitionTime
condition.LastTransitionTime = now
s.Conditions = append(s.Conditions, condition)
return s
}