Skip to content

Commit

Permalink
Add validation in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Shilpa-Gokul committed Nov 18, 2024
1 parent 664812e commit 4b297fc
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 172 deletions.
35 changes: 31 additions & 4 deletions api/v1beta2/ibmpowervscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,48 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterLoadBalancerNames() (allErr
}

func (r *IBMPowerVSCluster) validateIBMPowerVSClusterVPCSubnetNames() (allErrs field.ErrorList) {
found := make(map[string]bool)
subnetFound := make(map[string]bool)
zoneFound := make(map[string]bool)
for i, subnet := range r.Spec.VPCSubnets {
if subnet.Name == nil {
continue
}
if found[*subnet.Name] {
if subnetFound[*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
subnetFound[*subnet.Name] = true
if subnet.Zone == nil {
continue
}
if zoneFound[*subnet.Zone] {
allErrs = append(allErrs, field.Duplicate(field.NewPath("spec", fmt.Sprintf("vpcSubnets[%d]", i)), map[string]interface{}{"Zone": *subnet.Zone}))
continue
}
zoneFound[*subnet.Zone] = true
}

return allErrs
}

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

if r.Spec.VPC != nil && r.Spec.VPC.Region != nil {
vpcZones, err := regionUtil.VPCZonesForVPCRegion(*r.Spec.VPC.Region)
if err != nil {
ibmpowervsclusterlog.Error(fmt.Errorf("error validating vpc zones"), err.Error())
return allErrs
}
if len(r.Spec.VPCSubnets) > len(vpcZones) {
allErrs = append(allErrs, &field.Error{Type: field.ErrorTypeInvalid, BadValue: "spec.vpcSubnets", Detail: fmt.Sprintf("%s vpc region supports only %d subnets but %d subnets were provided", *r.Spec.VPC.Region, len(vpcZones), len(r.Spec.VPCSubnets))})
}
}
return allErrs
}

func (r *IBMPowerVSCluster) validateIBMPowerVSClusterTransitGateway() *field.Error {
if r.Spec.Zone == nil && r.Spec.VPC == nil {
return nil
Expand Down Expand Up @@ -209,7 +236,7 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSClusterCreateInfraPrereq() (allErr
if r.Spec.ResourceGroup == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.resourceGroup"), r.Spec.ResourceGroup, "value of resource group is empty"))
}
if err := r.validateIBMPowerVSClusterVPCSubnetNames(); err != nil {
if err := r.validateIBMPowerVSClusterVPCSubnets(); err != nil {
allErrs = append(allErrs, err...)
}

Expand Down
35 changes: 17 additions & 18 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/go-logr/logr"
regionUtil "github.com/ppc64le-cloud/powervs-utils"
"k8s.io/klog/v2"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/models"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/IBM/vpc-go-sdk/vpcv1"

kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -74,7 +74,12 @@ var (
)

// powerEdgeRouter is identifier for PER.
const powerEdgeRouter = "power-edge-router"
const (
powerEdgeRouter = "power-edge-router"
// vpcSubnetIPAddressCount is the total IP Addresses for the subnet.
// Support for custom address prefixes will be added at a later time. Currently, we use the ip count for subnet creation.
vpcSubnetIPAddressCount int64 = 256
)

// PowerVSClusterScopeParams defines the input parameters used to create a new PowerVSClusterScope.
type PowerVSClusterScopeParams struct {
Expand Down Expand Up @@ -1123,9 +1128,6 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) {
} else {
subnets = append(subnets, s.IBMPowerVSCluster.Spec.VPCSubnets...)
}
if len(subnets) > len(vpcZones) {
return false, fmt.Errorf("error validating input, total vpc subnets to be created cannot be more than the available vpc zones. subnets-%d, vpczones-%d", len(subnets), len(vpcZones))
}

for index, subnet := range subnets {
s.Info("Reconciling VPC subnet", "subnet", subnet)
Expand Down Expand Up @@ -1167,21 +1169,19 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) {
// check for next subnet
continue
}
var zone string
if subnet.Zone != nil {
zone = *subnet.Zone
} else {
zone = vpcZones[index]

if subnet.Zone == nil {
subnet.Zone = &vpcZones[index]
}
s.V(3).Info("Creating VPC subnet-shilpa")
subnetID, err = s.createVPCSubnet(subnet, zone)
s.V(3).Info("Creating VPC subnet")
subnetID, err = s.createVPCSubnet(subnet)
if err != nil {
s.Error(err, "failed to create VPC subnet")
return false, err
}
s.Info("Created VPC subnet", "id", subnetID)
s.Info("Created VPC subnet", "subnetID", subnetID)
s.SetVPCSubnetStatus(*subnet.Name, infrav1beta2.ResourceReference{ID: subnetID, ControllerCreated: ptr.To(true)})
// Requeue only when all subnets' creation are triggered
// Requeue only when the creation of all subnets has been triggered.
if index == len(subnets)-1 {
return true, nil
}
Expand All @@ -1203,7 +1203,7 @@ func (s *PowerVSClusterScope) checkVPCSubnet(subnetName string) (string, error)
}

// createVPCSubnet creates a VPC subnet.
func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet, zone string) (*string, error) {
func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*string, error) {
// TODO(karthik-k-n): consider moving to clusterscope
// fetch resource group id
resourceGroupID := s.GetResourceGroupID()
Expand All @@ -1217,19 +1217,18 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet, zone s
return nil, fmt.Errorf("VPC ID is empty")
}

var ipCount int64 = 256
ipVersion := vpcSubnetIPVersion4

options := &vpcv1.CreateSubnetOptions{}
options.SetSubnetPrototype(&vpcv1.SubnetPrototype{
IPVersion: &ipVersion,
TotalIpv4AddressCount: ptr.To(ipCount),
TotalIpv4AddressCount: ptr.To(vpcSubnetIPAddressCount),
Name: subnet.Name,
VPC: &vpcv1.VPCIdentity{
ID: vpcID,
},
Zone: &vpcv1.ZoneIdentity{
Name: &zone,
Name: subnet.Zone,
},
ResourceGroup: &vpcv1.ResourceGroupIdentity{
ID: &resourceGroupID,
Expand Down
Loading

0 comments on commit 4b297fc

Please sign in to comment.