gokubernetesclient-go

Is it possible to parse status field of k8s unstructured. Unstructured


I'm trying to write a k8s controller. Within the controller I want to parse the YAML file from GitHub to unstructured. Unstructured. After parsing, I want to track the status of the applied instance of unstructured. Unstructured. The tracking will try to catch if there's a specific key-value.

I failed to do so, since the unstructured.Unstructured doesn't have a method for getting status. Then I was trying to marshal it to JSON and find the status, but failed.

If you know a way to achieve these, it would be great.


Solution

  • The unstructured package provides "Nested" functions. https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured

    For status you would use:

    unstructured.NestedStringMap(myunstruct.Object, "status")
    

    For the status message:

    unstructured.NestedString(myunstruct.Object, "status", "message")
    

    See Chapter 4 of Programming Kubernetes by Stefan Schimanski and Michael Hausenblas for more discussion of the dynamic client.