Skip to content

Commit

Permalink
Refactored codebase to use camel case for spec parameter in field.New…
Browse files Browse the repository at this point in the history
…Path method (#4878)

* refactored field newPath method to use camel case for spec

* reformated code

* resolved comments
  • Loading branch information
lakshkeswani authored Jun 21, 2024
1 parent 287fa1f commit 90b76d3
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 212 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/azurecluster_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *AzureCluster) validateClusterSpec(old *AzureCluster) field.ErrorList {

// If ClusterSpec has non-nil ExtendedLocation field but not enable EdgeZone feature gate flag, ClusterSpec validation failed.
if !feature.Gates.Enabled(feature.EdgeZone) && c.Spec.ExtendedLocation != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ExtendedLocation"), "can be set only if the EdgeZone feature flag is enabled"))
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "extendedLocation"), "can be set only if the EdgeZone feature flag is enabled"))
}

if err := validateBastionSpec(c.Spec.BastionSpec, field.NewPath("spec").Child("azureBastion").Child("bastionSpec")); err != nil {
Expand All @@ -123,7 +123,7 @@ func (c *AzureCluster) validateClusterName() field.ErrorList {
fmt.Sprintf("Cluster Name longer than allowed length of %d characters", clusterNameMaxLength)))
}
if success, _ := regexp.MatchString(clusterNameRegex, c.Name); !success {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("Name"), c.Name,
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("name"), c.Name,
fmt.Sprintf("Cluster Name doesn't match regex %s, can contain only lowercase alphanumeric characters and '-', must start/end with an alphanumeric character",
clusterNameRegex)))
}
Expand Down
18 changes: 9 additions & 9 deletions api/v1beta1/azurecluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,36 @@ func (c *AzureCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings
old := oldRaw.(*AzureCluster)

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "ResourceGroup"),
field.NewPath("spec", "resourceGroup"),
old.Spec.ResourceGroup,
c.Spec.ResourceGroup); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SubscriptionID"),
field.NewPath("spec", "subscriptionID"),
old.Spec.SubscriptionID,
c.Spec.SubscriptionID); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Location"),
field.NewPath("spec", "location"),
old.Spec.Location,
c.Spec.Location); err != nil {
allErrs = append(allErrs, err)
}

if old.Spec.ControlPlaneEndpoint.Host != "" && c.Spec.ControlPlaneEndpoint.Host != old.Spec.ControlPlaneEndpoint.Host {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "ControlPlaneEndpoint", "Host"),
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint", "host"),
c.Spec.ControlPlaneEndpoint.Host, "field is immutable"),
)
}

if old.Spec.ControlPlaneEndpoint.Port != 0 && c.Spec.ControlPlaneEndpoint.Port != old.Spec.ControlPlaneEndpoint.Port {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "ControlPlaneEndpoint", "Port"),
field.Invalid(field.NewPath("spec", "controlPlaneEndpoint", "port"),
c.Spec.ControlPlaneEndpoint.Port, "field is immutable"),
)
}
Expand All @@ -102,14 +102,14 @@ func (c *AzureCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings
// if it's still not equal, return error.
if !reflect.DeepEqual(c.Spec.AzureEnvironment, old.Spec.AzureEnvironment) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "AzureEnvironment"),
field.Invalid(field.NewPath("spec", "azureEnvironment"),
c.Spec.AzureEnvironment, "field is immutable"),
)
}
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "NetworkSpec", "PrivateDNSZoneName"),
field.NewPath("spec", "networkSpec", "privateDNSZoneName"),
old.Spec.NetworkSpec.PrivateDNSZoneName,
c.Spec.NetworkSpec.PrivateDNSZoneName); err != nil {
allErrs = append(allErrs, err)
Expand All @@ -118,13 +118,13 @@ func (c *AzureCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings
// Allow enabling azure bastion but avoid disabling it.
if old.Spec.BastionSpec.AzureBastion != nil && !reflect.DeepEqual(old.Spec.BastionSpec.AzureBastion, c.Spec.BastionSpec.AzureBastion) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "BastionSpec", "AzureBastion"),
field.Invalid(field.NewPath("spec", "bastionSpec", "azureBastion"),
c.Spec.BastionSpec.AzureBastion, "azure bastion cannot be removed from a cluster"),
)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "NetworkSpec", "ControlPlaneOutboundLB"),
field.NewPath("spec", "networkSpec", "controlPlaneOutboundLB"),
old.Spec.NetworkSpec.ControlPlaneOutboundLB,
c.Spec.NetworkSpec.ControlPlaneOutboundLB); err != nil {
allErrs = append(allErrs, err)
Expand Down
8 changes: 4 additions & 4 deletions api/v1beta1/azuremachine_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ func ValidateSystemAssignedIdentityRole(identityType VMIdentity, roleAssignmentN
}
if identityType == VMIdentitySystemAssigned {
if role.DefinitionID == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "SystemAssignedIdentityRole", "DefinitionID"), role.DefinitionID, "the definitionID field cannot be empty"))
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "systemAssignedIdentityRole", "definitionID"), role.DefinitionID, "the definitionID field cannot be empty"))
}
if role.Scope == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("Spec", "SystemAssignedIdentityRole", "Scope"), role.Scope, "the scope field cannot be empty"))
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "systemAssignedIdentityRole", "scope"), role.Scope, "the scope field cannot be empty"))
}
}
if identityType != VMIdentitySystemAssigned && role != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("Spec", "Role"), "systemAssignedIdentityRole can only be set when identity is set to SystemAssigned"))
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "role"), "systemAssignedIdentityRole can only be set when identity is set to SystemAssigned"))
}
return allErrs
}
Expand Down Expand Up @@ -482,7 +482,7 @@ func ValidateVMExtensions(disableExtensionOperations *bool, vmExtensions []VMExt
allErrs := field.ErrorList{}

if ptr.Deref(disableExtensionOperations, false) && len(vmExtensions) > 0 {
allErrs = append(allErrs, field.Forbidden(field.NewPath("AzureMachineTemplate", "spec", "template", "spec", "VMExtensions"), "VMExtensions must be empty when DisableExtensionOperations is true"))
allErrs = append(allErrs, field.Forbidden(field.NewPath("AzureMachineTemplate", "spec", "template", "spec", "vmExtensions"), "VMExtensions must be empty when DisableExtensionOperations is true"))
}

return allErrs
Expand Down
22 changes: 11 additions & 11 deletions api/v1beta1/azuremachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,70 +86,70 @@ func (mw *azureMachineWebhook) ValidateUpdate(ctx context.Context, oldObj, newOb
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Image"),
field.NewPath("spec", "image"),
old.Spec.Image,
m.Spec.Image); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Identity"),
field.NewPath("spec", "identity"),
old.Spec.Identity,
m.Spec.Identity); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SystemAssignedIdentityRole"),
field.NewPath("spec", "systemAssignedIdentityRole"),
old.Spec.SystemAssignedIdentityRole,
m.Spec.SystemAssignedIdentityRole); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "UserAssignedIdentities"),
field.NewPath("spec", "userAssignedIdentities"),
old.Spec.UserAssignedIdentities,
m.Spec.UserAssignedIdentities); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "RoleAssignmentName"),
field.NewPath("spec", "roleAssignmentName"),
old.Spec.RoleAssignmentName,
m.Spec.RoleAssignmentName); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "OSDisk"),
field.NewPath("spec", "osDisk"),
old.Spec.OSDisk,
m.Spec.OSDisk); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "DataDisks"),
field.NewPath("spec", "dataDisks"),
old.Spec.DataDisks,
m.Spec.DataDisks); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "SSHPublicKey"),
field.NewPath("spec", "sshPublicKey"),
old.Spec.SSHPublicKey,
m.Spec.SSHPublicKey); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "AllocatePublicIP"),
field.NewPath("spec", "allocatePublicIP"),
old.Spec.AllocatePublicIP,
m.Spec.AllocatePublicIP); err != nil {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "EnableIPForwarding"),
field.NewPath("spec", "enableIPForwarding"),
old.Spec.EnableIPForwarding,
m.Spec.EnableIPForwarding); err != nil {
allErrs = append(allErrs, err)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (mw *azureMachineWebhook) ValidateUpdate(ctx context.Context, oldObj, newOb

if old.Spec.Diagnostics != nil {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Diagnostics"),
field.NewPath("spec", "diagnostics"),
old.Spec.Diagnostics,
m.Spec.Diagnostics); err != nil {
allErrs = append(allErrs, err)
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/azuremachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *AzureMachineTemplate) ValidateCreate(ctx context.Context, obj runtime.O
}

if ptr.Deref(r.Spec.Template.Spec.DisableExtensionOperations, false) && len(r.Spec.Template.Spec.VMExtensions) > 0 {
allErrs = append(allErrs, field.Forbidden(field.NewPath("AzureMachineTemplate", "spec", "template", "spec", "VMExtensions"), "VMExtensions must be empty when DisableExtensionOperations is true"))
allErrs = append(allErrs, field.Forbidden(field.NewPath("AzureMachineTemplate", "spec", "template", "spec", "vmExtensions"), "VMExtensions must be empty when DisableExtensionOperations is true"))
}

if len(allErrs) == 0 {
Expand Down
Loading

0 comments on commit 90b76d3

Please sign in to comment.