Skip to content

Commit 99ee2b6

Browse files
committed
PoC 2
1 parent 233ba84 commit 99ee2b6

3 files changed

Lines changed: 110 additions & 111 deletions

File tree

pkg/performanceprofile/controller/performanceprofile/components/handler/handler.go

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ func NewHandler(cli client.Client, scheme *runtime.Scheme) components.Handler {
3131
return &handler{Client: cli, scheme: scheme}
3232
}
3333

34+
func (h *handler) applyTuned(ctx context.Context, profile *performancev2.PerformanceProfile, mfs *manifestset.ManifestResultSet) ([]string, error) {
35+
// Get mutated performance tuned.
36+
performanceTunedMutated, err := resources.GetMutatedTuned(ctx, h.Client, mfs.Tuned)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
42+
if performanceTunedMutated != nil {
43+
if err := resources.CreateOrUpdateTuned(ctx, h.Client, performanceTunedMutated, profile.Name); err != nil {
44+
return nil, err
45+
}
46+
}
47+
48+
// Get kernel arguments from tuned bootcmdline. This will wait for tuned to calculate and agree on them.
49+
// In other words, if bootcmdline is not ready or nodes disagree, an error is returned.
50+
kernelArguments, err := resources.GetKernelArgumentsFromTunedBootcmdline(ctx, h.Client, profile)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
return kernelArguments, nil
56+
}
57+
3458
func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.EventRecorder, opts *components.Options) error {
3559
profile, ok := obj.(*performancev2.PerformanceProfile)
3660
if !ok {
@@ -42,12 +66,9 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
4266
klog.Infof("Ignoring reconcile loop for pause performance profile %s", profile.Name)
4367
return nil
4468
}
45-
4669
// set missing options
4770
opts.MachineConfig.MixedCPUsEnabled = opts.MixedCPUsFeatureGateEnabled && profileutil.IsMixedCPUsEnabled(profile)
4871

49-
// First, create components WITHOUT kernel arguments to bootstrap the system.
50-
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
5172
components, err := manifestset.GetNewComponents(profile, opts)
5273
if err != nil {
5374
return err
@@ -58,14 +79,21 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
5879
}
5980
}
6081

61-
// get mutated kubelet config
62-
kcMutated, err := resources.GetMutatedKubeletConfig(ctx, h.Client, components.KubeletConfig)
82+
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
83+
kernelArguments, err := h.applyTuned(ctx, profile, components)
6384
if err != nil {
6485
return err
6586
}
87+
opts.MachineConfig.KernelArguments = kernelArguments
6688

67-
// get mutated performance tuned
68-
performanceTunedMutated, err := resources.GetMutatedTuned(ctx, h.Client, components.Tuned)
89+
// get mutated machine config
90+
mcMutated, err := resources.GetMutatedMachineConfig(ctx, h.Client, components.MachineConfig)
91+
if err != nil {
92+
return err
93+
}
94+
95+
// get mutated kubelet config
96+
kcMutated, err := resources.GetMutatedKubeletConfig(ctx, h.Client, components.KubeletConfig)
6997
if err != nil {
7098
return err
7199
}
@@ -76,9 +104,18 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
76104
return err
77105
}
78106

79-
// Create/Update Tuned, KubeletConfig, and RuntimeClass first (without waiting for bootcmdline)
80-
if performanceTunedMutated != nil {
81-
if err := resources.CreateOrUpdateTuned(ctx, h.Client, performanceTunedMutated, profile.Name); err != nil {
107+
updated := mcMutated != nil ||
108+
kcMutated != nil ||
109+
runtimeClassMutated != nil
110+
111+
// does not update any resources if it no changes to relevant objects and continue to the status update
112+
if !updated {
113+
return nil
114+
}
115+
116+
// Update options with kernel arguments and create/update MachineConfig
117+
if mcMutated != nil {
118+
if err := resources.CreateOrUpdateMachineConfig(ctx, h.Client, mcMutated); err != nil {
82119
return err
83120
}
84121
}
@@ -94,20 +131,8 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
94131
return err
95132
}
96133
}
97-
98-
// Get kernel arguments from tuned bootcmdline. This will wait for tuned to calculate them.
99-
kernelArguments, err := resources.GetKernelArgumentsFromTunedBootcmdline(ctx, h.Client, profile)
100-
if err != nil {
101-
return err
102-
}
103-
104-
// Update options with kernel arguments and create/update MachineConfig
105-
opts.MachineConfig.KernelArguments = kernelArguments
106-
if err := h.syncMachineConfig(ctx, profile, opts); err != nil {
107-
return err
108-
}
109-
110134
recorder.Eventf(profile, corev1.EventTypeNormal, "Creation succeeded", "Succeeded to create all components")
135+
111136
return nil
112137
}
113138

@@ -154,33 +179,3 @@ func (h *handler) Exists(ctx context.Context, profileName string) bool {
154179
}
155180
return false
156181
}
157-
158-
// syncMachineConfig creates or updates the MachineConfig with the provided kernel arguments
159-
func (h *handler) syncMachineConfig(ctx context.Context, profile *performancev2.PerformanceProfile, opts *components.Options) error {
160-
// Generate MachineConfig with kernel arguments
161-
components, err := manifestset.GetNewComponents(profile, opts)
162-
if err != nil {
163-
return err
164-
}
165-
166-
for _, componentObj := range components.ToObjects() {
167-
if err := controllerutil.SetControllerReference(profile, componentObj, h.scheme); err != nil {
168-
return err
169-
}
170-
}
171-
172-
// get mutated machine config with kernel arguments
173-
mcMutated, err := resources.GetMutatedMachineConfig(ctx, h.Client, components.MachineConfig)
174-
if err != nil {
175-
return err
176-
}
177-
178-
// Create/Update MachineConfig only after bootcmdline is ready
179-
if mcMutated != nil {
180-
if err := resources.CreateOrUpdateMachineConfig(ctx, h.Client, mcMutated); err != nil {
181-
return err
182-
}
183-
}
184-
185-
return nil
186-
}

pkg/performanceprofile/controller/performanceprofile/hypershift/components/handler.go

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,35 @@ func (h *handler) Exists(ctx context.Context, profileName string) bool {
7070
return false
7171
}
7272

73+
func (h *handler) applyTuned(ctx context.Context, profile *performancev2.PerformanceProfile, mfs *manifestset.ManifestResultSet, cfgmap *corev1.ConfigMap) ([]string, error) {
74+
// Get mutated performance tuned.
75+
performanceTunedMutated, err := resources.GetMutatedTuned(ctx, h.controlPlaneClient, mfs.Tuned)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
81+
if performanceTunedMutated != nil {
82+
cm, err := EncapsulateObjInConfigMap(h.scheme, cfgmap, mfs.Tuned, profile.Name, hypershiftconsts.TuningKey, map[string]string{hypershiftconsts.ControllerGeneratedTunedConfigMapLabel: "true"})
83+
if err != nil {
84+
return nil, err
85+
}
86+
err = createOrUpdateTunedConfigMap(ctx, h.controlPlaneClient, cm)
87+
if err != nil {
88+
return nil, err
89+
}
90+
}
91+
92+
// Get kernel arguments from tuned bootcmdline. This will wait for tuned to calculate and agree on them.
93+
// In other words, if bootcmdline is not ready or nodes disagree, an error is returned.
94+
kernelArguments, err := resources.GetKernelArgumentsFromTunedBootcmdline(ctx, h.dataPlaneClient, profile)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
return kernelArguments, nil
100+
}
101+
73102
func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.EventRecorder, options *components.Options) error {
74103
instance, ok := obj.(*corev1.ConfigMap)
75104
if !ok {
@@ -91,25 +120,29 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
91120
klog.Infof("ignoring reconcile loop for pause performance profile %s", profile.Name)
92121
return nil
93122
}
94-
95123
// set missing options
96124
options.MachineConfig.MixedCPUsEnabled = options.MixedCPUsFeatureGateEnabled && profileutil.IsMixedCPUsEnabled(profile)
97125

98-
// First, create components WITHOUT kernel arguments to bootstrap the system.
99-
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
100126
mfs, err := manifestset.GetNewComponents(profile, options)
101127
if err != nil {
102128
return err
103129
}
104130

105-
// get mutated kubelet config
106-
kcMutated, err := resources.GetMutatedKubeletConfig(ctx, h.controlPlaneClient, mfs.KubeletConfig)
131+
// The Tuned CR must be created first so the tuned operand can calculate bootcmdline.
132+
kernelArguments, err := h.applyTuned(ctx, profile, mfs, instance)
107133
if err != nil {
108134
return err
109135
}
136+
options.MachineConfig.KernelArguments = kernelArguments
110137

111-
// get mutated performance tuned
112-
performanceTunedMutated, err := resources.GetMutatedTuned(ctx, h.controlPlaneClient, mfs.Tuned)
138+
// get mutated machine config
139+
mcMutated, err := resources.GetMutatedMachineConfig(ctx, h.controlPlaneClient, mfs.MachineConfig)
140+
if err != nil {
141+
return err
142+
}
143+
144+
// get mutated kubelet config
145+
kcMutated, err := resources.GetMutatedKubeletConfig(ctx, h.controlPlaneClient, mfs.KubeletConfig)
113146
if err != nil {
114147
return err
115148
}
@@ -120,15 +153,23 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
120153
return err
121154
}
122155

123-
// Create/Update Tuned, KubeletConfig, and RuntimeClass first (without waiting for bootcmdline)
124-
if performanceTunedMutated != nil {
125-
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.Tuned, profile.Name, hypershiftconsts.TuningKey, map[string]string{hypershiftconsts.ControllerGeneratedTunedConfigMapLabel: "true"})
156+
updated := mcMutated != nil ||
157+
kcMutated != nil ||
158+
runtimeClassMutated != nil
159+
160+
// do not update any resources if no changes are present and continue to the status update
161+
if !updated {
162+
return nil
163+
}
164+
165+
if mcMutated != nil {
166+
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.MachineConfig, profile.Name, hypershiftconsts.ConfigKey, map[string]string{hypershiftconsts.NTOGeneratedMachineConfigLabel: "true"})
126167
if err != nil {
127-
return fmt.Errorf("failed to encapsulate Tuned in ConfigMap: %w", err)
168+
return err
128169
}
129-
err = createOrUpdateTunedConfigMap(ctx, h.controlPlaneClient, cm)
170+
err = createOrUpdateMachineConfigConfigMap(ctx, h.controlPlaneClient, cm)
130171
if err != nil {
131-
return fmt.Errorf("failed to create/update tuned ConfigMap: %w", err)
172+
return err
132173
}
133174
}
134175

@@ -138,63 +179,23 @@ func (h *handler) Apply(ctx context.Context, obj client.Object, recorder record.
138179
hypershiftconsts.KubeletConfigConfigMapLabel: "true",
139180
})
140181
if err != nil {
141-
return fmt.Errorf("failed to encapsulate KubeletConfig in ConfigMap: %w", err)
182+
return err
142183
}
143184
err = createOrUpdateKubeletConfigConfigMap(ctx, h.controlPlaneClient, cm)
144185
if err != nil {
145-
return fmt.Errorf("failed to create/update kubeletConfig ConfigMap: %w", err)
186+
return err
146187
}
147188
}
148189

149190
if runtimeClassMutated != nil {
150-
if err := resources.CreateOrUpdateRuntimeClass(ctx, h.dataPlaneClient, runtimeClassMutated); err != nil {
151-
return fmt.Errorf("failed to create/update RuntimeClass: %w", err)
152-
}
153-
}
154-
155-
// Get kernel arguments from tuned bootcmdline. This will wait for tuned to calculate them.
156-
kernelArguments, err := resources.GetKernelArgumentsFromTunedBootcmdline(ctx, h.dataPlaneClient, profile)
157-
if err != nil {
158-
return err
159-
}
160-
161-
// Update options with kernel arguments and create/update MachineConfig
162-
options.MachineConfig.KernelArguments = kernelArguments
163-
if err := h.syncMachineConfig(ctx, instance, profile, options); err != nil {
164-
return err
165-
}
166-
167-
klog.InfoS("Processed ok", "instanceName", instance.Name)
168-
recorder.Eventf(instance, corev1.EventTypeNormal, "Creation succeeded", "Succeeded to create all components for PerformanceProfile")
169-
return nil
170-
}
171-
172-
// syncMachineConfig creates or updates the MachineConfig with the provided kernel arguments
173-
func (h *handler) syncMachineConfig(ctx context.Context, instance *corev1.ConfigMap, profile *performancev2.PerformanceProfile, options *components.Options) error {
174-
// Generate MachineConfig with kernel arguments
175-
mfs, err := manifestset.GetNewComponents(profile, options)
176-
if err != nil {
177-
return err
178-
}
179-
180-
// get mutated machine config with kernel arguments
181-
mcMutated, err := resources.GetMutatedMachineConfig(ctx, h.controlPlaneClient, mfs.MachineConfig)
182-
if err != nil {
183-
return err
184-
}
185-
186-
// Create/Update MachineConfig only after bootcmdline is ready
187-
if mcMutated != nil {
188-
cm, err := EncapsulateObjInConfigMap(h.scheme, instance, mfs.MachineConfig, profile.Name, hypershiftconsts.ConfigKey, map[string]string{hypershiftconsts.NTOGeneratedMachineConfigLabel: "true"})
189-
if err != nil {
190-
return err
191-
}
192-
err = createOrUpdateMachineConfigConfigMap(ctx, h.controlPlaneClient, cm)
191+
err = resources.CreateOrUpdateRuntimeClass(ctx, h.dataPlaneClient, runtimeClassMutated)
193192
if err != nil {
194193
return err
195194
}
196195
}
197196

197+
klog.InfoS("Processed ok", "instanceName", instance.Name)
198+
recorder.Eventf(instance, corev1.EventTypeNormal, "Creation succeeded", "Succeeded to create all components for PerformanceProfile")
198199
return nil
199200
}
200201

pkg/performanceprofile/controller/performanceprofile_controller_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ var _ = Describe("Controller", func() {
739739
Labels: map[string]string{
740740
"nodekey": "nodeValue",
741741
},
742+
Annotations: map[string]string{
743+
tunedv1.TunedBootcmdlineAnnotationKey: "",
744+
},
742745
},
743746
},
744747
{

0 commit comments

Comments
 (0)