Skip to content

Commit 90e5619

Browse files
authored
Implemented possibility to use Multus CNI, fixes #103 (#132)
improved some logging fixed small issue related to the change in the way CNI field is handled Fixing comments for validate func in webhook Cleaning up multus RKE2-on-CAPD manifests updated godoc comment on API field for rke2controlplane Removed unnecessary comment in external AWS templates Signed-off-by: Mohamed Belgaied Hassine <belgaied2@hotmail.com>
1 parent ae15f9e commit 90e5619

11 files changed

Lines changed: 186 additions & 15 deletions

File tree

bootstrap/internal/controllers/rke2config_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (r *RKE2ConfigReconciler) joinControlplane(ctx context.Context, scope *Scop
507507
scope.Logger.Info("RKE2 server token found in Secret!")
508508

509509
if len(scope.ControlPlane.Status.AvailableServerIPs) == 0 {
510-
scope.Logger.V(3).Info("No ControlPlane IP Address found for node registration")
510+
scope.Logger.Info("No ControlPlane IP Address found for node registration")
511511

512512
return ctrl.Result{RequeueAfter: DefaultRequeueAfter}, nil
513513
}
@@ -637,7 +637,7 @@ func (r *RKE2ConfigReconciler) joinWorker(ctx context.Context, scope *Scope) (re
637637
scope.Logger.Info("RKE2 server token found in Secret!")
638638

639639
if len(scope.ControlPlane.Status.AvailableServerIPs) == 0 {
640-
scope.Logger.V(3).Info("No ControlPlane IP Address found for node registration")
640+
scope.Logger.V(1).Info("No ControlPlane IP Address found for node registration")
641641

642642
return ctrl.Result{RequeueAfter: DefaultRequeueAfter}, nil
643643
}

controlplane/api/v1alpha1/rke2controlplane_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ type RKE2ServerConfig struct {
102102
//+optional
103103
CNI CNI `json:"cni,omitempty"`
104104

105+
// CNIMultusEnable enables multus as the first CNI plugin (default: false).
106+
// This option will automatically make Multus a primary CNI, and the value, if specified in the CNI field, as a secondary CNI plugin.
107+
//+optional
108+
CNIMultusEnable bool `json:"cniMultusEnable,omitempty"`
109+
105110
// PauseImage Override image to use for pause.
106111
//+optional
107112
PauseImage string `json:"pauseImage,omitempty"`

controlplane/api/v1alpha1/rke2controlplane_webhook.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2021
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/util/validation/field"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
logf "sigs.k8s.io/controller-runtime/pkg/log"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -50,12 +52,20 @@ var _ webhook.Validator = &RKE2ControlPlane{}
5052

5153
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
5254
func (r *RKE2ControlPlane) ValidateCreate() error {
53-
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
55+
if bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec) != nil {
56+
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
57+
}
58+
59+
return ValidateRKE2ControlPlaneSpec(r.Name, &r.Spec)
5460
}
5561

5662
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
5763
func (r *RKE2ControlPlane) ValidateUpdate(old runtime.Object) error {
58-
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
64+
if bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec) != nil {
65+
return bootstrapv1.ValidateRKE2ConfigSpec(r.Name, &r.Spec.RKE2ConfigSpec)
66+
}
67+
68+
return ValidateRKE2ControlPlaneSpec(r.Name, &r.Spec)
5969
}
6070

6171
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
@@ -64,3 +74,26 @@ func (r *RKE2ControlPlane) ValidateDelete() error {
6474

6575
return nil
6676
}
77+
78+
// ValidateRKE2ControlPlaneSpec validates the RKE2ControlPlaneSpec Object.
79+
func ValidateRKE2ControlPlaneSpec(name string, spec *RKE2ControlPlaneSpec) error {
80+
allErrs := spec.validate()
81+
if len(allErrs) == 0 {
82+
return nil
83+
}
84+
85+
return apierrors.NewInvalid(GroupVersion.WithKind("RKE2ControlPlane").GroupKind(), name, allErrs)
86+
}
87+
88+
// validate validates the RKE2ControlPlaneSpec Object.
89+
func (s *RKE2ControlPlaneSpec) validate() field.ErrorList {
90+
var allErrs field.ErrorList
91+
92+
if s.ServerConfig.CNIMultusEnable && s.ServerConfig.CNI == "" {
93+
allErrs = append(allErrs,
94+
field.Invalid(field.NewPath("spec", "serverConfig", "cni"),
95+
s.ServerConfig.CNI, "must be specified when cniMultusEnable is true"))
96+
}
97+
98+
return allErrs
99+
}

controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_rke2controlplanes.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,12 @@ spec:
707707
- canal
708708
- cilium
709709
type: string
710+
cniMultusEnable:
711+
description: 'CNIMultusEnable enables multus as the first CNI
712+
plugin (default: false). This option will automatically make
713+
Multus a primary CNI, and the value, if specified in the CNI
714+
field, as a secondary CNI plugin.'
715+
type: boolean
710716
disableComponents:
711717
description: DisableComponents lists Kubernetes components and
712718
RKE2 plugin components that will be disabled.

controlplane/internal/controllers/rke2controlplane_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (r *RKE2ControlPlaneReconciler) updateStatus(ctx context.Context, rcp *cont
256256

257257
readyMachines := ownedMachines.Filter(collections.IsReady())
258258
for _, readyMachine := range readyMachines {
259-
logger.Info("Ready Machine :", "machine-name", readyMachine.Name)
259+
logger.V(3).Info("Ready Machine : " + readyMachine.Name)
260260
}
261261

262262
controlPlane, err := rke2.NewControlPlane(ctx, r.Client, cluster, rcp, ownedMachines)
@@ -346,7 +346,8 @@ func (r *RKE2ControlPlaneReconciler) updateStatus(ctx context.Context, rcp *cont
346346
return fmt.Errorf("no Control Plane Machines are ready for RKE2ControlPlane %s/%s", rcp.Namespace, rcp.Name)
347347
}
348348

349-
availableCPMachines := readyMachines.Filter(collections.Not(collections.HasUnhealthyCondition))
349+
availableCPMachines := readyMachines
350+
350351
validIPAddresses := []string{}
351352

352353
for _, machine := range availableCPMachines {
@@ -355,9 +356,7 @@ func (r *RKE2ControlPlaneReconciler) updateStatus(ctx context.Context, rcp *cont
355356
break
356357
}
357358

358-
if !conditions.IsFalse(machine, clusterv1.MachineNodeHealthyCondition) {
359-
validIPAddresses = append(validIPAddresses, ipAddress)
360-
}
359+
validIPAddresses = append(validIPAddresses, ipAddress)
361360
}
362361

363362
rcp.Status.AvailableServerIPs = validIPAddresses

pkg/rke2/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type rke2ServerConfig struct {
8181
AdvertiseAddress string `json:"advertise-address,omitempty"`
8282
AuditPolicyFile string `json:"audit-policy-file,omitempty"`
8383
BindAddress string `json:"bind-address,omitempty"`
84-
CNI string `json:"cni,omitempty"`
84+
CNI []string `json:"cni,omitempty"`
8585
CloudControllerManagerExtraEnv map[string]string `json:"cloud-controller-manager-extra-env,omitempty"`
8686
CloudControllerManagerExtraMounts map[string]string `json:"cloud-controller-manager-extra-mount,omitempty"`
8787
CloudProviderConfig string `json:"cloud-provider-config,omitempty"`
@@ -153,7 +153,7 @@ type ServerConfigOpts struct {
153153
Client client.Client
154154
}
155155

156-
func newRKE2ServerConfig(opts ServerConfigOpts) (*rke2ServerConfig, []bootstrapv1.File, error) {
156+
func newRKE2ServerConfig(opts ServerConfigOpts) (*rke2ServerConfig, []bootstrapv1.File, error) { // nolint:gocyclo
157157
rke2ServerConfig := &rke2ServerConfig{}
158158
files := []bootstrapv1.File{}
159159
rke2ServerConfig.AdvertiseAddress = opts.ServerConfig.AdvertiseAddress
@@ -195,7 +195,12 @@ func newRKE2ServerConfig(opts ServerConfigOpts) (*rke2ServerConfig, []bootstrapv
195195
}
196196

197197
rke2ServerConfig.BindAddress = opts.ServerConfig.BindAddress
198-
rke2ServerConfig.CNI = string(opts.ServerConfig.CNI)
198+
if opts.ServerConfig.CNIMultusEnable {
199+
rke2ServerConfig.CNI = append([]string{"multus"}, string(opts.ServerConfig.CNI))
200+
} else if opts.ServerConfig.CNI != "" {
201+
rke2ServerConfig.CNI = []string{string(opts.ServerConfig.CNI)}
202+
}
203+
199204
rke2ServerConfig.ClusterDNS = opts.ServerConfig.ClusterDNS
200205
rke2ServerConfig.ClusterDomain = opts.ServerConfig.ClusterDomain
201206

pkg/rke2/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var _ = Describe("RKE2ServerConfig", func() {
174174
Expect(rke2ServerConfig.AdvertiseAddress).To(Equal(serverConfig.AdvertiseAddress))
175175
Expect(rke2ServerConfig.AuditPolicyFile).To(Equal("/etc/rancher/rke2/audit-policy.yaml"))
176176
Expect(rke2ServerConfig.BindAddress).To(Equal(serverConfig.BindAddress))
177-
Expect(rke2ServerConfig.CNI).To(Equal(string(serverConfig.CNI)))
177+
Expect(rke2ServerConfig.CNI).To(Equal([]string{string(serverConfig.CNI)}))
178178
Expect(rke2ServerConfig.ClusterCIDR).To(Equal("192.168.0.0/16"))
179179
Expect(rke2ServerConfig.ServiceCIDR).To(Equal("192.169.0.0/16"))
180180
Expect(rke2ServerConfig.ClusterDNS).To(Equal(serverConfig.ClusterDNS))

samples/aws/external/cluster-template-external-cloud-provider.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ metadata:
120120
spec:
121121
template:
122122
spec:
123-
#preRKE2Commands:
124-
#- sudo hostnamectl set-hostname $(curl -s http://169.254.169.254/1.0/meta-data/hostname)
125123
agentConfig:
126124
version: ${KUBERNETES_VERSION}+rke2r1
127125
---
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: ${CABPR_NAMESPACE}
5+
---
6+
apiVersion: cluster.x-k8s.io/v1beta1
7+
kind: Cluster
8+
metadata:
9+
namespace: ${CABPR_NAMESPACE}
10+
name: ${CLUSTER_NAME}
11+
spec:
12+
clusterNetwork:
13+
pods:
14+
cidrBlocks:
15+
- 10.45.0.0/16
16+
services:
17+
cidrBlocks:
18+
- 10.46.0.0/16
19+
serviceDomain: cluster.local
20+
controlPlaneRef:
21+
apiVersion: controlplane.cluster.x-k8s.io/v1alpha1
22+
kind: RKE2ControlPlane
23+
name: ${CLUSTER_NAME}-control-plane
24+
infrastructureRef:
25+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
26+
kind: DockerCluster
27+
name: ${CLUSTER_NAME}
28+
---
29+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
30+
kind: DockerCluster
31+
metadata:
32+
name: ${CLUSTER_NAME}
33+
namespace: ${CABPR_NAMESPACE}
34+
---
35+
apiVersion: controlplane.cluster.x-k8s.io/v1alpha1
36+
kind: RKE2ControlPlane
37+
metadata:
38+
name: ${CLUSTER_NAME}-control-plane
39+
namespace: ${CABPR_NAMESPACE}
40+
spec:
41+
replicas: ${CABPR_CP_REPLICAS}
42+
agentConfig:
43+
version: ${KUBERNETES_VERSION}+rke2r1
44+
serverConfig:
45+
cniMultusEnable: true
46+
cni: calico
47+
infrastructureRef:
48+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
49+
kind: DockerMachineTemplate
50+
name: controlplane
51+
nodeDrainTimeout: 2m
52+
---
53+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
54+
kind: DockerMachineTemplate
55+
metadata:
56+
name: controlplane
57+
namespace: ${CABPR_NAMESPACE}
58+
spec:
59+
template:
60+
spec: {}
61+
---
62+
apiVersion: cluster.x-k8s.io/v1beta1
63+
kind: MachineDeployment
64+
metadata:
65+
name: worker-md-0
66+
namespace: ${CABPR_NAMESPACE}
67+
spec:
68+
clusterName: ${CLUSTER_NAME}
69+
replicas: ${CABPR_WK_REPLICAS}
70+
selector:
71+
matchLabels:
72+
cluster.x-k8s.io/cluster-name: ${CLUSTER_NAME}
73+
template:
74+
spec:
75+
version: ${KUBERNETES_VERSION}
76+
clusterName: ${CLUSTER_NAME}
77+
bootstrap:
78+
configRef:
79+
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha1
80+
kind: RKE2ConfigTemplate
81+
name: ${CLUSTER_NAME}-agent
82+
namespace: ${CABPR_NAMESPACE}
83+
infrastructureRef:
84+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
85+
kind: DockerMachineTemplate
86+
name: worker
87+
namespace: ${CABPR_NAMESPACE}
88+
---
89+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
90+
kind: DockerMachineTemplate
91+
metadata:
92+
name: worker
93+
namespace: ${CABPR_NAMESPACE}
94+
spec:
95+
template:
96+
spec: {}
97+
---
98+
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha1
99+
kind: RKE2ConfigTemplate
100+
metadata:
101+
namespace: ${CABPR_NAMESPACE}
102+
name: ${CLUSTER_NAME}-agent
103+
spec:
104+
template:
105+
spec:
106+
agentConfig:
107+
version: ${KUBERNETES_VERSION}+rke2r1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: controlplane.cluster.x-k8s.io/v1alpha1
2+
kind: RKE2ControlPlane
3+
metadata:
4+
name: rke2cp-test
5+
namespace: webhook-test
6+
spec:
7+
replicas: 3
8+
agentConfig:
9+
version: v1.24.11+rke2r1
10+
serverConfig:
11+
cniMultusEnable: true
12+
infrastructureRef:
13+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
14+
kind: DockerMachineTemplate
15+
name: controlplane
16+
nodeDrainTimeout: 2m

0 commit comments

Comments
 (0)