Skip to content

Commit

Permalink
Merge pull request #1915 from openshift-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1892-to-release-4.15

[release-4.15] UPSTREAM: <carry>: OCPBUGS-31348: fix cpu manager cpuset check
  • Loading branch information
openshift-merge-bot[bot] authored Apr 8, 2024
2 parents 7db3502 + 7bfb5d2 commit 359ecc8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
16 changes: 13 additions & 3 deletions pkg/kubelet/cm/cpumanager/policy_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func (p *staticPolicy) validateState(s state.State) error {
// 1. Check if the reserved cpuset is not part of default cpuset because:
// - kube/system reserved have changed (increased) - may lead to some containers not being able to start
// - user tampered with file
if !p.reservedCPUs.Intersection(tmpDefaultCPUset).Equals(p.reservedCPUs) {
// 2. This only applies when managed mode is disabled. Active workload partitioning feature
// removes the reserved cpus from the default cpu mask on purpose.
if !managed.IsEnabled() && !p.reservedCPUs.Intersection(tmpDefaultCPUset).Equals(p.reservedCPUs) {
return fmt.Errorf("not all reserved cpus: \"%s\" are present in defaultCpuSet: \"%s\"",
p.reservedCPUs.String(), tmpDefaultCPUset.String())
}
Expand Down Expand Up @@ -245,9 +247,17 @@ func (p *staticPolicy) validateState(s state.State) error {
}
}
totalKnownCPUs = totalKnownCPUs.Union(tmpCPUSets...)
if !totalKnownCPUs.Equals(p.topology.CPUDetails.CPUs()) {
availableCPUs := p.topology.CPUDetails.CPUs()

// CPU (workload) partitioning removes reserved cpus
// from the default mask intentionally
if managed.IsEnabled() {
availableCPUs = availableCPUs.Difference(p.reservedCPUs)
}

if !totalKnownCPUs.Equals(availableCPUs) {
return fmt.Errorf("current set of available CPUs \"%s\" doesn't match with CPUs in state \"%s\"",
p.topology.CPUDetails.CPUs().String(), totalKnownCPUs.String())
availableCPUs.String(), totalKnownCPUs.String())
}

return nil
Expand Down
48 changes: 37 additions & 11 deletions pkg/kubelet/cm/cpumanager/policy_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"reflect"
"testing"

"k8s.io/kubernetes/pkg/kubelet/managed"

v1 "k8s.io/api/core/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
Expand Down Expand Up @@ -894,17 +896,18 @@ func TestTopologyAwareAllocateCPUs(t *testing.T) {
// above test cases are without kubelet --reserved-cpus cmd option
// the following tests are with --reserved-cpus configured
type staticPolicyTestWithResvList struct {
description string
topo *topology.CPUTopology
numReservedCPUs int
reserved cpuset.CPUSet
stAssignments state.ContainerCPUAssignments
stDefaultCPUSet cpuset.CPUSet
pod *v1.Pod
expErr error
expNewErr error
expCPUAlloc bool
expCSet cpuset.CPUSet
description string
topo *topology.CPUTopology
numReservedCPUs int
reserved cpuset.CPUSet
stAssignments state.ContainerCPUAssignments
stDefaultCPUSet cpuset.CPUSet
pod *v1.Pod
expErr error
expNewErr error
expCPUAlloc bool
expCSet cpuset.CPUSet
managementPartition bool
}

func TestStaticPolicyStartWithResvList(t *testing.T) {
Expand Down Expand Up @@ -936,9 +939,32 @@ func TestStaticPolicyStartWithResvList(t *testing.T) {
stDefaultCPUSet: cpuset.New(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
expNewErr: fmt.Errorf("[cpumanager] unable to reserve the required amount of CPUs (size of 0-1 did not equal 1)"),
},
{
description: "reserved cores 0 & 6 are not present in available cpuset when management partitioning is enabled",
topo: topoDualSocketHT,
numReservedCPUs: 2,
stAssignments: state.ContainerCPUAssignments{},
managementPartition: true,
expCSet: cpuset.New(1, 2, 3, 4, 5, 7, 8, 9, 10, 11),
},
{
description: "reserved cores 0 & 6 are not present in available cpuset when management partitioning is enabled during recovery",
topo: topoDualSocketHT,
numReservedCPUs: 2,
stAssignments: state.ContainerCPUAssignments{},
stDefaultCPUSet: cpuset.New(1, 2, 3, 4, 5, 7, 8, 9, 10, 11),
managementPartition: true,
expCSet: cpuset.New(1, 2, 3, 4, 5, 7, 8, 9, 10, 11),
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
wasManaged := managed.IsEnabled()
managed.TestOnlySetEnabled(testCase.managementPartition)
defer func() {
managed.TestOnlySetEnabled(wasManaged)
}()

p, err := NewStaticPolicy(testCase.topo, testCase.numReservedCPUs, testCase.reserved, topologymanager.NewFakeManager(), nil)
if !reflect.DeepEqual(err, testCase.expNewErr) {
t.Errorf("StaticPolicy Start() error (%v). expected error: %v but got: %v",
Expand Down
6 changes: 6 additions & 0 deletions pkg/kubelet/managed/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func readEnablementFile() {
}
}

// TestOnlySetEnabled allows changing the state of management partition enablement
// This method MUST NOT be used outside of test code
func TestOnlySetEnabled(enabled bool) {
pinnedManagementEnabled = enabled
}

func IsEnabled() bool {
return pinnedManagementEnabled
}
Expand Down

0 comments on commit 359ecc8

Please sign in to comment.