-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathclusterapi_utils.go
286 lines (237 loc) · 9.88 KB
/
clusterapi_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clusterapi
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
deprecatedNodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size"
deprecatedNodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size"
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name"
cpuKey = "capacity.cluster-autoscaler.kubernetes.io/cpu"
memoryKey = "capacity.cluster-autoscaler.kubernetes.io/memory"
gpuTypeKey = "capacity.cluster-autoscaler.kubernetes.io/gpu-type"
gpuCountKey = "capacity.cluster-autoscaler.kubernetes.io/gpu-count"
maxPodsKey = "capacity.cluster-autoscaler.kubernetes.io/maxPods"
)
var (
// clusterNameLabel is the label applied to objects(Machine, MachineSet, MachineDeployment)
// to identify which cluster they are owned by. Because the label can be
// affected by the CAPI_GROUP environment variable, it is initialized here.
clusterNameLabel = getClusterNameLabel()
// errMissingMinAnnotation is the error returned when a
// machine set does not have an annotation keyed by
// nodeGroupMinSizeAnnotationKey.
errMissingMinAnnotation = errors.New("missing min annotation")
// errMissingMaxAnnotation is the error returned when a
// machine set does not have an annotation keyed by
// nodeGroupMaxSizeAnnotationKey.
errMissingMaxAnnotation = errors.New("missing max annotation")
// errInvalidMinAnnotationValue is the error returned when a
// machine set has a non-integral min annotation value.
errInvalidMinAnnotation = errors.New("invalid min annotation")
// errInvalidMaxAnnotationValue is the error returned when a
// machine set has a non-integral max annotation value.
errInvalidMaxAnnotation = errors.New("invalid max annotation")
// machineDeleteAnnotationKey is the annotation used by cluster-api to indicate
// that a machine should be deleted. Because this key can be affected by the
// CAPI_GROUP env variable, it is initialized here.
machineDeleteAnnotationKey = getMachineDeleteAnnotationKey()
// machineAnnotationKey is the annotation used by the cluster-api on Node objects
// to specify the name of the related Machine object. Because this can be affected
// by the CAPI_GROUP env variable, it is initialized here.
machineAnnotationKey = getMachineAnnotationKey()
// nodeGroupMinSizeAnnotationKey and nodeGroupMaxSizeAnnotationKey are the keys
// used in MachineSet and MachineDeployment annotations to specify the limits
// for the node group. Because the keys can be affected by the CAPI_GROUP env
// variable, they are initialized here.
nodeGroupMinSizeAnnotationKey = getNodeGroupMinSizeAnnotationKey()
nodeGroupMaxSizeAnnotationKey = getNodeGroupMaxSizeAnnotationKey()
zeroQuantity = resource.MustParse("0")
)
type normalizedProviderID string
// minSize returns the minimum value encoded in the annotations keyed
// by nodeGroupMinSizeAnnotationKey. Returns errMissingMinAnnotation
// if the annotation doesn't exist or errInvalidMinAnnotation if the
// value is not of type int.
func minSize(annotations map[string]string) (int, error) {
val, found := annotations[nodeGroupMinSizeAnnotationKey]
if !found {
return 0, errMissingMinAnnotation
}
i, err := strconv.Atoi(val)
if err != nil {
return 0, errors.Wrapf(err, "%s", errInvalidMinAnnotation)
}
return i, nil
}
// maxSize returns the maximum value encoded in the annotations keyed
// by nodeGroupMaxSizeAnnotationKey. Returns errMissingMaxAnnotation
// if the annotation doesn't exist or errInvalidMaxAnnotation if the
// value is not of type int.
func maxSize(annotations map[string]string) (int, error) {
val, found := annotations[nodeGroupMaxSizeAnnotationKey]
if !found {
return 0, errMissingMaxAnnotation
}
i, err := strconv.Atoi(val)
if err != nil {
return 0, errors.Wrapf(err, "%s", errInvalidMaxAnnotation)
}
return i, nil
}
func parseScalingBounds(annotations map[string]string) (int, int, error) {
minSize, err := minSize(annotations)
if err != nil && err != errMissingMinAnnotation {
return 0, 0, err
}
if minSize < 0 {
return 0, 0, errInvalidMinAnnotation
}
maxSize, err := maxSize(annotations)
if err != nil && err != errMissingMaxAnnotation {
return 0, 0, err
}
if maxSize < 0 {
return 0, 0, errInvalidMaxAnnotation
}
if maxSize < minSize {
return 0, 0, errInvalidMaxAnnotation
}
return minSize, maxSize, nil
}
func getOwnerForKind(u *unstructured.Unstructured, kind string) *metav1.OwnerReference {
for _, ref := range u.GetOwnerReferences() {
if ref.Kind == kind && ref.Name != "" {
return ref.DeepCopy()
}
}
return nil
}
func machineOwnerRef(machine *unstructured.Unstructured) *metav1.OwnerReference {
return getOwnerForKind(machine, machineSetKind)
}
func machineSetOwnerRef(machineSet *unstructured.Unstructured) *metav1.OwnerReference {
return getOwnerForKind(machineSet, machineDeploymentKind)
}
func machineSetHasMachineDeploymentOwnerRef(machineSet *unstructured.Unstructured) bool {
return machineSetOwnerRef(machineSet) != nil
}
// normalizedProviderString splits s on '/' returning everything after
// the last '/'.
func normalizedProviderString(s string) normalizedProviderID {
split := strings.Split(s, "/")
return normalizedProviderID(split[len(split)-1])
}
func scaleFromZeroAnnotationsEnabled(annotations map[string]string) bool {
cpu := annotations[cpuKey]
mem := annotations[memoryKey]
if cpu != "" && mem != "" {
return true
}
return false
}
func parseKey(annotations map[string]string, key string) (resource.Quantity, error) {
if val, exists := annotations[key]; exists && val != "" {
return resource.ParseQuantity(val)
}
return zeroQuantity.DeepCopy(), nil
}
func parseIntKey(annotations map[string]string, key string) (resource.Quantity, error) {
if val, exists := annotations[key]; exists && val != "" {
valInt, err := strconv.ParseInt(val, 10, 0)
if err != nil {
return zeroQuantity.DeepCopy(), fmt.Errorf("value %q from annotation %q expected to be an integer: %v", val, key, err)
}
return *resource.NewQuantity(valInt, resource.DecimalSI), nil
}
return zeroQuantity.DeepCopy(), nil
}
func parseCPUCapacity(annotations map[string]string) (resource.Quantity, error) {
return parseKey(annotations, cpuKey)
}
func parseMemoryCapacity(annotations map[string]string) (resource.Quantity, error) {
return parseKey(annotations, memoryKey)
}
func parseGPUCount(annotations map[string]string) (resource.Quantity, error) {
return parseIntKey(annotations, gpuCountKey)
}
// The GPU type is not currently considered by the autoscaler when planning
// expansion, but most likely will be in the future. This method is being added
// in expectation of that arrival.
// see https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/utils/gpu/gpu.go
func parseGPUType(annotations map[string]string) string {
if val, found := annotations[gpuTypeKey]; found {
return val
}
return ""
}
func parseMaxPodsCapacity(annotations map[string]string) (resource.Quantity, error) {
return parseIntKey(annotations, maxPodsKey)
}
func clusterNameFromResource(r *unstructured.Unstructured) string {
// Use Spec.ClusterName if defined (only available on v1alpha3+ types)
clusterName, found, err := unstructured.NestedString(r.Object, "spec", "clusterName")
if err != nil {
return ""
}
if found {
return clusterName
}
// Fallback to value of clusterNameLabel
if clusterName, ok := r.GetLabels()[clusterNameLabel]; ok {
return clusterName
}
return ""
}
// getNodeGroupMinSizeAnnotationKey returns the key that is used for the
// node group minimum size annotation. This function is needed because the user can
// change the default group name by using the CAPI_GROUP environment variable.
func getNodeGroupMinSizeAnnotationKey() string {
key := fmt.Sprintf("%s/cluster-api-autoscaler-node-group-min-size", getCAPIGroup())
return key
}
// getNodeGroupMaxSizeAnnotationKey returns the key that is used for the
// node group maximum size annotation. This function is needed because the user can
// change the default group name by using the CAPI_GROUP environment variable.
func getNodeGroupMaxSizeAnnotationKey() string {
key := fmt.Sprintf("%s/cluster-api-autoscaler-node-group-max-size", getCAPIGroup())
return key
}
// getMachineDeleteAnnotationKey returns the key that is used by cluster-api for marking
// machines to be deleted. This function is needed because the user can change the default
// group name by using the CAPI_GROUP environment variable.
func getMachineDeleteAnnotationKey() string {
key := fmt.Sprintf("%s/delete-machine", getCAPIGroup())
return key
}
// getMachineAnnotationKey returns the key that is used by cluster-api for annotating
// nodes with their related machine objects. This function is needed because the user can change
// the default group name by using the CAPI_GROUP environment variable.
func getMachineAnnotationKey() string {
key := fmt.Sprintf("%s/machine", getCAPIGroup())
return key
}
// getClusterNameLabel returns the key that is used by cluster-api for labeling
// which cluster an object belongs to. This function is needed because the user can change
// the default group name by using the CAPI_GROUP environment variable.
func getClusterNameLabel() string {
key := fmt.Sprintf("%s/cluster-name", getCAPIGroup())
return key
}