@@ -6,13 +6,29 @@ import (
6
6
v1 "k8s.io/api/core/v1"
7
7
)
8
8
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
+
9
25
const (
10
- openshiftIdentifier = "node.openshift.io/os_id"
11
- k3sIdentifier = "k3s"
12
- awsIdentifier = "aws"
13
26
gkeIdentifier = "gce"
27
+ awsIdentifier = "aws"
14
28
azureIdentifier = "azure"
15
29
kindIdentifier = "kind"
30
+ k3sIdentifier = "k3s"
31
+ openshiftIdentifier = "node.openshift.io/os_id"
16
32
rancherIdentifier = "cattle-system"
17
33
18
34
platformGKE = "gke"
@@ -22,87 +38,66 @@ const (
22
38
platformK3S = "k3s"
23
39
platformOpenShift = "openshift"
24
40
platformRancher = "rancher"
25
- platformOther = "other"
26
41
)
27
42
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
+ }
48
47
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 ),
50
54
}
51
55
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 ,
56
60
}
57
61
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
+ }
60
69
}
61
70
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
+ }
82
76
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
+ }
86
81
87
- func isGKEPlatform (node v1.Node ) bool {
88
- return strings .HasPrefix (node .Spec .ProviderID , gkeIdentifier )
82
+ return "other: " + providerName
89
83
}
90
84
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
+ }
94
90
95
- func isKindPlatform (node v1.Node ) bool {
96
- return strings .HasPrefix (node .Spec .ProviderID , kindIdentifier )
91
+ return "" , false
97
92
}
98
93
99
- func isRancherPlatform ( namespaces v1. NamespaceList ) bool {
94
+ func rancherExtractor ( state k8sState ) ( string , bool ) {
100
95
// rancher platform won't show up in the node's ProviderID
101
- for _ , ns := range namespaces .Items {
96
+ for _ , ns := range state . namespaces .Items {
102
97
if ns .Name == rancherIdentifier {
103
- return true
98
+ return platformRancher , true
104
99
}
105
100
}
106
101
107
- return false
102
+ return "" , false
108
103
}
0 commit comments