/
types_environment.go
362 lines (307 loc) · 16.7 KB
/
types_environment.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package v1
import (
batchv1 "k8s.io/api/batch/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true
// Environment represents an environment like Dev, Test, Staging, Production where code lives
type Environment struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Spec EnvironmentSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
Status EnvironmentStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
// EnvironmentSpec is the specification of an Environment
type EnvironmentSpec struct {
Label string `json:"label,omitempty" protobuf:"bytes,1,opt,name=label"`
Namespace string `json:"namespace,omitempty" protobuf:"bytes,2,opt,name=namespace"`
Cluster string `json:"cluster,omitempty" protobuf:"bytes,3,opt,name=cluster"`
PromotionStrategy PromotionStrategyType `json:"promotionStrategy,omitempty" protobuf:"bytes,4,opt,name=promotionStrategy"`
Source EnvironmentRepository `json:"source,omitempty" protobuf:"bytes,5,opt,name=source"`
Order int32 `json:"order,omitempty" protobuf:"bytes,6,opt,name=order"`
Kind EnvironmentKindType `json:"kind,omitempty" protobuf:"bytes,7,opt,name=kind"`
PullRequestURL string `json:"pullRequestURL,omitempty" protobuf:"bytes,8,opt,name=pullRequestURL"`
TeamSettings TeamSettings `json:"teamSettings,omitempty" protobuf:"bytes,9,opt,name=teamSettings"`
PreviewGitSpec PreviewGitSpec `json:"previewGitInfo,omitempty" protobuf:"bytes,10,opt,name=previewGitInfo"`
WebHookEngine WebHookEngineType `json:"webHookEngine,omitempty" protobuf:"bytes,11,opt,name=webHookEngine"`
}
// EnvironmentStatus is the status for an Environment resource
type EnvironmentStatus struct {
Version string `json:"version,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// EnvironmentList is a list of TypeMeta resources
type EnvironmentList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []Environment `json:"items"`
}
// PromotionStrategyType is the type of a promotion strategy
type PromotionStrategyType string
const (
// PromotionStrategyTypeManual specifies that promotion happens manually
PromotionStrategyTypeManual PromotionStrategyType = "Manual"
// PromotionStrategyTypeAutomatic specifies that promotion happens automatically
PromotionStrategyTypeAutomatic PromotionStrategyType = "Auto"
// PromotionStrategyTypeNever specifies that promotion is disabled for this environment
PromotionStrategyTypeNever PromotionStrategyType = "Never"
)
// EnvironmentKindType is the kind of an environment
type EnvironmentKindType string
const (
// EnvironmentKindTypePermanent specifies that the environment is a regular permanent one
EnvironmentKindTypePermanent EnvironmentKindType = "Permanent"
// EnvironmentKindTypePreview specifies that an environment is a Preview environment that lasts as long as a Pull Request
EnvironmentKindTypePreview EnvironmentKindType = "Preview"
// EnvironmentKindTypeTest specifies that an environment is a temporary one for a test
EnvironmentKindTypeTest EnvironmentKindType = "Test"
// EnvironmentKindTypeEdit specifies that an environment is a developers editing workspace
EnvironmentKindTypeEdit EnvironmentKindType = "Edit"
// EnvironmentKindTypeDevelopment specifies that an environment is a development environment; for developer tools like Jenkins, Nexus etc
EnvironmentKindTypeDevelopment EnvironmentKindType = "Development"
)
// PromotionEngineType is the type of promotion implementation the team uses
type PromotionEngineType string
const (
PromotionEngineJenkins PromotionEngineType = "Jenkins"
PromotionEngineProw PromotionEngineType = "Prow"
)
// ImportModeType is the type of import mode for new projects in a team
type ImportModeType string
const (
// ImportModeTypeJenkinsfile when importing we create a Jenkinfiles in the git repository of the project
ImportModeTypeJenkinsfile ImportModeType = "Jenkinsfile"
// ImportModeTypeYAML when importing we add a `jenkins-x.yml` file for the Next Generation Pipeline as YAML
ImportModeTypeYAML ImportModeType = "YAML"
)
// ProwEngineType is the type of prow execution engine
type ProwEngineType string
const (
// ProwEngineTypeKnativeBuild represents the Knative Build engine for use with Prow
ProwEngineTypeKnativeBuild ProwEngineType = "KnativeBuild"
// ProwEngineTypeTekton represents using Tekton as the execution engine with Prow
ProwEngineTypeTekton ProwEngineType = "Tekton"
)
// WebHookEngineType is the type of webhook processing implementation the team uses
type WebHookEngineType string
const (
WebHookEngineNone WebHookEngineType = ""
WebHookEngineJenkins WebHookEngineType = "Jenkins"
WebHookEngineProw WebHookEngineType = "Prow"
)
// IsPermanent returns true if this environment is permanent
func (e EnvironmentKindType) IsPermanent() bool {
switch e {
case EnvironmentKindTypePreview, EnvironmentKindTypeTest, EnvironmentKindTypeEdit:
return false
default:
return true
}
}
// PromotionStrategyTypeValues is the list of all values
var PromotionStrategyTypeValues = []string{
string(PromotionStrategyTypeAutomatic),
string(PromotionStrategyTypeManual),
string(PromotionStrategyTypeNever),
}
// EnvironmentRepositoryType is the repository type
type EnvironmentRepositoryType string
const (
// EnvironmentRepositoryTypeGit specifies that a git repository is used
EnvironmentRepositoryTypeGit EnvironmentRepositoryType = "Git"
)
// EnvironmentRepository is the repository for an environment using GitOps
type EnvironmentRepository struct {
Kind EnvironmentRepositoryType `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"`
URL string `json:"url,omitempty" protobuf:"bytes,2,opt,name=url"`
Ref string `json:"ref,omitempty" protobuf:"bytes,3,opt,name=ref"`
}
// TeamSettings the default settings for a team
type TeamSettings struct {
UseGitOps bool `json:"useGitOps,omitempty" protobuf:"bytes,1,opt,name=useGitOps"`
AskOnCreate bool `json:"askOnCreate,omitempty" protobuf:"bytes,2,opt,name=askOnCreate"`
BranchPatterns string `json:"branchPatterns,omitempty" protobuf:"bytes,3,opt,name=branchPatterns"`
ForkBranchPatterns string `json:"forkBranchPatterns,omitempty" protobuf:"bytes,4,opt,name=forkBranchPatterns"`
QuickstartLocations []QuickStartLocation `json:"quickstartLocations,omitempty" protobuf:"bytes,5,opt,name=quickstartLocations"`
BuildPackURL string `json:"buildPackUrl,omitempty" protobuf:"bytes,6,opt,name=buildPackUrl"`
BuildPackRef string `json:"buildPackRef,omitempty" protobuf:"bytes,7,opt,name=buildPackRef"`
HelmBinary string `json:"helmBinary,omitempty" protobuf:"bytes,8,opt,name=helmBinary"`
PostPreviewJobs []batchv1.Job `json:"postPreviewJobs,omitempty" protobuf:"bytes,9,opt,name=postPreviewJobs"`
PromotionEngine PromotionEngineType `json:"promotionEngine,omitempty" protobuf:"bytes,10,opt,name=promotionEngine"`
NoTiller bool `json:"noTiller,omitempty" protobuf:"bytes,11,opt,name=noTiller"`
HelmTemplate bool `json:"helmTemplate,omitempty" protobuf:"bytes,12,opt,name=helmTemplate"`
GitServer string `json:"gitServer,omitempty" protobuf:"bytes,13,opt,name=gitServer" command:"gitserver" commandUsage:"Default git server for new repositories"`
Organisation string `json:"organisation,omitempty" protobuf:"bytes,14,opt,name=organisation" command:"organisation" commandUsage:"Default git organisation for new repositories"`
EnvOrganisation string `json:"envOrganisation,omitempty" protobuf:"bytes,14,opt,name=envOrganisation" command:"envOrganisation" commandUsage:"Default git organisation for new environment repositories"`
PipelineUsername string `json:"pipelineUsername,omitempty" protobuf:"bytes,15,opt,name=pipelineUsername" command:"pipelineusername" commandUsage:"User used by pipeline. Is given write permission on new repositories."`
DockerRegistryOrg string `json:"dockerRegistryOrg,omitempty" protobuf:"bytes,16,opt,name=dockerRegistryOrg" command:"dockerregistryorg" commandUsage:"Docker registry organisation used for new projects in Jenkins X."`
GitPrivate bool `json:"gitPrivate,omitempty" protobuf:"bytes,17,opt,name=gitPrivate" command:"gitprivate" commandUsage:"Are new repositories private by default"`
KubeProvider string `json:"kubeProvider,omitempty" protobuf:"bytes,18,opt,name=kubeProvider"`
AppsRepository string `json:"appsRepository,omitempty" protobuf:"bytes,19,opt,name=appsRepository"`
BuildPackName string `json:"buildPackName,omitempty" protobuf:"bytes,20,opt,name=buildPackName"`
StorageLocations []StorageLocation `json:"storageLocations,omitempty" protobuf:"bytes,21,opt,name=storageLocations"`
// ImportMode indicates what kind of
ImportMode ImportModeType `json:"importMode,omitempty" protobuf:"bytes,22,opt,name=importMode"`
// ProwEngine is the kind of prow engine used such as knative build or build pipeline
ProwEngine ProwEngineType `json:"prowEngine,omitempty" protobuf:"bytes,23,opt,name=prowEngine"`
}
// StorageLocation
type StorageLocation struct {
Classifier string `json:"classifier,omitempty" protobuf:"bytes,1,opt,name=classifier"`
GitURL string `json:"gitUrl,omitempty" protobuf:"bytes,2,opt,name=gitUrl"`
GitBranch string `json:"gitBranch,omitempty" protobuf:"bytes,3,opt,name=gitBranch"`
BucketURL string `json:"bucketUrl,omitempty" protobuf:"bytes,4,opt,name=bucketUrl"`
}
// QuickStartLocation
type QuickStartLocation struct {
GitURL string `json:"gitUrl,omitempty" protobuf:"bytes,1,opt,name=gitUrl"`
GitKind string `json:"gitKind,omitempty" protobuf:"bytes,2,opt,name=gitKind"`
Owner string `json:"owner,omitempty" protobuf:"bytes,3,opt,name=owner"`
Includes []string `json:"includes,omitempty" protobuf:"bytes,4,opt,name=includes"`
Excludes []string `json:"excludes,omitempty" protobuf:"bytes,5,opt,name=excludes"`
}
// PreviewGitSpec is the preview git branch/pull request details
type PreviewGitSpec struct {
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
URL string `json:"url,omitempty" protobuf:"bytes,2,opt,name=url"`
User UserSpec `json:"user,omitempty" protobuf:"bytes,3,opt,name=user"`
Title string `json:"title,omitempty" protobuf:"bytes,4,opt,name=title"`
Description string `json:"description,omitempty" protobuf:"bytes,5,opt,name=description"`
BuildStatus string `json:"buildStatus,omitempty" protobuf:"bytes,6,opt,name=buildStatus"`
BuildStatusURL string `json:"buildStatusUrl,omitempty" protobuf:"bytes,7,opt,name=buildStatusUrl"`
ApplicationName string `json:"appName,omitempty" protobuf:"bytes,8,opt,name=appName"`
ApplicationURL string `json:"applicationURL,omitempty" protobuf:"bytes,9,opt,name=applicationURL"`
}
// UserSpec is the user details
type UserSpec struct {
Username string `json:"username,omitempty" protobuf:"bytes,1,opt,name=username"`
Name string `json:"name,omitempty" protobuf:"bytes,2,opt,name=name"`
LinkURL string `json:"linkUrl,omitempty" protobuf:"bytes,3,opt,name=linkUrl"`
ImageURL string `json:"imageUrl,omitempty" protobuf:"bytes,4,opt,name=imageUrl"`
}
// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +k8s:openapi-gen=true
// EnvironmentRoleBinding is like a vanilla RoleBinding but applies to a set of Namespaces based on an Environment filter
// so that roles can be bound to multiple namespaces easily.
//
// For example to specify the binding of roles on all Preview environments or on all permanent environments.
type EnvironmentRoleBinding struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Spec EnvironmentRoleBindingSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
Status EnvironmentRoleBindingStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
// EnvironmentRoleBindingSpec is the specification of an EnvironmentRoleBinding
type EnvironmentRoleBindingSpec struct {
// Subjects holds references to the objects the role applies to.
Subjects []rbacv1.Subject `json:"subjects" protobuf:"bytes,2,rep,name=subjects"`
// RoleRef can reference a Role in the current namespace or a ClusterRole in the global namespace.
// If the RoleRef cannot be resolved, the Authorizer must return an error.
RoleRef rbacv1.RoleRef `json:"roleRef" protobuf:"bytes,3,opt,name=roleRef"`
// specifies which sets of environments this binding applies to
Environments []EnvironmentFilter `json:"environments,omitempty" protobuf:"bytes,4,opt,name=environments"`
}
// EnvironmentFilter specifies the environments to apply the role binding to
type EnvironmentFilter struct {
Kind EnvironmentKindType `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"`
Includes []string `json:"includes,omitempty" protobuf:"bytes,2,opt,name=includes"`
Excludes []string `json:"excludes,omitempty" protobuf:"bytes,3,opt,name=excludes"`
}
// EnvironmentRoleBindingStatus is the status for an EnvironmentRoleBinding resource
type EnvironmentRoleBindingStatus struct {
Version string `json:"version,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// EnvironmentRoleBindingList is a list of TypeMeta resources
type EnvironmentRoleBindingList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []EnvironmentRoleBinding `json:"items"`
}
// StorageLocationOrDefault returns the storage location if there is one or returns the default storage configuration
func (t *TeamSettings) StorageLocationOrDefault(classifier string) *StorageLocation {
for idx, sl := range t.StorageLocations {
if sl.Classifier == classifier {
return &t.StorageLocations[idx]
}
}
return t.StorageLocation("default")
}
// StorageLocation returns the storage location, lazily creating one if one does not already exist
func (t *TeamSettings) StorageLocation(classifier string) *StorageLocation {
for idx, sl := range t.StorageLocations {
if sl.Classifier == classifier {
return &t.StorageLocations[idx]
}
}
t.StorageLocations = append(t.StorageLocations, StorageLocation{
Classifier: classifier,
})
return &t.StorageLocations[len(t.StorageLocations)-1]
}
// SetStorageLocation stores the given storage location in the team settings
func (t *TeamSettings) SetStorageLocation(classifier string, storage StorageLocation) {
storage.Classifier = classifier
for idx, sl := range t.StorageLocations {
if sl.Classifier == classifier {
t.StorageLocations[idx] = storage
return
}
}
t.StorageLocations = append(t.StorageLocations, storage)
}
// GetImportMode returns the import mode - returning a default value if it has not been populated yet
func (t *TeamSettings) GetImportMode() ImportModeType {
if string(t.ImportMode) == "" {
return ImportModeTypeJenkinsfile
}
return t.ImportMode
}
// GetProwEngine returns the import mode - returning a default value if it has not been populated yet
func (t *TeamSettings) GetProwEngine() ProwEngineType {
if string(t.ProwEngine) == "" {
return ProwEngineTypeKnativeBuild
}
return t.ProwEngine
}
// IsEmpty returns true if the storage location is empty
func (s *StorageLocation) IsEmpty() bool {
return s.GitURL == "" && s.BucketURL == ""
}
// Description returns the textual description of the storage location
func (s *StorageLocation) Description() string {
if s.GitURL != "" {
return s.GitURL + " branch: " + s.GetGitBranch()
}
if s.BucketURL != "" {
return s.BucketURL
}
return "current git repo"
}
// GetGitBranch returns the git branch to use when using git storage
func (s *StorageLocation) GetGitBranch() string {
branch := s.GitBranch
if branch == "" {
branch = "gh-pages"
}
return branch
}
var (
// ImportModeStrings contains the list of strings of all the available import modes
ImportModeStrings = []string{
string(ImportModeTypeJenkinsfile),
string(ImportModeTypeYAML),
}
)