From 9ea7a8e32395dc3865d5e96b3f9c3e481d1a5702 Mon Sep 17 00:00:00 2001 From: Dexter Yan Date: Mon, 20 May 2024 16:02:40 +1200 Subject: [PATCH 1/3] feat(analyzer): let cluster resource case insensitive --- pkg/analyze/kube_resource.go | 31 +++++++++++++++++-------------- pkg/analyze/kube_resource_test.go | 8 ++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pkg/analyze/kube_resource.go b/pkg/analyze/kube_resource.go index 992f1b19e..a9866fa59 100644 --- a/pkg/analyze/kube_resource.go +++ b/pkg/analyze/kube_resource.go @@ -17,22 +17,22 @@ import ( ) var Filemap = map[string]string{ - "Deployment": constants.CLUSTER_RESOURCES_DEPLOYMENTS, - "StatefulSet": constants.CLUSTER_RESOURCES_STATEFULSETS, - "NetworkPolicy": constants.CLUSTER_RESOURCES_NETWORK_POLICY, - "Pod": constants.CLUSTER_RESOURCES_PODS, - "Ingress": constants.CLUSTER_RESOURCES_INGRESS, - "Service": constants.CLUSTER_RESOURCES_SERVICES, - "ResourceQuota": constants.CLUSTER_RESOURCES_RESOURCE_QUOTA, - "Job": constants.CLUSTER_RESOURCES_JOBS, - "PersistentVolumeClaim": constants.CLUSTER_RESOURCES_PVCS, + "deployment": constants.CLUSTER_RESOURCES_DEPLOYMENTS, + "statefulset": constants.CLUSTER_RESOURCES_STATEFULSETS, + "networkpolicy": constants.CLUSTER_RESOURCES_NETWORK_POLICY, + "pod": constants.CLUSTER_RESOURCES_PODS, + "ingress": constants.CLUSTER_RESOURCES_INGRESS, + "service": constants.CLUSTER_RESOURCES_SERVICES, + "resourcequota": constants.CLUSTER_RESOURCES_RESOURCE_QUOTA, + "job": constants.CLUSTER_RESOURCES_JOBS, + "persistentvolumeclaim": constants.CLUSTER_RESOURCES_PVCS, "pvc": constants.CLUSTER_RESOURCES_PVCS, - "ReplicaSet": constants.CLUSTER_RESOURCES_REPLICASETS, - "Namespace": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NAMESPACES), - "PersistentVolume": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS), + "replicaset": constants.CLUSTER_RESOURCES_REPLICASETS, + "namespace": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NAMESPACES), + "persistentvolume": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS), "pv": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS), - "Node": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NODES), - "StorageClass": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_STORAGE_CLASS), + "node": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NODES), + "storageclass": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_STORAGE_CLASS), } type AnalyzeClusterResource struct { @@ -66,6 +66,9 @@ func FindResource(kind string, clusterScoped bool, namespace string, name string var datapath string + // lowercase the kind to avoid case sensitivity + kind = strings.ToLower(kind) + resourceLocation, ok := Filemap[kind] if !ok { diff --git a/pkg/analyze/kube_resource_test.go b/pkg/analyze/kube_resource_test.go index b12e85d01..abf1421e2 100644 --- a/pkg/analyze/kube_resource_test.go +++ b/pkg/analyze/kube_resource_test.go @@ -21,13 +21,13 @@ func Test_findResource(t *testing.T) { resourceExists: true, analyzer: troubleshootv1beta2.ClusterResource{ CollectorName: "Check namespaced resource", - Kind: "Deployment", + Kind: "deployment", Namespace: "kube-system", Name: "coredns", }, }, { - name: "check default fallthrough", + name: "check default fallthrough with case insensitivity", resourceExists: true, analyzer: troubleshootv1beta2.ClusterResource{ CollectorName: "Check namespaced resource", @@ -40,7 +40,7 @@ func Test_findResource(t *testing.T) { resourceExists: true, analyzer: troubleshootv1beta2.ClusterResource{ CollectorName: "Check namespaced resource", - Kind: "Node", + Kind: "node", ClusterScoped: true, Name: "repldev-marc", }, @@ -50,7 +50,7 @@ func Test_findResource(t *testing.T) { resourceExists: false, analyzer: troubleshootv1beta2.ClusterResource{ CollectorName: "Check namespaced resource", - Kind: "Node", + Kind: "node", ClusterScoped: true, Name: "resource-does-not-exist", }, From 5c3c53033f21414bc6beef15cf0d8c39ec51e48e Mon Sep 17 00:00:00 2001 From: Dexter Yan Date: Mon, 20 May 2024 18:00:55 +1200 Subject: [PATCH 2/3] add configmap --- pkg/analyze/kube_resource.go | 1 + pkg/analyze/kube_resource_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/pkg/analyze/kube_resource.go b/pkg/analyze/kube_resource.go index a9866fa59..53dc517bc 100644 --- a/pkg/analyze/kube_resource.go +++ b/pkg/analyze/kube_resource.go @@ -28,6 +28,7 @@ var Filemap = map[string]string{ "persistentvolumeclaim": constants.CLUSTER_RESOURCES_PVCS, "pvc": constants.CLUSTER_RESOURCES_PVCS, "replicaset": constants.CLUSTER_RESOURCES_REPLICASETS, + "configmap": constants.CLUSTER_RESOURCES_CONFIGMAPS, "namespace": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NAMESPACES), "persistentvolume": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS), "pv": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS), diff --git a/pkg/analyze/kube_resource_test.go b/pkg/analyze/kube_resource_test.go index abf1421e2..7482181e6 100644 --- a/pkg/analyze/kube_resource_test.go +++ b/pkg/analyze/kube_resource_test.go @@ -55,6 +55,16 @@ func Test_findResource(t *testing.T) { Name: "resource-does-not-exist", }, }, + { + name: "configmap does exist", + resourceExists: true, + analyzer: troubleshootv1beta2.ClusterResource{ + CollectorName: "Check namespaced resource", + Kind: "configmap", + Namespace: "kube-public", + Name: "kube-root-ca.crt", + }, + }, } for _, test := range tests { From 13650120ea263cb4fea52ded3aecdfca9d93ddc5 Mon Sep 17 00:00:00 2001 From: Dexter Yan Date: Mon, 20 May 2024 18:07:55 +1200 Subject: [PATCH 3/3] add missing file --- .../configmaps/kube-public.json | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pkg/analyze/files/support-bundle/cluster-resources/configmaps/kube-public.json diff --git a/pkg/analyze/files/support-bundle/cluster-resources/configmaps/kube-public.json b/pkg/analyze/files/support-bundle/cluster-resources/configmaps/kube-public.json new file mode 100644 index 000000000..d53f2469b --- /dev/null +++ b/pkg/analyze/files/support-bundle/cluster-resources/configmaps/kube-public.json @@ -0,0 +1,47 @@ +{ + "kind": "ConfigMapList", + "apiVersion": "v1", + "metadata": { + "resourceVersion": "4825753" + }, + "items": [ + { + "kind": "ConfigMap", + "apiVersion": "v1", + "metadata": { + "name": "kube-root-ca.crt", + "namespace": "kube-public", + "uid": "37a912b2-a666-480f-a1a8-05ab012432e4", + "resourceVersion": "332", + "creationTimestamp": "2023-05-29T23:33:07Z", + "annotations": { + "kubernetes.io/description": "Contains a CA bundle that can be used to verify the kube-apiserver when using internal endpoints such as the internal service IP or kubernetes.default.svc. No other usage is guaranteed across distributions of Kubernetes clusters." + }, + "managedFields": [ + { + "manager": "kube-controller-manager", + "operation": "Update", + "apiVersion": "v1", + "time": "2023-05-29T23:33:07Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:data": { + ".": {}, + "f:ca.crt": {} + }, + "f:metadata": { + "f:annotations": { + ".": {}, + "f:kubernetes.io/description": {} + } + } + } + } + ] + }, + "data": { + "ca.crt": "-----BEGIN CERTIFICATE-----\nMIIDBjCCAe6gAwIBAgIBATANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwptaW5p\na3ViZUNBMB4XDTIzMDUyODIzMzIzOVoXDTMzMDUyNjIzMzIzOVowFTETMBEGA1UE\nAxMKbWluaWt1YmVDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOpU\nswd15tD+QCelgYlu5MRB260+Ke63gEDTkXIyAr+mR+TjW/v9TPPIP9iidvTUg+jq\nryrf1PwURVx3EUSZWtd9NpAEDT1ov/ggx16xxj2El7KLen0SXQutSF28zjCFXxYG\nMkPxXu+qYsn2mLJX5i1kaCZNffyToCJ0n2bxxx83rOS+fgz11Jnt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