Skip to content

Commit 8c67b5c

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 b9ed68f commit 8c67b5c

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

pkg/kubelet/cm/cpumanager/policy_options.go

Lines changed: 20 additions & 0 deletions
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.
@@ -59,6 +61,14 @@ func CheckPolicyOptionAvailable(option string) error {
5961
return fmt.Errorf("unknown CPU Manager Policy option: %q", option)
6062
}
6163

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

pkg/kubelet/cm/cpumanager/policy_options_test.go

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

pkg/kubelet/llcalign/llcalign.go

Lines changed: 46 additions & 0 deletions
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)