Skip to content

Commit 92319e7

Browse files
committed
Introduce scale from zero annotations module
1 parent 2cc7fcf commit 92319e7

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

pkg/util/machineset/util.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package util
2+
3+
import (
4+
"errors"
5+
)
6+
7+
const (
8+
// Deprecated OpenShift introduced annotations for scaling from zero.
9+
CpuKeyDeprecated = "machine.openshift.io/vCPU"
10+
MemoryKeyDeprecated = "machine.openshift.io/memoryMb"
11+
GpuCountKeyDeprecated = "machine.openshift.io/GPU"
12+
MaxPodsKeyDeprecated = "machine.openshift.io/maxPods"
13+
14+
// Upstream preferred annotations for scaling from zero.
15+
CpuKey = "capacity.cluster-autoscaler.kubernetes.io/cpu"
16+
MemoryKey = "capacity.cluster-autoscaler.kubernetes.io/memory"
17+
GpuTypeKey = "capacity.cluster-autoscaler.kubernetes.io/gpu-type"
18+
GpuCountKey = "capacity.cluster-autoscaler.kubernetes.io/gpu-count"
19+
MaxPodsKey = "capacity.cluster-autoscaler.kubernetes.io/maxPods"
20+
21+
GpuNvidiaType = "nvidia.com/gpu"
22+
)
23+
24+
// This module's intended use is to perform changes and basic checks
25+
// to scale from zero annotations on MachineSets. Example use of the module:
26+
//
27+
// ann := machineSet.Annotations
28+
// ann = SetCpuAnnotation(ann, 10)
29+
// ann = SetMemoryAnnotation(ann, 10)
30+
// if isScaleFromZeroAnnotations := HasScaleFromZeroAnnotationsEnabled(ann); !isScaleFromZeroAnnotations { ... }
31+
32+
var (
33+
// errAnnotationKeyNotFound signals to the user that the selected key was not found in the MachineSet's annotations
34+
errAnnotationKeyNotFound = errors.New("could not find the selected annotation key in the MachineSet's annotations")
35+
)
36+
37+
// ParseMachineSetAnnotationKey parses MachineSet's annotations and look for a key
38+
func ParseMachineSetAnnotationKey(annotations map[string]string, key string) (string, error) {
39+
if val, exists := annotations[key]; exists && key != "" {
40+
return val, nil
41+
}
42+
43+
return "", errAnnotationKeyNotFound
44+
}
45+
46+
// HasScaleFromZeroAnnotationsEnabled checks that cpu and memory upstream annotations are set in a MachineSet.
47+
func HasScaleFromZeroAnnotationsEnabled(annotations map[string]string) bool {
48+
cpu := annotations[CpuKey]
49+
mem := annotations[MemoryKey]
50+
51+
if cpu != "" && mem != "" {
52+
return true
53+
}
54+
return false
55+
}
56+
57+
// SetCpuAnnotation sets a value for a cpu key in the annotations of a MachineSet.
58+
func SetCpuAnnotation(annotations map[string]string, value string) map[string]string {
59+
annotations[CpuKey] = value
60+
61+
return annotations
62+
}
63+
64+
// SetMemoryAnnotation sets a value for a mempory key in the annotations of a MachineSet.
65+
func SetMemoryAnnotation(annotations map[string]string, value string) map[string]string {
66+
annotations[MemoryKey] = value
67+
68+
return annotations
69+
}
70+
71+
// SetGpuCountAnnotation sets a value for a gpu count key in the annotations of a MachineSet.
72+
func SetGpuCountAnnotation(annotations map[string]string, value string) map[string]string {
73+
annotations[GpuCountKey] = value
74+
75+
return annotations
76+
}
77+
78+
// SetGpuTypeAnnotation sets a value for gpu type in the annotations of a MachineSet.
79+
// Currently, we only support nvidia as a gpu type.
80+
func SetGpuTypeAnnotation(annotations map[string]string, _ string) map[string]string {
81+
// TODO: Once we introduce proper gpu types, this needs to be changed.
82+
annotations[GpuTypeKey] = GpuNvidiaType
83+
84+
return annotations
85+
}
86+
87+
// SetMaxPodsAnnotation sets a value for a maxPods key in the annotations of a MachineSet.
88+
func SetMaxPodsAnnotation(annotations map[string]string, value string) map[string]string {
89+
annotations[MaxPodsKey] = value
90+
91+
return annotations
92+
}

0 commit comments

Comments
 (0)