Skip to content

Commit

Permalink
feat: rancher rke2 version support (#293)
Browse files Browse the repository at this point in the history
* feat: rancher rke2 version support

Signed-off-by: chenk <[email protected]>

* feat: rancher rke2 version support

Signed-off-by: chenk <[email protected]>

---------

Signed-off-by: chenk <[email protected]>
  • Loading branch information
chen-keinan authored Jan 23, 2024
1 parent 9df2e22 commit c2af0ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 20 additions & 10 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,10 @@ func NodeInfo(node v1.Node) bom.NodeInfo {
}
return bom.NodeInfo{
NodeName: node.Name,
KubeletVersion: node.Status.NodeInfo.KubeletVersion,
KubeletVersion: trimString(k8sVersions(node.Status.NodeInfo.KubeletVersion), []string{"v", "V"}),
ContainerRuntimeVersion: node.Status.NodeInfo.ContainerRuntimeVersion,
OsImage: node.Status.NodeInfo.OSImage,
KubeProxyVersion: node.Status.NodeInfo.KubeProxyVersion,
KubeProxyVersion: trimString(k8sVersions(node.Status.NodeInfo.KubeProxyVersion), []string{"v", "V"}),
Properties: map[string]string{
"NodeRole": nodeRole,
"HostName": node.ObjectMeta.Name,
Expand Down Expand Up @@ -521,20 +521,30 @@ func PodInfo(pod corev1.Pod, labelSelector string) (*bom.Component, error) {

func findComponentVersion(containers []bom.Container, name string) string {
for _, c := range containers {
switch {
case strings.Contains(c.Version, "-rke2"):
index := strings.Index(c.Version, "-rke2")
return c.Version[:index]
case strings.Contains(c.Version, "-k3s"):
index := strings.Index(c.Version, "-k3s")
return c.Version[:index]
case strings.Contains(c.ID, name):
if strings.Contains(c.Version, "rke2") || strings.Contains(c.Version, "k3s") {
return k8sVersions(c.Version)
} else if strings.Contains(c.ID, name) {
return c.Version
}
}
return ""
}

func k8sVersions(version string) string {
switch {
case strings.Contains(version, "+rke2"):
index := strings.Index(version, "+rke2")
return version[:index]
case strings.Contains(version, "-rke2"):
index := strings.Index(version, "-rke2")
return version[:index]
case strings.Contains(version, "-k3s"):
index := strings.Index(version, "-k3s")
return version[:index]
}
return version
}

func (c *cluster) isOpenShift() bool {
ctx := context.Background()
_, err := c.clientset.CoreV1().Namespaces().Get(ctx, "openshift-kube-apiserver", metav1.GetOptions{})
Expand Down
8 changes: 4 additions & 4 deletions pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,19 @@ func TestNodeInfo(t *testing.T) {
NodeInfo: v1.NodeSystemInfo{
Architecture: "amd64",
ContainerRuntimeVersion: "containerd://1.5.2",
KubeletVersion: "v1.21.1",
KubeletVersion: "1.21.1",
KernelVersion: "6.5.9-300.fc39.aarch64",
OperatingSystem: "linux",
OSImage: "Ubuntu 21.04",
KubeProxyVersion: "v1.21.1",
KubeProxyVersion: "1.21.1",
},
},
},

want: bom.NodeInfo{
NodeName: "node1",
KubeletVersion: "v1.21.1",
KubeProxyVersion: "v1.21.1",
KubeletVersion: "1.21.1",
KubeProxyVersion: "1.21.1",
ContainerRuntimeVersion: "containerd://1.5.2",
OsImage: "Ubuntu 21.04",
Properties: map[string]string{
Expand Down

0 comments on commit c2af0ec

Please sign in to comment.