Skip to content

Commit

Permalink
corrected resource issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elankath committed Nov 12, 2024
1 parent 69a3ac9 commit f42789a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
23 changes: 9 additions & 14 deletions cluster-autoscaler/cloudprovider/mcm/mcm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ var (

// ErrInvalidNodeTemplate is a sentinel error that indicates that the nodeTemplate is invalid.
ErrInvalidNodeTemplate = errors.New("invalid node template")
coreResourceNames = []v1.ResourceName{v1.ResourceCPU, v1.ResourceMemory}
gpuResourceNames = []v1.ResourceName{"gpu", gpu.ResourceNvidiaGPU}
knownResourceNames = slices.Concat(coreResourceNames, gpuResourceNames, []v1.ResourceName{v1.ResourcePods, v1.ResourceEphemeralStorage})
coreResourceNames = []v1.ResourceName{v1.ResourceCPU, v1.ResourceMemory, "gpu"}
extraResourceNames = []v1.ResourceName{gpu.ResourceNvidiaGPU, v1.ResourcePods, v1.ResourceEphemeralStorage}
knownResourceNames = slices.Concat(coreResourceNames, extraResourceNames)
)

// McmManager manages the client communication for MachineDeployments.
Expand Down Expand Up @@ -715,7 +715,6 @@ func validateNodeTemplate(nodeTemplateAttributes *v1alpha1.NodeTemplate) error {

if allErrs != nil {
return errors.Join(allErrs...)
//return fmt.Errorf("%s", allErrs)
}

return nil
Expand Down Expand Up @@ -979,7 +978,7 @@ func (m *McmManager) buildNodeFromTemplate(name string, template *nodeTemplate)

// populate extended resources from nodeTemplate
if len(template.InstanceType.ExtendedResources) > 0 {
klog.V(3).Infof("Copying extended resources %v to template node.Status.Capacity", template.InstanceType.ExtendedResources)
klog.V(2).Infof("Copying extended resources %v to template node.Status.Capacity", template.InstanceType.ExtendedResources)
maps.Copy(node.Status.Capacity, template.InstanceType.ExtendedResources)
}

Expand Down Expand Up @@ -1031,15 +1030,11 @@ func isMachineFailedOrTerminating(machine *v1alpha1.Machine) bool {
return false
}

// filterExtendedResources removes knownResourceNames from allResources and retains only the extendedResources.
func filterExtendedResources(allResources v1.ResourceList) (extendedResources v1.ResourceList) {
extendedResources = make(v1.ResourceList)
for _, n := range knownResourceNames {
r, ok := allResources[n]
if ok {
// don't add known resources to extendedResources
continue
}
extendedResources[n] = r
}
extendedResources = allResources.DeepCopy()
maps.DeleteFunc(extendedResources, func(name v1.ResourceName, _ resource.Quantity) bool {
return slices.Contains(knownResourceNames, name)
})
return
}
23 changes: 21 additions & 2 deletions cluster-autoscaler/cloudprovider/mcm/mcm_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"k8s.io/utils/ptr"
"maps"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -196,14 +198,31 @@ func TestBuildNodeFromTemplate(t *testing.T) {
assert.Contains(t, node.Status.Capacity, k, "node.Status.Capacity should contain the mandatory resource named: %s", k)
}
var hasGpuResource bool
for _, k := range gpuResourceNames {
for _, k := range extraResourceNames {
q, ok := node.Status.Capacity[k]
if ok {
hasGpuResource = true
assert.Equal(t, gpuQuantity, q, "node.Status.Capacity has gpu resource named %q with value %s instead of %s", k, q, gpuQuantity)
}
}
assert.True(t, hasGpuResource, "node.Status.Capacity should have a gpu resource with one of the names %q", gpuResourceNames)
assert.True(t, hasGpuResource, "node.Status.Capacity should have a gpu resource with one of the names %q", extraResourceNames)
}

func TestFilterExtendedResources(t *testing.T) {
resources := make(apiv1.ResourceList)
for _, n := range knownResourceNames {
resources[n] = *resource.NewQuantity(rand.Int64(), resource.DecimalSI)
}
customResources := make(apiv1.ResourceList)
customResources["resource.com/dongle"] = resource.MustParse("50")
customResources["quantum.com/memory"] = resource.MustParse("100Gi")

allResources := resources.DeepCopy()
maps.Copy(allResources, customResources)

extendedResources := filterExtendedResources(allResources)
t.Logf("TestFilterExtendedResources obtained: %+v", extendedResources)
assert.Equal(t, customResources, extendedResources)
}

func createSampleInstanceType(instanceTypeName string, customResourceName apiv1.ResourceName, customResourceQuantity resource.Quantity) *instanceType {
Expand Down

0 comments on commit f42789a

Please sign in to comment.