<p>I have below code, I would like to convert yaml to client go data structure and get the object name from it</p>

<pre><code>package main

import (
  "fmt"

  "k8s.io/api/extensions/v1beta1"
  "k8s.io/client-go/kubernetes/scheme"
)

var yml = `
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
name: testnginx
replicas: 1
spec: 
template:
  metadata:
    labels:
      run: testnginx
  spec:
    containers:
    - image: nginx
      name: testnginx
      ports:
      - containerPort: 8080
`

func main() {
    decode := scheme.Codecs.UniversalDeserializer().Decode

    obj, _, err := decode([]byte(yml), nil, nil)
    if err != nil {
        fmt.Printf("%#v", err)
    }

  //fmt.Printf("%#v\n", obj)
  deployment := obj.(*v1beta1.Deployment)

    fmt.Printf("%#v\n", deployment.ObjectMeta.Name)
}
</code></pre>

<p>The return result supposes to be testnginx but it is empty</p>

<pre><code>$ ./decode-k8s-exercise 
""
</code></pre>

<p>Not sure why. thanks</p>

Honord
<p>The problem is in the yaml.  The <code>name</code> field should be an attribute inside <code>metadata</code> but they are currently at the same level.  If you space indent the name then it should work (the same applies to <code>template</code> later on). <code>relicas</code> should also be inside <code>spec</code>:</p>

<pre><code>var yml = `
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
  name: testnginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: testnginx
    spec:
      containers:
      - image: nginx
        name: testnginx
        ports:
        - containerPort: 8080
`
</code></pre>
