<p>I'm looking into writing a tool that generates Kubernetes definitions programatically for our project.</p>

<p>I've found that the API types in Kubernetes can be found in <code>k8s.io/kubernetes/pkg/api</code>. I would like to output YAML based on these types.</p>

<p>Given an object like this:</p>

<pre><code>ns := &amp;api.Namespace{
    ObjectMeta: api.ObjectMeta{
        Name: "test",
    },
}
</code></pre>

<p>What's the best way to generate the YAML output expected by <code>kubectl create</code>?</p>

<p>Their API has updated, so this is how it looks like:</p>

<pre><code>import k8sJson "k8s.io/apimachinery/pkg/runtime/serializer/json"
...
serializer := k8sJson.NewSerializerWithOptions(
    k8sJson.DefaultMetaFactory, nil, nil,
    k8sJson.SerializerOptions{
        Yaml:   true,
        Pretty: true,
        Strict: true,
    },
)
</code></pre>
