Skip to content

Commit

Permalink
Merge pull request #409 from afdesk/feat/default-platform
Browse files Browse the repository at this point in the history
feat: add default commands for unknown platform
  • Loading branch information
simar7 authored Nov 1, 2024
2 parents 2606b7e + 3da704c commit 89bffc3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 57 deletions.
2 changes: 2 additions & 0 deletions pkg/jobs/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ func filterCommandByPlatform(commands map[string][]any, platform string) NodeCom
filteredCommands := make([]any, 0)
if command, ok := commands[platform]; ok {
filteredCommands = append(filteredCommands, command...)
} else {
filteredCommands = append(filteredCommands, commands["k8s"]...)
}
return NodeCommands{Commands: filteredCommands}
}
Expand Down
133 changes: 76 additions & 57 deletions pkg/jobs/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,71 +178,90 @@ func TestFilterCommands(t *testing.T) {
}

func TestFilterCommandsByPlatform(t *testing.T) {
commandsK8s := []any{
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "rke2"},
},
map[string]interface{}{
"id": "CMD-0002",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "rke2"},
},
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s"},
},
}
commandsRKE2 := []any{
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "rke2"},
},
map[string]interface{}{
"id": "CMD-0002",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "rke2"},
},
}
commandsMap := map[string][]any{
"k8s": commandsK8s,
"rke2": commandsRKE2,
}

tests := []struct {
name string
platform string
commandsMap map[string][]any
want *NodeCommands
}{
{name: "node-collector template",
platform: "k8s",
commandsMap: map[string][]any{
"k8s": {
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
map[string]interface{}{
"id": "CMD-0002",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
},
"aks": {
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
map[string]interface{}{
"id": "CMD-0002",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
},
{
name: "select k8s commands",
platform: "k8s",
commandsMap: commandsMap,
want: &NodeCommands{
Commands: commandsK8s,
},
},
{
name: "select RKE2 commands",
platform: "rke2",
commandsMap: commandsMap,
want: &NodeCommands{
Commands: []any{
map[string]interface{}{
"id": "CMD-0001",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
map[string]interface{}{
"id": "CMD-0002",
"title": "kubelet.conf file permissions",
"key": "kubeletConfFilePermissions",
"nodeType": "worker",
"audit": "stat -c %a $kubelet.kubeconfig",
"platforms": []interface{}{"k8s", "aks"},
},
},
Commands: commandsRKE2,
},
},
{
name: "select commands for unknown platform (by default, using `k8s`)",
platform: "unknown-ks",
commandsMap: commandsMap,
want: &NodeCommands{
Commands: commandsK8s,
},
},
{
name: "without command maps",
platform: "aks",
want: &NodeCommands{
Commands: make([]any, 0),
},
},
}
Expand Down

0 comments on commit 89bffc3

Please sign in to comment.