Skip to content

Commit

Permalink
Refactor reconcilation of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Amulyam24 committed Oct 3, 2024
1 parent 7716671 commit 523596d
Show file tree
Hide file tree
Showing 4 changed files with 734 additions and 551 deletions.
34 changes: 33 additions & 1 deletion api/v1beta2/ibmpowervscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,36 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterNetwork() *field.Error {
return nil
}

func (r *IBMPowerVSCluster) validateIBMPowerVSClusterLoadBalancers() (allErrs field.ErrorList) {
if err := r.validateIBMPowerVSClusterLoadBalancerNames(); err != nil {
allErrs = append(allErrs, err...)
}

isPublic := false
for _, loadbalancer := range r.Spec.LoadBalancers {
if *loadbalancer.Public {
isPublic = true
break
}
}

if !isPublic {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.LoadBalancers"), r.Spec.LoadBalancers, "Expect atleast one of the load balancer to be public"))
}

return allErrs
}

func (r *IBMPowerVSCluster) validateIBMPowerVSClusterLoadBalancerNames() (allErrs field.ErrorList) {
found := make(map[string]bool)
for i, loadbalancer := range r.Spec.LoadBalancers {
if loadbalancer.Name == "" {
continue
}

if found[loadbalancer.Name] {
allErrs = append(allErrs, field.Duplicate(field.NewPath("spec", fmt.Sprintf("loadbalancers[%d]", i)), map[string]interface{}{"Name": loadbalancer.Name}))
continue
}
found[loadbalancer.Name] = true
}
Expand All @@ -117,8 +142,12 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterLoadBalancerNames() (allErr
func (r *IBMPowerVSCluster) validateIBMPowerVSClusterVPCSubnetNames() (allErrs field.ErrorList) {
found := make(map[string]bool)
for i, subnet := range r.Spec.VPCSubnets {
if subnet.Name == nil {
continue
}
if found[*subnet.Name] {
allErrs = append(allErrs, field.Duplicate(field.NewPath("spec", fmt.Sprintf("vpcSubnets[%d]", i)), map[string]interface{}{"Name": *subnet.Name}))
continue
}
found[*subnet.Name] = true
}
Expand All @@ -130,6 +159,9 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterTransitGateway() *field.Err
if r.Spec.Zone == nil && r.Spec.VPC == nil {
return nil
}
if r.Spec.TransitGateway == nil {
return nil
}
if _, globalRouting, _ := genUtil.GetTransitGatewayLocationAndRouting(r.Spec.Zone, r.Spec.VPC.Region); r.Spec.TransitGateway.GlobalRouting != nil && !*r.Spec.TransitGateway.GlobalRouting && globalRouting != nil && *globalRouting {
return field.Invalid(field.NewPath("spec.transitGateway.globalRouting"), r.Spec.TransitGateway.GlobalRouting, "global routing is required since PowerVS and VPC region are from different region")
}
Expand Down Expand Up @@ -183,7 +215,7 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterCreateInfraPrereq() (allErr
allErrs = append(allErrs, err...)
}

if err := r.validateIBMPowerVSClusterLoadBalancerNames(); err != nil {
if err := r.validateIBMPowerVSClusterLoadBalancers(); err != nil {
allErrs = append(allErrs, err...)
}

Expand Down
Loading

0 comments on commit 523596d

Please sign in to comment.