From c2cde0c9ba117e2b760ddc4a033e213ebefabeaa Mon Sep 17 00:00:00 2001 From: Hussein Galal Date: Wed, 26 Feb 2025 11:32:17 +0200 Subject: [PATCH] Fix the default CIDRs for both modes (#271) * Fix the default CIDRs for both modes Signed-off-by: galal-hussein * Fix service/cluster cidr Signed-off-by: galal-hussein --------- Signed-off-by: galal-hussein --- charts/k3k/crds/k3k.io_clusters.yaml | 6 +++-- docs/advanced-usage.md | 6 ++--- docs/crds/crd-docs.md | 4 ++-- pkg/apis/k3k.io/v1alpha1/types.go | 4 ++-- pkg/controller/cluster/cluster.go | 33 +++++++++++++++++----------- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/charts/k3k/crds/k3k.io_clusters.yaml b/charts/k3k/crds/k3k.io_clusters.yaml index 69267f1e..c9b3cbdd 100644 --- a/charts/k3k/crds/k3k.io_clusters.yaml +++ b/charts/k3k/crds/k3k.io_clusters.yaml @@ -66,7 +66,8 @@ spec: rule: self >= 0 clusterCIDR: description: ClusterCIDR is the CIDR range for the pods of the cluster. - Defaults to 10.42.0.0/16. + Defaults to 10.42.0.0/16 in shared mode and 10.52.0.0/16 in virtual + mode. type: string x-kubernetes-validations: - message: clusterCIDR is immutable @@ -211,7 +212,8 @@ spec: rule: self >= 1 serviceCIDR: description: ServiceCIDR is the CIDR range for the services in the - cluster. Defaults to 10.43.0.0/16. + cluster. Defaults to 10.43.0.0/16 in shared mode and 10.53.0.0/16 + in virtual mode. type: string x-kubernetes-validations: - message: serviceCIDR is immutable diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index f7e64a82..dfd220eb 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -94,14 +94,14 @@ In this example we are exposing the Cluster with a Nginx ingress-controller, tha ### `clusterCIDR` -The `clusterCIDR` field specifies the CIDR range for the pods of the cluster. The default value is `10.42.0.0/16`. +The `clusterCIDR` field specifies the CIDR range for the pods of the cluster. The default value is `10.42.0.0/16` in shared mode, and `10.52.0.0/16` in virtual mode. ### `serviceCIDR` -The `serviceCIDR` field specifies the CIDR range for the services in the cluster. The default value is `10.43.0.0/16`. +The `serviceCIDR` field specifies the CIDR range for the services in the cluster. The default value is `10.43.0.0/16` in shared mode, and `10.53.0.0/16` in virtual mode. -**Note:** In `shared` mode, the `serviceCIDR` should match the host cluster's `serviceCIDR` to prevent conflicts. +**Note:** In `shared` mode, the `serviceCIDR` should match the host cluster's `serviceCIDR` to prevent conflicts and in `virtual` mode both `serviceCIDR` and `clusterCIDR` should be different than the host cluster. ### `clusterDNS` diff --git a/docs/crds/crd-docs.md b/docs/crds/crd-docs.md index 6d05534b..da59eed9 100644 --- a/docs/crds/crd-docs.md +++ b/docs/crds/crd-docs.md @@ -118,8 +118,8 @@ _Appears in:_ | `priorityClass` _string_ | PriorityClass is the priorityClassName that will be applied to all server/agent pods.
In "shared" mode the priorityClassName will be applied also to the workloads. | | | | `clusterLimit` _[ClusterLimit](#clusterlimit)_ | Limit is the limits that apply for the server/worker nodes. | | | | `tokenSecretRef` _[SecretReference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#secretreference-v1-core)_ | TokenSecretRef is Secret reference used as a token join server and worker nodes to the cluster. The controller
assumes that the secret has a field "token" in its data, any other fields in the secret will be ignored. | | | -| `clusterCIDR` _string_ | ClusterCIDR is the CIDR range for the pods of the cluster. Defaults to 10.42.0.0/16. | | | -| `serviceCIDR` _string_ | ServiceCIDR is the CIDR range for the services in the cluster. Defaults to 10.43.0.0/16. | | | +| `clusterCIDR` _string_ | ClusterCIDR is the CIDR range for the pods of the cluster. Defaults to 10.42.0.0/16 in shared mode and 10.52.0.0/16 in virtual mode. | | | +| `serviceCIDR` _string_ | ServiceCIDR is the CIDR range for the services in the cluster. Defaults to 10.43.0.0/16 in shared mode and 10.53.0.0/16 in virtual mode. | | | | `clusterDNS` _string_ | ClusterDNS is the IP address for the coredns service. Needs to be in the range provided by ServiceCIDR or CoreDNS may not deploy.
Defaults to 10.43.0.10. | | | | `serverArgs` _string array_ | ServerArgs are the ordered key value pairs (e.x. "testArg", "testValue") for the K3s pods running in server mode. | | | | `agentArgs` _string array_ | AgentArgs are the ordered key value pairs (e.x. "testArg", "testValue") for the K3s pods running in agent mode. | | | diff --git a/pkg/apis/k3k.io/v1alpha1/types.go b/pkg/apis/k3k.io/v1alpha1/types.go index f9024c3a..ce08adf1 100644 --- a/pkg/apis/k3k.io/v1alpha1/types.go +++ b/pkg/apis/k3k.io/v1alpha1/types.go @@ -58,11 +58,11 @@ type ClusterSpec struct { // +optional TokenSecretRef *v1.SecretReference `json:"tokenSecretRef"` - // ClusterCIDR is the CIDR range for the pods of the cluster. Defaults to 10.42.0.0/16. + // ClusterCIDR is the CIDR range for the pods of the cluster. Defaults to 10.42.0.0/16 in shared mode and 10.52.0.0/16 in virtual mode. // +kubebuilder:validation:XValidation:message="clusterCIDR is immutable",rule="self == oldSelf" ClusterCIDR string `json:"clusterCIDR,omitempty"` - // ServiceCIDR is the CIDR range for the services in the cluster. Defaults to 10.43.0.0/16. + // ServiceCIDR is the CIDR range for the services in the cluster. Defaults to 10.43.0.0/16 in shared mode and 10.53.0.0/16 in virtual mode. // +kubebuilder:validation:XValidation:message="serviceCIDR is immutable",rule="self == oldSelf" ServiceCIDR string `json:"serviceCIDR,omitempty"` diff --git a/pkg/controller/cluster/cluster.go b/pkg/controller/cluster/cluster.go index a17a64b2..86d376d8 100644 --- a/pkg/controller/cluster/cluster.go +++ b/pkg/controller/cluster/cluster.go @@ -40,8 +40,10 @@ const ( maxConcurrentReconciles = 1 - defaultClusterCIDR = "10.42.0.0/16" - defaultClusterServiceCIDR = "10.43.0.0/16" + defaultVirtualClusterCIDR = "10.52.0.0/16" + defaultVirtualServiceCIDR = "10.53.0.0/16" + defaultSharedClusterCIDR = "10.42.0.0/16" + defaultSharedServiceCIDR = "10.43.0.0/16" defaultStoragePersistentSize = "1G" memberRemovalTimeout = time.Minute * 1 ) @@ -171,24 +173,29 @@ func (c *ClusterReconciler) reconcileCluster(ctx context.Context, cluster *v1alp cluster.Status.ClusterCIDR = cluster.Spec.ClusterCIDR if cluster.Status.ClusterCIDR == "" { - cluster.Status.ClusterCIDR = defaultClusterCIDR + cluster.Status.ClusterCIDR = defaultVirtualClusterCIDR + if cluster.Spec.Mode == v1alpha1.SharedClusterMode { + cluster.Status.ClusterCIDR = defaultSharedClusterCIDR + } } cluster.Status.ServiceCIDR = cluster.Spec.ServiceCIDR if cluster.Status.ServiceCIDR == "" { - log.Info("serviceCIDR not set") - - serviceCIDR, err := c.lookupServiceCIDR(ctx) - if err != nil { - log.Error(err, "error while looking up Cluster ServiceCIDR") + // in shared mode try to lookup the serviceCIDR + if cluster.Spec.Mode == v1alpha1.SharedClusterMode { + log.Info("looking up Service CIDR for shared mode") + cluster.Status.ServiceCIDR, err = c.lookupServiceCIDR(ctx) + if err != nil { + log.Error(err, "error while looking up Cluster Service CIDR") + cluster.Status.ServiceCIDR = defaultSharedServiceCIDR + } } - // update Status ServiceCIDR - if serviceCIDR == "" { - log.Info("setting default ServiceCIDR") - serviceCIDR = defaultClusterServiceCIDR + // in virtual mode assign a default serviceCIDR + if cluster.Spec.Mode == v1alpha1.VirtualClusterMode { + log.Info("assign default service CIDR for virtual mode") + cluster.Status.ServiceCIDR = defaultVirtualServiceCIDR } - cluster.Status.ServiceCIDR = serviceCIDR } service, err := c.ensureClusterService(ctx, cluster)