yonahd / kor

A Golang Tool to discover unused Kubernetes Resources
MIT License
974 stars 91 forks source link

Bug: kor exporter cannot parse json #226

Closed Yuni-sa closed 6 months ago

Yuni-sa commented 6 months ago

Describe the bug kor exporter cannot parse json:

Server listening on :8080
collecting unused resources
Error parsing JSON: invalid character '{' after top-level value

json output:

{
  "test": {
    "ConfigMap": null,
    "DaemonSet": null,
    "Deployment": null,
    "Hpa": [
      "php-apache"
    ],
    "Ingress": [
      "test-ingress-1-work"
    ],
    "Job": null,
    "Pdb": null,
    "Pvc": [
      "myclaim"
    ],
    "ReplicaSet": null,
    "Role": null,
    "Secret": null,
    "Service": [
      "app1-service",
      "app2-service"
    ],
    "ServiceAccount": null,
    "StatefulSet": null
  }
}{
  "": {
    "ClusterRole": [
      "admin",
      "view"
    ],
    "Crd": [
      "thanosrulers.monitoring.coreos.com"
    ],
    "Pv": null,
    "StorageClass": null
  }
}
Error parsing JSON: invalid character '{' after top-level value

To Reproduce run kor exporter or kor all -o json after #216 ,also happens with -n namepsace

Expected behavior

{
  "": {
    "ClusterRole": [
      "view"
    ],
    "Crd": [
      "thanosrulers.monitoring.coreos.com"
    ],
    "Pv": null
  },
  "test": {
    "ConfigMap": null,
    "DaemonSet": null,
    "Deployment": null,
    "Hpa": [
      "php-apache"
    ],
    "Ingress": [
      "test-ingress-1-work"
    ],
    "Job": null,
    "Pdb": null,
    "Pvc": [
      "myclaim"
    ],
    "ReplicaSet": null,
    "Role": null,
    "Secret": null,
    "Service": [
      "app1-service",
      "app2-service"
    ],
    "ServiceAccount": null,
    "StatefulSet": null
  }
}

OS version, architecture and kor version Darwin x86_64 Kor version: 3.6.0 after #216 got merged.

Additional context seems like a problem when creating the JSON for namespaced and non namespaced resources with the new GetUnusedAll() which means we also get it when just running kor all -o json. multi seems to work fine:

go run . crd,deploy,svc,storageclass -o json
{
  "": {
    "Crd": [
      "thanosrulers.monitoring.coreos.com"
    ],
    "StorageClass": null
  },
  "default": {
    "Deployment": [
      "test-deployment0"
    ],
    "Service": [
      "alertmanager-example",
      "example-service"
    ]
  },
  "kube-system": {
    "Deployment": null,
    "Service": [
      "docker.io-hostpath"
    ]
  },
  "test": {
    "Deployment": null,
    "Service": [
      "app1-service",
      "app2-service"
    ]
  }
}
Yuni-sa commented 6 months ago

looks like it basically creates two parent json objects: one for namespaced resources and one for non namespaced, while the kor exporter can only parse a single parent object, the objects are added together by using a simple string concatenation with the "+" operator. https://github.com/yonahd/kor/blob/4a79f02b361557d9363e02212f1fbddbef2e0e03/pkg/kor/all.go#L319

doronkg commented 6 months ago

Good catch @Yuni-sa! Must've missed the JSON concatenation in #216 when refactored the GetUnusedAll() function. I'd like to resolve it.

/assign

doronkg commented 6 months ago

WIP in #227.