Skip to content

Commit

Permalink
UPSTREAM: <carry>: require configuration file enablement
Browse files Browse the repository at this point in the history
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.

This can be dropped when the feature per  KEP
kubernetes/enhancements#4800 goes beta.

Signed-off-by: Francesco Romani <fromani@redhat.com>
  • Loading branch information
ffromani committed Dec 4, 2024
1 parent fa8e99e commit 61a8b58
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/kubelet/cm/cpumanager/policy_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func CheckPolicyOptionAvailable(option string) error {
if alphaOptions.Has(option) && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManagerPolicyAlphaOptions) {
return fmt.Errorf("CPU Manager Policy Alpha-level Options not enabled, but option %q provided", option)
}
if alphaOptions.Has(option) && utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManagerPolicyAlphaOptions) {
return checkAlphaPolicyOptionHasEnablement(option)
}

if betaOptions.Has(option) && !utilfeature.DefaultFeatureGate.Enabled(kubefeatures.CPUManagerPolicyBetaOptions) {
return fmt.Errorf("CPU Manager Policy Beta-level Options not enabled, but option %q provided", option)
Expand Down
50 changes: 50 additions & 0 deletions pkg/kubelet/cm/cpumanager/policy_options_enablement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cpumanager

import (
"fmt"

"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/optenable"
)

var (
forceEnablement bool
)

func testOnlyForceEnablement() func() {
forceEnablement = true
return func() {
forceEnablement = false
}
}

func checkAlphaPolicyOptionHasEnablement(option string) error {
switch option {
case PreferAlignByUnCoreCacheOption:
klog.InfoS("CPU Manager Policy Alpha level", "option", option, "enablementFile", optenable.LLCAlignment())
if optenable.LLCAlignment() {
return nil
}
return fmt.Errorf("CPU Manager Policy Alpha-level Option %q is enabled but miss enablement file", option)
}
if forceEnablement {
return nil
}
return fmt.Errorf("CPU Manager Policy Alpha-level Option %q is not eligible for enablement", option)
}
8 changes: 8 additions & 0 deletions pkg/kubelet/cm/cpumanager/policy_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func TestPolicyOptionsAvailable(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.option, func(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, testCase.featureGate, testCase.featureGateEnable)
if testCase.featureGateEnable {
revertEnablement := testOnlyForceEnablement()
t.Cleanup(revertEnablement)
}
err := CheckPolicyOptionAvailable(testCase.option)
isEnabled := (err == nil)
if isEnabled != testCase.expectedAvailable {
Expand Down Expand Up @@ -191,6 +195,8 @@ func TestValidateStaticPolicyOptions(t *testing.T) {
topoMgrStore := topologymanager.NewFakeManagerWithPolicy(topoMgrPolicy)

featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)
revertEnablement := testOnlyForceEnablement()
t.Cleanup(revertEnablement)
policyOpt, _ := NewStaticPolicyOptions(testCase.policyOption)
err := ValidateStaticPolicyOptions(policyOpt, testCase.topology, topoMgrStore)
gotError := (err != nil)
Expand Down Expand Up @@ -238,6 +244,8 @@ func TestPolicyOptionsCompatibility(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, testCase.featureGate, true)
revertEnablement := testOnlyForceEnablement()
t.Cleanup(revertEnablement)
_, err := NewStaticPolicyOptions(testCase.policyOptions)
gotError := err != nil
if gotError != testCase.expectedErr {
Expand Down
2 changes: 2 additions & 0 deletions pkg/kubelet/cm/cpumanager/policy_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ func runStaticPolicyTestCase(t *testing.T, testCase staticPolicyTest) {

func runStaticPolicyTestCaseWithFeatureGate(t *testing.T, testCase staticPolicyTest) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)
revertEnablement := testOnlyForceEnablement()
t.Cleanup(revertEnablement)
runStaticPolicyTestCase(t, testCase)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/kubelet/cm/cpumanager/topology_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ func TestGetPodTopologyHintsWithPolicyOptions(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, pkgfeatures.CPUManagerPolicyAlphaOptions, true)
revertEnablement := testOnlyForceEnablement()
t.Cleanup(revertEnablement)

var activePods []*v1.Pod
for p := range testCase.assignments {
Expand Down
41 changes: 41 additions & 0 deletions pkg/kubelet/optenable/optenable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package optenable

import (
"os"
)

var (
// cpumanager: prefer-align-cpus-by-uncorecache
llcAlignmentEnabled bool
llcAlignmentFilename = "/etc/kubernetes/openshift-llc-alignment"
)

func init() {
readEnablementFiles()
}

func readEnablementFiles() {
if _, err := os.Stat(llcAlignmentFilename); err == nil {
llcAlignmentEnabled = true
}
}

func LLCAlignment() bool {
return llcAlignmentEnabled
}

0 comments on commit 61a8b58

Please sign in to comment.