Skip to content

perfprof: add enablement annotation #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ type Options struct {
type MachineConfigOptions struct {
PinningMode *apiconfigv1.CPUPartitioningMode
MixedCPUsEnabled bool
LLCFileEnabled bool
}

func (mco *MachineConfigOptions) Clone() *MachineConfigOptions {
ret := MachineConfigOptions{
MixedCPUsEnabled: mco.MixedCPUsEnabled,
LLCFileEnabled: mco.LLCFileEnabled,
}
if mco.PinningMode != nil {
ret.PinningMode = new(apiconfigv1.CPUPartitioningMode)
*ret.PinningMode = *mco.PinningMode
}
return &ret
}

type KubeletConfigOptions struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2025 Red Hat, Inc.
*
* 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 components

import (
"testing"

apiconfigv1 "github.com/openshift/api/config/v1"
)

func TestMachineConfigOptionsClone(t *testing.T) {
mode := apiconfigv1.CPUPartitioningAllNodes
mco := &MachineConfigOptions{
PinningMode: &mode,
}

mode2 := apiconfigv1.CPUPartitioningNone
mco2 := mco.Clone()
mco2.PinningMode = &mode2
mco2.LLCFileEnabled = true

// verify changes did not propagate back to the original copy
if *mco.PinningMode != apiconfigv1.CPUPartitioningAllNodes {
t.Fatalf("mutation of the cloned PinningMode altered back the original")
}
if mco.LLCFileEnabled {
t.Fatalf("mutation of the cloned LLCFileEnabled altered back the original")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubeletconfig

import (
"encoding/json"
"strconv"
"time"

corev1 "k8s.io/api/core/v1"
Expand All @@ -16,6 +17,10 @@ import (
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components"
)

const (
CPUManagerPolicyOptionPreferAlignCPUsByUncoreCache = "prefer-align-cpus-by-uncorecache"
)

const (
// experimentalKubeletSnippetAnnotation contains the annotation key that should be used to provide a KubeletConfig snippet with additional
// configurations you want to apply on top of the generated KubeletConfig resource.
Expand All @@ -27,35 +32,33 @@ const (
// 4. Reserved CPUs
// 5. Memory manager policy
// Please avoid specifying them and use the relevant API to configure these parameters.
experimentalKubeletSnippetAnnotation = "kubeletconfig.experimental"
cpuManagerPolicyStatic = "static"
cpuManagerPolicyOptionFullPCPUsOnly = "full-pcpus-only"
memoryManagerPolicyStatic = "Static"
defaultKubeReservedMemory = "500Mi"
defaultSystemReservedMemory = "500Mi"
defaultHardEvictionThresholdMemory = "100Mi"
defaultHardEvictionThresholdNodefs = "10%"
defaultHardEvictionThresholdImagefs = "15%"
defaultHardEvictionThresholdNodefsInodesFree = "5%"
evictionHardMemoryAvailable = "memory.available"
evictionHardNodefsAvaialble = "nodefs.available"
evictionHardImagefsAvailable = "imagefs.available"
evictionHardNodefsInodesFree = "nodefs.inodesFree"
experimentalKubeletSnippetAnnotation = "kubeletconfig.experimental"
// experimentalKubeletPreferAlignCPUsByUncoreCacheAnnotation is an annotation which serves as master flag to control the `prefer-align-cpus-by-uncorecache`
// cpumanager option. While is still possible to do the required steps manually, so inject the option using `kubelet.experimental` and adding the
// enablement file using the MachineConfig, this master flag automates all the steps. Furthermore, it overrides the injected configuration both ways,
// so we can disable the injected value. When the feature graduates to GA upstream, we will add the proper support in our API and retire this annotation.
experimentalKubeletPreferAlignCPUsByUncoreCacheAnnotation = "kubeletconfig.prefer-align-cpus-by-uncorecache"
cpuManagerPolicyStatic = "static"
cpuManagerPolicyOptionFullPCPUsOnly = "full-pcpus-only"
memoryManagerPolicyStatic = "Static"
defaultKubeReservedMemory = "500Mi"
defaultSystemReservedMemory = "500Mi"
defaultHardEvictionThresholdMemory = "100Mi"
defaultHardEvictionThresholdNodefs = "10%"
defaultHardEvictionThresholdImagefs = "15%"
defaultHardEvictionThresholdNodefsInodesFree = "5%"
evictionHardMemoryAvailable = "memory.available"
evictionHardNodefsAvaialble = "nodefs.available"
evictionHardImagefsAvailable = "imagefs.available"
evictionHardNodefsInodesFree = "nodefs.inodesFree"
)

// New returns new KubeletConfig object for performance sensetive workflows
func New(profile *performancev2.PerformanceProfile, opts *components.KubeletConfigOptions) (*machineconfigv1.KubeletConfig, error) {
name := components.GetComponentName(profile.Name, components.ComponentNamePrefix)
kubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{}
if v, ok := profile.Annotations[experimentalKubeletSnippetAnnotation]; ok {
if err := json.Unmarshal([]byte(v), kubeletConfig); err != nil {
return nil, err
}
}

kubeletConfig.TypeMeta = metav1.TypeMeta{
APIVersion: kubeletconfigv1beta1.SchemeGroupVersion.String(),
Kind: "KubeletConfiguration",
kubeletConfig, err := NewFromExperimentalAnnotation(profile)
if err != nil {
return nil, err
}

kubeletConfig.CPUManagerPolicy = cpuManagerPolicyStatic
Expand Down Expand Up @@ -193,3 +196,34 @@ func addStringToQuantity(q *resource.Quantity, value string) error {

return nil
}

func NewFromExperimentalAnnotation(profile *performancev2.PerformanceProfile) (*kubeletconfigv1beta1.KubeletConfiguration, error) {
kubeletConfig := &kubeletconfigv1beta1.KubeletConfiguration{}

if v, ok := profile.Annotations[experimentalKubeletSnippetAnnotation]; ok {
if err := json.Unmarshal([]byte(v), kubeletConfig); err != nil {
return nil, err
}
}

// mae sure to setup properly the metadata
kubeletConfig.TypeMeta = metav1.TypeMeta{
APIVersion: kubeletconfigv1beta1.SchemeGroupVersion.String(),
Kind: "KubeletConfiguration",
}
return kubeletConfig, nil
}

func IsPreferAlignCPUsByUncoreCacheEnabled(profile *performancev2.PerformanceProfile) (value bool, found bool) {
var rawVal string
rawVal, found = profile.Annotations[experimentalKubeletPreferAlignCPUsByUncoreCacheAnnotation]
if !found {
return false, false
}
var err error
value, err = strconv.ParseBool(rawVal)
if err != nil {
return false, false
}
return value, true
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ var _ = Describe("Kubelet Config", func() {
Expect(manifest).To(ContainSubstring("net.core.somaxconn"))
Expect(manifest).To(ContainSubstring(`full-pcpus-only: "false"`))
})
})

Context("with annotated performance profile", func() {
It("should create a kubeletconfig object", func() {
profile := testutils.NewPerformanceProfile("kubeletconfig-test")
profile.Annotations = map[string]string{
experimentalKubeletSnippetAnnotation: `{"allowedUnsafeSysctls": ["net.core.somaxconn"], "cpuManagerPolicyOptions": {"full-pcpus-only": "false"}}`,
}
kubeletConfig, err := NewFromExperimentalAnnotation(profile)
Expect(err).ToNot(HaveOccurred())
Expect(kubeletConfig.AllowedUnsafeSysctls).Should(ContainElement("net.core.somaxconn"))
Expect(kubeletConfig.CPUManagerPolicyOptions).Should(HaveKeyWithValue("full-pcpus-only", "false"))
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const (
ovsDynamicPinningTriggerHostFile = "/var/lib/ovn-ic/etc/enable_dynamic_cpu_affinity"

cpusetConfigure = "cpuset-configure"

// required until the LLC support code (KEP-4800) goes at least Beta
llcEnablementFile = "openshift-llc-alignment"
)

const (
Expand Down Expand Up @@ -392,6 +395,15 @@ func getIgnitionConfig(profile *performancev2.PerformanceProfile, opts *componen
}
addContent(ignitionConfig, content, filepath.Join(kubernetesConfDir, mixedCPUsConfig), ptr.To[int](0644))
}

// required until the LLC support code (KEP-4800) goes at least Beta
if opts.LLCFileEnabled {
// Note: the content of LLC (enablement) file does not matter. What matters is only the file presence.
// If the file is present, the feature is available and can be enabled via the PerformanceProfile object.\
// If the file is missing, the feature is forced off regardless of any PerformanceProfile object content.
addContent(ignitionConfig, []byte{}, filepath.Join(kubernetesConfDir, llcEnablementFile), ptr.To[int](0644))
}

return ignitionConfig, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package manifestset

import (
"k8s.io/klog/v2"

mcov1 "github.com/openshift/api/machineconfiguration/v1"
performancev2 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/performanceprofile/v2"
tunedv1 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/tuned/v1"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/kubeletconfig"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/machineconfig"
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"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/runtimeclass"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components/tuned"

Expand Down Expand Up @@ -53,7 +56,15 @@ func (ms *ManifestResultSet) ToManifestTable() ManifestTable {
func GetNewComponents(profile *performancev2.PerformanceProfile, opts *components.Options) (*ManifestResultSet, error) {
machineConfigPoolSelector := profilecomponent.GetMachineConfigPoolSelector(profile, opts.ProfileMCP)

mc, err := machineconfig.New(profile, &opts.MachineConfig)
llcEnabled, found := kubeletconfig.IsPreferAlignCPUsByUncoreCacheEnabled(profile)
if !found {
llcEnabled = profileutil.IsLLCAlignmentEnabled(profile)
}
klog.V(4).InfoS("components manifests", "LLCEnabled", llcEnabled, "annotationPresent", found)

mcOpts := opts.MachineConfig.Clone()
mcOpts.LLCFileEnabled = llcEnabled
mc, err := machineconfig.New(profile, mcOpts)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package manifestset

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestManifestSet(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manifest Set Suite")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package manifestset

import (
"encoding/json"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

igntypes "github.com/coreos/ignition/v2/config/v3_2/types"

performancev2 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/performanceprofile/v2"
"github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/controller/performanceprofile/components"

testutils "github.com/openshift/cluster-node-tuning-operator/pkg/performanceprofile/utils/testing"
)

var _ = Describe("LLC Enablement file", func() {
var profile *performancev2.PerformanceProfile
var opts *components.Options
var expectedPath string

BeforeEach(func() {
profile = testutils.NewPerformanceProfile("test-llc-ann")
opts = &components.Options{}
expectedPath = "/etc/kubernetes/openshift-llc-alignment"
})

When("the option is configured", func() {
It("should not be generated if missing", func() {
mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeFalse(), "expected path %q found in ignition", expectedPath)
})

It("should be generated if missing control annotation, but injected in experimental", func() {
profile.Annotations = map[string]string{
"kubeletconfig.experimental": `{"cpuManagerPolicyOptions": { "prefer-align-cpus-by-uncorecache": "true", "full-pcpus-only": "false" }}`,
}

mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeTrue(), "expected path %q not found in ignition", expectedPath)
})

It("should be generated if the master flag is enabled, but the injected value is disabled", func() {
profile.Annotations = map[string]string{
"kubeletconfig.prefer-align-cpus-by-uncorecache": "true",
"kubeletconfig.experimental": `{"cpuManagerPolicyOptions": { "prefer-align-cpus-by-uncorecache": "false", "full-pcpus-only": "false" }}`,
}

mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeTrue(), "expected path %q not found in ignition", expectedPath)
})

It("should NOT be generated if the master flag is disabled, but the injected value is enabled", func() {
profile.Annotations = map[string]string{
"kubeletconfig.prefer-align-cpus-by-uncorecache": "false",
"kubeletconfig.experimental": `{"cpuManagerPolicyOptions": { "prefer-align-cpus-by-uncorecache": "true", "full-pcpus-only": "false" }}`,
}

mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeFalse(), "expected path %q found in ignition", expectedPath)
})

It("should be generated if enabled both by the master flag and injected", func() {
profile.Annotations = map[string]string{
"kubeletconfig.prefer-align-cpus-by-uncorecache": "true",
"kubeletconfig.experimental": `{"cpuManagerPolicyOptions": { "prefer-align-cpus-by-uncorecache": "true", "full-pcpus-only": "false" }}`,
}

mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeTrue(), "expected path %q not found in ignition", expectedPath)
})

It("should be generated if enabled both by the master flag but not injected", func() {
profile.Annotations = map[string]string{
"kubeletconfig.prefer-align-cpus-by-uncorecache": "true",
}

mfs, err := GetNewComponents(profile, opts)
Expect(err).ToNot(HaveOccurred())

ok, err := findFileInIgnition(mfs, expectedPath)
Expect(err).ToNot(HaveOccurred())
Expect(ok).To(BeTrue(), "expected path %q not found in ignition", expectedPath)
})
})
})

func findFileInIgnition(mfs *ManifestResultSet, filePath string) (bool, error) {
result := igntypes.Config{}
err := json.Unmarshal(mfs.MachineConfig.Spec.Config.Raw, &result)
if err != nil {
return false, err
}

for _, ignFile := range result.Storage.Files {
if ignFile.Path == filePath {
return true, nil
}
}
return false, nil
}
Loading