@@ -18,8 +18,11 @@ package clusterapi
18
18
19
19
import (
20
20
"fmt"
21
+ "k8s.io/klog/v2"
22
+ "os"
21
23
"strconv"
22
24
"strings"
25
+ "sync"
23
26
24
27
"github.com/pkg/errors"
25
28
"k8s.io/apimachinery/pkg/api/resource"
@@ -36,6 +39,20 @@ const (
36
39
maxPodsKey = "capacity.cluster-autoscaler.kubernetes.io/maxPods"
37
40
taintsKey = "capacity.cluster-autoscaler.kubernetes.io/taints"
38
41
labelsKey = "capacity.cluster-autoscaler.kubernetes.io/labels"
42
+ // UnknownArch is used if the Architecture is Unknown
43
+ UnknownArch SystemArchitecture = ""
44
+ // Amd64 is used if the Architecture is x86_64
45
+ Amd64 SystemArchitecture = "amd64"
46
+ // Arm64 is used if the Architecture is ARM64
47
+ Arm64 SystemArchitecture = "arm64"
48
+ // Ppc64le is used if the Architecture is ppc64le
49
+ Ppc64le SystemArchitecture = "ppc64le"
50
+ // S390x is used if the Architecture is s390x
51
+ S390x SystemArchitecture = "s390x"
52
+ // DefaultArch should be used as a fallback if not passed by the environment via the --scale-up-from-zero-default-arch
53
+ DefaultArch = Amd64
54
+ // scaleUpFromZeroDefaultEnvVar is the name of the env var for the default architecture
55
+ scaleUpFromZeroDefaultArchEnvVar = "CAPI_SCALE_ZERO_DEFAULT_ARCH"
39
56
)
40
57
41
58
var (
@@ -79,10 +96,25 @@ var (
79
96
nodeGroupMinSizeAnnotationKey = getNodeGroupMinSizeAnnotationKey ()
80
97
nodeGroupMaxSizeAnnotationKey = getNodeGroupMaxSizeAnnotationKey ()
81
98
zeroQuantity = resource .MustParse ("0" )
99
+
100
+ systemArchitecture * SystemArchitecture
101
+ once sync.Once
82
102
)
83
103
84
104
type normalizedProviderID string
85
105
106
+ // SystemArchitecture represents a CPU architecture (e.g., amd64, arm64, ppc64le, s390x).
107
+ // It is used to determine the default architecture to use when building the nodes templates for scaling up from zero
108
+ // by some cloud providers. This code is the same as the GCE implementation at
109
+ // https://github.com/kubernetes/autoscaler/blob/3852f352d96b8763292a9122163c1152dfedec55/cluster-autoscaler/cloudprovider/gce/templates.go#L611-L657
110
+ // which is kept to allow for a smooth transition to this package, once the GCE team is ready to use it.
111
+ type SystemArchitecture string
112
+
113
+ // Name returns the string value for SystemArchitecture
114
+ func (s SystemArchitecture ) Name () string {
115
+ return string (s )
116
+ }
117
+
86
118
// minSize returns the minimum value encoded in the annotations keyed
87
119
// by nodeGroupMinSizeAnnotationKey. Returns errMissingMinAnnotation
88
120
// if the annotation doesn't exist or errInvalidMinAnnotation if the
@@ -279,3 +311,37 @@ func getClusterNameLabel() string {
279
311
key := fmt .Sprintf ("%s/cluster-name" , getCAPIGroup ())
280
312
return key
281
313
}
314
+
315
+ // SystemArchitectureFromString parses a string to SystemArchitecture. Returns UnknownArch if the string doesn't represent a
316
+ // valid architecture.
317
+ func SystemArchitectureFromString (arch string ) SystemArchitecture {
318
+ switch arch {
319
+ case string (Arm64 ):
320
+ return Arm64
321
+ case string (Amd64 ):
322
+ return Amd64
323
+ case string (Ppc64le ):
324
+ return Ppc64le
325
+ case string (S390x ):
326
+ return S390x
327
+ default :
328
+ return UnknownArch
329
+ }
330
+ }
331
+
332
+ // GetDefaultScaleFromZeroArchitecture returns the SystemArchitecture from the environment variable
333
+ // CAPI_SCALE_ZERO_DEFAULT_ARCH or DefaultArch if the variable is set to an invalid value.
334
+ func GetDefaultScaleFromZeroArchitecture () SystemArchitecture {
335
+ once .Do (func () {
336
+ archStr := os .Getenv (scaleUpFromZeroDefaultArchEnvVar )
337
+ arch := SystemArchitectureFromString (archStr )
338
+ klog .V (5 ).Infof ("the default scale from zero architecture value is set to %s (%s)" , scaleUpFromZeroDefaultArchEnvVar , archStr , arch .Name ())
339
+ if arch == UnknownArch {
340
+ arch = DefaultArch
341
+ klog .Errorf ("Unrecognized architecture '%s', falling back to %s" ,
342
+ scaleUpFromZeroDefaultArchEnvVar , DefaultArch .Name ())
343
+ }
344
+ systemArchitecture = & arch
345
+ })
346
+ return * systemArchitecture
347
+ }
0 commit comments