Skip to content

Commit

Permalink
test: add more unit tests
Browse files Browse the repository at this point in the history
- add tests for kwok helpers
- fix and update kwok config tests
- fix a bug where gpu label was getting assigned to `kwokConfig.status.key`
- expose `loadConfigFile` -> `LoadConfigFile`
- throw error if templates configmap does not have `templates` key (value of which is node templates)
- finish test for `GPULabel()`
- add tests for `NodeGroupForNode()`
- expose `loadNodeTemplatesFromConfigMap` -> `LoadNodeTemplatesFromConfigMap`
- fix `KwokCloudProvider`'s kwok config was empty (this caused `GPULabel()` to return empty)
Signed-off-by: vadasambar <[email protected]>
  • Loading branch information
vadasambar committed Sep 27, 2023
1 parent 7fda113 commit bc4001d
Show file tree
Hide file tree
Showing 6 changed files with 790 additions and 171 deletions.
4 changes: 2 additions & 2 deletions cluster-autoscaler/cloudprovider/kwok/kwok_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func getConfigMapName() string {
return configMapName
}

func loadConfigFile(kubeClient kubeclient.Interface) (*KwokProviderConfig, error) {
func LoadConfigFile(kubeClient kubeclient.Interface) (*KwokProviderConfig, error) {
configMapName := getConfigMapName()

currentNamespace := getCurrentNamespace()
Expand Down Expand Up @@ -146,8 +146,8 @@ func loadConfigFile(kubeClient kubeclient.Interface) (*KwokProviderConfig, error
} else {
if kwokConfig.Nodes.GPUConfig.GPULabelKey != "" &&
kwokConfig.Nodes.GPUConfig.AvailableGPUTypes != nil {
kwokConfig.status.key = kwokConfig.Nodes.GPUConfig.GPULabelKey
kwokConfig.status.availableGPUTypes = kwokConfig.Nodes.GPUConfig.AvailableGPUTypes
kwokConfig.status.gpuLabel = kwokConfig.Nodes.GPUConfig.GPULabelKey
} else {
return nil, errors.New("nodes.gpuConfig.gpuLabelKey or file.nodes.gpuConfig.availableGPUTypes is empty")
}
Expand Down
22 changes: 13 additions & 9 deletions cluster-autoscaler/cloudprovider/kwok/kwok_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ nodegroups:
# node3: m5.xlarge
# nodegroup1: [node1,node3]
# nodegroup2: [node2]
fromNodeLabelKey: "node.kubernetes.io/instance-type"
fromNodeLabelKey: "kubernetes.io/hostname"
# you can either specify fromNodeLabelKey OR fromNodeAnnotation
# (both are not allowed)
# fromNodeAnnotation: "eks.amazonaws.com/nodegroup"
Expand All @@ -59,7 +59,7 @@ nodes:
"nvidia-tesla-k80": {}
"nvidia-tesla-p100": {}
configmap:
name: kwok-provider-config
name: kwok-provider-templates
kwok: {}
`

Expand All @@ -86,7 +86,7 @@ nodes:
"nvidia-tesla-k80": {}
"nvidia-tesla-p100": {}
configmap:
name: without-kwok
name: kwok-provider-templates
`

const withStaticKwokRelease = `
Expand Down Expand Up @@ -114,7 +114,7 @@ nodes:
kwok:
release: "v0.2.1"
configmap:
name: with-static-kwok-release
name: kwok-provider-templates
`

const skipKwokInstall = `
Expand All @@ -140,7 +140,7 @@ nodes:
"nvidia-tesla-k80": {}
"nvidia-tesla-p100": {}
configmap:
name: skip-kwok-install
name: kwok-provider-templates
kwok:
skipInstall: true
`
Expand Down Expand Up @@ -168,31 +168,35 @@ func TestLoadConfigFile(t *testing.T) {

os.Setenv("POD_NAMESPACE", "kube-system")

kwokConfig, err := loadConfigFile(fakeClient)
kwokConfig, err := LoadConfigFile(fakeClient)
assert.Nil(t, err)
assert.NotNil(t, kwokConfig)
assert.NotNil(t, kwokConfig.status)
assert.NotEmpty(t, kwokConfig.status.gpuLabel)
assert.NotEmpty(t, kwokConfig.status.kwokRelease)

os.Setenv("KWOK_CONFIG_MAP_NAME", "without-kwok")
kwokConfig, err = loadConfigFile(fakeClient)
kwokConfig, err = LoadConfigFile(fakeClient)
assert.Nil(t, err)
assert.NotNil(t, kwokConfig)
assert.NotNil(t, kwokConfig.status)
assert.NotEmpty(t, kwokConfig.status.gpuLabel)
assert.NotEmpty(t, kwokConfig.status.kwokRelease)

os.Setenv("KWOK_CONFIG_MAP_NAME", "with-static-kwok-release")
kwokConfig, err = loadConfigFile(fakeClient)
kwokConfig, err = LoadConfigFile(fakeClient)
assert.Nil(t, err)
assert.NotNil(t, kwokConfig)
assert.NotNil(t, kwokConfig.status)
assert.NotEmpty(t, kwokConfig.status.gpuLabel)
assert.NotEmpty(t, kwokConfig.status.kwokRelease)
assert.Equal(t, kwokConfig.status.kwokRelease, "v0.2.1")

os.Setenv("KWOK_CONFIG_MAP_NAME", "skip-kwok-install")
kwokConfig, err = loadConfigFile(fakeClient)
kwokConfig, err = LoadConfigFile(fakeClient)
assert.Nil(t, err)
assert.NotNil(t, kwokConfig)
assert.NotNil(t, kwokConfig.status)
assert.NotEmpty(t, kwokConfig.status.gpuLabel)
assert.Empty(t, kwokConfig.status.kwokRelease)
}
6 changes: 5 additions & 1 deletion cluster-autoscaler/cloudprovider/kwok/kwok_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func loadNodeTemplatesFromCluster(kc *KwokProviderConfig,
}

// check https://github.com/vadafoss/node-templates for more info on the parsing logic
func loadNodeTemplatesFromConfigMap(kc *KwokProviderConfig,
func LoadNodeTemplatesFromConfigMap(kc *KwokProviderConfig,
kubeClient kubernetes.Interface,
lister kube_util.NodeLister) ([]*apiv1.Node, error) {
currentNamespace := getCurrentNamespace()
Expand All @@ -68,6 +68,10 @@ func loadNodeTemplatesFromConfigMap(kc *KwokProviderConfig,
return nil, fmt.Errorf("failed to get configmap '%s': %v", kc.ConfigMap.Name, err)
}

if c.Data[templatesKey] == "" {
return nil, fmt.Errorf("configmap '%s' doesn't have 'templates' key", kc.ConfigMap.Name)
}

scheme := runtime.NewScheme()
clientscheme.AddToScheme(scheme)

Expand Down
Loading

0 comments on commit bc4001d

Please sign in to comment.