lang2
lang2

Reputation: 11986

Create API object from a list of YAML object

I have a list of kubernetes objects defined like this:

apiVersion: v1
items:
kind: List
- .....

I'd like to parse it with something like client-go and gather some information on it.

So I searched and found some piece of code like this:

decode := api.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(f), nil, nil)

lst, err := meta.ExtractList(obj)
for _, o := range lst {
    doSomeThing(o)
}

So obj and each of its element is runtime.Object kind. But I can't seem to find a way to convert that into a concrete kubernetes object like v1.Pod. How can I do that?

Upvotes: 0

Views: 471

Answers (1)

user6723657
user6723657

Reputation: 11

To convert runtime objects to Kubernetes objects, you can do something like:

switch typed := obj.(type) {
  case *apiv1.Pod:
    log.Info(typed.Name) // type is Pod
  case *apiv1.Namespace:
  // typed is Namespace
}

Upvotes: 1

Related Questions