mirror of
https://github.com/rancher/k3k.git
synced 2026-03-03 18:20:53 +00:00
Revert CIDR pool allocation and fix delete (#35)
Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user