@@ -18,6 +18,7 @@ package v1beta1
18
18
19
19
import (
20
20
"fmt"
21
+ "reflect"
21
22
22
23
"github.com/aws/cluster-api-provider-cloudstack/pkg/webhookutil"
23
24
"k8s.io/apimachinery/pkg/api/errors"
@@ -43,7 +44,7 @@ var _ webhook.Defaulter = &CloudStackCluster{}
43
44
44
45
// Default implements webhook.Defaulter so a webhook will be registered for the type
45
46
func (r * CloudStackCluster ) Default () {
46
- cloudstackclusterlog .Info ("default" , "name" , r .Name )
47
+ cloudstackclusterlog .V ( 1 ). Info ("entered api default setting webhook " , "api resource name" , r .Name )
47
48
// No defaulted values supported yet.
48
49
}
49
50
@@ -53,7 +54,7 @@ var _ webhook.Validator = &CloudStackCluster{}
53
54
54
55
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
55
56
func (r * CloudStackCluster ) ValidateCreate () error {
56
- cloudstackclusterlog .Info ("validate create" , "name" , r .Name )
57
+ cloudstackclusterlog .V ( 1 ). Info ("entered validate create webhook " , "api resource name" , r .Name )
57
58
58
59
var errorList field.ErrorList
59
60
@@ -63,23 +64,31 @@ func (r *CloudStackCluster) ValidateCreate() error {
63
64
}
64
65
65
66
if (r .Spec .Account != "" ) && (r .Spec .Domain == "" ) {
66
- errorList = append (errorList , field .Required (field .NewPath ("spec" , "account" ), "specifying account requires additionally specifying domain" ))
67
+ errorList = append (errorList , field .Required (
68
+ field .NewPath ("spec" , "account" ), "specifying account requires additionally specifying domain" ))
67
69
}
68
70
69
- // Zone and Network are required fields
70
- errorList = webhookutil .EnsureFieldExists (r .Spec .Zone , "Zone" , errorList )
71
- errorList = webhookutil .EnsureFieldExists (r .Spec .Network , "Network" , errorList )
71
+ // Require Zones and their respective Networks.
72
+ if len (r .Spec .Zones ) <= 0 {
73
+ errorList = append (errorList , field .Required (field .NewPath ("spec" , "Zones" ), "Zones" ))
74
+ } else {
75
+ for _ , zone := range r .Spec .Zones {
76
+ if zone .Network .Name == "" && zone .Network .ID == "" {
77
+ errorList = append (errorList , field .Required (
78
+ field .NewPath ("spec" , "Zones" , "Network" ), "each Zone requires a Network specification" ))
79
+ }
80
+ }
81
+ }
72
82
73
83
return webhookutil .AggregateObjErrors (r .GroupVersionKind ().GroupKind (), r .Name , errorList )
74
84
}
75
85
76
86
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
77
87
func (r * CloudStackCluster ) ValidateUpdate (old runtime.Object ) error {
78
- cloudstackclusterlog .Info ("validate update" , "name" , r .Name )
88
+ cloudstackclusterlog .V ( 1 ). Info ("entered validate update webhook " , "api resource name" , r .Name )
79
89
80
90
var (
81
- errorList field.ErrorList
82
- spec = r .Spec
91
+ spec = r .Spec
83
92
)
84
93
85
94
oldCluster , ok := old .(* CloudStackCluster )
@@ -88,31 +97,37 @@ func (r *CloudStackCluster) ValidateUpdate(old runtime.Object) error {
88
97
}
89
98
oldSpec := oldCluster .Spec
90
99
91
- // IdentityRefs must be Secrets.
92
- if spec .IdentityRef != nil && spec .IdentityRef .Kind != defaultIdentityRefKind {
93
- errorList = append (errorList , field .Forbidden (field .NewPath ("spec" , "identityRef" , "kind" ), "must be a Secret" ))
100
+ // No spec fields may be updated.
101
+ errorList := field .ErrorList (nil )
102
+ if ! reflect .DeepEqual (oldSpec .Zones , spec .Zones ) {
103
+ errorList = append (errorList , field .Forbidden (
104
+ field .NewPath ("spec" , "Zones" ), "Zones and sub-attributes may not be modified after creation" ))
94
105
}
95
-
96
- // No spec fields may be changed
97
- errorList = webhookutil .EnsureStringFieldsAreEqual (spec .Zone , oldSpec .Zone , "zone" , errorList )
98
- errorList = webhookutil .EnsureStringFieldsAreEqual (spec .Network , oldSpec .Network , "network" , errorList )
99
106
if oldSpec .ControlPlaneEndpoint .Host != "" { // Need to allow one time endpoint setting via CAPC cluster controller.
100
107
errorList = webhookutil .EnsureStringFieldsAreEqual (
101
- spec .ControlPlaneEndpoint .Host , oldSpec .ControlPlaneEndpoint .Host , "controlplaneendpointhost " , errorList )
108
+ spec .ControlPlaneEndpoint .Host , oldSpec .ControlPlaneEndpoint .Host , "controlplaneendpoint.host " , errorList )
102
109
errorList = webhookutil .EnsureStringFieldsAreEqual (
103
- string (spec .ControlPlaneEndpoint .Port ), string (oldSpec .ControlPlaneEndpoint .Port ), "controlplaneendpointport" , errorList )
110
+ string (spec .ControlPlaneEndpoint .Port ), string (oldSpec .ControlPlaneEndpoint .Port ),
111
+ "controlplaneendpoint.port" , errorList )
104
112
}
105
113
if spec .IdentityRef != nil && oldSpec .IdentityRef != nil {
106
- errorList = webhookutil .EnsureStringFieldsAreEqual (spec .IdentityRef .Kind , oldSpec .IdentityRef .Kind , "identityRef.Kind" , errorList )
107
- errorList = webhookutil .EnsureStringFieldsAreEqual (spec .IdentityRef .Name , oldSpec .IdentityRef .Name , "identityRef.Name" , errorList )
114
+ errorList = webhookutil .EnsureStringFieldsAreEqual (
115
+ spec .IdentityRef .Kind , oldSpec .IdentityRef .Kind , "identityref.kind" , errorList )
116
+ errorList = webhookutil .EnsureStringFieldsAreEqual (spec .IdentityRef .Name , oldSpec .IdentityRef .Name ,
117
+ "identityref.name" , errorList )
118
+ }
119
+
120
+ // IdentityRefs must be Secrets.
121
+ if spec .IdentityRef != nil && spec .IdentityRef .Kind != defaultIdentityRefKind {
122
+ errorList = append (errorList , field .Forbidden (field .NewPath ("spec" , "identityRef" , "kind" ), "must be a Secret" ))
108
123
}
109
124
110
125
return webhookutil .AggregateObjErrors (r .GroupVersionKind ().GroupKind (), r .Name , errorList )
111
126
}
112
127
113
128
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
114
129
func (r * CloudStackCluster ) ValidateDelete () error {
115
- cloudstackclusterlog .Info ("validate delete" , "name" , r .Name )
130
+ cloudstackclusterlog .V ( 1 ). Info ("entered validate delete webhook " , "api resource name" , r .Name )
116
131
// No deletion validations. Deletion webhook not enabled.
117
132
return nil
118
133
}
0 commit comments