Skip to content

Commit a876b83

Browse files
committed
Add additional information when reporting platform as other
1 parent b8f72fb commit a876b83

File tree

3 files changed

+116
-68
lines changed

3 files changed

+116
-68
lines changed

internal/mode/static/telemetry/collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func collectClusterInformation(ctx context.Context, k8sClient client.Reader) (cl
309309
return clusterInformation{}, fmt.Errorf("failed to collect cluster information: %w", err)
310310
}
311311

312-
clusterInfo.Platform = collectK8sPlatform(node, namespaces)
312+
clusterInfo.Platform = getPlatform(node, namespaces)
313313

314314
return clusterInfo, nil
315315
}

internal/mode/static/telemetry/collector_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,60 @@ var _ = Describe("Collector", Ordered, func() {
537537
})
538538

539539
When("platform is none of the above", func() {
540-
It("marks the platform as 'other'", func() {
540+
It("marks the platform as 'other: ' with whatever was in the providerID's providerName", func() {
541+
nodes := &v1.NodeList{
542+
Items: []v1.Node{
543+
{
544+
ObjectMeta: metav1.ObjectMeta{
545+
Name: "node1",
546+
},
547+
Spec: v1.NodeSpec{
548+
ProviderID: "other-cloud-provider://test-here",
549+
},
550+
Status: v1.NodeStatus{
551+
NodeInfo: v1.NodeSystemInfo{
552+
KubeletVersion: "v1.29.2",
553+
},
554+
},
555+
},
556+
},
557+
}
558+
559+
k8sClientReader.ListCalls(createListCallsFunc(nodes))
560+
expData.K8sVersion = "1.29.2"
561+
expData.K8sPlatform = "other: other-cloud-provider"
562+
563+
data, err := dataCollector.Collect(ctx)
564+
565+
Expect(err).To(BeNil())
566+
Expect(expData).To(Equal(data))
567+
})
568+
It("marks the platform as 'other: ' when providerID is empty", func() {
569+
nodes := &v1.NodeList{
570+
Items: []v1.Node{
571+
{
572+
ObjectMeta: metav1.ObjectMeta{
573+
Name: "node1",
574+
},
575+
Status: v1.NodeStatus{
576+
NodeInfo: v1.NodeSystemInfo{
577+
KubeletVersion: "v1.29.2",
578+
},
579+
},
580+
},
581+
},
582+
}
583+
584+
k8sClientReader.ListCalls(createListCallsFunc(nodes))
585+
expData.K8sVersion = "1.29.2"
586+
expData.K8sPlatform = "other: "
587+
588+
data, err := dataCollector.Collect(ctx)
589+
590+
Expect(err).To(BeNil())
591+
Expect(expData).To(Equal(data))
592+
})
593+
It("marks the platform as 'other: ' when providerID is missing '://' separator", func() {
541594
nodes := &v1.NodeList{
542595
Items: []v1.Node{
543596
{
@@ -558,7 +611,7 @@ var _ = Describe("Collector", Ordered, func() {
558611

559612
k8sClientReader.ListCalls(createListCallsFunc(nodes))
560613
expData.K8sVersion = "1.29.2"
561-
expData.K8sPlatform = "other"
614+
expData.K8sPlatform = "other: "
562615

563616
data, err := dataCollector.Collect(ctx)
564617

internal/mode/static/telemetry/platform.go

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@ import (
66
v1 "k8s.io/api/core/v1"
77
)
88

9+
type k8sState struct {
10+
node v1.Node
11+
namespaces v1.NamespaceList
12+
}
13+
14+
type platformExtractor func(k8sState) (string, bool)
15+
16+
func buildProviderIDExtractor(id string, platform string) platformExtractor {
17+
return func(state k8sState) (string, bool) {
18+
if strings.HasPrefix(state.node.Spec.ProviderID, id) {
19+
return platform, true
20+
}
21+
return "", false
22+
}
23+
}
24+
925
const (
10-
openshiftIdentifier = "node.openshift.io/os_id"
11-
k3sIdentifier = "k3s"
12-
awsIdentifier = "aws"
1326
gkeIdentifier = "gce"
27+
awsIdentifier = "aws"
1428
azureIdentifier = "azure"
1529
kindIdentifier = "kind"
30+
k3sIdentifier = "k3s"
31+
openshiftIdentifier = "node.openshift.io/os_id"
1632
rancherIdentifier = "cattle-system"
1733

1834
platformGKE = "gke"
@@ -22,87 +38,66 @@ const (
2238
platformK3S = "k3s"
2339
platformOpenShift = "openshift"
2440
platformRancher = "rancher"
25-
platformOther = "other"
2641
)
2742

28-
func collectK8sPlatform(node v1.Node, namespaces v1.NamespaceList) string {
29-
if result := isMultiplePlatforms(node, namespaces); result != "" {
30-
return result
31-
}
32-
33-
if isAWSPlatform(node) {
34-
return platformAWS
35-
}
36-
if isGKEPlatform(node) {
37-
return platformGKE
38-
}
39-
if isAzurePlatform(node) {
40-
return platformAzure
41-
}
42-
if isKindPlatform(node) {
43-
return platformKind
44-
}
45-
if isK3SPlatform(node) {
46-
return platformK3S
47-
}
43+
var multiDistributionPlatformExtractors = []platformExtractor{
44+
rancherExtractor,
45+
openShiftExtractor,
46+
}
4847

49-
return platformOther
48+
var platformExtractors = []platformExtractor{
49+
buildProviderIDExtractor(gkeIdentifier, platformGKE),
50+
buildProviderIDExtractor(awsIdentifier, platformAWS),
51+
buildProviderIDExtractor(azureIdentifier, platformAzure),
52+
buildProviderIDExtractor(kindIdentifier, platformKind),
53+
buildProviderIDExtractor(k3sIdentifier, platformK3S),
5054
}
5155

52-
// isMultiplePlatforms checks for platforms that run on other platforms. e.g. Rancher on K3s.
53-
func isMultiplePlatforms(node v1.Node, namespaces v1.NamespaceList) string {
54-
if isRancherPlatform(namespaces) {
55-
return platformRancher
56+
func getPlatform(node v1.Node, namespaces v1.NamespaceList) string {
57+
state := k8sState{
58+
node: node,
59+
namespaces: namespaces,
5660
}
5761

58-
if isOpenshiftPlatform(node) {
59-
return platformOpenShift
62+
// must be run before providerIDPlatformExtractors as these platforms
63+
// may have multiple platforms e.g. Rancher on K3S, and we want to record the
64+
// higher level platform.
65+
for _, extractor := range multiDistributionPlatformExtractors {
66+
if platform, ok := extractor(state); ok {
67+
return platform
68+
}
6069
}
6170

62-
return ""
63-
}
64-
65-
// For each of these, if we want to we can do both check the providerID AND check labels/annotations,
66-
// I'm not too sure why we would want to do BOTH.
67-
//
68-
// I think doing both would add a greater certainty of a specific platform, however will potentially add to upkeep
69-
// where if either the label/annotation or providerID changes it will mess this up and may group more clusters in
70-
// the "Other" platform if they messed with any of the node labels/annotations.
71-
72-
func isOpenshiftPlatform(node v1.Node) bool {
73-
// openshift platform won't show up in node's ProviderID
74-
value, ok := node.Labels[openshiftIdentifier]
75-
76-
return ok && value != ""
77-
}
78-
79-
func isK3SPlatform(node v1.Node) bool {
80-
return strings.HasPrefix(node.Spec.ProviderID, k3sIdentifier)
81-
}
71+
for _, extractor := range platformExtractors {
72+
if platform, ok := extractor(state); ok {
73+
return platform
74+
}
75+
}
8276

83-
func isAWSPlatform(node v1.Node) bool {
84-
return strings.HasPrefix(node.Spec.ProviderID, awsIdentifier)
85-
}
77+
var providerName string
78+
if prefix, _, found := strings.Cut(node.Spec.ProviderID, "://"); found {
79+
providerName = prefix
80+
}
8681

87-
func isGKEPlatform(node v1.Node) bool {
88-
return strings.HasPrefix(node.Spec.ProviderID, gkeIdentifier)
82+
return "other: " + providerName
8983
}
9084

91-
func isAzurePlatform(node v1.Node) bool {
92-
return strings.HasPrefix(node.Spec.ProviderID, azureIdentifier)
93-
}
85+
func openShiftExtractor(state k8sState) (string, bool) {
86+
// openshift platform won't show up in node's ProviderID
87+
if value, ok := state.node.Labels[openshiftIdentifier]; ok && value != "" {
88+
return platformOpenShift, true
89+
}
9490

95-
func isKindPlatform(node v1.Node) bool {
96-
return strings.HasPrefix(node.Spec.ProviderID, kindIdentifier)
91+
return "", false
9792
}
9893

99-
func isRancherPlatform(namespaces v1.NamespaceList) bool {
94+
func rancherExtractor(state k8sState) (string, bool) {
10095
// rancher platform won't show up in the node's ProviderID
101-
for _, ns := range namespaces.Items {
96+
for _, ns := range state.namespaces.Items {
10297
if ns.Name == rancherIdentifier {
103-
return true
98+
return platformRancher, true
10499
}
105100
}
106101

107-
return false
102+
return "", false
108103
}

0 commit comments

Comments
 (0)