How to check if namespace exists or not?

I cannot find a way to check if a specific namespace exists or not, while printing a helpful error message (without using “run” collector).

Any ideas?

P.S. I tried the following, which works well for the positive case (namespace exists), but for the negative case (namespace doesn’t exist), it will return an inaccurate message “YAML path provided is invalid”, even if the resource doesn’t exist at all. I would rather expect “Resource doesn’t exist” or something.

---
apiVersion: troubleshoot.sh/v1beta2
kind: SupportBundle
metadata:
  name: namespace-check
spec:
  collectors:
    - clusterResources: {}
    - clusterInfo:
        exclude: true
  analyzers:
    - clusterResource:
      checkName: "Namespace check"
      kind: namespace
      name: foobar
      yamlPath: "status.phase"
      regex: Active
      clusterScoped: true
      outcomes:
        - pass:
          when: "true"
          message: "Namespace 'foobar' exists"
        - warn:
          when: "false"
          message: "Namespace 'foobar' doesn't exist"

Thank you for bringing this to our attention. I think there is indeed a bug in troubleshoot. The error message YAML path provided is invalid is very misleading, since the YAML path is correct, but the specified resource was not found.

I drafted a quick PR to test and fix this issue, but I will need to run this first by some other engineers and our product team. If this fix is good, we will pull it in and it should be in the next release. If not, I will update you here.

Thanks for your quick reply and efforts!

Besides this specific issue: Do you know another way of checking if a namespace exists or not?

The way how you are doing this is the most natural way of checking for the existence of a namespace. There is not better way in troubleshoot.

The PR has been merged. This change will be in the next Troubleshoot release

Great, thank you very much! :slight_smile: