From ea1e7e486fc7bef8937913201eca7b351dfd4a69 Mon Sep 17 00:00:00 2001 From: Hussein Galal Date: Tue, 28 Mar 2023 23:45:57 +0200 Subject: [PATCH] Revert CIDR pool allocation and fix delete (#35) Signed-off-by: galal-hussein --- charts/k3k/crds/cidrallocationpool.yaml | 38 ----- charts/k3k/values.yaml | 6 +- pkg/apis/k3k.io/v1alpha1/register.go | 4 +- pkg/apis/k3k.io/v1alpha1/types.go | 8 +- pkg/controller/cluster/cidr_allocation.go | 19 ++- pkg/controller/cluster/config/server.go | 8 +- pkg/controller/cluster/controller.go | 164 ++++++++++------------ 7 files changed, 96 insertions(+), 151 deletions(-) delete mode 100644 charts/k3k/crds/cidrallocationpool.yaml diff --git a/charts/k3k/crds/cidrallocationpool.yaml b/charts/k3k/crds/cidrallocationpool.yaml deleted file mode 100644 index b82204a..0000000 --- a/charts/k3k/crds/cidrallocationpool.yaml +++ /dev/null @@ -1,38 +0,0 @@ -apiVersion: apiextensions.k8s.io/v1 -kind: CustomResourceDefinition -metadata: - name: cidrallocationpools.k3k.io -spec: - group: k3k.io - versions: - - name: v1alpha1 - served: true - storage: true - schema: - openAPIV3Schema: - type: object - properties: - spec: - type: object - properties: - defaultClusterCIDR: - type: string - status: - type: object - properties: - pool: - type: array - items: - type: object - properties: - clusterName: - type: string - issued: - type: integer - ipNet: - type: string - scope: Cluster - names: - plural: cidrallocationpools - singular: cidrallocationpool - kind: CIDRAllocationPool diff --git a/charts/k3k/values.yaml b/charts/k3k/values.yaml index 1f2cf9f..2ecdf70 100644 --- a/charts/k3k/values.yaml +++ b/charts/k3k/values.yaml @@ -3,9 +3,9 @@ namespace: k3k-system image: repository: husseingalal/k3k - pullPolicy: IfNotPresent + pullPolicy: Always # Overrides the image tag whose default is the chart appVersion. - tag: "v0.0.0-alpha5" + tag: "dev" imagePullSecrets: [] nameOverride: "" @@ -16,4 +16,4 @@ serviceAccount: create: true # The name of the service account to use. # If not set and create is true, a name is generated using the fullname template - name: "" \ No newline at end of file + name: "" diff --git a/pkg/apis/k3k.io/v1alpha1/register.go b/pkg/apis/k3k.io/v1alpha1/register.go index 5a4a80f..de00c52 100644 --- a/pkg/apis/k3k.io/v1alpha1/register.go +++ b/pkg/apis/k3k.io/v1alpha1/register.go @@ -21,9 +21,7 @@ func Resource(resource string) schema.GroupResource { func addKnownTypes(s *runtime.Scheme) error { s.AddKnownTypes(SchemeGroupVersion, &Cluster{}, - &ClusterList{}, - &CIDRAllocationPool{}, - &CIDRAllocationPoolList{}) + &ClusterList{}) metav1.AddToGroupVersion(s, SchemeGroupVersion) return nil } diff --git a/pkg/apis/k3k.io/v1alpha1/types.go b/pkg/apis/k3k.io/v1alpha1/types.go index 357b459..76e3707 100644 --- a/pkg/apis/k3k.io/v1alpha1/types.go +++ b/pkg/apis/k3k.io/v1alpha1/types.go @@ -55,11 +55,9 @@ type LoadBalancerConfig struct { } type ClusterStatus struct { - OverrideClusterCIDR bool `json:"overrideClusterCIDR"` - OverrideServiceCIDR bool `json:"overrideServiceCIDR"` - ClusterCIDR string `json:"clusterCIDR,omitempty"` - ServiceCIDR string `json:"serviceCIDR,omitempty"` - ClusterDNS string `json:"clusterDNS,omitempty"` + ClusterCIDR string `json:"clusterCIDR,omitempty"` + ServiceCIDR string `json:"serviceCIDR,omitempty"` + ClusterDNS string `json:"clusterDNS,omitempty"` } type Allocation struct { diff --git a/pkg/controller/cluster/cidr_allocation.go b/pkg/controller/cluster/cidr_allocation.go index 443c020..db6e507 100644 --- a/pkg/controller/cluster/cidr_allocation.go +++ b/pkg/controller/cluster/cidr_allocation.go @@ -7,6 +7,7 @@ import ( "time" "github.com/galal-hussein/k3k/pkg/apis/k3k.io/v1alpha1" + "github.com/galal-hussein/k3k/pkg/controller/util" "k8s.io/apimachinery/pkg/types" ) @@ -67,11 +68,20 @@ func (c *ClusterReconciler) nextCIDR(ctx context.Context, cidrAllocationPoolName Name: cidrAllocationPoolName, } if err := c.Client.Get(ctx, nn, &cidrPool); err != nil { - return nil, err + return nil, util.WrapErr("failed to get cidrpool", err) } var ipNet *net.IPNet + for _, pool := range cidrPool.Status.Pool { + if pool.ClusterName == clusterName { + _, ipn, err := net.ParseCIDR(pool.IPNet) + if err != nil { + return nil, util.WrapErr("failed to parse cidr", err) + } + return ipn, nil + } + } for i := 0; i < len(cidrPool.Status.Pool); i++ { if cidrPool.Status.Pool[i].ClusterName == "" && cidrPool.Status.Pool[i].Issued == 0 { cidrPool.Status.Pool[i].ClusterName = clusterName @@ -79,11 +89,10 @@ func (c *ClusterReconciler) nextCIDR(ctx context.Context, cidrAllocationPoolName _, ipn, err := net.ParseCIDR(cidrPool.Status.Pool[i].IPNet) if err != nil { - return nil, err + return nil, util.WrapErr("failed to parse cidr", err) } - - if err := c.Client.Status().Update(ctx, &cidrPool); err != nil { - return nil, err + if err := c.Client.Update(ctx, &cidrPool); err != nil { + return nil, util.WrapErr("failed to update cidr pool", err) } ipNet = ipn diff --git a/pkg/controller/cluster/config/server.go b/pkg/controller/cluster/config/server.go index 4027c1f..eadcaf2 100644 --- a/pkg/controller/cluster/config/server.go +++ b/pkg/controller/cluster/config/server.go @@ -47,11 +47,11 @@ func serverOptions(cluster *v1alpha1.Cluster) string { if cluster.Spec.Token != "" { opts = "token: " + cluster.Spec.Token + "\n" } - if cluster.Spec.ClusterCIDR != "" { - opts = opts + "cluster-cidr: " + cluster.Spec.ClusterCIDR + "\n" + if cluster.Status.ClusterCIDR != "" { + opts = opts + "cluster-cidr: " + cluster.Status.ClusterCIDR + "\n" } - if cluster.Spec.ServiceCIDR != "" { - opts = opts + "service-cidr: " + cluster.Spec.ServiceCIDR + "\n" + if cluster.Status.ServiceCIDR != "" { + opts = opts + "service-cidr: " + cluster.Status.ServiceCIDR + "\n" } if cluster.Spec.ClusterDNS != "" { opts = opts + "cluster-dns: " + cluster.Spec.ClusterDNS + "\n" diff --git a/pkg/controller/cluster/controller.go b/pkg/controller/cluster/controller.go index b0d45c9..ae92d2f 100644 --- a/pkg/controller/cluster/controller.go +++ b/pkg/controller/cluster/controller.go @@ -40,68 +40,6 @@ func Add(ctx context.Context, mgr manager.Manager) error { Scheme: mgr.GetScheme(), } - clusterSubnets, err := generateSubnets(defaultClusterCIDR) - if err != nil { - return err - } - - var clusterSubnetAllocations []v1alpha1.Allocation - for _, cs := range clusterSubnets { - clusterSubnetAllocations = append(clusterSubnetAllocations, v1alpha1.Allocation{ - IPNet: cs, - }) - } - - cidrClusterPool := v1alpha1.CIDRAllocationPool{ - ObjectMeta: metav1.ObjectMeta{ - Name: cidrAllocationClusterPoolName, - }, - Spec: v1alpha1.CIDRAllocationPoolSpec{ - DefaultClusterCIDR: defaultClusterCIDR, - }, - Status: v1alpha1.CIDRAllocationPoolStatus{ - Pool: clusterSubnetAllocations, - }, - } - if err := reconciler.Client.Create(ctx, &cidrClusterPool); err != nil { - if !apierrors.IsAlreadyExists(err) { - // return nil since the resource has - // already been created - return err - } - } - - clusterServiceSubnets, err := generateSubnets(defaultClusterServiceCIDR) - if err != nil { - return err - } - - var clusterServiceSubnetAllocations []v1alpha1.Allocation - for _, ss := range clusterServiceSubnets { - clusterServiceSubnetAllocations = append(clusterServiceSubnetAllocations, v1alpha1.Allocation{ - IPNet: ss, - }) - } - - cidrServicePool := v1alpha1.CIDRAllocationPool{ - ObjectMeta: metav1.ObjectMeta{ - Name: cidrAllocationServicePoolName, - }, - Spec: v1alpha1.CIDRAllocationPoolSpec{ - DefaultClusterCIDR: defaultClusterCIDR, - }, - Status: v1alpha1.CIDRAllocationPoolStatus{ - Pool: clusterServiceSubnetAllocations, - }, - } - if err := reconciler.Client.Create(ctx, &cidrServicePool); err != nil { - if !apierrors.IsAlreadyExists(err) { - // return nil since the resource has - // already been created - return err - } - } - // create a new controller and add it to the manager //this can be replaced by the new builder functionality in controller-runtime controller, err := controller.New(clusterController, mgr, controller.Options{ @@ -147,18 +85,6 @@ func (c *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request } if controllerutil.ContainsFinalizer(&cluster, clusterFinalizerName) { - if !cluster.Status.OverrideClusterCIDR { - if err := c.releaseCIDR(ctx, cluster.Status.ClusterCIDR, cluster.Name); err != nil { - return reconcile.Result{}, err - } - } - - if !cluster.Status.OverrideServiceCIDR { - if err := c.releaseCIDR(ctx, cluster.Status.ServiceCIDR, cluster.Name); err != nil { - return reconcile.Result{}, err - } - } - // remove our finalizer from the list and update it. controllerutil.RemoveFinalizer(&cluster, clusterFinalizerName) if err := c.Client.Update(ctx, &cluster); err != nil { @@ -176,27 +102,14 @@ func (c *ClusterReconciler) createCluster(ctx context.Context, cluster *v1alpha1 return util.WrapErr("failed to create ns", err) } - klog.Info(cluster) - if cluster.Spec.ClusterCIDR == "" { - clusterCIDR, err := c.nextCIDR(ctx, cidrAllocationClusterPoolName, cluster.Name) - if err != nil { - return err - } - cluster.Status.ClusterCIDR = clusterCIDR.String() - } else { - cluster.Status.OverrideClusterCIDR = true - cluster.Status.ClusterCIDR = cluster.Spec.ClusterCIDR + cluster.Status.ClusterCIDR = cluster.Spec.ClusterCIDR + if cluster.Status.ClusterCIDR == "" { + cluster.Status.ClusterCIDR = defaultClusterCIDR } - if cluster.Spec.ServiceCIDR == "" { - serviceCIDR, err := c.nextCIDR(ctx, cidrAllocationServicePoolName, cluster.Name) - if err != nil { - return err - } - cluster.Status.ServiceCIDR = serviceCIDR.String() - } else { - cluster.Status.OverrideServiceCIDR = true - cluster.Status.ClusterCIDR = cluster.Spec.ClusterCIDR + cluster.Status.ServiceCIDR = cluster.Spec.ServiceCIDR + if cluster.Status.ServiceCIDR == "" { + cluster.Status.ServiceCIDR = defaultClusterServiceCIDR } klog.Infof("creating cluster service") @@ -374,3 +287,68 @@ func (c *ClusterReconciler) createDeployments(ctx context.Context, cluster *v1al return nil } + +func (c *ClusterReconciler) createCIDRPools(ctx context.Context) error { + clusterSubnets, err := generateSubnets(defaultClusterCIDR) + if err != nil { + return err + } + + var clusterSubnetAllocations []v1alpha1.Allocation + for _, cs := range clusterSubnets { + clusterSubnetAllocations = append(clusterSubnetAllocations, v1alpha1.Allocation{ + IPNet: cs, + }) + } + + cidrClusterPool := v1alpha1.CIDRAllocationPool{ + ObjectMeta: metav1.ObjectMeta{ + Name: cidrAllocationClusterPoolName, + }, + Spec: v1alpha1.CIDRAllocationPoolSpec{ + DefaultClusterCIDR: defaultClusterCIDR, + }, + Status: v1alpha1.CIDRAllocationPoolStatus{ + Pool: clusterSubnetAllocations, + }, + } + if err := c.Client.Create(ctx, &cidrClusterPool); err != nil { + if !apierrors.IsAlreadyExists(err) { + // return nil since the resource has + // already been created + return err + } + } + + clusterServiceSubnets, err := generateSubnets(defaultClusterServiceCIDR) + if err != nil { + return err + } + + var clusterServiceSubnetAllocations []v1alpha1.Allocation + for _, ss := range clusterServiceSubnets { + clusterServiceSubnetAllocations = append(clusterServiceSubnetAllocations, v1alpha1.Allocation{ + IPNet: ss, + }) + } + + cidrServicePool := v1alpha1.CIDRAllocationPool{ + ObjectMeta: metav1.ObjectMeta{ + Name: cidrAllocationServicePoolName, + }, + Spec: v1alpha1.CIDRAllocationPoolSpec{ + DefaultClusterCIDR: defaultClusterCIDR, + }, + Status: v1alpha1.CIDRAllocationPoolStatus{ + Pool: clusterServiceSubnetAllocations, + }, + } + if err := c.Client.Create(ctx, &cidrServicePool); err != nil { + if !apierrors.IsAlreadyExists(err) { + // return nil since the resource has + // already been created + return err + } + } + return nil +}