/
mc.go
323 lines (281 loc) · 9.53 KB
/
mc.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package operator
import (
"encoding/json"
"fmt"
"reflect"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"github.com/openshift/cluster-node-tuning-operator/pkg/util"
ign3error "github.com/coreos/ignition/v2/config/shared/errors"
ign3 "github.com/coreos/ignition/v2/config/v3_2"
ign3types "github.com/coreos/ignition/v2/config/v3_2/types"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)
const (
GeneratedByControllerVersionAnnotationKey string = "tuned.openshift.io/generated-by-controller-version"
MachineConfigPrefix string = "50-nto"
)
func newMachineConfig(name string, annotations map[string]string, labels map[string]string, kernelArguments []string,
ignFiles []ign3types.File, ignUnits []ign3types.Unit) *mcfgv1.MachineConfig {
if labels == nil {
labels = map[string]string{}
}
if annotations == nil {
annotations = map[string]string{}
}
ignTypesCfg := ign3types.Config{
Ignition: ign3types.Ignition{
Version: ign3types.MaxVersion.String(),
},
}
if ignFiles != nil {
ignTypesCfg.Storage = ign3types.Storage{Files: ignFiles}
}
if ignUnits != nil {
ignTypesCfg.Systemd = ign3types.Systemd{Units: ignUnits}
}
rawNewIgnCfg, err := json.Marshal(ignTypesCfg)
if err != nil {
// This should never happen
panic(err)
}
return &mcfgv1.MachineConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: mcfgv1.SchemeGroupVersion.String(),
Kind: "MachineConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: annotations,
Labels: labels,
},
Spec: mcfgv1.MachineConfigSpec{
Config: runtime.RawExtension{
Raw: rawNewIgnCfg,
},
KernelArguments: kernelArguments,
},
}
}
// IgnParseWrapper parses rawIgn for V3.2 ignition config and returns
// a V3.2 Config or an error.
func ignParseWrapper(rawIgn []byte) (interface{}, error) {
ignCfgV3_2, rptV3_2, errV3_2 := ign3.Parse(rawIgn)
if errV3_2 == nil && !rptV3_2.IsFatal() {
return ignCfgV3_2, nil
}
if errV3_2.Error() == ign3error.ErrUnknownVersion.Error() {
// NTO handles NTO-created MachineConfigs only. The first Ignition version
// used was 2.2.0 and only Ignition version was provided by the Ignition
// config. Later a switch to 3.1.0 was made as we started support for
// Storage/Systemd types. As of 3.2.0 it is safe to ignore this error and
// provide Ignition config with only Ignition version without pulling old
// ignition dependencies for unneeded parsing. Existing Ignition configs
// will automatically be converted to the latest NTO-used Ignition version.
ignTypesCfg := ign3types.Config{
Ignition: ign3types.Ignition{
Version: ign3types.MaxVersion.String(),
},
}
return ignTypesCfg, nil
}
return ign3types.Config{}, fmt.Errorf("parsing Ignition config spec v3.2 failed with error: %v\nReport: %v", errV3_2, rptV3_2)
}
func parseAndConvertConfig(rawIgn []byte) (ign3types.Config, error) {
ignconfigi, err := ignParseWrapper(rawIgn)
if err != nil {
return ign3types.Config{}, fmt.Errorf("failed to parse Ignition config: %v", err)
}
switch typedConfig := ignconfigi.(type) {
case ign3types.Config:
return ignconfigi.(ign3types.Config), nil
default:
return ign3types.Config{}, fmt.Errorf("unexpected type for ignition config: %v", typedConfig)
}
}
func ignEqual(mcOld, mcNew *mcfgv1.MachineConfig) (bool, error) {
ignOld, err := parseAndConvertConfig(mcOld.Spec.Config.Raw)
if err != nil {
return false, fmt.Errorf("parsing old Ignition config failed with error: %v", err)
}
ignNew, err := parseAndConvertConfig(mcNew.Spec.Config.Raw)
if err != nil {
return false, fmt.Errorf("parsing new Ignition config failed with error: %v", err)
}
return reflect.DeepEqual(ignOld.Storage.Files, ignNew.Storage.Files) && reflect.DeepEqual(ignOld.Systemd.Units, ignNew.Systemd.Units), nil
}
func getMachineConfigNameForPools(pools []*mcfgv1.MachineConfigPool) string {
var (
sb strings.Builder
sbPrimary strings.Builder
)
sb.WriteString(MachineConfigPrefix)
for _, pool := range pools {
if pool == nil {
continue
}
sb.WriteString("-")
if pool.Name == "master" || pool.Name == "worker" {
sbPrimary.WriteString(pool.ObjectMeta.Name)
} else {
// This is a custom pool; a node can be a member of only one custom pool => return its name.
sb.WriteString(pool.ObjectMeta.Name)
return sb.String()
}
}
sb.WriteString(sbPrimary.String())
return sb.String()
}
func (pc *ProfileCalculator) getMachineCountForMachineConfigPool(mcpName string) (int32, error) {
mcp, err := pc.listers.MachineConfigPools.Get(mcpName)
if err != nil {
return 0, err
}
return mcp.Status.MachineCount, nil
}
// getPoolsForMachineConfigLabels chooses the MachineConfigPools that use MachineConfigs with labels 'mcLabels'.
// Errors are only returned in cases that warrant event reques (e.g. a failure to list k8s objects).
func (pc *ProfileCalculator) getPoolsForMachineConfigLabels(mcLabels map[string]string) ([]*mcfgv1.MachineConfigPool, error) {
if len(mcLabels) == 0 {
return nil, nil
}
mcpList, err := pc.listers.MachineConfigPools.List(labels.Everything())
if err != nil {
return nil, err
}
var pools []*mcfgv1.MachineConfigPool
for _, p := range mcpList {
selector, err := metav1.LabelSelectorAsSelector(p.Spec.MachineConfigSelector)
if err != nil {
klog.Errorf("invalid label selector %s: %v", util.ObjectInfo(selector), err)
return nil, nil
}
// A pool with a nil or empty selector matches nothing.
if selector.Empty() || !selector.Matches(labels.Set(mcLabels)) {
continue
}
pools = append(pools, p)
}
return pools, nil
}
// getPoolsForNode chooses the MachineConfigPools that should be used for a given node.
// It disambiguates in the case where e.g. a node has both master/worker roles applied,
// and where a custom role may be used. It returns a slice of all the pools the node belongs to.
// Errors are only returned in cases that warrant event reques (e.g. a failure to list k8s objects).
func (pc *ProfileCalculator) getPoolsForNode(node *corev1.Node) ([]*mcfgv1.MachineConfigPool, error) {
pl, err := pc.listers.MachineConfigPools.List(labels.Everything())
if err != nil {
return nil, err
}
var pools []*mcfgv1.MachineConfigPool
for _, p := range pl {
selector, err := metav1.LabelSelectorAsSelector(p.Spec.NodeSelector)
if err != nil {
klog.Errorf("invalid label selector %s in MachineConfigPool %s: %v", util.ObjectInfo(selector), p.ObjectMeta.Name, err)
continue
}
// A pool with a nil or empty selector matches nothing.
if selector.Empty() || !selector.Matches(labels.Set(node.Labels)) {
continue
}
pools = append(pools, p)
}
if len(pools) == 0 {
// This is not an error, as there might be nodes in cluster that are not managed by machineconfigpool.
return nil, nil
}
var master, worker *mcfgv1.MachineConfigPool
var custom []*mcfgv1.MachineConfigPool
for _, pool := range pools {
if pool.Name == "master" {
master = pool
} else if pool.Name == "worker" {
worker = pool
} else {
custom = append(custom, pool)
}
}
if len(custom) > 1 {
klog.Errorf("node %s belongs to %d custom roles, cannot proceed with this Node", node.Name, len(custom))
return nil, nil
} else if len(custom) == 1 {
// We don't support making custom pools for masters
if master != nil {
klog.Errorf("node %s has both master role and custom role %s", node.Name, custom[0].Name)
return nil, nil
}
// One custom role, let's use its pool
pls := []*mcfgv1.MachineConfigPool{custom[0]}
if worker != nil {
pls = append(pls, worker)
}
return pls, nil
} else if master != nil {
// In the case where a node is both master/worker, have it live under
// the master pool. This occurs in CodeReadyContainers and general
// "single node" deployments, which one may want to do for testing bare
// metal, etc.
return []*mcfgv1.MachineConfigPool{master}, nil
}
// Otherwise, it's a worker with no custom roles.
return []*mcfgv1.MachineConfigPool{worker}, nil
}
// getNodesForPool returns a list of Nodes for MachineConfigPool 'pool'.
func (pc *ProfileCalculator) getNodesForPool(pool *mcfgv1.MachineConfigPool) ([]*corev1.Node, error) {
selector, err := metav1.LabelSelectorAsSelector(pool.Spec.NodeSelector)
if err != nil {
return nil, fmt.Errorf("invalid label selector %s in MachineConfigPool %s: %v", util.ObjectInfo(selector), pool.ObjectMeta.Name, err)
}
initialNodes, err := pc.listers.Nodes.List(selector)
if err != nil {
return nil, err
}
nodes := []*corev1.Node{}
for _, n := range initialNodes {
p, err := pc.getPrimaryPoolForNode(n)
if err != nil {
klog.Warningf("cannot get pool for node %q: %v", n.Name, err)
continue
}
if p == nil {
continue
}
if p.Name != pool.Name {
continue
}
nodes = append(nodes, n)
}
return nodes, nil
}
// getPrimaryPoolForNode uses getPoolsForNode and returns the first one which is the one the node targets
func (pc *ProfileCalculator) getPrimaryPoolForNode(node *corev1.Node) (*mcfgv1.MachineConfigPool, error) {
pools, err := pc.getPoolsForNode(node)
if err != nil {
return nil, err
}
if pools == nil {
return nil, nil
}
return pools[0], nil
}
func machineConfigGenerationLogLine(bIgn, bCmdline bool, bootcmdline string) string {
var (
sb strings.Builder
)
if bIgn {
sb.WriteString(" ignition")
if bCmdline {
sb.WriteString(" and")
}
}
if bCmdline {
sb.WriteString(" kernel parameters: [")
sb.WriteString(bootcmdline)
sb.WriteString("]")
}
return sb.String()
}