From e1b2f7c25f9666e71d58bdeef3698508e2b0b432 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Thu, 2 Feb 2023 14:45:00 -0700 Subject: [PATCH] update per pr review Signed-off-by: Brian Downs --- pkg/controller/cluster/cidr_allocation.go | 4 +-- pkg/controller/cluster/controller.go | 32 +++++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pkg/controller/cluster/cidr_allocation.go b/pkg/controller/cluster/cidr_allocation.go index 98bed9c6..443c0207 100644 --- a/pkg/controller/cluster/cidr_allocation.go +++ b/pkg/controller/cluster/cidr_allocation.go @@ -60,7 +60,7 @@ func generateSubnets(cidr string) ([]string, error) { } // nextCIDR retrieves the next available CIDR address from the given pool. -func (c *ClusterReconciler) nextCIDR(ctx context.Context, namespace, cidrAllocationPoolName, clusterName string) (*net.IPNet, error) { +func (c *ClusterReconciler) nextCIDR(ctx context.Context, cidrAllocationPoolName, clusterName string) (*net.IPNet, error) { var cidrPool v1alpha1.CIDRAllocationPool nn := types.NamespacedName{ @@ -95,7 +95,7 @@ func (c *ClusterReconciler) nextCIDR(ctx context.Context, namespace, cidrAllocat } // releaseCIDR updates the given CIDR pool by marking the address as available. -func (c *ClusterReconciler) releaseCIDR(ctx context.Context, namespace, cidrAllocationPoolName, clusterName string) error { +func (c *ClusterReconciler) releaseCIDR(ctx context.Context, cidrAllocationPoolName, clusterName string) error { var cidrPool v1alpha1.CIDRAllocationPool nn := types.NamespacedName{ diff --git a/pkg/controller/cluster/controller.go b/pkg/controller/cluster/controller.go index 2c1042c7..1dd2aed3 100644 --- a/pkg/controller/cluster/controller.go +++ b/pkg/controller/cluster/controller.go @@ -145,18 +145,6 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request } } - clusterCIDR, err := c.nextCIDR(ctx, ns.String(), req.Name, cidrAllocationClusterPoolName) - if err != nil { - return reconcile.Result{}, err - } - cluster.Status.ClusterCIDR = clusterCIDR.String() - - serviceCIDR, err := c.nextCIDR(ctx, ns.String(), req.Name, cidrAllocationServicePoolName) - if err != nil { - return reconcile.Result{}, err - } - cluster.Status.ServiceCIDR = serviceCIDR.String() - klog.Infof("enqueue cluster [%s]", cluster.Name) return reconcile.Result{}, c.createCluster(ctx, &cluster) @@ -164,7 +152,7 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request if controllerutil.ContainsFinalizer(&cluster, clusterFinalizerName) { // TODO: handle CIDR deletion - if err := c.releaseCIDR(ctx, cluster.Namespace, cluster.Status.ClusterCIDR, cluster.Name); err != nil { + if err := c.releaseCIDR(ctx, cluster.Status.ClusterCIDR, cluster.Name); err != nil { return reconcile.Result{}, err } @@ -185,6 +173,22 @@ func (c *ClusterReconciler) createCluster(ctx context.Context, cluster *v1alpha1 return util.WrapErr("failed to create ns", err) } + if cluster.Spec.ClusterCIDR == "" && cluster.Status.ClusterCIDR == "" { + clusterCIDR, err := c.nextCIDR(ctx, cidrAllocationClusterPoolName, cluster.Name) + if err != nil { + return err + } + cluster.Status.ClusterCIDR = clusterCIDR.String() + } + + if cluster.Spec.ServiceCIDR == "" && cluster.Status.ServiceCIDR == "" { + serviceCIDR, err := c.nextCIDR(ctx, cidrAllocationServicePoolName, cluster.Name) + if err != nil { + return err + } + cluster.Status.ServiceCIDR = serviceCIDR.String() + } + serviceIP, err := c.createClusterService(ctx, cluster) if err != nil { return util.WrapErr("failed to create cluster service", err) @@ -222,7 +226,7 @@ func (c *ClusterReconciler) createCluster(ctx context.Context, cluster *v1alpha1 } } - return nil + return c.Client.Update(ctx, cluster) } func (c *ClusterReconciler) createNamespace(ctx context.Context, cluster *v1alpha1.Cluster) error {