@@ -17,8 +17,11 @@ limitations under the License.
17
17
package v1beta2
18
18
19
19
import (
20
+ "fmt"
21
+ "reflect"
20
22
"strings"
21
23
24
+ "k8s.io/apimachinery/pkg/api/errors"
22
25
"k8s.io/apimachinery/pkg/runtime"
23
26
"k8s.io/apimachinery/pkg/util/validation/field"
24
27
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/webhookutil"
@@ -76,7 +79,30 @@ func (r *CloudStackMachineTemplate) ValidateCreate() error {
76
79
77
80
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
78
81
func (r * CloudStackMachineTemplate ) ValidateUpdate (old runtime.Object ) error {
79
- return r .ValidateCreate ()
82
+ cloudstackmachinetemplatelog .V (1 ).Info ("entered validate update webhook" , "api resource name" , r .Name )
83
+
84
+ oldMachineTemplate , ok := old .(* CloudStackMachineTemplate )
85
+ if ! ok {
86
+ return errors .NewBadRequest (fmt .Sprintf ("expected a CloudStackMachineTemplate but got a %T" , old ))
87
+ }
88
+
89
+ // CloudStackMachineTemplateSpec.CloudStackMachineTemplateResource.CloudStackMachineSpec
90
+ spec := r .Spec .Spec .Spec
91
+ oldSpec := oldMachineTemplate .Spec .Spec .Spec
92
+
93
+ errorList := field .ErrorList (nil )
94
+ errorList = webhookutil .EnsureBothFieldsAreEqual (spec .Offering .ID , spec .Offering .Name , oldSpec .Offering .ID , oldSpec .Offering .Name , "offering" , errorList )
95
+ errorList = webhookutil .EnsureBothFieldsAreEqual (spec .DiskOffering .ID , spec .DiskOffering .Name , oldSpec .DiskOffering .ID , oldSpec .DiskOffering .Name , "diskOffering" , errorList )
96
+ errorList = webhookutil .EnsureStringFieldsAreEqual (spec .SSHKey , oldSpec .SSHKey , "sshkey" , errorList )
97
+ errorList = webhookutil .EnsureBothFieldsAreEqual (spec .Template .ID , spec .Template .Name , oldSpec .Template .ID , oldSpec .Template .Name , "template" , errorList )
98
+ errorList = webhookutil .EnsureStringStringMapFieldsAreEqual (& spec .Details , & oldSpec .Details , "details" , errorList )
99
+ errorList = webhookutil .EnsureStringFieldsAreEqual (spec .Affinity , oldSpec .Affinity , "affinity" , errorList )
100
+
101
+ if ! reflect .DeepEqual (spec .AffinityGroupIDs , oldSpec .AffinityGroupIDs ) { // Equivalent to other Ensure funcs.
102
+ errorList = append (errorList , field .Forbidden (field .NewPath ("spec" , "AffinityGroupIDs" ), "AffinityGroupIDs" ))
103
+ }
104
+
105
+ return webhookutil .AggregateObjErrors (r .GroupVersionKind ().GroupKind (), r .Name , errorList )
80
106
}
81
107
82
108
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
0 commit comments