@@ -17,8 +17,11 @@ limitations under the License.
1717package v1beta2
1818
1919import (
20+ "fmt"
21+ "reflect"
2022 "strings"
2123
24+ "k8s.io/apimachinery/pkg/api/errors"
2225 "k8s.io/apimachinery/pkg/runtime"
2326 "k8s.io/apimachinery/pkg/util/validation/field"
2427 "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/webhookutil"
@@ -76,7 +79,30 @@ func (r *CloudStackMachineTemplate) ValidateCreate() error {
7679
7780// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
7881func (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 )
80106}
81107
82108// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
0 commit comments