@@ -47,6 +47,23 @@ func (c *PacketCluster) Default() {
47
47
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
48
48
func (c * PacketCluster ) ValidateCreate () error {
49
49
clusterlog .Info ("validate create" , "name" , c .Name )
50
+ allErrs := field.ErrorList {}
51
+
52
+ // Must have either one of Metro or Facility
53
+ if len (c .Spec .Facility ) == 0 && len (c .Spec .Metro ) == 0 {
54
+ allErrs = append (allErrs ,
55
+ field .Invalid (field .NewPath ("spec" , "Metro" ),
56
+ c .Spec .Metro , "field is required when Facility is not set" ),
57
+ )
58
+ }
59
+
60
+ // If both Metro and Facility are set, ignore Facility, we'll leave this to
61
+ // the controller to deal with - the facility will need to reside in the
62
+ // metro.
63
+
64
+ if len (allErrs ) > 0 {
65
+ return apierrors .NewInvalid (GroupVersion .WithKind ("PacketCluster" ).GroupKind (), c .Name , allErrs )
66
+ }
50
67
51
68
return nil
52
69
}
@@ -64,20 +81,6 @@ func (c *PacketCluster) ValidateUpdate(oldRaw runtime.Object) error {
64
81
)
65
82
}
66
83
67
- if ! reflect .DeepEqual (c .Spec .Facility , old .Spec .Facility ) {
68
- allErrs = append (allErrs ,
69
- field .Invalid (field .NewPath ("spec" , "Facility" ),
70
- c .Spec .Facility , "field is immutable" ),
71
- )
72
- }
73
-
74
- if ! reflect .DeepEqual (c .Spec .Metro , old .Spec .Metro ) {
75
- allErrs = append (allErrs ,
76
- field .Invalid (field .NewPath ("spec" , "Metro" ),
77
- c .Spec .Metro , "field is immutable" ),
78
- )
79
- }
80
-
81
84
if ! reflect .DeepEqual (c .Spec .VIPManager , old .Spec .VIPManager ) {
82
85
allErrs = append (allErrs ,
83
86
field .Invalid (field .NewPath ("spec" , "VIPManager" ),
@@ -93,11 +96,6 @@ func (c *PacketCluster) ValidateUpdate(oldRaw runtime.Object) error {
93
96
)
94
97
}
95
98
96
- // If both Metro and Facility are set, ignore Facility
97
- if len (c .Spec .Facility ) > 0 && len (c .Spec .Metro ) > 0 {
98
- clusterlog .Info ("Metro and Facility are both set, ignoring Facility." )
99
- }
100
-
101
99
if len (allErrs ) == 0 {
102
100
return nil
103
101
}
0 commit comments