<p>i would like to parse kubernetes manifest file (json/yaml) and be able to convert them to k8s structures (to later on manipulate them)</p>

<p>I know there is the NewYAMLOrJSONDecoder().Decode() function (<a href="https://github.com/kubernetes/apimachinery/blob/master/pkg/util/yaml/decoder.go" rel="noreferrer">https://github.com/kubernetes/apimachinery/blob/master/pkg/util/yaml/decoder.go</a>) to read a json/yaml file, but the next step is: how to convert them to k8s structure/type?</p>

<p>i.e. if I read a yaml file with a Namespace object, how to convert it to a core/v1/namespace interface for example</p>

<p>Regards,</p>

<pre class="lang-golang prettyprint-override"><code>   import (
        v1 &quot;k8s.io/api/core/v1&quot;
        &quot;k8s.io/apimachinery/pkg/runtime&quot;
        &quot;k8s.io/apimachinery/pkg/runtime/serializer&quot;
        &quot;k8s.io/client-go/kubernetes/scheme&quot;
    )
    
    func decode(data []byte) (*v1.Namespace, error) {
        decoder := serializer.NewCodecFactory(scheme.Scheme).UniversalDecoder()
        object := &amp;v1.Namespace{}
        err := runtime.DecodeInto(decoder, data, object)
        if err != nil {
            return nil, err
        }
        return object, nil
    }
</code></pre>
<p>Pass <code>SchemeGroupVersion</code> generated for your CRDs to <code>UniversalDecoder</code> call, if needed.</p>
<p>Use <code>runtime.Decode</code> instead of <code>runtime.DecodeInto</code> to decode unspecified object type.</p>
