/
node_affinity_label_parser.go
71 lines (64 loc) · 2.54 KB
/
node_affinity_label_parser.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
// Copyright 2019 Google Inc. All Rights Reserved.
//
// 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 compute
import (
"strings"
daisy "github.com/GoogleCloudPlatform/compute-daisy"
computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
)
// ParseNodeAffinityLabels parses sole tenant affinities
// labels - array of strings with node affinity label info. Each label is of the following format:
// <key>,<operator>,<value>,<value2>... where <operator> can be one of: IN, NOT.
// For example: workload,IN,prod,test is a label with key 'workload' and values 'prod' and 'test'
func ParseNodeAffinityLabels(labels []string) ([]*compute.SchedulingNodeAffinity, []*computeBeta.SchedulingNodeAffinity, error) {
var nodeAffinities []*compute.SchedulingNodeAffinity
var nodeAffinitiesBeta []*computeBeta.SchedulingNodeAffinity
for _, label := range labels {
labelParts := strings.Split(label, ",")
if len(labelParts) < 3 {
return nil, nil, daisy.Errf(
"node affinity label `%v` should be of <key>,<operator>,<values> format", label)
}
key := strings.TrimSpace(labelParts[0])
if key == "" {
return nil, nil, daisy.Errf("affinity label key cannot be empty")
}
operator := strings.TrimSpace(labelParts[1])
if operator != "IN" && operator != "NOT_IN" && operator != "OPERATOR_UNSPECIFIED" {
return nil, nil, daisy.Errf(
"node affinity label operator should be one of: `IN`, `NOT_IN` or `OPERATOR_UNSPECIFIED`, but instead received `%v`",
operator,
)
}
values := labelParts[2:]
for i, value := range values {
values[i] = strings.TrimSpace(value)
if values[i] == "" {
return nil, nil, daisy.Errf("affinity label value cannot be empty")
}
}
nodeAffinities = append(nodeAffinities, &compute.SchedulingNodeAffinity{
Key: key,
Operator: operator,
Values: values,
})
nodeAffinitiesBeta = append(nodeAffinitiesBeta, &computeBeta.SchedulingNodeAffinity{
Key: key,
Operator: operator,
Values: values,
})
}
return nodeAffinities, nodeAffinitiesBeta, nil
}