Skip to content

Commit

Permalink
feat: add k3s cluster provider detection
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Dec 11, 2023
1 parent fd64fc6 commit e38bc1d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
25 changes: 20 additions & 5 deletions pkg/provider/k8scloudproviderprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
ClusterProviderAWS = ClusterProvider("AWS")
// ClusterProviderKubernetesInDocker identifies kind (kubernetes in docker) as cluster provider.
ClusterProviderKubernetesInDocker = ClusterProvider("kind")
// ClusterProviderK3S identifies k3s cluster provider.
ClusterProviderK3S = ClusterProvider("k3s")
// ClusterProviderUnknown represents an unknown cluster provider.
ClusterProviderUnknown = ClusterProvider("UNKNOWN")
)
Expand Down Expand Up @@ -123,7 +125,9 @@ func getClusterProviderFromNodesProviderID(nodeList *corev1.NodeList) (ClusterPr
providerIDPrefixGKE = "gce"
providerIDPrefixAWS = "aws"
providerIDPrefixKind = "kind"
providerIDPrefixK3s = "k3s"
)

d := make(clusterProviderDistribution)
for _, n := range nodeList.Items {
if strings.HasPrefix(n.Spec.ProviderID, providerIDPrefixGKE) {
Expand All @@ -138,6 +142,10 @@ func getClusterProviderFromNodesProviderID(nodeList *corev1.NodeList) (ClusterPr
d[ClusterProviderKubernetesInDocker]++
continue
}
if strings.HasPrefix(n.Spec.ProviderID, providerIDPrefixK3s) {
d[ClusterProviderK3S]++
continue
}
}
if p, ok := getMostCommonClusterProviderFromDistribution(d); ok {
return p, true
Expand All @@ -164,12 +172,16 @@ func getMostCommonClusterProviderFromDistribution(d clusterProviderDistribution)
}

func getClusterProviderFromAnnotations(annotations map[string]string) (ClusterProvider, bool) {
const (
annotationNameGKEInstanceID = "container.googleapis.com/instance_id"
)

annotationsGKE := map[string]struct{}{
annotationNameGKEInstanceID: {},
"container.googleapis.com/instance_id": {},
}

annotationsK3S := map[string]struct{}{
"k3s.io/hostname": {},
"k3s.io/internal-ip": {},
"k3s.io/node-args": {},
"k3s.io/node-config-hash": {},
"k3s.io/node-env": {},
}

// This approach currently loops through the provided annotations and checks
Expand All @@ -181,6 +193,9 @@ func getClusterProviderFromAnnotations(annotations map[string]string) (ClusterPr
if _, ok := annotationsGKE[aName]; ok {
return ClusterProviderGKE, true
}
if _, ok := annotationsK3S[aName]; ok {
return ClusterProviderK3S, true
}
}

return ClusterProviderUnknown, false
Expand Down
62 changes: 62 additions & 0 deletions pkg/provider/k8scloudproviderprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,68 @@ func TestClusterProvider(t *testing.T) {
},
expected: ClusterProviderKubernetesInDocker,
},
// k3s
{
name: "k3s gets inferred from provider ID prefix",
clientFunc: func() *clientgo_fake.Clientset {
return clientgo_fake.NewSimpleClientset(
&corev1.Node{
Spec: corev1.NodeSpec{
ProviderID: "k3s://orbstack",
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"beta.kubernetes.io/arch": "arm64",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/arch": "arm64",
"kubernetes.io/hostname": "orbstack",
"kubernetes.io/os": "linux",
"node-role.kubernetes.io/control-plane": "",
},
},
},
)
},
expected: ClusterProviderK3S,
},
{
name: "k3s gets inferred from annotations",
clientFunc: func() *clientgo_fake.Clientset {
return clientgo_fake.NewSimpleClientset(
&corev1.Node{
Spec: corev1.NodeSpec{
ProviderID: "k3s://orbstack",
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"flannel.alpha.coreos.com/backend-data": "null",
"flannel.alpha.coreos.com/backend-type": "host-gw",
"flannel.alpha.coreos.com/backend-v6-data": "null",
"flannel.alpha.coreos.com/kube-subnet-manager": "true",
"flannel.alpha.coreos.com/public-ip": "198.19.249.2",
"flannel.alpha.coreos.com/public-ipv6": "fd07:b51a:cc66::2",
"k3s.io/hostname": "orbstack",
"k3s.io/internal-ip": "198.19.249.2,fd07:b51a:cc66::2",
"k3s.io/node-args": `["server","--disable","metrics-server,traefik,coredns","--https-listen-port","26443","--lb-server-port","26444","--docker","--container-runtime-endpoint","/var/run/docker.sock","--protect-kernel-defaults","--flannel-backend","host-gw","--cluster-cidr","192.168.194.0/25,fd07:b51a:cc66:a::/72","--service-cidr","192.168.194.128/25,fd07:b51a:cc66:a:8000::/112","--kube-controller-manager-arg","node-cidr-mask-size-ipv4=25","--kube-controller-manager-arg","node-cidr-mask-size-ipv6=72","--write-kubeconfig","/run/kubeconfig.yml"]`,
"k3s.io/node-config-hash": "sjrlvsvk5fwsmiduwp6tm23ofcbjl3t4qk4p3sozvzjpzaefar2a====",
"k3s.io/node-env": `{"k3s_data_dir"":/var/lib/rancher/k3s/data/8f922a49dfab9ed75d85cba9b81b9ac7f20a633da393551392030e41ea4d569b"}`,
"node.alpha.kubernetes.io/ttl": "0",
"volumes.kubernetes.io/controller-managed-attach-detach": "true",
},
Labels: map[string]string{
"beta.kubernetes.io/arch": "arm64",
"beta.kubernetes.io/os": "linux",
"kubernetes.io/arch": "arm64",
"kubernetes.io/hostname": "orbstack",
"kubernetes.io/os": "linux",
"node-role.kubernetes.io/control-plane": "",
},
},
},
)
},
expected: ClusterProviderK3S,
},
}

for _, tc := range testcases {
Expand Down

0 comments on commit e38bc1d

Please sign in to comment.