Skip to content

Commit 51c07b4

Browse files
authored
feat(analyzer): let cluster resource case insensitive to fix name inconsistent (#1547)
* feat(analyzer): let cluster resource case insensitive
1 parent bd7bae6 commit 51c07b4

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"kind": "ConfigMapList",
3+
"apiVersion": "v1",
4+
"metadata": {
5+
"resourceVersion": "4825753"
6+
},
7+
"items": [
8+
{
9+
"kind": "ConfigMap",
10+
"apiVersion": "v1",
11+
"metadata": {
12+
"name": "kube-root-ca.crt",
13+
"namespace": "kube-public",
14+
"uid": "37a912b2-a666-480f-a1a8-05ab012432e4",
15+
"resourceVersion": "332",
16+
"creationTimestamp": "2023-05-29T23:33:07Z",
17+
"annotations": {
18+
"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."
19+
},
20+
"managedFields": [
21+
{
22+
"manager": "kube-controller-manager",
23+
"operation": "Update",
24+
"apiVersion": "v1",
25+
"time": "2023-05-29T23:33:07Z",
26+
"fieldsType": "FieldsV1",
27+
"fieldsV1": {
28+
"f:data": {
29+
".": {},
30+
"f:ca.crt": {}
31+
},
32+
"f:metadata": {
33+
"f:annotations": {
34+
".": {},
35+
"f:kubernetes.io/description": {}
36+
}
37+
}
38+
}
39+
}
40+
]
41+
},
42+
"data": {
43+
"ca.crt": "-----BEGIN CERTIFICATE-----\nMIIDBjCCAe6gAwIBAgIBATANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwptaW5p\na3ViZUNBMB4XDTIzMDUyODIzMzIzOVoXDTMzMDUyNjIzMzIzOVowFTETMBEGA1UE\nAxMKbWluaWt1YmVDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOpU\nswd15tD+QCelgYlu5MRB260+Ke63gEDTkXIyAr+mR+TjW/v9TPPIP9iidvTUg+jq\nryrf1PwURVx3EUSZWtd9NpAEDT1ov/ggx16xxj2El7KLen0SXQutSF28zjCFXxYG\nMkPxXu+qYsn2mLJX5i1kaCZNffyToCJ0n2bxxx83rOS+fgz11JntAwcgC8V3Mtq6\nIv+2Xb9PSxPs38ef7r15j5KSTOrmWCR5texPFz/WU/YbZ3W42pj9T/EiuGwamfmI\ngevuwv7AxlfjKutp5UQEth5GhY6V4kJyVIUExN3ddEsTPLQD9zvdsP4DlkGpZmiR\n/Ip3rXY/ldxeeGZ0HUUCAwEAAaNhMF8wDgYDVR0PAQH/BAQDAgKkMB0GA1UdJQQW\nMBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQW\nBBS0XUXhCu/DOH8/0W5L0n4BOw9b6zANBgkqhkiG9w0BAQsFAAOCAQEAHrXOxKdy\n+DvPtvgfzzV3qqmIvTuuFyW0BdZdXV8yo1tZajkEO0B0HvLUyn8ljKgoK5YtCQcr\nzSj6QEewgP+JCBTsWCKzbOhMcDKw1pa6bSeLcQWwMxox+1Zcj7edMPlPcQ3SVLxZ\n6y7fD7BArTKRBKCr8Uwudwox5Vm0URWLRAvb+8jPv9BDuC/uMPJ4UrexL/Q2QQs8\nQkyWYeSk4mBCM3qAahQhYc0WSHbk+a/5iua/y+VUaa208CUbm5glBoAroHDk5eTN\nYstOSLUQAlzTdr4kCEVi+a3+NgmuvzYXWubAdy/PT860aFBJlmVNuDhy8V/bGpnS\nKxmG7B3yJmbmzg==\n-----END CERTIFICATE-----\n"
44+
}
45+
}
46+
]
47+
}

pkg/analyze/kube_resource.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@ import (
1717
)
1818

1919
var Filemap = map[string]string{
20-
"Deployment": constants.CLUSTER_RESOURCES_DEPLOYMENTS,
21-
"StatefulSet": constants.CLUSTER_RESOURCES_STATEFULSETS,
22-
"NetworkPolicy": constants.CLUSTER_RESOURCES_NETWORK_POLICY,
23-
"Pod": constants.CLUSTER_RESOURCES_PODS,
24-
"Ingress": constants.CLUSTER_RESOURCES_INGRESS,
25-
"Service": constants.CLUSTER_RESOURCES_SERVICES,
26-
"ResourceQuota": constants.CLUSTER_RESOURCES_RESOURCE_QUOTA,
27-
"Job": constants.CLUSTER_RESOURCES_JOBS,
28-
"PersistentVolumeClaim": constants.CLUSTER_RESOURCES_PVCS,
20+
"deployment": constants.CLUSTER_RESOURCES_DEPLOYMENTS,
21+
"statefulset": constants.CLUSTER_RESOURCES_STATEFULSETS,
22+
"networkpolicy": constants.CLUSTER_RESOURCES_NETWORK_POLICY,
23+
"pod": constants.CLUSTER_RESOURCES_PODS,
24+
"ingress": constants.CLUSTER_RESOURCES_INGRESS,
25+
"service": constants.CLUSTER_RESOURCES_SERVICES,
26+
"resourcequota": constants.CLUSTER_RESOURCES_RESOURCE_QUOTA,
27+
"job": constants.CLUSTER_RESOURCES_JOBS,
28+
"persistentvolumeclaim": constants.CLUSTER_RESOURCES_PVCS,
2929
"pvc": constants.CLUSTER_RESOURCES_PVCS,
30-
"ReplicaSet": constants.CLUSTER_RESOURCES_REPLICASETS,
31-
"Namespace": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NAMESPACES),
32-
"PersistentVolume": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS),
30+
"replicaset": constants.CLUSTER_RESOURCES_REPLICASETS,
31+
"configmap": constants.CLUSTER_RESOURCES_CONFIGMAPS,
32+
"namespace": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NAMESPACES),
33+
"persistentvolume": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS),
3334
"pv": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_PVS),
34-
"Node": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NODES),
35-
"StorageClass": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_STORAGE_CLASS),
35+
"node": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_NODES),
36+
"storageclass": fmt.Sprintf("%s.json", constants.CLUSTER_RESOURCES_STORAGE_CLASS),
3637
}
3738

3839
type AnalyzeClusterResource struct {
@@ -66,6 +67,9 @@ func FindResource(kind string, clusterScoped bool, namespace string, name string
6667

6768
var datapath string
6869

70+
// lowercase the kind to avoid case sensitivity
71+
kind = strings.ToLower(kind)
72+
6973
resourceLocation, ok := Filemap[kind]
7074

7175
if !ok {

pkg/analyze/kube_resource_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func Test_findResource(t *testing.T) {
2121
resourceExists: true,
2222
analyzer: troubleshootv1beta2.ClusterResource{
2323
CollectorName: "Check namespaced resource",
24-
Kind: "Deployment",
24+
Kind: "deployment",
2525
Namespace: "kube-system",
2626
Name: "coredns",
2727
},
2828
},
2929
{
30-
name: "check default fallthrough",
30+
name: "check default fallthrough with case insensitivity",
3131
resourceExists: true,
3232
analyzer: troubleshootv1beta2.ClusterResource{
3333
CollectorName: "Check namespaced resource",
@@ -40,7 +40,7 @@ func Test_findResource(t *testing.T) {
4040
resourceExists: true,
4141
analyzer: troubleshootv1beta2.ClusterResource{
4242
CollectorName: "Check namespaced resource",
43-
Kind: "Node",
43+
Kind: "node",
4444
ClusterScoped: true,
4545
Name: "repldev-marc",
4646
},
@@ -50,11 +50,21 @@ func Test_findResource(t *testing.T) {
5050
resourceExists: false,
5151
analyzer: troubleshootv1beta2.ClusterResource{
5252
CollectorName: "Check namespaced resource",
53-
Kind: "Node",
53+
Kind: "node",
5454
ClusterScoped: true,
5555
Name: "resource-does-not-exist",
5656
},
5757
},
58+
{
59+
name: "configmap does exist",
60+
resourceExists: true,
61+
analyzer: troubleshootv1beta2.ClusterResource{
62+
CollectorName: "Check namespaced resource",
63+
Kind: "configmap",
64+
Namespace: "kube-public",
65+
Name: "kube-root-ca.crt",
66+
},
67+
},
5868
}
5969

6070
for _, test := range tests {

0 commit comments

Comments
 (0)