-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathopenstackcontrolplane_webhook.go
More file actions
1726 lines (1519 loc) · 67.9 KB
/
openstackcontrolplane_webhook.go
File metadata and controls
1726 lines (1519 loc) · 67.9 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2022.
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 v1beta1
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"slices"
"strings"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common/object"
"github.com/openstack-k8s-operators/lib-common/modules/common/route"
common_webhook "github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
placementv1 "github.com/openstack-k8s-operators/nova-operator/api/placement/v1beta1"
watcherv1 "github.com/openstack-k8s-operators/watcher-operator/api/v1beta1"
"golang.org/x/exp/maps"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
barbicanv1 "github.com/openstack-k8s-operators/barbican-operator/api/v1beta1"
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
designatev1 "github.com/openstack-k8s-operators/designate-operator/api/v1beta1"
glancev1 "github.com/openstack-k8s-operators/glance-operator/api/v1beta1"
heatv1 "github.com/openstack-k8s-operators/heat-operator/api/v1beta1"
horizonv1 "github.com/openstack-k8s-operators/horizon-operator/api/v1beta1"
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
networkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1"
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
redisv1 "github.com/openstack-k8s-operators/infra-operator/apis/redis/v1beta1"
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
ironicv1 "github.com/openstack-k8s-operators/ironic-operator/api/v1beta1"
manilav1 "github.com/openstack-k8s-operators/manila-operator/api/v1beta1"
neutronv1 "github.com/openstack-k8s-operators/neutron-operator/api/v1beta1"
novav1 "github.com/openstack-k8s-operators/nova-operator/api/nova/v1beta1"
octaviav1 "github.com/openstack-k8s-operators/octavia-operator/api/v1beta1"
swiftv1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
)
// log is for logging in this package.
var openstackcontrolplanelog = logf.Log.WithName("openstackcontrolplane-resource")
// generateRandomID generates a random 5-character hexadecimal ID
// Used for service naming when UniquePodNames is enabled and UID is not yet available
func generateRandomID() (string, error) {
bytes := make([]byte, 3) // 3 bytes = 6 hex chars, we'll take first 5
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes)[:5], nil
}
// lookupServiceCR attempts to find an existing service CR in the cluster owned by this OpenStackControlPlane
// Returns the CR name if found, empty string if not found or not owned by the given owner UID
// serviceName should be the base service name (e.g., CinderName, GlanceName)
// ownerUID is the UID of the OpenStackControlPlane that should own the CR
// This function lists CRs and finds ones that start with the service name prefix and are owned by ownerUID
func lookupServiceCR(ctx context.Context, c client.Client, namespace, serviceName string, ownerUID types.UID) (string, error) {
switch serviceName {
case CinderName:
cinderList := &cinderv1.CinderList{}
if err := c.List(ctx, cinderList, client.InNamespace(namespace)); err != nil {
return "", fmt.Errorf("failed to list Cinder CRs: %w", err)
}
// Find any Cinder CR that starts with "cinder" and is owned by this OpenStackControlPlane
for _, cinder := range cinderList.Items {
if strings.HasPrefix(cinder.Name, CinderName) && object.CheckOwnerRefExist(ownerUID, cinder.GetOwnerReferences()) {
return cinder.Name, nil
}
}
case GlanceName:
glanceList := &glancev1.GlanceList{}
if err := c.List(ctx, glanceList, client.InNamespace(namespace)); err != nil {
return "", fmt.Errorf("failed to list Glance CRs: %w", err)
}
// Find any Glance CR that starts with "glance" and is owned by this OpenStackControlPlane
for _, glance := range glanceList.Items {
if strings.HasPrefix(glance.Name, GlanceName) && object.CheckOwnerRefExist(ownerUID, glance.GetOwnerReferences()) {
return glance.Name, nil
}
}
default:
return "", fmt.Errorf("unsupported service name: %s", serviceName)
}
return "", nil // Not found or not owned
}
// CacheServiceNameForCreate handles service name caching during CREATE operations
// Generates a random ID since UID is not yet available
func (r *OpenStackControlPlane) CacheServiceNameForCreate(serviceName string) (string, error) {
randomID, err := generateRandomID()
if err != nil {
return "", fmt.Errorf("failed to generate random ID: %w", err)
}
return fmt.Sprintf("%s-%s", serviceName, randomID), nil
}
// CacheServiceNameForUpdate handles service name caching during UPDATE operations
// Uses existing CR name if it's owned by this OpenStackControlPlane, otherwise generates based on current settings
// This provides robust flip detection: if we created a CR previously, we preserve its name to avoid creating duplicates
func (r *OpenStackControlPlane) CacheServiceNameForUpdate(ctx context.Context, c client.Client, serviceName string) (string, error) {
// Lookup existing CR owned by this OpenStackControlPlane
existingName, err := lookupServiceCR(ctx, c, r.Namespace, serviceName, r.UID)
if err != nil {
return "", fmt.Errorf("failed to lookup existing CR: %w", err)
}
// If we find a CR owned by us, preserve its name regardless of format
// This handles both flip scenarios and prevents creating duplicate CRs:
// - If UniquePodNames changed from false→true, we keep the old "cinder" name
// - If UniquePodNames changed from true→false, we keep the old "cinder-abc" name
// - If UniquePodNames didn't change, we keep the existing name
if existingName != "" {
return existingName, nil
}
// No existing CR found owned by us - generate name based on current UniquePodNames setting
// This handles:
// - First time deployment
// - Operator upgrade scenarios where ServiceName wasn't cached yet
name, _ := r.GetServiceName(serviceName, true)
if name == serviceName {
// GetServiceName returned base name, meaning UID is not available
return "", fmt.Errorf("unable to generate service name: no existing CR and UID not available")
}
return name, nil
}
// ValidateCreate validates the OpenStackControlPlane on creation
func (r *OpenStackControlPlane) ValidateCreate(ctx context.Context, c client.Client) (admission.Warnings, error) {
openstackcontrolplanelog.Info("validate create", "name", r.Name)
var allWarn []string
basePath := field.NewPath("spec")
ctlplaneList := &OpenStackControlPlaneList{}
listOpts := []client.ListOption{
client.InNamespace(r.Namespace),
}
if err := c.List(ctx, ctlplaneList, listOpts...); err != nil {
return nil, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackControlPlane").Group,
Resource: GroupVersion.WithKind("OpenStackControlPlane").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Name,
Detail: err.Error(),
},
)
}
if len(ctlplaneList.Items) >= 1 {
return nil, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackControlPlane").Group,
Resource: GroupVersion.WithKind("OpenStackControlPlane").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Name,
Detail: "Only one OpenStackControlPlane instance per namespace is supported at this time.",
},
)
}
allErrs, err := r.ValidateVersion(ctx, c)
// Version validation can generate non-field errors, so we consider those first
if err != nil {
return nil, err
}
// Validate deprecated fields using centralized validation
warnings, errs := r.validateDeprecatedFieldsCreate(basePath)
allWarn = append(allWarn, warnings...)
allErrs = append(allErrs, errs...)
warns, errs := r.ValidateCreateServices(basePath)
allWarn = append(allWarn, warns...)
allErrs = append(allErrs, errs...)
if err := r.ValidateTopology(basePath); err != nil {
allErrs = append(allErrs, err)
}
if errs := r.ValidateMessagingBusConfig(basePath); len(errs) != 0 {
allErrs = append(allErrs, errs...)
}
if len(allErrs) != 0 {
return allWarn, apierrors.NewInvalid(
schema.GroupKind{Group: "core.openstack.org", Kind: "OpenStackControlPlane"},
r.Name, allErrs)
}
return allWarn, nil
}
// ValidateUpdate validates the OpenStackControlPlane on update
func (r *OpenStackControlPlane) ValidateUpdate(ctx context.Context, old runtime.Object, c client.Client) (admission.Warnings, error) {
openstackcontrolplanelog.Info("validate update", "name", r.Name)
oldControlPlane, ok := old.(*OpenStackControlPlane)
if !ok || oldControlPlane == nil {
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
}
// Handle annotation-triggered migration from controller
const reconcileTriggerAnnotation = "openstack.org/reconcile-trigger"
if annotations := r.GetAnnotations(); annotations != nil {
if _, exists := annotations[reconcileTriggerAnnotation]; exists {
openstackcontrolplanelog.Info("Reconcile trigger annotation detected, performing migration",
"instance", r.Name)
r.migrateDeprecatedFields()
delete(annotations, reconcileTriggerAnnotation)
r.SetAnnotations(annotations)
}
}
var allWarn []string
var allErrs field.ErrorList
basePath := field.NewPath("spec")
// Validate deprecated fields using centralized validation
warnings, errs := r.validateDeprecatedFieldsUpdate(*oldControlPlane, basePath)
allWarn = append(allWarn, warnings...)
allErrs = append(allErrs, errs...)
warns, errs := r.ValidateUpdateServices(oldControlPlane.Spec, basePath)
allWarn = append(allWarn, warns...)
allErrs = append(allErrs, errs...)
if err := r.ValidateTopology(basePath); err != nil {
allErrs = append(allErrs, err)
}
if errs := r.ValidateMessagingBusConfig(basePath); len(errs) != 0 {
allErrs = append(allErrs, errs...)
}
if len(allErrs) != 0 {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "core.openstack.org", Kind: "OpenStackControlPlane"},
r.Name, allErrs)
}
return allWarn, nil
}
// ValidateDelete validates the OpenStackControlPlane on deletion
func (r *OpenStackControlPlane) ValidateDelete(ctx context.Context, c client.Client) (admission.Warnings, error) {
openstackcontrolplanelog.Info("validate delete", "name", r.Name)
return nil, nil
}
// checkDepsEnabled - returns a non-empty string if required services are missing (disabled) for "name" service
func (r *OpenStackControlPlane) checkDepsEnabled(name string) string {
// "msg" will hold any dependency validation error we might find
msg := ""
// "reqs" will be set to the required services for "name" service
// if any of those required services are improperly disabled/missing
reqs := ""
switch name {
case "Keystone":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled) {
reqs = "Galera, Memcached, RabbitMQ"
}
case "Glance":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Keystone.Enabled) {
reqs = "Galera, Memcached, Keystone"
}
case "Cinder":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled) {
reqs = "Galera, Memcached, RabbitMQ, Keystone"
}
case "Placement":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Keystone.Enabled) {
reqs = "Galera, Memcached, Keystone"
}
case "Neutron":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled) {
reqs = "Galera, RabbitMQ, Keystone"
}
case "Nova":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled && r.Spec.Placement.Enabled && r.Spec.Neutron.Enabled && r.Spec.Glance.Enabled) {
reqs = "Galera, Memcached, RabbitMQ, Keystone, Glance, Neutron, Placement"
}
case "Heat":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled) {
reqs = "Galera, Memcached, RabbitMQ, Keystone"
}
case "Swift":
if !(r.Spec.Memcached.Enabled && r.Spec.Keystone.Enabled) {
reqs = "Memcached, Keystone"
}
case "Horizon":
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Keystone.Enabled) {
reqs = "Galera, Memcached, Keystone"
}
case "Barbican":
if !((r.Spec.Galera.Enabled) && r.Spec.Keystone.Enabled) {
reqs = "Galera, Keystone"
}
case "Octavia":
// TODO(beagles): So far we haven't declared Redis as dependency for Octavia, but we might.
if !((r.Spec.Galera.Enabled) && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled && r.Spec.Neutron.Enabled && r.Spec.Glance.Enabled && r.Spec.Nova.Enabled &&
r.Spec.Ovn.Enabled) {
reqs = "Galera, Memcached, RabbitMQ, Keystone, Glance, Neutron, Nova, OVN"
}
case "Telemetry.Autoscaling":
if !(r.Spec.Galera.Enabled && r.Spec.Heat.Enabled && r.Spec.Rabbitmq.Enabled && r.Spec.Keystone.Enabled) {
reqs = "Galera, Heat, RabbitMQ, Keystone"
}
case "Telemetry.Ceilometer":
if !(r.Spec.Rabbitmq.Enabled && r.Spec.Keystone.Enabled) {
reqs = "RabbitMQ, Keystone"
}
case "Telemetry.CloudKitty":
if !(r.Spec.Rabbitmq.Enabled && r.Spec.Keystone.Enabled) {
reqs = "RabbitMQ, Keystone"
}
case "Watcher":
if !(r.Spec.Galera.Enabled && r.Spec.Memcached.Enabled && r.Spec.Rabbitmq.Enabled &&
r.Spec.Keystone.Enabled && r.Spec.Telemetry.Enabled && *r.Spec.Telemetry.Template.Ceilometer.Enabled &&
*r.Spec.Telemetry.Template.MetricStorage.Enabled) {
reqs = "Galera, Memcached, RabbitMQ, Keystone, Telemetry, Telemetry.Ceilometer, Telemetry.MetricStorage"
}
}
// If "reqs" is not the empty string, we have missing requirements
if reqs != "" {
msg = fmt.Sprintf("%s requires these services to be enabled: %s.", name, reqs)
}
return msg
}
// ValidateCreateServices validating service definitions during the OpenstackControlPlane CR creation
func (r *OpenStackControlPlane) ValidateCreateServices(basePath *field.Path) (admission.Warnings, field.ErrorList) {
var errors field.ErrorList
var warnings []string
errors = append(errors, r.ValidateServiceDependencies(basePath)...)
// Call internal validation logic for individual service operators
if r.Spec.Keystone.Enabled {
warns, errs := r.Spec.Keystone.Template.ValidateCreate(basePath.Child("keystone").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Keystone.APIOverride.Route, basePath.Child("keystone").Child("apiOverride").Child("route"))...)
}
if r.Spec.Ironic.Enabled {
warns, errs := r.Spec.Ironic.Template.ValidateCreate(basePath.Child("ironic").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Ironic.APIOverride.Route, basePath.Child("ironic").Child("apiOverride").Child("route"))...)
}
if r.Spec.Nova.Enabled {
warns, errs := r.Spec.Nova.Template.ValidateCreate(basePath.Child("nova").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Nova.APIOverride.Route, basePath.Child("nova").Child("apiOverride").Child("route"))...)
}
if r.Spec.Placement.Enabled {
errors = append(errors, r.Spec.Placement.Template.ValidateCreate(basePath.Child("placement").Child("template"), r.Namespace)...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Placement.APIOverride.Route, basePath.Child("placement").Child("apiOverride").Child("route"))...)
}
if r.Spec.Barbican.Enabled {
warns, errs := r.Spec.Barbican.Template.ValidateCreate(basePath.Child("barbican").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Barbican.APIOverride.Route, basePath.Child("barbican").Child("apiOverride").Child("route"))...)
}
if r.Spec.Neutron.Enabled {
warns, errs := r.Spec.Neutron.Template.ValidateCreate(basePath.Child("neutron").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Neutron.APIOverride.Route, basePath.Child("neutron").Child("apiOverride").Child("route"))...)
}
if r.Spec.Glance.Enabled {
glanceName, _ := r.GetServiceNameCached(GlanceName, r.Spec.Glance.UniquePodNames, r.Spec.Glance.ServiceName)
for key, glanceAPI := range r.Spec.Glance.Template.GlanceAPIs {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("glance").Child("template").Child("glanceAPIs"),
[]string{key},
glancev1.GetCrMaxLengthCorrection(glanceName, glanceAPI.Type)) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
warns, errs := r.Spec.Glance.Template.ValidateCreate(basePath.Child("glance").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
for key, override := range r.Spec.Glance.APIOverride {
overridePath := basePath.Child("glance").Child("apiOverride").Key(key)
errors = append(errors, validateTLSOverrideSpec(&override.Route, overridePath.Child("route"))...)
}
}
if r.Spec.Cinder.Enabled {
cinderName, _ := r.GetServiceNameCached(CinderName, r.Spec.Cinder.UniquePodNames, r.Spec.Cinder.ServiceName)
errs := common_webhook.ValidateDNS1123Label(
basePath.Child("cinder").Child("template").Child("cinderVolumes"),
maps.Keys(r.Spec.Cinder.Template.CinderVolumes),
cinderv1.GetCrMaxLengthCorrection(cinderName)) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, errs...)
warns, errs := r.Spec.Cinder.Template.ValidateCreate(basePath.Child("cinder").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Cinder.APIOverride.Route, basePath.Child("cinder").Child("apiOverride").Child("route"))...)
}
if r.Spec.Heat.Enabled {
warns, errs := r.Spec.Heat.Template.ValidateCreate(basePath.Child("heat").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Heat.APIOverride.Route, basePath.Child("heat").Child("apiOverride").Child("route"))...)
}
if r.Spec.Manila.Enabled {
warns, errs := r.Spec.Manila.Template.ValidateCreate(basePath.Child("manila").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Manila.APIOverride.Route, basePath.Child("manila").Child("apiOverride").Child("route"))...)
}
if r.Spec.Swift.Enabled {
errors = append(errors, r.Spec.Swift.Template.ValidateCreate(basePath.Child("swift").Child("template"), r.Namespace)...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Swift.ProxyOverride.Route, basePath.Child("swift").Child("apiOverride").Child("route"))...)
}
if r.Spec.Octavia.Enabled {
warns, errs := r.Spec.Octavia.Template.ValidateCreate(basePath.Child("octavia").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Octavia.APIOverride.Route, basePath.Child("octavia").Child("apiOverride").Child("route"))...)
}
if r.Spec.Designate.Enabled {
warns, errs := r.Spec.Designate.Template.ValidateCreate(basePath.Child("designate").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Designate.APIOverride.Route, basePath.Child("designate").Child("apiOverride").Child("route"))...)
}
if r.Spec.Watcher.Enabled {
warns, errs := r.Spec.Watcher.Template.ValidateCreate(basePath.Child("watcher").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Watcher.APIOverride.Route, basePath.Child("watcher").Child("apiOverride").Child("route"))...)
}
if r.Spec.Telemetry.Enabled {
warns, errs := r.Spec.Telemetry.Template.ValidateCreate(basePath.Child("telemetry").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.AodhAPIOverride.Route, basePath.Child("telemetry").Child("aodhApiOverride").Child("route"))...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.PrometheusOverride.Route, basePath.Child("telemetry").Child("prometheusOverride").Child("route"))...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.AlertmanagerOverride.Route, basePath.Child("telemetry").Child("alertmanagerOverride").Child("route"))...)
}
// Validation for remaining services...
if r.Spec.Galera.Enabled {
for key, s := range *r.Spec.Galera.Templates {
warn, err := s.ValidateCreate(basePath.Child("galera").Child("template").Key(key), r.Namespace)
errors = append(errors, err...)
warnings = append(warnings, warn...)
}
}
if r.Spec.Memcached.Enabled {
if r.Spec.Memcached.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("memcached").Child("templates"),
maps.Keys(*r.Spec.Memcached.Templates),
memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
}
if r.Spec.Redis.Enabled {
if r.Spec.Redis.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("redis").Child("templates"),
maps.Keys(*r.Spec.Redis.Templates),
redisv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
}
if r.Spec.Rabbitmq.Enabled {
if r.Spec.Rabbitmq.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("rabbitmq").Child("templates"),
maps.Keys(*r.Spec.Rabbitmq.Templates),
memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
for rabbitmqName, rabbitmqSpec := range *r.Spec.Rabbitmq.Templates {
warn, errs := rabbitmqSpec.ValidateCreate(basePath.Child("rabbitmq").Child("template").Key(rabbitmqName), r.Namespace)
warnings = append(warnings, warn...)
errors = append(errors, errs...)
}
}
}
if r.Spec.Galera.Enabled {
if r.Spec.Galera.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("galera").Child("templates"),
maps.Keys(*r.Spec.Galera.Templates),
mariadbv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
}
return warnings, errors
}
// ValidateUpdateServices validating service definitions during the OpenstackControlPlane CR update
func (r *OpenStackControlPlane) ValidateUpdateServices(old OpenStackControlPlaneSpec, basePath *field.Path) (admission.Warnings, field.ErrorList) {
var errors field.ErrorList
var warnings []string
errors = append(errors, r.ValidateServiceDependencies(basePath)...)
// Call internal validation logic for individual service operators
if r.Spec.Keystone.Enabled {
if old.Keystone.Template == nil {
old.Keystone.Template = &keystonev1.KeystoneAPISpecCore{}
}
warns, errs := r.Spec.Keystone.Template.ValidateUpdate(*old.Keystone.Template, basePath.Child("keystone").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Keystone.APIOverride.Route, basePath.Child("keystone").Child("apiOverride").Child("route"))...)
}
if r.Spec.Ironic.Enabled {
if old.Ironic.Template == nil {
old.Ironic.Template = &ironicv1.IronicSpecCore{}
}
warns, errs := r.Spec.Ironic.Template.ValidateUpdate(*old.Ironic.Template, basePath.Child("ironic").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Ironic.APIOverride.Route, basePath.Child("ironic").Child("apiOverride").Child("route"))...)
}
if r.Spec.Nova.Enabled {
if old.Nova.Template == nil {
old.Nova.Template = &novav1.NovaSpecCore{}
}
warns, errs := r.Spec.Nova.Template.ValidateUpdate(*old.Nova.Template, basePath.Child("nova").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Nova.APIOverride.Route, basePath.Child("nova").Child("apiOverride").Child("route"))...)
}
if r.Spec.Placement.Enabled {
if old.Placement.Template == nil {
old.Placement.Template = &placementv1.PlacementAPISpecCore{}
}
errors = append(errors, r.Spec.Placement.Template.ValidateUpdate(*old.Placement.Template, basePath.Child("placement").Child("template"), r.Namespace)...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Placement.APIOverride.Route, basePath.Child("placement").Child("apiOverride").Child("route"))...)
}
if r.Spec.Barbican.Enabled {
if old.Barbican.Template == nil {
old.Barbican.Template = &barbicanv1.BarbicanSpecCore{}
}
warns, errs := r.Spec.Barbican.Template.ValidateUpdate(*old.Barbican.Template, basePath.Child("barbican").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Barbican.APIOverride.Route, basePath.Child("barbican").Child("apiOverride").Child("route"))...)
}
if r.Spec.Neutron.Enabled {
if old.Neutron.Template == nil {
old.Neutron.Template = &neutronv1.NeutronAPISpecCore{}
}
warns, errs := r.Spec.Neutron.Template.ValidateUpdate(*old.Neutron.Template, basePath.Child("neutron").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Neutron.APIOverride.Route, basePath.Child("neutron").Child("apiOverride").Child("route"))...)
}
if r.Spec.Glance.Enabled {
if old.Glance.Template == nil {
old.Glance.Template = &glancev1.GlanceSpecCore{}
}
glanceName, _ := r.GetServiceNameCached(GlanceName, r.Spec.Glance.UniquePodNames, r.Spec.Glance.ServiceName)
for key, glanceAPI := range r.Spec.Glance.Template.GlanceAPIs {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("glance").Child("template").Child("glanceAPIs"),
[]string{key},
glancev1.GetCrMaxLengthCorrection(glanceName, glanceAPI.Type)) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
warns, errs := r.Spec.Glance.Template.ValidateUpdate(*old.Glance.Template, basePath.Child("glance").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
for key, override := range r.Spec.Glance.APIOverride {
overridePath := basePath.Child("glance").Child("apiOverride").Key(key)
errors = append(errors, validateTLSOverrideSpec(&override.Route, overridePath.Child("route"))...)
}
}
if r.Spec.Cinder.Enabled {
if old.Cinder.Template == nil {
old.Cinder.Template = &cinderv1.CinderSpecCore{}
}
cinderName, _ := r.GetServiceNameCached(CinderName, r.Spec.Cinder.UniquePodNames, r.Spec.Cinder.ServiceName)
errs := common_webhook.ValidateDNS1123Label(
basePath.Child("cinder").Child("template").Child("cinderVolumes"),
maps.Keys(r.Spec.Cinder.Template.CinderVolumes),
cinderv1.GetCrMaxLengthCorrection(cinderName)) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, errs...)
warns, errs := r.Spec.Cinder.Template.ValidateUpdate(*old.Cinder.Template, basePath.Child("cinder").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Cinder.APIOverride.Route, basePath.Child("cinder").Child("apiOverride").Child("route"))...)
}
if r.Spec.Heat.Enabled {
if old.Heat.Template == nil {
old.Heat.Template = &heatv1.HeatSpecCore{}
}
warns, errs := r.Spec.Heat.Template.ValidateUpdate(*old.Heat.Template, basePath.Child("heat").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Heat.APIOverride.Route, basePath.Child("heat").Child("apiOverride").Child("route"))...)
}
if r.Spec.Manila.Enabled {
if old.Manila.Template == nil {
old.Manila.Template = &manilav1.ManilaSpecCore{}
}
warns, errs := r.Spec.Manila.Template.ValidateUpdate(*old.Manila.Template, basePath.Child("manila").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Manila.APIOverride.Route, basePath.Child("manila").Child("apiOverride").Child("route"))...)
}
if r.Spec.Swift.Enabled {
if old.Swift.Template == nil {
old.Swift.Template = &swiftv1.SwiftSpecCore{}
}
errors = append(errors, r.Spec.Swift.Template.ValidateUpdate(*old.Swift.Template, basePath.Child("swift").Child("template"), r.Namespace)...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Swift.ProxyOverride.Route, basePath.Child("swift").Child("apiOverride").Child("route"))...)
}
if r.Spec.Octavia.Enabled {
if old.Octavia.Template == nil {
old.Octavia.Template = &octaviav1.OctaviaSpecCore{}
}
warns, errs := r.Spec.Octavia.Template.ValidateUpdate(*old.Octavia.Template, basePath.Child("octavia").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Octavia.APIOverride.Route, basePath.Child("octavia").Child("apiOverride").Child("route"))...)
}
if r.Spec.Designate.Enabled {
if old.Designate.Template == nil {
old.Designate.Template = &designatev1.DesignateSpecCore{}
}
warns, errs := r.Spec.Designate.Template.ValidateUpdate(*old.Designate.Template, basePath.Child("designate").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Designate.APIOverride.Route, basePath.Child("designate").Child("apiOverride").Child("route"))...)
}
if r.Spec.Watcher.Enabled {
if old.Watcher.Template == nil {
old.Watcher.Template = &watcherv1.WatcherSpecCore{}
}
warns, errs := r.Spec.Watcher.Template.ValidateUpdate(*old.Watcher.Template, basePath.Child("watcher").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Watcher.APIOverride.Route, basePath.Child("watcher").Child("apiOverride").Child("route"))...)
}
if r.Spec.Telemetry.Enabled {
if old.Telemetry.Template == nil {
old.Telemetry.Template = &telemetryv1.TelemetrySpecCore{}
}
warns, errs := r.Spec.Telemetry.Template.ValidateUpdate(*old.Telemetry.Template, basePath.Child("telemetry").Child("template"), r.Namespace)
errors = append(errors, errs...)
warnings = append(warnings, warns...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.AodhAPIOverride.Route, basePath.Child("telemetry").Child("aodhApiOverride").Child("route"))...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.PrometheusOverride.Route, basePath.Child("telemetry").Child("prometheusOverride").Child("route"))...)
errors = append(errors, validateTLSOverrideSpec(&r.Spec.Telemetry.AlertmanagerOverride.Route, basePath.Child("telemetry").Child("alertmanagerOverride").Child("route"))...)
}
if r.Spec.Memcached.Enabled {
if r.Spec.Memcached.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("memcached").Child("templates"),
maps.Keys(*r.Spec.Memcached.Templates),
memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
}
if r.Spec.Rabbitmq.Enabled {
if old.Rabbitmq.Templates == nil {
old.Rabbitmq.Templates = &map[string]rabbitmqv1.RabbitMqSpecCore{}
}
if r.Spec.Rabbitmq.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("rabbitmq").Child("templates"),
maps.Keys(*r.Spec.Rabbitmq.Templates),
memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
oldRabbitmqs := *old.Rabbitmq.Templates
for rabbitmqName, rabbitmqSpec := range *r.Spec.Rabbitmq.Templates {
if oldRabbitmq, ok := oldRabbitmqs[rabbitmqName]; ok {
warn, errs := rabbitmqSpec.ValidateUpdate(oldRabbitmq, basePath.Child("rabbitmq").Child("template").Key(rabbitmqName), r.Namespace)
warnings = append(warnings, warn...)
errors = append(errors, errs...)
}
}
}
if r.Spec.Galera.Enabled {
if r.Spec.Galera.Templates != nil {
err := common_webhook.ValidateDNS1123Label(
basePath.Child("galera").Child("templates"),
maps.Keys(*r.Spec.Galera.Templates),
mariadbv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
errors = append(errors, err...)
}
}
return warnings, errors
}
// ValidateServiceDependencies ensures that when a service is enabled then all the services it depends on are also
// enabled
func (r *OpenStackControlPlane) ValidateServiceDependencies(basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
// Add service dependency validations
if r.Spec.Keystone.Enabled {
if depErrorMsg := r.checkDepsEnabled("Keystone"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("keystone").Child("enabled"), r.Spec.Keystone.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Glance.Enabled {
if depErrorMsg := r.checkDepsEnabled("Glance"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("glance").Child("enabled"), r.Spec.Glance.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Cinder.Enabled {
if depErrorMsg := r.checkDepsEnabled("Cinder"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("cinder").Child("enabled"), r.Spec.Cinder.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Placement.Enabled {
if depErrorMsg := r.checkDepsEnabled("Placement"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("placement").Child("enabled"), r.Spec.Placement.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Neutron.Enabled {
if depErrorMsg := r.checkDepsEnabled("Neutron"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("neutron").Child("enabled"), r.Spec.Neutron.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Nova.Enabled {
if depErrorMsg := r.checkDepsEnabled("Nova"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("nova").Child("enabled"), r.Spec.Nova.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Heat.Enabled {
if depErrorMsg := r.checkDepsEnabled("Heat"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("heat").Child("enabled"), r.Spec.Heat.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Swift.Enabled {
if depErrorMsg := r.checkDepsEnabled("Swift"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("swift").Child("enabled"), r.Spec.Swift.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Horizon.Enabled {
if depErrorMsg := r.checkDepsEnabled("Horizon"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("horizon").Child("enabled"), r.Spec.Horizon.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Octavia.Enabled {
if depErrorMsg := r.checkDepsEnabled("Octavia"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("octavia").Child("enabled"), r.Spec.Octavia.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Barbican.Enabled {
if depErrorMsg := r.checkDepsEnabled("Barbican"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("barbican").Child("enabled"), r.Spec.Barbican.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Telemetry.Enabled &&
r.Spec.Telemetry.Template.Ceilometer.Enabled != nil &&
*r.Spec.Telemetry.Template.Ceilometer.Enabled {
if depErrorMsg := r.checkDepsEnabled("Telemetry.Ceilometer"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("telemetry").Child("template").Child("ceilometer").Child("enabled"),
*r.Spec.Telemetry.Template.Ceilometer.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Telemetry.Enabled &&
r.Spec.Telemetry.Template.Autoscaling.Enabled != nil &&
*r.Spec.Telemetry.Template.Autoscaling.Enabled {
if depErrorMsg := r.checkDepsEnabled("Telemetry.Autoscaling"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("telemetry").Child("template").Child("autoscaling").Child("enabled"),
*r.Spec.Telemetry.Template.Autoscaling.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
if r.Spec.Watcher.Enabled {
if depErrorMsg := r.checkDepsEnabled("Watcher"); depErrorMsg != "" {
err := field.Invalid(basePath.Child("watcher").Child("enabled"), r.Spec.Watcher.Enabled, depErrorMsg)
allErrs = append(allErrs, err)
}
}
return allErrs
}
// ValidateVersion validates the OpenStackVersion reference in the OpenStackControlPlane
func (r *OpenStackControlPlane) ValidateVersion(ctx context.Context, c client.Client) (field.ErrorList, error) {
var allErrs field.ErrorList
openStackVersionList, err := GetOpenStackVersions(r.Namespace, c)
if err != nil {
return allErrs, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackControlPlane").Group,
Resource: GroupVersion.WithKind("OpenStackControlPlane").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Name,
Detail: err.Error(),
},
)
}
if len(openStackVersionList.Items) > 0 {
if len(openStackVersionList.Items) > 1 {
return allErrs, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackControlPlane").Group,
Resource: GroupVersion.WithKind("OpenStackControlPlane").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Name,
Detail: fmt.Sprintf(
"multiple (%d) OpenStackVersions found in namespace %s: only one may be present. Please rectify before creating OpenStackControlPlane",
len(openStackVersionList.Items), r.Namespace),
},
)
}
openStackVersion := openStackVersionList.Items[0]
if openStackVersion.Name != r.Name {
err := field.Invalid(field.NewPath("metadata").Child("name"),
r.Name, fmt.Sprintf("OpenStackControlPlane '%s' must have same name as the existing '%s' OpenStackVersion",
r.Name, openStackVersion.Name))
allErrs = append(allErrs, err)
}
}
return allErrs, nil
}
// Default sets default values for the OpenStackControlPlane
func (r *OpenStackControlPlane) Default() {
openstackcontrolplanelog.Info("default", "name", r.Name)
r.DefaultLabel()
r.migrateDeprecatedFields()
r.DefaultServices()
}
// Helper function to initialize overrideSpec object. Could be moved to lib-common.
func initializeOverrideSpec(override **route.OverrideSpec, initAnnotations bool) {
if *override == nil {
*override = &route.OverrideSpec{}
}
if initAnnotations {
if (*override).EmbeddedLabelsAnnotations == nil {
(*override).EmbeddedLabelsAnnotations = &route.EmbeddedLabelsAnnotations{}
}
if (*override).Annotations == nil {
(*override).Annotations = make(map[string]string)
}
}
}
func setOverrideSpec(override **route.OverrideSpec, anno map[string]string) {
initializeOverrideSpec(override, false)
(*override).AddAnnotation(anno)
}
// DefaultServices - common function for calling individual services' defaulting functions
func (r *OpenStackControlPlane) DefaultServices() {
// Cinder
if r.Spec.Cinder.Enabled || r.Spec.Cinder.Template != nil {
if r.Spec.Cinder.Template == nil {
r.Spec.Cinder.Template = &cinderv1.CinderSpecCore{}
}
r.Spec.Cinder.Template.Default()
initializeOverrideSpec(&r.Spec.Cinder.APIOverride.Route, true)
r.Spec.Cinder.Template.SetDefaultRouteAnnotations(r.Spec.Cinder.APIOverride.Route.Annotations)
}
// Galera
if r.Spec.Galera.Enabled || r.Spec.Galera.Templates != nil {
if r.Spec.Galera.Templates == nil {
r.Spec.Galera.Templates = ptr.To(map[string]mariadbv1.GaleraSpecCore{})
}
for key, template := range *r.Spec.Galera.Templates {
if template.StorageClass == "" {
template.StorageClass = r.Spec.StorageClass
}
// Don't default Secret here - it's handled conditionally in reconciliation
// to support both default (osp-secret) and auto-generated (blank) passwords
template.Default()
// By-value copy, need to update
(*r.Spec.Galera.Templates)[key] = template
}
}