forked from openshift/cluster-node-tuning-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machineconfig.go
651 lines (572 loc) · 22.6 KB
/
machineconfig.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package machineconfig
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"text/template"
assets "github.com/openshift/cluster-node-tuning-operator/assets/performanceprofile"
"github.com/coreos/go-systemd/unit"
igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
"k8s.io/utils/pointer"
apiconfigv1 "github.com/openshift/api/config/v1"
performancev2 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/performanceprofile/v2"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components"
profilecomponent "github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/profile"
profileutil "github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/profile"
machineconfigv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)
const (
defaultIgnitionVersion = "3.2.0"
defaultIgnitionContentSource = "data:text/plain;charset=utf-8;base64"
)
const (
// MCKernelRT is the value of the kernel setting in MachineConfig for the RT kernel
MCKernelRT = "realtime"
// MCKernelDefault is the value of the kernel setting in MachineConfig for the default kernel
MCKernelDefault = "default"
// HighPerformanceRuntime contains the name of the high-performance runtime
HighPerformanceRuntime = "high-performance"
bashScriptsDir = "/usr/local/bin"
crioConfd = "/etc/crio/crio.conf.d"
crioRuntimesConfig = "99-runtimes.conf"
// sysctl config
defaultRPSMaskConfig = "99-default-rps-mask.conf"
sysctlConfigDir = "/etc/sysctl.d/"
sysctlTemplateRPSMask = "RPSMask"
// Workload partitioning configs
kubernetesConfDir = "/etc/kubernetes"
crioPartitioningConfig = "99-workload-pinning.conf"
ocpPartitioningConfig = "openshift-workload-pinning"
udevRulesDir = "/etc/udev/rules.d"
udevPhysicalRpsRules = "99-netdev-physical-rps.rules"
// scripts
hugepagesAllocation = "hugepages-allocation"
setCPUsOffline = "set-cpus-offline"
setRPSMask = "set-rps-mask"
clearIRQBalanceBannedCPUs = "clear-irqbalance-banned-cpus"
cpusetConfigure = "cpuset-configure"
)
const (
systemdSectionUnit = "Unit"
systemdSectionService = "Service"
systemdSectionInstall = "Install"
systemdDescription = "Description"
systemdBefore = "Before"
systemdEnvironment = "Environment"
systemdType = "Type"
systemdRemainAfterExit = "RemainAfterExit"
systemdExecStart = "ExecStart"
systemdWantedBy = "WantedBy"
)
const (
systemdServiceIRQBalance = "irqbalance.service"
systemdServiceKubelet = "kubelet.service"
systemdServiceCrio = "crio.service"
systemdServiceTypeOneshot = "oneshot"
systemdTargetMultiUser = "multi-user.target"
systemdTargetNetworkOnline = "network-online.target"
systemdTrue = "true"
)
const (
environmentHugepagesSize = "HUGEPAGES_SIZE"
environmentHugepagesCount = "HUGEPAGES_COUNT"
environmentNUMANode = "NUMA_NODE"
environmentOfflineCpus = "OFFLINE_CPUS"
)
const (
templateReservedCpus = "ReservedCpus"
templateWorkload = "Workload"
templateRuntimePath = "RuntimePath"
templateRuntimeRoot = "RuntimeRoot"
)
// New returns new machine configuration object for performance sensitive workloads
func New(profile *performancev2.PerformanceProfile, pinningMode *apiconfigv1.CPUPartitioningMode, defaultRuntime machineconfigv1.ContainerRuntimeDefaultRuntime) (*machineconfigv1.MachineConfig, error) {
name := GetMachineConfigName(profile)
mc := &machineconfigv1.MachineConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: machineconfigv1.GroupVersion.String(),
Kind: "MachineConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: profilecomponent.GetMachineConfigLabel(profile),
},
Spec: machineconfigv1.MachineConfigSpec{},
}
ignitionConfig, err := getIgnitionConfig(profile, pinningMode, defaultRuntime)
if err != nil {
return nil, err
}
rawIgnition, err := json.Marshal(ignitionConfig)
if err != nil {
return nil, err
}
mc.Spec.Config = runtime.RawExtension{Raw: rawIgnition}
enableRTKernel := profile.Spec.RealTimeKernel != nil &&
profile.Spec.RealTimeKernel.Enabled != nil &&
*profile.Spec.RealTimeKernel.Enabled
if enableRTKernel {
mc.Spec.KernelType = MCKernelRT
} else {
mc.Spec.KernelType = MCKernelDefault
}
return mc, nil
}
// GetMachineConfigName generates machine config name from the performance profile
func GetMachineConfigName(profile *performancev2.PerformanceProfile) string {
name := components.GetComponentName(profile.Name, components.ComponentNamePrefix)
return fmt.Sprintf("50-%s", name)
}
func getIgnitionConfig(profile *performancev2.PerformanceProfile, pinningMode *apiconfigv1.CPUPartitioningMode, defaultRuntime machineconfigv1.ContainerRuntimeDefaultRuntime) (*igntypes.Config, error) {
var scripts []string
ignitionConfig := &igntypes.Config{
Ignition: igntypes.Ignition{
Version: defaultIgnitionVersion,
},
Storage: igntypes.Storage{
Files: []igntypes.File{},
},
}
// add script files under the node /usr/local/bin directory
if profileutil.IsRpsEnabled(profile) || profile.Spec.WorkloadHints == nil ||
profile.Spec.WorkloadHints.RealTime == nil || *profile.Spec.WorkloadHints.RealTime {
scripts = []string{hugepagesAllocation, setRPSMask, setCPUsOffline, clearIRQBalanceBannedCPUs}
} else {
// realtime is explicitly disabled by workload hint
scripts = []string{hugepagesAllocation, setCPUsOffline, clearIRQBalanceBannedCPUs}
}
mode := 0700
for _, script := range scripts {
dst := getBashScriptPath(script)
content, err := assets.Scripts.ReadFile(fmt.Sprintf("scripts/%s.sh", script))
if err != nil {
return nil, err
}
addContent(ignitionConfig, content, dst, &mode)
}
// add crio config snippet under the node /etc/crio/crio.conf.d/ directory
crioConfdRuntimesMode := 0644
crioConfigSnippetContent, err := renderCrioConfigSnippet(profile, defaultRuntime, filepath.Join("configs", crioRuntimesConfig))
if err != nil {
return nil, err
}
crioConfSnippetDst := filepath.Join(crioConfd, crioRuntimesConfig)
addContent(ignitionConfig, crioConfigSnippetContent, crioConfSnippetDst, &crioConfdRuntimesMode)
// do not add RPS handling when realtime is explicitly disabled by workload hint
if profileutil.IsRpsEnabled(profile) || profile.Spec.WorkloadHints == nil ||
profile.Spec.WorkloadHints.RealTime == nil || *profile.Spec.WorkloadHints.RealTime {
// configure default rps mask applied to all network devices
sysctlConfContent, err := renderSysctlConf(profile, filepath.Join("configs", defaultRPSMaskConfig))
if err != nil {
return nil, err
}
sysctlConfFileMode := 0644
sysctlConfDst := filepath.Join(sysctlConfigDir, defaultRPSMaskConfig)
addContent(ignitionConfig, sysctlConfContent, sysctlConfDst, &sysctlConfFileMode)
// if RPS disabled for physical devices revert the default RPS mask to 0
if !profileutil.IsPhysicalRpsEnabled(profile) {
// add rps udev rule
rpsRulesMode := 0644
var rpsRulesContent []byte
rpsRulesContent, err = assets.Configs.ReadFile(filepath.Join("configs", udevPhysicalRpsRules))
if err != nil {
return nil, err
}
rpsRulesDst := filepath.Join(udevRulesDir, udevPhysicalRpsRules)
addContent(ignitionConfig, rpsRulesContent, rpsRulesDst, &rpsRulesMode)
rpsService, err := getSystemdContent(getRPSUnitOptions("0"))
if err != nil {
return nil, err
}
ignitionConfig.Systemd.Units = append(ignitionConfig.Systemd.Units, igntypes.Unit{
Contents: &rpsService,
Name: getSystemdService("update-rps@"),
})
}
}
if profile.Spec.HugePages != nil {
for _, page := range profile.Spec.HugePages.Pages {
// we already allocated non NUMA specific hugepages via kernel arguments
if page.Node == nil {
continue
}
hugepagesSize, err := GetHugepagesSizeKilobytes(page.Size)
if err != nil {
return nil, err
}
hugepagesService, err := getSystemdContent(getHugepagesAllocationUnitOptions(
hugepagesSize,
page.Count,
*page.Node,
))
if err != nil {
return nil, err
}
ignitionConfig.Systemd.Units = append(ignitionConfig.Systemd.Units, igntypes.Unit{
Contents: &hugepagesService,
Enabled: pointer.BoolPtr(true),
Name: getSystemdService(fmt.Sprintf("%s-%skB-NUMA%d", hugepagesAllocation, hugepagesSize, *page.Node)),
})
}
}
if profile.Spec.CPU != nil && profile.Spec.CPU.Reserved != nil {
// Workload partitioning specific configuration
clusterIsPinned := pinningMode != nil && *pinningMode == apiconfigv1.CPUPartitioningAllNodes
if clusterIsPinned {
crioPartitionFileData, err := renderManagementCPUPinningConfig(profile.Spec.CPU, crioPartitioningConfig)
if err != nil {
return nil, err
}
crioPartitionDst := filepath.Join(crioConfd, crioPartitioningConfig)
addContent(ignitionConfig, crioPartitionFileData, crioPartitionDst, &crioConfdRuntimesMode)
ocpPartitionFileData, err := renderManagementCPUPinningConfig(profile.Spec.CPU, ocpPartitioningConfig)
if err != nil {
return nil, err
}
ocpPartitionDst := filepath.Join(kubernetesConfDir, ocpPartitioningConfig)
addContent(ignitionConfig, ocpPartitionFileData, ocpPartitionDst, &crioConfdRuntimesMode)
}
// Support for cpu balancing configuration on RHEL 9 with cgroupv1
cpusetConfigureService, err := getSystemdContent(getCpusetConfigureServiceOptions())
if err != nil {
return nil, err
}
ignitionConfig.Systemd.Units = append(ignitionConfig.Systemd.Units, igntypes.Unit{
Contents: &cpusetConfigureService,
Enabled: pointer.BoolPtr(true),
Name: getSystemdService(cpusetConfigure),
})
dst := getBashScriptPath(cpusetConfigure)
content, err := assets.Scripts.ReadFile(fmt.Sprintf("scripts/%s.sh", cpusetConfigure))
if err != nil {
return nil, err
}
addContent(ignitionConfig, content, dst, &mode)
}
if profile.Spec.CPU.Offlined != nil {
offlinedCPUSList, err := cpuset.Parse(string(*profile.Spec.CPU.Offlined))
if err != nil {
return nil, err
}
offlinedCPUSstring := components.ListToString(offlinedCPUSList.ToSlice())
offlineCPUsService, err := getSystemdContent(getOfflineCPUs(offlinedCPUSstring))
if err != nil {
return nil, err
}
ignitionConfig.Systemd.Units = append(ignitionConfig.Systemd.Units, igntypes.Unit{
Contents: &offlineCPUsService,
Enabled: pointer.BoolPtr(true),
Name: getSystemdService(setCPUsOffline),
})
}
clearIRQBalanceBannedCPUsService, err := getSystemdContent(getIRQBalanceBannedCPUsOptions())
if err != nil {
return nil, err
}
ignitionConfig.Systemd.Units = append(ignitionConfig.Systemd.Units, igntypes.Unit{
Contents: &clearIRQBalanceBannedCPUsService,
Enabled: pointer.BoolPtr(true),
Name: getSystemdService(clearIRQBalanceBannedCPUs),
})
return ignitionConfig, nil
}
func getBashScriptPath(scriptName string) string {
return fmt.Sprintf("%s/%s.sh", bashScriptsDir, scriptName)
}
func getSystemdEnvironment(key string, value string) string {
return fmt.Sprintf("%s=%s", key, value)
}
func getSystemdService(serviceName string) string {
return fmt.Sprintf("%s.service", serviceName)
}
func getSystemdContent(options []*unit.UnitOption) (string, error) {
outReader := unit.Serialize(options)
outBytes, err := ioutil.ReadAll(outReader)
if err != nil {
return "", err
}
return string(outBytes), nil
}
// GetHugepagesSizeKilobytes retruns hugepages size in kilobytes
func GetHugepagesSizeKilobytes(hugepagesSize performancev2.HugePageSize) (string, error) {
switch hugepagesSize {
case "1G":
return "1048576", nil
case "2M":
return "2048", nil
default:
return "", fmt.Errorf("can not convert size %q to kilobytes", hugepagesSize)
}
}
func getCpusetConfigureServiceOptions() []*unit.UnitOption {
return []*unit.UnitOption{
// [Unit]
// Description
unit.NewUnitOption(systemdSectionUnit, systemdDescription, "Move services to reserved cpuset"),
// Before
unit.NewUnitOption(systemdSectionUnit, systemdBefore, systemdTargetNetworkOnline),
// Type
unit.NewUnitOption(systemdSectionService, systemdType, systemdServiceTypeOneshot),
// ExecStart
unit.NewUnitOption(systemdSectionService, systemdExecStart, getBashScriptPath(cpusetConfigure)),
// [Install]
// WantedBy
unit.NewUnitOption(systemdSectionInstall, systemdWantedBy, systemdTargetMultiUser+" "+systemdServiceCrio),
}
}
func getIRQBalanceBannedCPUsOptions() []*unit.UnitOption {
return []*unit.UnitOption{
// [Unit]
// Description
unit.NewUnitOption(systemdSectionUnit, systemdDescription, "Clear the IRQBalance Banned CPU mask early in the boot"),
// Before
unit.NewUnitOption(systemdSectionUnit, systemdBefore, systemdServiceKubelet),
unit.NewUnitOption(systemdSectionUnit, systemdBefore, systemdServiceIRQBalance),
// [Service]
// Type
unit.NewUnitOption(systemdSectionService, systemdType, systemdServiceTypeOneshot),
// RemainAfterExit
unit.NewUnitOption(systemdSectionService, systemdRemainAfterExit, systemdTrue),
// ExecStart
unit.NewUnitOption(systemdSectionService, systemdExecStart, getBashScriptPath(clearIRQBalanceBannedCPUs)),
// [Install]
// WantedBy
unit.NewUnitOption(systemdSectionInstall, systemdWantedBy, systemdTargetMultiUser),
}
}
func getHugepagesAllocationUnitOptions(hugepagesSize string, hugepagesCount int32, numaNode int32) []*unit.UnitOption {
return []*unit.UnitOption{
// [Unit]
// Description
unit.NewUnitOption(systemdSectionUnit, systemdDescription, fmt.Sprintf("Hugepages-%skB allocation on the node %d", hugepagesSize, numaNode)),
// Before
unit.NewUnitOption(systemdSectionUnit, systemdBefore, systemdServiceKubelet),
// [Service]
// Environment
unit.NewUnitOption(systemdSectionService, systemdEnvironment, getSystemdEnvironment(environmentHugepagesCount, fmt.Sprint(hugepagesCount))),
unit.NewUnitOption(systemdSectionService, systemdEnvironment, getSystemdEnvironment(environmentHugepagesSize, hugepagesSize)),
unit.NewUnitOption(systemdSectionService, systemdEnvironment, getSystemdEnvironment(environmentNUMANode, fmt.Sprint(numaNode))),
// Type
unit.NewUnitOption(systemdSectionService, systemdType, systemdServiceTypeOneshot),
// RemainAfterExit
unit.NewUnitOption(systemdSectionService, systemdRemainAfterExit, systemdTrue),
// ExecStart
unit.NewUnitOption(systemdSectionService, systemdExecStart, getBashScriptPath(hugepagesAllocation)),
// [Install]
// WantedBy
unit.NewUnitOption(systemdSectionInstall, systemdWantedBy, systemdTargetMultiUser),
}
}
func getOfflineCPUs(offlineCpus string) []*unit.UnitOption {
return []*unit.UnitOption{
// [Unit]
// Description
unit.NewUnitOption(systemdSectionUnit, systemdDescription, fmt.Sprintf("Set cpus offline: %s", offlineCpus)),
// Before
unit.NewUnitOption(systemdSectionUnit, systemdBefore, systemdServiceKubelet),
// Environment
unit.NewUnitOption(systemdSectionService, systemdEnvironment, getSystemdEnvironment(environmentOfflineCpus, offlineCpus)),
// Type
unit.NewUnitOption(systemdSectionService, systemdType, systemdServiceTypeOneshot),
// RemainAfterExit
unit.NewUnitOption(systemdSectionService, systemdRemainAfterExit, systemdTrue),
// ExecStart
unit.NewUnitOption(systemdSectionService, systemdExecStart, getBashScriptPath(setCPUsOffline)),
// [Install]
// WantedBy
unit.NewUnitOption(systemdSectionInstall, systemdWantedBy, systemdTargetMultiUser),
}
}
func getRPSUnitOptions(rpsMask string) []*unit.UnitOption {
cmd := fmt.Sprintf("%s %%i %s", getBashScriptPath(setRPSMask), rpsMask)
return []*unit.UnitOption{
// [Unit]
// Description
unit.NewUnitOption(systemdSectionUnit, systemdDescription, "Sets network devices RPS mask"),
// [Service]
// Type
unit.NewUnitOption(systemdSectionService, systemdType, systemdServiceTypeOneshot),
// ExecStart
unit.NewUnitOption(systemdSectionService, systemdExecStart, cmd),
}
}
func addContent(ignitionConfig *igntypes.Config, content []byte, dst string, mode *int) {
contentBase64 := base64.StdEncoding.EncodeToString(content)
ignitionConfig.Storage.Files = append(ignitionConfig.Storage.Files, igntypes.File{
Node: igntypes.Node{
Path: dst,
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: pointer.StringPtr(fmt.Sprintf("%s,%s", defaultIgnitionContentSource, contentBase64)),
},
Mode: mode,
},
})
}
func renderCrioConfigSnippet(profile *performancev2.PerformanceProfile, defaultRuntime machineconfigv1.ContainerRuntimeDefaultRuntime, src string) ([]byte, error) {
templateArgs := map[string]string{
templateRuntimePath: "/bin/runc",
templateRuntimeRoot: "/run/runc",
}
if profile.Spec.CPU.Reserved != nil {
templateArgs[templateReservedCpus] = string(*profile.Spec.CPU.Reserved)
}
if defaultRuntime == machineconfigv1.ContainerRuntimeDefaultRuntimeCrun {
templateArgs[templateRuntimePath] = "/usr/bin/crun"
templateArgs[templateRuntimeRoot] = "/run/crun"
}
profileTemplate, err := template.ParseFS(assets.Configs, src)
if err != nil {
return nil, err
}
crioConfig := &bytes.Buffer{}
if err := profileTemplate.Execute(crioConfig, templateArgs); err != nil {
return nil, err
}
return crioConfig.Bytes(), nil
}
// Render out the CPU pinning configuration for CRIO and Kubelet
// The rendered files will make use of the `reserved` CPUs to generate the configuration files.
// The template files listed below
// [./assets/performanceprofile/configs/99-workload-pinning.conf] - CRI-O Config
// [./assets/performanceprofile/configs/openshift-workload-pinning] - Kubelet Config
//
// Note:
// The CRI-O Config for `cpushares` must be set to zero.
// Not setting it causes the TOML to not be parsed by CRI-O which is why the template value is set to zero.
// Further, it should not be configurable through the API, as the Kubelet will inject the correct cpu share annotations according to the pod spec.
// Carried patches in https://github.com/openshift/kubernetes/pull/706
func renderManagementCPUPinningConfig(cpuv2 *performancev2.CPU, src string) ([]byte, error) {
if cpuv2 == nil {
return nil, fmt.Errorf("cpu value is required, skipping generating file")
}
// In the future we might enhance this to allow more annotations, currently it's assumed
// this will only be related to management "Infrastrcture" workloads
vars := map[string]string{
templateWorkload: "management",
templateReservedCpus: string(*cpuv2.Reserved),
}
embdedFilePath := filepath.Join("configs", src)
profileTemplate, err := template.ParseFS(assets.Configs, embdedFilePath)
if err != nil {
return nil, err
}
pinningConfigData := &bytes.Buffer{}
if err := profileTemplate.Execute(pinningConfigData, vars); err != nil {
return nil, err
}
return pinningConfigData.Bytes(), nil
}
// BootstrapWorkloadPinningMC creates an initial state MachineConfig resource that establishes an empty CPU set for both
// CRIO config and Kubelet config. The purpose is provide empty state initialization for both CRIO and Kubelet so that the nodes
// always start and join the cluster in a Workload Pinning configuration.
//
// When a performance profiles is created, they will override the config files in this MC with the desired CPU Set. If that performance
// profile were to be deleted later on, this initial MC will be the fallback that MCO re-renders and maintain a workload pinning configuration.
func BootstrapWorkloadPinningMC(role string, pinningMode *apiconfigv1.CPUPartitioningMode) (*machineconfigv1.MachineConfig, error) {
if pinningMode == nil {
return nil, fmt.Errorf("can not generate configs, CPUPartitioningMode is nil")
}
if *pinningMode != apiconfigv1.CPUPartitioningAllNodes {
return nil, nil
}
mode := 420
/*
Contents for emptyKubeletConfig:
{
"management": {
"cpuset": ""
}
}
Contents for emptyCrioConfig:
[crio.runtime.workloads.management]
activation_annotation = "target.workload.openshift.io/management"
annotation_prefix = "resources.workload.openshift.io"
resources = { "cpushares" = 0, "cpuset" = "" }
*/
emptyKubeletConfig := "ewogICJtYW5hZ2VtZW50IjogewogICAgImNwdXNldCI6ICIiCiAgfQp9Cg=="
emptyCrioConfig := "W2NyaW8ucnVudGltZS53b3JrbG9hZHMubWFuYWdlbWVudF0KYWN0aXZhdGlvbl9hbm5vdGF0aW9uID0gInRhcmdldC53b3JrbG9hZC5vcGVuc2hpZnQuaW8vbWFuYWdlbWVudCIKYW5ub3RhdGlvbl9wcmVmaXggPSAicmVzb3VyY2VzLndvcmtsb2FkLm9wZW5zaGlmdC5pbyIKcmVzb3VyY2VzID0geyAiY3B1c2hhcmVzIiA9IDAsICJjcHVzZXQiID0gIiIgfQo="
emptyKubeletSource := fmt.Sprintf("%s,%s", defaultIgnitionContentSource, emptyKubeletConfig)
emptyCrioSource := fmt.Sprintf("%s,%s", defaultIgnitionContentSource, emptyCrioConfig)
// We add lower hierarchical config file name so as to not disturb any
// pre-existing files that users might have for workload pinning
crioDefaultPartitioningConfig := "01-workload-pinning-default.conf"
mc := &machineconfigv1.MachineConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: machineconfigv1.GroupVersion.String(),
Kind: "MachineConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("01-%s-cpu-partitioning", role),
Labels: map[string]string{
"machineconfiguration.openshift.io/role": role,
},
},
Spec: machineconfigv1.MachineConfigSpec{},
}
ignitionConfig := &igntypes.Config{
Ignition: igntypes.Ignition{
Version: igntypes.MaxVersion.String(),
},
Storage: igntypes.Storage{
Files: []igntypes.File{
{
Node: igntypes.Node{
Path: filepath.Join(kubernetesConfDir, ocpPartitioningConfig),
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: &emptyKubeletSource,
},
Mode: &mode,
},
},
{
Node: igntypes.Node{
Path: filepath.Join(crioConfd, crioDefaultPartitioningConfig),
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: &emptyCrioSource,
},
Mode: &mode,
},
},
},
},
}
rawIgnition, err := json.Marshal(ignitionConfig)
if err != nil {
return nil, err
}
mc.Spec.Config = runtime.RawExtension{Raw: rawIgnition}
return mc, nil
}
func renderSysctlConf(profile *performancev2.PerformanceProfile, src string) ([]byte, error) {
if profile.Spec.CPU == nil || profile.Spec.CPU.Reserved == nil {
return nil, nil
}
rpsMask, err := components.CPUListToMaskList(string(*profile.Spec.CPU.Reserved))
if err != nil {
return nil, err
}
templateArgs := map[string]string{
sysctlTemplateRPSMask: rpsMask,
}
sysctlConfigTemplate, err := template.ParseFS(assets.Configs, src)
if err != nil {
return nil, err
}
sysctlConfig := &bytes.Buffer{}
if err = sysctlConfigTemplate.Execute(sysctlConfig, templateArgs); err != nil {
return nil, err
}
return sysctlConfig.Bytes(), nil
}