Skip to content

Commit

Permalink
feat(analyzer): let cluster resource case insensitive to fix name inc…
Browse files Browse the repository at this point in the history
…onsistent (#1547)

* feat(analyzer): let cluster resource case insensitive
  • Loading branch information
DexterYan authored May 21, 2024
1 parent bd7bae6 commit 51c07b4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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+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"
}
}
]
}
32 changes: 18 additions & 14 deletions pkg/analyze/kube_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ 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,
"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),
"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 {
Expand Down Expand Up @@ -66,6 +67,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 {
Expand Down
18 changes: 14 additions & 4 deletions pkg/analyze/kube_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
Expand All @@ -50,11 +50,21 @@ 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",
},
},
{
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 {
Expand Down

0 comments on commit 51c07b4

Please sign in to comment.