Skip to content

Commit 9bddbd4

Browse files
ffromanibertinatto
authored andcommitted
UPSTREAM: <carry>: require configuration file enablement
similarly to what we do for the managed CPU (aka workload partitioning) feature, introduce a master configuration file `/etc/kubernetes/openshift-llc-alignment` which needs to be present for the LLC alignment feature to be activated, in addition to the policy option being required. Note this replace the standard upstream feature gate check. This can be dropped when the feature per KEP kubernetes/enhancements#4800 goes beta. Signed-off-by: Francesco Romani <[email protected]>
1 parent fac68a7 commit 9bddbd4

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

pkg/kubelet/cm/cpumanager/policy_options.go

+20
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222

2323
"k8s.io/apimachinery/pkg/util/sets"
2424
utilfeature "k8s.io/apiserver/pkg/util/feature"
25+
"k8s.io/klog/v2"
2526
kubefeatures "k8s.io/kubernetes/pkg/features"
2627
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
2728
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
29+
"k8s.io/kubernetes/pkg/kubelet/llcalign"
2830
)
2931

3032
// Names of the options, as part of the user interface.
@@ -58,6 +60,14 @@ func CheckPolicyOptionAvailable(option string) error {
5860
return fmt.Errorf("unknown CPU Manager Policy option: %q", option)
5961
}
6062

63+
// must override the base feature gate check. Relevant only for alpha (disabled by default).
64+
// for beta options are enabled by default and we totally want to keep the possibility to
65+
// disable them explicitly.
66+
if alphaOptions.Has(option) && checkPolicyOptionHasEnablementFile(option) {
67+
// note that we override the decision and shortcut exit with success
68+
// all other cases exit early with failure.
69+
return nil
70+
}
6171
if alphaOptions.Has(option) && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManagerPolicyAlphaOptions) {
6272
return fmt.Errorf("CPU Manager Policy Alpha-level Options not enabled, but option %q provided", option)
6373
}
@@ -183,3 +193,13 @@ func ValidateStaticPolicyOptions(opts StaticPolicyOptions, topology *topology.CP
183193
}
184194
return nil
185195
}
196+
197+
func checkPolicyOptionHasEnablementFile(option string) bool {
198+
switch option {
199+
case PreferAlignByUnCoreCacheOption:
200+
val := llcalign.IsEnabled()
201+
klog.InfoS("policy option enablement file check", "option", option, "enablementFile", val)
202+
return val
203+
}
204+
return false
205+
}

pkg/kubelet/cm/cpumanager/policy_options_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
pkgfeatures "k8s.io/kubernetes/pkg/features"
2626
"k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
2727
"k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
28+
"k8s.io/kubernetes/pkg/kubelet/llcalign"
2829
)
2930

3031
type optionAvailTest struct {
@@ -258,3 +259,65 @@ func TestPolicyOptionsCompatibility(t *testing.T) {
258259
})
259260
}
260261
}
262+
263+
func TestPolicyOptionsAvailableWithEnablement(t *testing.T) {
264+
265+
type optionAvailEnabTest struct {
266+
name string
267+
option string
268+
featureGate featuregate.Feature
269+
featureGateEnable bool
270+
featureEnablementFlag bool
271+
expectedAvailable bool
272+
}
273+
274+
testCases := []optionAvailEnabTest{
275+
{
276+
name: "all disabled",
277+
option: PreferAlignByUnCoreCacheOption,
278+
featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions,
279+
featureGateEnable: false, // expected standard case
280+
featureEnablementFlag: false,
281+
expectedAvailable: false,
282+
},
283+
{
284+
name: "all enabled",
285+
option: PreferAlignByUnCoreCacheOption,
286+
featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions,
287+
featureGateEnable: true, // this should not be allowed by OCP profiles
288+
featureEnablementFlag: true,
289+
expectedAvailable: true,
290+
},
291+
{
292+
name: "enabled by feature gate",
293+
option: PreferAlignByUnCoreCacheOption,
294+
featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions,
295+
featureGateEnable: true, // this should not be allowed by OCP profiles, makes no sense either
296+
featureEnablementFlag: false,
297+
expectedAvailable: true,
298+
},
299+
{
300+
name: "enabled by enablement file",
301+
option: PreferAlignByUnCoreCacheOption,
302+
featureGate: pkgfeatures.CPUManagerPolicyAlphaOptions,
303+
featureGateEnable: false,
304+
featureEnablementFlag: true,
305+
expectedAvailable: true,
306+
},
307+
}
308+
for _, testCase := range testCases {
309+
t.Run(testCase.name, func(t *testing.T) {
310+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, testCase.featureGate, testCase.featureGateEnable)
311+
oldEnablementFlag := llcalign.TestOnlySetEnabled(testCase.featureEnablementFlag)
312+
313+
err := CheckPolicyOptionAvailable(testCase.option)
314+
315+
_ = llcalign.TestOnlySetEnabled(oldEnablementFlag)
316+
317+
isEnabled := (err == nil)
318+
if isEnabled != testCase.expectedAvailable {
319+
t.Errorf("option %q available got=%v expected=%v", testCase.option, isEnabled, testCase.expectedAvailable)
320+
}
321+
})
322+
}
323+
}

pkg/kubelet/llcalign/llcalign.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package llcalign
18+
19+
import (
20+
"os"
21+
)
22+
23+
var (
24+
llcAlignmentEnabled bool
25+
llcAlignmentFilename = "/etc/kubernetes/openshift-llc-alignment"
26+
)
27+
28+
func init() {
29+
readEnablementFile()
30+
}
31+
32+
func readEnablementFile() {
33+
if _, err := os.Stat(llcAlignmentFilename); err == nil {
34+
llcAlignmentEnabled = true
35+
}
36+
}
37+
38+
func IsEnabled() bool {
39+
return llcAlignmentEnabled
40+
}
41+
42+
func TestOnlySetEnabled(enabled bool) bool {
43+
oldEnabled := llcAlignmentEnabled
44+
llcAlignmentEnabled = enabled
45+
return oldEnabled
46+
}

0 commit comments

Comments
 (0)