forked from goharbor/harbor-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harborcluster_webhook.go
305 lines (249 loc) · 10.6 KB
/
harborcluster_webhook.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
package v1beta1
import (
"context"
"fmt"
"github.com/plotly/harbor-operator/pkg/version"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func (harborcluster *HarborCluster) SetupWebhookWithManager(_ context.Context, mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(harborcluster).
Complete()
}
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
const (
// The minimal number of volumes created by minio.
minimalVolumeCount = 4
)
// Log used this webhook.
var clog = logf.Log.WithName("harborcluster-resource")
// +kubebuilder:webhook:verbs=create;update,path=/mutate-goharbor-io-v1beta1-harborcluster,mutating=true,failurePolicy=fail,groups=goharbor.io,resources=harborclusters,versions=v1beta1,name=mharborcluster.kb.io,admissionReviewVersions={"v1beta1","v1"},sideEffects=None
var _ webhook.Defaulter = &HarborCluster{}
func (harborcluster *HarborCluster) Default() { //nolint:funlen
switch harborcluster.Spec.Cache.Kind {
case KindCacheRedis:
harborcluster.Spec.Cache.Spec.RedisFailover = nil
case KindCacheRedisFailover:
harborcluster.Spec.Cache.Spec.Redis = nil
}
switch harborcluster.Spec.Database.Kind {
case KindDatabasePostgreSQL:
harborcluster.Spec.Database.Spec.ZlandoPostgreSQL = nil
case KindDatabaseZlandoPostgreSQL:
harborcluster.Spec.Database.Spec.PostgreSQL = nil
}
switch harborcluster.Spec.Storage.Kind {
case KindStorageFileSystem:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
case KindStorageS3:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
case KindStorageSwift:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
case KindStorageMinIO:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
case KindStorageAzure:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
case KindStorageGcs:
harborcluster.Spec.Storage.Spec.Oss = nil
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
case KindStorageOss:
harborcluster.Spec.Storage.Spec.Azure = nil
harborcluster.Spec.Storage.Spec.S3 = nil
harborcluster.Spec.Storage.Spec.Swift = nil
harborcluster.Spec.Storage.Spec.FileSystem = nil
harborcluster.Spec.Storage.Spec.MinIO = nil
harborcluster.Spec.Storage.Spec.Gcs = nil
}
}
// +kubebuilder:webhook:verbs=create;update,path=/validate-goharbor-io-v1beta1-harborcluster,mutating=false,failurePolicy=fail,groups=goharbor.io,resources=harborclusters,versions=v1beta1,name=vharborcluster.kb.io,admissionReviewVersions={"v1beta1","v1"},sideEffects=None
var _ webhook.Validator = &HarborCluster{}
func (harborcluster *HarborCluster) ValidateCreate() error {
clog.Info("validate creation", "name", harborcluster.Name, "namespace", harborcluster.Namespace)
return harborcluster.validate(harborcluster)
}
func (harborcluster *HarborCluster) ValidateUpdate(old runtime.Object) error {
clog.Info("validate updating", "name", harborcluster.Name, "namespace", harborcluster.Namespace)
obj, ok := old.(*HarborCluster)
if !ok {
return errors.Errorf("failed type assertion on kind: %s", old.GetObjectKind().GroupVersionKind().String())
}
return harborcluster.validate(obj)
}
func (harborcluster *HarborCluster) ValidateDelete() error {
clog.Info("validate deletion", "name", harborcluster.Name, "namespace", harborcluster.Namespace)
return nil
}
func (harborcluster *HarborCluster) validate(old *HarborCluster) error {
var allErrs field.ErrorList
if err := harborcluster.Spec.Network.Validate(nil); err != nil {
allErrs = append(allErrs, err)
}
if err := harborcluster.Spec.Trace.Validate(nil); err != nil {
allErrs = append(allErrs, err)
}
// For database(psql), cache(Redis) and storage, either external services or in-cluster services MUST be configured
if err := harborcluster.validateStorage(); err != nil {
allErrs = append(allErrs, err)
}
if err := harborcluster.validateDatabase(); err != nil {
allErrs = append(allErrs, err)
}
if err := harborcluster.validateCache(); err != nil {
allErrs = append(allErrs, err)
}
if old == nil { // create harbor resource
if err := version.Validate(harborcluster.Spec.Version); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("version"), harborcluster.Spec.Version, err.Error()))
}
} else {
if err := version.UpgradeAllowed(old.Spec.Version, harborcluster.Spec.Version); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("version"), harborcluster.Spec.Version, err.Error()))
}
}
if old.Spec.Cache.Kind != harborcluster.Spec.Cache.Kind {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec").Child("cache"),
"don't allow to switch cache between incluster and external"))
}
if old.Spec.Database.Kind != harborcluster.Spec.Database.Kind {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec").Child("database"),
"don't allow to switch database between incluster and external"))
}
if old.Spec.Storage.Kind != harborcluster.Spec.Storage.Kind {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec").Child("storage"),
"don't allow to switch storage between incluster and external"))
}
if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "HarborCluster"}, harborcluster.Name, allErrs)
}
func (harborcluster *HarborCluster) validateStorage() *field.Error { //nolint:funlen,gocognit
// in cluster storage has high priority
fp := field.NewPath("spec").Child("storage").Child("spec")
// Storage
if harborcluster.Spec.Storage.Kind == KindStorageS3 && harborcluster.Spec.Storage.Spec.S3 == nil {
// Invalid and not acceptable
return required(fp.Child("s3"))
}
if harborcluster.Spec.Storage.Kind == KindStorageMinIO && harborcluster.Spec.Storage.Spec.MinIO == nil {
// Invalid and not acceptable
return required(fp.Child("minIO"))
}
if harborcluster.Spec.Storage.Kind == KindStorageSwift && harborcluster.Spec.Storage.Spec.Swift == nil {
// Invalid and not acceptable
return required(fp.Child("swift"))
}
if harborcluster.Spec.Storage.Kind == KindStorageAzure && harborcluster.Spec.Storage.Spec.Azure == nil {
// Invalid and not acceptable
return required(fp.Child("azure"))
}
if harborcluster.Spec.Storage.Kind == KindStorageGcs && harborcluster.Spec.Storage.Spec.Gcs == nil {
// Invalid and not acceptable
return required(fp.Child("gcs"))
}
if harborcluster.Spec.Storage.Kind == KindStorageOss && harborcluster.Spec.Storage.Spec.Oss == nil {
// Invalid and not acceptable
return required(fp.Child("oss"))
}
if harborcluster.Spec.Storage.Kind == KindStorageFileSystem && harborcluster.Spec.Storage.Spec.FileSystem == nil {
// Invalid and not acceptable
return required(fp.Child("fileSystem"))
}
// Validate more if incluster storage is configured.
if harborcluster.Spec.Storage.Kind == KindStorageMinIO { //nolint:nestif
desiredReplicas := harborcluster.Spec.Storage.Spec.MinIO.Replicas
volumePerServer := harborcluster.Spec.Storage.Spec.MinIO.VolumesPerServer
if desiredReplicas*volumePerServer < minimalVolumeCount {
return invalid(fp, harborcluster.Spec.Storage.Spec.MinIO, fmt.Sprintf("minIO.replicas * minIO.volumesPerServer should be >=%d", minimalVolumeCount))
}
redirect := harborcluster.Spec.Storage.Spec.Redirect
rp := fp.Child("redirect")
if redirect == nil && harborcluster.Spec.Storage.Spec.MinIO != nil {
redirect = harborcluster.Spec.Storage.Spec.MinIO.Redirect
rp = fp.Child("minio").Child("redirect")
}
if redirect != nil && redirect.Enable {
if redirect.Expose == nil || redirect.Expose.Ingress == nil {
return required(rp.Child("ingress"))
}
}
}
return nil
}
func (harborcluster *HarborCluster) validateDatabase() *field.Error {
// in cluster database has high priority
fp := field.NewPath("spec").Child("database").Child("spec")
// Database
// External is not configured
// And also in-cluster psql is not specified
if harborcluster.Spec.Database.Kind == KindDatabasePostgreSQL && harborcluster.Spec.Database.Spec.PostgreSQL == nil {
// Invalid and not acceptable
return required(fp.Child("postgreSQL"))
}
// Both are configured then conflict
if harborcluster.Spec.Database.Kind == KindDatabaseZlandoPostgreSQL && harborcluster.Spec.Database.Spec.ZlandoPostgreSQL == nil {
// Invalid and not acceptable
return required(fp.Child("zlandoPostgreSQL"))
}
return nil
}
func (harborcluster *HarborCluster) validateCache() *field.Error {
fp := field.NewPath("spec").Child("cache").Child("spec")
// Cache
// External is not configured
if harborcluster.Spec.Cache.Kind == KindCacheRedis && harborcluster.Spec.Cache.Spec.Redis == nil {
// Invalid and not acceptable
return required(fp.Child("redis"))
}
// Both are configured and then conflict
if harborcluster.Spec.Cache.Kind == KindCacheRedisFailover && harborcluster.Spec.Cache.Spec.RedisFailover == nil {
// Invalid and not acceptable
return required(fp.Child("redisFailover"))
}
return nil
}
func required(mainPath *field.Path) *field.Error {
return field.Required(mainPath, fmt.Sprintf("%s should be configured", mainPath.String()))
}
func invalid(mainPath *field.Path, value interface{}, details string) *field.Error {
return field.Invalid(mainPath, value, details)
}