Skip to content
Merged
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 @@ -204,17 +204,17 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch bool
}{{
name: "no clustername, namespace, or label selector specified should match any MachineSet",
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
shouldMatch: true,
}, {
name: "no clustername, namespace, or label selector specified should match any MachineDeployment",
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{labelSelector: labels.NewSelector()},
shouldMatch: true,
}, {
name: "clustername specified does not match MachineSet, namespace matches, no labels specified",
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -223,7 +223,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -232,7 +232,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "namespace specified does not match MachineSet, clusterName matches, no labels specified",
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -241,7 +241,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "clustername specified does not match MachineDeployment, namespace matches, no labels specified",
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec(RandomString(6), "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -250,7 +250,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "namespace and clusterName matches MachineSet, no labels specified",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -259,7 +259,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: true,
}, {
name: "namespace and clusterName matches MachineDeployment, no labels specified",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -268,7 +268,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: true,
}, {
name: "namespace and clusterName matches MachineSet, does not match label selector",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -277,7 +277,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "namespace and clusterName matches MachineDeployment, does not match label selector",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
namespace: "default",
Expand All @@ -286,7 +286,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: false,
}, {
name: "namespace, clusterName, and label selector matches MachineSet",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
Expand All @@ -296,7 +296,7 @@ func Test_allowedByAutoDiscoverySpec(t *testing.T) {
shouldMatch: true,
}, {
name: "namespace, clusterName, and label selector matches MachineDeployment",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoveryConfig: &clusterAPIAutoDiscoveryConfig{
clusterName: "foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,23 +1288,23 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
shouldMatch bool
}{{
name: "autodiscovery specs includes permissive spec that should match any MachineSet",
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, false, nil, nil, map[string]string{}),
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{labelSelector: labels.NewSelector()},
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
},
shouldMatch: true,
}, {
name: "autodiscovery specs includes permissive spec that should match any MachineDeployment",
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec(RandomString(6), RandomString(6), RandomString(6), 1, true, nil, nil, map[string]string{}),
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{labelSelector: labels.NewSelector()},
{clusterName: "foo", namespace: "bar", labelSelector: labels.Nothing()},
},
shouldMatch: true,
}, {
name: "autodiscovery specs includes a restrictive spec that should match specific MachineSet",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
Expand All @@ -1313,7 +1313,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
shouldMatch: true,
}, {
name: "autodiscovery specs includes a restrictive spec that should match specific MachineDeployment",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{clusterName: "foo", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "green"})},
Expand All @@ -1322,7 +1322,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
shouldMatch: true,
}, {
name: "autodiscovery specs does not include any specs that should match specific MachineSet",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, false, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},
Expand All @@ -1331,7 +1331,7 @@ func Test_machineController_allowedByAutoDiscoverySpecs(t *testing.T) {
shouldMatch: false,
}, {
name: "autodiscovery specs does not include any specs that should match specific MachineDeployment",
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil),
testSpec: createTestSpec("default", "foo", RandomString(6), 1, true, nil, nil, map[string]string{}),
additionalLabels: map[string]string{"color": "green"},
autoDiscoverySpecs: []*clusterAPIAutoDiscoveryConfig{
{clusterName: "test", namespace: "default", labelSelector: labels.SelectorFromSet(labels.Set{"color": "blue"})},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {

type testCaseConfig struct {
nodeLabels map[string]string
managedLabels map[string]string
includeNodes bool
expectedErr error
expectedCapacity map[corev1.ResourceName]int64
Expand Down Expand Up @@ -1618,6 +1619,37 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
},
},
{
name: "When the NodeGroup can scale from zero, and the scalable resource contains managed labels",
nodeGroupAnnotations: map[string]string{
memoryKey: "2048Mi",
cpuKey: "2",
gpuTypeKey: gpuapis.ResourceNvidiaGPU,
gpuCountKey: "1",
},
config: testCaseConfig{
expectedErr: nil,
nodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
},
managedLabels: map[string]string{
"node-role.kubernetes.io/test": "test",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"kubernetes.io/hostname": "random value",
"node-role.kubernetes.io/test": "test",
},
},
},
}

test := func(t *testing.T, testConfig *TestConfig, config testCaseConfig) {
Expand Down Expand Up @@ -1704,6 +1736,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
WithNamespace(testNamespace).
WithNodeCount(10).
WithAnnotations(cloudprovider.JoinStringMaps(enableScaleAnnotations, tc.nodeGroupAnnotations)).
WithManagedLabels(tc.config.managedLabels).
Build()
test(t, testConfig, tc.config)
})
Expand All @@ -1714,6 +1747,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
WithNamespace(testNamespace).
WithNodeCount(10).
WithAnnotations(cloudprovider.JoinStringMaps(enableScaleAnnotations, tc.nodeGroupAnnotations)).
WithManagedLabels(tc.config.managedLabels).
Build()
test(t, testConfig, tc.config)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,26 @@ const (
)

type testConfigBuilder struct {
scalableType scalableTestType
namespace string
clusterName string
namePrefix string
nodeCount int
annotations map[string]string
capacity map[string]string
scalableType scalableTestType
namespace string
clusterName string
namePrefix string
nodeCount int
annotations map[string]string
capacity map[string]string
managedLabels map[string]string
}

// NewTestConfigBuilder returns a builder for dynamically constructing mock ClusterAPI resources for testing.
func NewTestConfigBuilder() *testConfigBuilder {
return &testConfigBuilder{
namespace: RandomString(6),
clusterName: RandomString(6),
namePrefix: RandomString(6),
nodeCount: 0,
annotations: nil,
capacity: nil,
namespace: RandomString(6),
clusterName: RandomString(6),
namePrefix: RandomString(6),
nodeCount: 0,
annotations: nil,
capacity: nil,
managedLabels: nil,
}
}

Expand All @@ -91,6 +93,7 @@ func (b *testConfigBuilder) Build() *TestConfig {
isMachineDeployment,
b.annotations,
b.capacity,
b.managedLabels,
)[0],
)[0]
}
Expand All @@ -111,6 +114,7 @@ func (b *testConfigBuilder) BuildMultiple(configCount int) []*TestConfig {
isMachineDeployment,
b.annotations,
b.capacity,
b.managedLabels,
)...,
)
}
Expand Down Expand Up @@ -171,6 +175,19 @@ func (b *testConfigBuilder) WithCapacity(c map[string]string) *testConfigBuilder
return b
}

func (b *testConfigBuilder) WithManagedLabels(l map[string]string) *testConfigBuilder {
if l == nil {
// explicitly setting managed labels to nil
b.managedLabels = nil
} else {
if b.managedLabels == nil {
b.managedLabels = map[string]string{}
}
maps.Insert(b.managedLabels, maps.All(l))
}
return b
}

// TestConfig contains clusterspecific information about a single test configuration.
type TestConfig struct {
spec *TestSpec
Expand Down Expand Up @@ -220,6 +237,9 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
"kind": machineTemplateKind,
"name": "TestMachineTemplate",
},
"metadata": map[string]interface{}{
"labels": map[string]interface{}{},
},
},
},
},
Expand All @@ -229,6 +249,12 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {

config.machineSet.SetAnnotations(make(map[string]string))

if spec.managedLabels != nil {
if err := unstructured.SetNestedStringMap(config.machineSet.Object, spec.managedLabels, "spec", "template", "spec", "metadata", "labels"); err != nil {
panic(err)
}
}

if !spec.rootIsMachineDeployment {
config.machineSet.SetAnnotations(spec.annotations)
} else {
Expand Down Expand Up @@ -258,6 +284,9 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
"kind": machineTemplateKind,
"name": "TestMachineTemplate",
},
"metadata": map[string]interface{}{
"labels": map[string]interface{}{},
},
},
},
},
Expand All @@ -278,6 +307,12 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
},
}
config.machineSet.SetOwnerReferences(ownerRefs)

if spec.managedLabels != nil {
if err := unstructured.SetNestedStringMap(config.machineDeployment.Object, spec.managedLabels, "spec", "template", "spec", "metadata", "labels"); err != nil {
panic(err)
}
}
}
config.machineSet.SetLabels(machineSetLabels)
if err := unstructured.SetNestedStringMap(config.machineSet.Object, machineSetLabels, "spec", "selector", "matchLabels"); err != nil {
Expand Down Expand Up @@ -324,6 +359,7 @@ func createTestConfigs(specs ...TestSpec) []*TestConfig {
type TestSpec struct {
annotations map[string]string
capacity map[string]string
managedLabels map[string]string
machineDeploymentName string
machineSetName string
machinePoolName string
Expand All @@ -333,20 +369,21 @@ type TestSpec struct {
rootIsMachineDeployment bool
}

func createTestSpecs(namespace, clusterName, namePrefix string, scalableResourceCount, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string) []TestSpec {
func createTestSpecs(namespace, clusterName, namePrefix string, scalableResourceCount, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string, managedLabels map[string]string) []TestSpec {
var specs []TestSpec

for i := 0; i < scalableResourceCount; i++ {
specs = append(specs, createTestSpec(namespace, clusterName, fmt.Sprintf("%s-%d", namePrefix, i), nodeCount, isMachineDeployment, annotations, capacity))
specs = append(specs, createTestSpec(namespace, clusterName, fmt.Sprintf("%s-%d", namePrefix, i), nodeCount, isMachineDeployment, annotations, capacity, managedLabels))
}

return specs
}

func createTestSpec(namespace, clusterName, name string, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string) TestSpec {
func createTestSpec(namespace, clusterName, name string, nodeCount int, isMachineDeployment bool, annotations map[string]string, capacity map[string]string, managedLabels map[string]string) TestSpec {
return TestSpec{
annotations: annotations,
capacity: capacity,
managedLabels: managedLabels,
machineDeploymentName: name,
machineSetName: name,
clusterName: clusterName,
Expand Down
Loading
Loading