From 443e19e798e413cbd7116ce9c651ab1dd706b116 Mon Sep 17 00:00:00 2001 From: afdesk Date: Tue, 29 Oct 2024 13:52:34 +0600 Subject: [PATCH 1/4] feat: add default commands for unknown platform --- pkg/jobs/collector.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/jobs/collector.go b/pkg/jobs/collector.go index 31f9a78..2e82cde 100644 --- a/pkg/jobs/collector.go +++ b/pkg/jobs/collector.go @@ -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} } From 095153475284a328f00e11c84c06a57b24b18e72 Mon Sep 17 00:00:00 2001 From: afdesk Date: Tue, 29 Oct 2024 13:53:34 +0600 Subject: [PATCH 2/4] test: improve test cases --- pkg/jobs/collector_test.go | 125 ++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/pkg/jobs/collector_test.go b/pkg/jobs/collector_test.go index 6ee3315..fd4d579 100644 --- a/pkg/jobs/collector_test.go +++ b/pkg/jobs/collector_test.go @@ -178,71 +178,82 @@ 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"}, + }, + } + 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 AKS commands", + platform: "aks", + commandsMap: commandsMap, + want: &NodeCommands{ + Commands: commandsK8s, + }, + }, + { + name: "without command maps", + platform: "aks", + want: &NodeCommands{ + Commands: make([]any, 0), }, }, } From 34ec6fbc63aeef46089a17d963e54fe3ae3d90e3 Mon Sep 17 00:00:00 2001 From: afdesk Date: Tue, 29 Oct 2024 15:19:41 +0600 Subject: [PATCH 3/4] test: add k8s specific command --- pkg/jobs/collector_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/jobs/collector_test.go b/pkg/jobs/collector_test.go index fd4d579..bde9b6c 100644 --- a/pkg/jobs/collector_test.go +++ b/pkg/jobs/collector_test.go @@ -195,6 +195,14 @@ func TestFilterCommandsByPlatform(t *testing.T) { "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{}{ From 3da704c56a8af98a204acbee390d607bdfc08f0e Mon Sep 17 00:00:00 2001 From: afdesk Date: Fri, 1 Nov 2024 15:59:26 +0600 Subject: [PATCH 4/4] refactor: change test values --- pkg/jobs/collector_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/jobs/collector_test.go b/pkg/jobs/collector_test.go index bde9b6c..c2a1619 100644 --- a/pkg/jobs/collector_test.go +++ b/pkg/jobs/collector_test.go @@ -250,8 +250,8 @@ func TestFilterCommandsByPlatform(t *testing.T) { }, }, { - name: "select AKS commands", - platform: "aks", + name: "select commands for unknown platform (by default, using `k8s`)", + platform: "unknown-ks", commandsMap: commandsMap, want: &NodeCommands{ Commands: commandsK8s,