/
affinity.go
executable file
·213 lines (174 loc) · 6.31 KB
/
affinity.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
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
"github.com/kubenext/lissio/pkg/view/component"
)
func printAffinity(podSpec corev1.PodSpec) (component.Component, error) {
ad := &affinityDescriber{podSpec: podSpec}
return ad.Create()
}
type affinityDescriber struct {
podSpec corev1.PodSpec
}
func (ad *affinityDescriber) Create() (*component.Table, error) {
cols := component.NewTableCols("Type", "Description")
table := component.NewTable("Affinities and Anti-Affinities",
"There are no affinities or anti-affinities!", cols)
if affinity := ad.podSpec.Affinity; affinity != nil {
for _, nodeAffinity := range ad.nodeAffinity(*affinity) {
table.Add(component.TableRow{
"Type": component.NewText("Node"),
"Description": nodeAffinity,
})
}
for _, podAffinity := range ad.podAffinity(*affinity) {
table.Add(component.TableRow{
"Type": component.NewText("Pod"),
"Description": podAffinity,
})
}
}
return table, nil
}
type podAffinityOptions struct {
weight int32
isRequired bool
anti bool
}
func (ad *affinityDescriber) podAffinity(affinity corev1.Affinity) []component.Component {
var items []component.Component
if podAffinity := affinity.PodAffinity; podAffinity != nil {
requiredOptions := podAffinityOptions{isRequired: true}
items = append(items,
ad.podAffinityTerms(
podAffinity.RequiredDuringSchedulingIgnoredDuringExecution,
requiredOptions)...)
for _, weighted := range podAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
preferredOptions := podAffinityOptions{weight: weighted.Weight}
items = append(items,
ad.podAffinityTerms(
[]corev1.PodAffinityTerm{weighted.PodAffinityTerm},
preferredOptions)...)
}
}
if podAntiAffinity := affinity.PodAntiAffinity; podAntiAffinity != nil {
requiredOptions := podAffinityOptions{isRequired: true, anti: true}
items = append(items,
ad.podAffinityTerms(
podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution,
requiredOptions)...)
for _, weighted := range podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
preferredOptions := podAffinityOptions{weight: weighted.Weight, anti: true}
items = append(items,
ad.podAffinityTerms(
[]corev1.PodAffinityTerm{weighted.PodAffinityTerm},
preferredOptions)...)
}
}
return items
}
func (ad *affinityDescriber) podAffinityTerms(terms []corev1.PodAffinityTerm, options podAffinityOptions) []component.Component {
var items []component.Component
for _, term := range terms {
var b strings.Builder
switch {
case options.isRequired && !options.anti:
b.WriteString("Schedule with pod")
case options.isRequired && options.anti:
b.WriteString("Do not schedule with pod")
case !options.isRequired && !options.anti:
b.WriteString("Prefer to schedule with pod")
case !options.isRequired && options.anti:
b.WriteString("Prefer to not schedule with pod")
}
if term.LabelSelector != nil {
matchLabels := printMatchLabels(term.LabelSelector.MatchLabels)
if matchLabels != "" {
b.WriteString(fmt.Sprintf(" labeled %s", matchLabels))
}
matchExpressions := printLabelSelectorRequirement(term.LabelSelector.MatchExpressions)
if matchExpressions != "" {
b.WriteString(fmt.Sprintf(" where %s", matchExpressions))
}
}
b.WriteString(fmt.Sprintf(" in topology %s.", term.TopologyKey))
if options.weight > 0 {
b.WriteString(fmt.Sprintf(" Weight %d.", options.weight))
}
items = append(items, component.NewText(b.String()))
}
return items
}
func (ad *affinityDescriber) nodeAffinity(affinity corev1.Affinity) []component.Component {
var items []component.Component
if nodeAffinity := affinity.NodeAffinity; nodeAffinity != nil {
for _, preferred := range nodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
options := nodeSelectorRequirementOptions{
weight: preferred.Weight,
}
items = append(items, ad.nodeSelectorTerms([]corev1.NodeSelectorTerm{preferred.Preference}, options)...)
}
if required := nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution; required != nil {
options := nodeSelectorRequirementOptions{
isRequired: true,
}
items = append(items, ad.nodeSelectorTerms(required.NodeSelectorTerms, options)...)
}
}
return items
}
type nodeSelectorRequirementOptions struct {
isRequired bool
weight int32
}
func (ad *affinityDescriber) nodeSelectorTerms(terms []corev1.NodeSelectorTerm, options nodeSelectorRequirementOptions) []component.Component {
var items []component.Component
for _, term := range terms {
items = append(items, ad.nodeSelectorRequirement("label", options, term.MatchExpressions)...)
items = append(items, ad.nodeSelectorRequirement("field", options, term.MatchFields)...)
}
return items
}
func (ad *affinityDescriber) nodeSelectorRequirement(itemType string, options nodeSelectorRequirementOptions, nodeSelectorRequirements []corev1.NodeSelectorRequirement) []component.Component {
preamble := "Prefer to schedule on nodes"
if options.isRequired {
preamble = "Schedule on nodes"
}
var items []component.Component
for _, nsr := range nodeSelectorRequirements {
var b strings.Builder
switch nsr.Operator {
case corev1.NodeSelectorOpIn:
b.WriteString(fmt.Sprintf("%s with %s %s with values %s.",
preamble, itemType, nsr.Key, strings.Join(nsr.Values, ", ")))
case corev1.NodeSelectorOpNotIn:
b.WriteString(fmt.Sprintf("%s with %s %s without values %s.",
preamble, itemType, nsr.Key, strings.Join(nsr.Values, ", ")))
case corev1.NodeSelectorOpExists:
b.WriteString(fmt.Sprintf("%s where %s %s exists.",
preamble, itemType, nsr.Key))
case corev1.NodeSelectorOpDoesNotExist:
b.WriteString(fmt.Sprintf("%s where %s %s does not exist.",
preamble, itemType, nsr.Key))
case corev1.NodeSelectorOpGt:
b.WriteString(fmt.Sprintf("%s where %s %s is greater than %s.",
preamble, itemType, nsr.Key, nsr.Values[0]))
case corev1.NodeSelectorOpLt:
b.WriteString(fmt.Sprintf("%s where %s %s is less than %s.",
preamble, itemType, nsr.Key, nsr.Values[0]))
default:
continue
}
if options.weight > 0 {
b.WriteString(fmt.Sprintf(" Weight %d.", options.weight))
}
items = append(items, component.NewText(b.String()))
}
return items
}