@@ -2,6 +2,7 @@ package telemetry
2
2
3
3
import (
4
4
"context"
5
+ "strings"
5
6
"time"
6
7
7
8
"k8s.io/apimachinery/pkg/version"
@@ -16,11 +17,26 @@ import (
16
17
const (
17
18
unknown = "Unknown"
18
19
eks = "AWS (EKS)"
20
+ vmware = "VmWare"
19
21
gke = "Google (GKE)"
20
22
aks = "Azure (AKS)"
21
23
openshift = "Openshift"
24
+ rke = "RKE"
25
+ rke2 = "RKE2"
22
26
)
23
27
28
+ var kubernetesFlavourLabelsMapping = map [string ]string {
29
+ "eks.amazonaws.com/nodegroup" : eks ,
30
+ "cloud.google.com/gke-nodepool" : gke ,
31
+ "kubernetes.azure.com/agentpool" : aks ,
32
+ "node.openshift.io/os_id" : openshift ,
33
+ }
34
+
35
+ var kubernetesFlavourAnnotationsMapping = map [string ]string {
36
+ "rke.cattle.io/external-ip" : rke ,
37
+ "rke.cattle.io/internal-ip" : rke ,
38
+ }
39
+
24
40
// detectClusterInfo detects the Kubernetes version and cluster flavor
25
41
func detectClusterInfos (ctx context.Context , memberClusterMap map [string ]ConfigClient ) []KubernetesClusterUsageSnapshotProperties {
26
42
var clusterProperties []KubernetesClusterUsageSnapshotProperties
@@ -53,7 +69,7 @@ func getKubernetesClusterProperty(ctx context.Context, discoveryClient discovery
53
69
}
54
70
}
55
71
56
- kubernetesFlavour := detectKubernetesFlavour (ctx , uncachedClient )
72
+ kubernetesFlavour := detectKubernetesFlavour (ctx , uncachedClient , kubernetesAPIVersion )
57
73
58
74
property := KubernetesClusterUsageSnapshotProperties {
59
75
KubernetesClusterID : kubeClusterUUID ,
@@ -74,38 +90,46 @@ func getServerVersion(discoveryClient discovery.DiscoveryInterface) *version.Inf
74
90
}
75
91
76
92
// detectKubernetesFlavour detects the cloud provider based on node labels.
77
- func detectKubernetesFlavour (ctx context.Context , uncachedClient kubeclient.Reader ) string {
78
- nodes := & corev1.NodeList {}
79
- // Limit is propagated to the apiserver which propagates to etcd as it is. Thus, there is not a lot of
80
- // work required on the APIServer and ETCD to retrieve that node even in large clusters
81
- listOptions := & kubeclient.ListOptions {
82
- Limit : 1 ,
93
+ func detectKubernetesFlavour (ctx context.Context , uncachedClient kubeclient.Reader , kubeGitApiVersion string ) string {
94
+ // Check Kubernetes API version for known cloud providers
95
+ switch {
96
+ case strings .Contains (kubeGitApiVersion , "+rke2" ):
97
+ return rke2
98
+ case strings .Contains (kubeGitApiVersion , "-gke" ):
99
+ return gke
100
+ case strings .Contains (kubeGitApiVersion , "-eks" ):
101
+ return eks
102
+ case strings .Contains (kubeGitApiVersion , "+vmware" ):
103
+ return vmware
83
104
}
84
105
85
- if err := uncachedClient .List (ctx , nodes , listOptions ); err != nil {
106
+ // Limit is propagated to the apiserver which propagates to etcd as it is. Thus, there is not a lot of
107
+ // work required on the APIServer and ETCD to retrieve that node even in large clusters
108
+ nodes := & corev1.NodeList {}
109
+ if err := uncachedClient .List (ctx , nodes , & kubeclient.ListOptions {Limit : 1 }); err != nil {
86
110
Logger .Debugf ("Failed to fetch node to detect the cloud provider: %s" , err )
87
111
return unknown
88
112
}
89
-
90
113
if len (nodes .Items ) == 0 {
91
114
Logger .Debugf ("No nodes found, returning Unknown" )
92
115
return unknown
93
116
}
94
117
95
- labels := nodes .Items [0 ].Labels
118
+ node := nodes .Items [0 ]
119
+ labels , annotations := node .Labels , node .Annotations
96
120
97
- if _ , ok := labels ["eks.amazonaws.com/nodegroup" ]; ok {
98
- return eks
99
- }
100
- if _ , ok := labels ["cloud.google.com/gke-nodepool" ]; ok {
101
- return gke
102
- }
103
- if _ , ok := labels ["kubernetes.azure.com/agentpool" ]; ok {
104
- return aks
121
+ for key , provider := range kubernetesFlavourLabelsMapping {
122
+ if _ , exists := labels [key ]; exists {
123
+ return provider
124
+ }
105
125
}
106
- if _ , ok := labels ["node.openshift.io/os_id" ]; ok {
107
- return openshift
126
+
127
+ for key , provider := range kubernetesFlavourAnnotationsMapping {
128
+ if _ , exists := annotations [key ]; exists {
129
+ return provider
130
+ }
108
131
}
132
+
109
133
return unknown
110
134
}
111
135
0 commit comments