Skip to content

Commit 4954ae3

Browse files
PWX-37366: Add function to check if k3s deployment or rke2 (#1555)
* check if k3s deployment or rke2 * adding UT and rename function * renaming all isk3s functions
1 parent 20fd910 commit 4954ae3

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

drivers/storage/portworx/component/pvccontroller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ func (c *pvcController) createDeployment(
331331
command = append(command, "--address=0.0.0.0")
332332
if port, ok := cluster.Annotations[pxutil.AnnotationPVCControllerPort]; ok && port != "" {
333333
command = append(command, "--port="+port)
334-
} else if pxutil.IsAKS(cluster) || c.isK3sDeployment() {
334+
} else if pxutil.IsAKS(cluster) || c.isK3sOrRke2Deployment() {
335335
command = append(command, "--port="+CustomPVCControllerInsecurePort)
336336
}
337337
}
338338

339339
if securePort, ok := cluster.Annotations[pxutil.AnnotationPVCControllerSecurePort]; ok && securePort != "" {
340340
command = append(command, "--secure-port="+securePort)
341-
} else if pxutil.IsAKS(cluster) || c.isK3sDeployment() {
341+
} else if pxutil.IsAKS(cluster) || c.isK3sOrRke2Deployment() {
342342
command = append(command, "--secure-port="+CustomPVCControllerSecurePort)
343343
}
344344

@@ -420,15 +420,15 @@ func (c *pvcController) getPVCControllerDeploymentSpec(
420420
if c.k8sVersion.GreaterThanOrEqual(k8sutil.K8sVer1_22) {
421421
if port, ok := cluster.Annotations[pxutil.AnnotationPVCControllerSecurePort]; ok && port != "" {
422422
healthCheckPort = port
423-
} else if pxutil.IsAKS(cluster) || c.isK3sDeployment() {
423+
} else if pxutil.IsAKS(cluster) || c.isK3sOrRke2Deployment() {
424424
healthCheckPort = CustomPVCControllerSecurePort
425425
} else {
426426
healthCheckPort = defaultPVCControllerSecurePort
427427
}
428428
healthCheckScheme = v1.URISchemeHTTPS
429429
} else if port, ok := cluster.Annotations[pxutil.AnnotationPVCControllerPort]; ok && port != "" {
430430
healthCheckPort = port
431-
} else if pxutil.IsAKS(cluster) || c.isK3sDeployment() {
431+
} else if pxutil.IsAKS(cluster) || c.isK3sOrRke2Deployment() {
432432
healthCheckPort = CustomPVCControllerInsecurePort
433433
}
434434

@@ -551,7 +551,7 @@ func (c *pvcController) getPVCControllerDeploymentSpec(
551551
return deployment
552552
}
553553

554-
func (c *pvcController) isK3sDeployment() bool {
554+
func (c *pvcController) isK3sOrRke2Deployment() bool {
555555

556556
if c.isK3s != nil {
557557
return *c.isK3s
@@ -562,7 +562,7 @@ func (c *pvcController) isK3sDeployment() bool {
562562
return false
563563
}
564564
if len(ext) > 0 {
565-
ok := pxutil.IsK3sClusterExt(ext)
565+
ok := pxutil.IsK3sOrRke2ClusterExt(ext)
566566
c.isK3s = boolPtr(ok)
567567
return *c.isK3s
568568
}

drivers/storage/portworx/components_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,6 +2097,49 @@ func TestPVCControllerInstallForK3s(t *testing.T) {
20972097
verifyPVCControllerDeploymentObject(t, cluster, k8sClient, pvcControllerDeployment)
20982098
}
20992099

2100+
func TestPVCControllerInstallForRke2(t *testing.T) {
2101+
coreops.SetInstance(coreops.New(fakek8sclient.NewSimpleClientset()))
2102+
reregisterComponents()
2103+
2104+
k8sClient := testutil.FakeK8sClient()
2105+
2106+
versionClient := fakek8sclient.NewSimpleClientset()
2107+
versionClient.Discovery().(*fakediscovery.FakeDiscovery).FakedServerVersion = &version.Info{
2108+
GitVersion: "v1.18.0+rke2r1",
2109+
}
2110+
coreops.SetInstance(coreops.New(versionClient))
2111+
2112+
driver := portworx{}
2113+
err := driver.Init(k8sClient, runtime.NewScheme(), record.NewFakeRecorder(0))
2114+
require.NoError(t, err)
2115+
2116+
cluster := &corev1.StorageCluster{
2117+
ObjectMeta: metav1.ObjectMeta{
2118+
Name: "px-cluster",
2119+
Namespace: "kube-system",
2120+
Annotations: map[string]string{
2121+
pxutil.AnnotationPVCController: "true",
2122+
},
2123+
},
2124+
}
2125+
2126+
err = driver.PreInstall(cluster)
2127+
require.NoError(t, err)
2128+
2129+
verifyPVCControllerInstall(t, cluster, k8sClient, "pvcControllerClusterRole.yaml")
2130+
2131+
specFileName := "pvcControllerDeployment.yaml"
2132+
pvcControllerDeployment := testutil.GetExpectedDeployment(t, specFileName)
2133+
command := pvcControllerDeployment.Spec.Template.Spec.Containers[0].Command
2134+
command = append(command, "--port="+component.CustomPVCControllerInsecurePort)
2135+
command = append(command, "--secure-port="+component.CustomPVCControllerSecurePort)
2136+
pvcControllerDeployment.Spec.Template.Spec.Containers[0].Command = command
2137+
pvcControllerDeployment.Spec.Template.Spec.Containers[0].Image = "gcr.io/google_containers/kube-controller-manager-amd64:v1.18.0"
2138+
pvcControllerDeployment.Spec.Template.Spec.Containers[0].LivenessProbe.ProbeHandler.HTTPGet.Port = intstr.Parse(component.CustomPVCControllerInsecurePort)
2139+
2140+
verifyPVCControllerDeploymentObject(t, cluster, k8sClient, pvcControllerDeployment)
2141+
}
2142+
21002143
func TestPVCControllerWhenPVCControllerDisabledExplicitly(t *testing.T) {
21012144
coreops.SetInstance(coreops.New(fakek8sclient.NewSimpleClientset()))
21022145
reregisterComponents()

drivers/storage/portworx/deployment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func newTemplate(
123123
return nil, err
124124
}
125125

126-
t.isK3s = isK3sClusterExt(ext)
126+
t.isK3s = isK3sOrRke2ClusterExt(ext)
127127
t.runOnMaster = t.isK3s || pxutil.RunOnMaster(cluster)
128128
t.pxVersion = pxutil.GetPortworxVersion(cluster)
129129
deprecatedCSIDriverName := pxutil.UseDeprecatedCSIDriverName(cluster)
@@ -1948,9 +1948,9 @@ func getCommonVolumeList(pxVersion *version.Version) []volumeInfo {
19481948
return list
19491949
}
19501950

1951-
func isK3sClusterExt(ext string) bool {
1951+
func isK3sOrRke2ClusterExt(ext string) bool {
19521952
if len(ext) > 0 {
1953-
return pxutil.IsK3sClusterExt(ext) || strings.HasPrefix(ext[1:], "rke2")
1953+
return pxutil.IsK3sOrRke2ClusterExt(ext)
19541954
}
19551955
return false
19561956
}

drivers/storage/portworx/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,8 +1914,8 @@ func isVersionSupported(current, target string) bool {
19141914
return currentVersion.Core().GreaterThanOrEqual(targetVersion)
19151915
}
19161916

1917-
func IsK3sClusterExt(ext string) bool {
1918-
return strings.HasPrefix(ext[1:], "k3s")
1917+
func IsK3sOrRke2ClusterExt(ext string) bool {
1918+
return strings.HasPrefix(ext[1:], "k3s") || strings.HasPrefix(ext[1:], "rke2")
19191919
}
19201920

19211921
// ShouldUseQuorumFlag checks if the node should use the quorum member flag to decide storage status

pkg/util/test/util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ func GetExpectedPxNodeList(cluster *corev1.StorageCluster) ([]v1.Node, error) {
18101810
NodeAffinity: cluster.Spec.Placement.NodeAffinity.DeepCopy(),
18111811
}
18121812
} else {
1813-
if IsK3sCluster() || IsPxDeployedOnMaster(cluster) {
1813+
if IsK3sOrRke2Cluster() || IsPxDeployedOnMaster(cluster) {
18141814
runOnMaster = true
18151815
}
18161816

@@ -1889,8 +1889,8 @@ func GetFullVersion() (*version.Version, string, error) {
18891889
return ver, "", err
18901890
}
18911891

1892-
// IsK3sCluster returns true or false, based on this kubernetes cluster is k3s or not
1893-
func IsK3sCluster() bool {
1892+
// IsK3sOrRke2Cluster returns true or false, based on this kubernetes cluster is k3s or rke2 or not
1893+
func IsK3sOrRke2Cluster() bool {
18941894
// Get k8s version ext
18951895
_, ext, _ := GetFullVersion()
18961896

@@ -3155,7 +3155,7 @@ func validatePvcControllerPorts(cluster *corev1.StorageCluster, pvcControllerDep
31553155
for _, containerCommand := range container.Command {
31563156
if strings.Contains(containerCommand, "--secure-port") {
31573157
if len(pvcSecurePort) == 0 {
3158-
if isAKS(cluster) || IsK3sCluster() {
3158+
if isAKS(cluster) || IsK3sOrRke2Cluster() {
31593159
if strings.Split(containerCommand, "=")[1] != CustomPVCControllerSecurePort {
31603160
return nil, true, fmt.Errorf("failed to validate secure-port, secure-port is missing in the PVC Controler pod %s", pod.Name)
31613161
}

0 commit comments

Comments
 (0)