Skip to content

Commit

Permalink
Add CustomSubnet feature
Browse files Browse the repository at this point in the history
Signed-off-by: Shaad7 <[email protected]>
  • Loading branch information
AbdullahAlShaad committed Jun 22, 2023
1 parent ccab0cf commit 38b60ac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
5 changes: 2 additions & 3 deletions cloud/services/compute/subnets/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package subnets

import (
"context"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"google.golang.org/api/compute/v1"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/gcperrors"
Expand All @@ -43,7 +42,7 @@ func (s *Service) Delete(ctx context.Context) error {
logger := log.FromContext(ctx)
for _, subnetSpec := range s.scope.SubnetSpecs() {
logger.V(2).Info("Deleting a subnet", "name", subnetSpec.Name)
subnetKey := meta.RegionalKey(subnetSpec.Name, s.scope.Region())
subnetKey := meta.RegionalKey(subnetSpec.Name, subnetSpec.Region)
err := s.subnets.Delete(ctx, subnetKey)
if err != nil && !gcperrors.IsNotFound(err) {
logger.Error(err, "Error deleting subnet", "name", subnetSpec.Name)
Expand All @@ -60,7 +59,7 @@ func (s *Service) createOrGetSubnets(ctx context.Context) ([]*compute.Subnetwork
subnets := []*compute.Subnetwork{}
for _, subnetSpec := range s.scope.SubnetSpecs() {
logger.V(2).Info("Looking for subnet", "name", subnetSpec.Name)
subnetKey := meta.RegionalKey(subnetSpec.Name, s.scope.Region())
subnetKey := meta.RegionalKey(subnetSpec.Name, subnetSpec.Region)
subnet, err := s.subnets.Get(ctx, subnetKey)
if err != nil {
if !gcperrors.IsNotFound(err) {
Expand Down
15 changes: 12 additions & 3 deletions cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
}

isRegional := shared.IsRegional(s.scope.Region())

cluster := &containerpb.Cluster{
Name: s.scope.ClusterName(),
Network: *s.scope.GCPManagedCluster.Spec.Network.Name,
Name: s.scope.ClusterName(),
Network: *s.scope.GCPManagedCluster.Spec.Network.Name,
Subnetwork: s.getSubnetName(),
Autopilot: &containerpb.Autopilot{
Enabled: s.scope.GCPManagedControlPlane.Spec.EnableAutopilot,
},
Expand Down Expand Up @@ -281,6 +281,15 @@ func (s *Service) createCluster(ctx context.Context, log *logr.Logger) error {
return nil
}

func (s *Service) getSubnetName() string {
for _, subnet := range s.scope.GCPManagedCluster.Spec.Network.Subnets {
if subnet.Region == s.scope.Region() {
return subnet.Name
}
}
return ""
}

func (s *Service) updateCluster(ctx context.Context, updateClusterRequest *containerpb.UpdateClusterRequest, log *logr.Logger) error {
_, err := s.scope.ManagedControlPlaneClient().UpdateCluster(ctx, updateClusterRequest)
if err != nil {
Expand Down
36 changes: 35 additions & 1 deletion exp/api/v1beta1/gcpmanagedcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/google/go-cmp/cmp"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -52,7 +53,7 @@ var _ webhook.Validator = &GCPManagedCluster{}
func (r *GCPManagedCluster) ValidateCreate() error {
gcpmanagedclusterlog.Info("validate create", "name", r.Name)

return nil
return r.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
Expand Down Expand Up @@ -95,3 +96,36 @@ func (r *GCPManagedCluster) ValidateDelete() error {

return nil
}

func (r *GCPManagedCluster) validate() error {
validators := []func() error{
r.validateCustomSubnet,
}

var errs []error
for _, validator := range validators {
if err := validator(); err != nil {
errs = append(errs, err)
}
}

return kerrors.NewAggregate(errs)
}

func (r *GCPManagedCluster) validateCustomSubnet() error {
gcpmanagedclusterlog.Info("validate custom subnet", "name", r.Name)
if r.Spec.Network.AutoCreateSubnetworks == nil || *r.Spec.Network.AutoCreateSubnetworks {
return nil
}
var isSubnetExistInClusterRegion = false
for _, subnet := range r.Spec.Network.Subnets {
if subnet.Region == r.Spec.Region {
isSubnetExistInClusterRegion = true
}
}

if !isSubnetExistInClusterRegion {
return field.Required(field.NewPath("spec", "network", "subnet"), "at least one given subnets region should be same as spec.network.region when spec.network.autoCreateSubnetworks is false")
}
return nil
}

0 comments on commit 38b60ac

Please sign in to comment.