<p>I'm using <a href="https://github.com/kubernetes/client-go" rel="noreferrer">https://github.com/kubernetes/client-go</a> and all works well.</p>

<p>I have a manifest (YAML) for the official Kubernetes Dashboard: <a href="https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml" rel="noreferrer">https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml</a></p>

<p>I want to mimic <code>kubectl apply</code> of this manifest in Go code, using client-go.</p>

<p>I understand that I need to do some (un)marshalling of the YAML bytes into the correct API types defined in package: <a href="https://github.com/kubernetes/api" rel="noreferrer">https://github.com/kubernetes/api</a></p>

<p>I have successfully <code>Create</code>ed single API types to my cluster, <strong>but how do I do this for a manifest that contains a list of types that are not the same</strong>? Is there a resource <code>kind: List*</code> that supports these different types?</p>

<p>My current workaround is to split the YAML file using <code>csplit</code> with --- as the delimiter</p>

<pre class="lang-sh prettyprint-override"><code>csplit /path/to/recommended.yaml /---/ '{*}' --prefix='dashboard.' --suffix-format='%03d.yaml'
</code></pre>

<p>Next, I loop over the new (14) parts that were created, read their bytes, switch on the type of the object returned by the UniversalDeserializer's decoder and call the correct API methods using my k8s clientset.</p>

<p>I would like to do this to programmatically to make updates to any new versions of the dashboard into my cluster. I will also need to do this for the Metrics Server and many other resources. The alternative (maybe simpler) method is to ship my code with kubectl installed to the container image and directly call <code>kubectl apply -f -</code>; but that means I also need to write the kube config to disk or maybe pass it inline so that kubectl can use it.</p>

<p>I found this issue to be helpful: <a href="https://github.com/kubernetes/client-go/issues/193" rel="noreferrer">https://github.com/kubernetes/client-go/issues/193</a>
The decoder lives here: <a href="https://github.com/kubernetes/apimachinery/tree/master/pkg/runtime/serializer" rel="noreferrer">https://github.com/kubernetes/apimachinery/tree/master/pkg/runtime/serializer</a></p>

<p>It's exposed in client-go here: <a href="https://github.com/kubernetes/client-go/blob/master/kubernetes/scheme/register.go#L69" rel="noreferrer">https://github.com/kubernetes/client-go/blob/master/kubernetes/scheme/register.go#L69</a></p>

<p>I've also taken a look at the RunConvert method that is used by kubectl: <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/convert/convert.go#L139" rel="noreferrer">https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/convert/convert.go#L139</a> and assume that I can provide my own <a href="https://github.com/kubernetes/cli-runtime/blob/master/pkg/genericclioptions/io_options.go#L27" rel="noreferrer">genericclioptions.IOStreams</a> to get the output?</p>

<p><a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/convert/convert.go#L141-L145" rel="noreferrer">It looks like RunConvert is on a deprecation path</a></p>

<p>I've also looked at other questions tagged [client-go] but most use old examples or use a YAML file with a single <code>kind</code> defined, and the API has changed since.</p>

<p>Edit: Because I need to do this for more than one cluster and am creating clusters programmatically (AWS EKS API + CloudFormation/<a href="https://github.com/weaveworks/eksctl" rel="noreferrer">eksctl</a>), I would like to minimize the overhead of creating <code>ServiceAccount</code>s across many cluster contexts, across many AWS accounts. Ideally, the only authentication step involved in creating my clientset is using <a href="https://github.com/kubernetes-sigs/aws-iam-authenticator" rel="noreferrer">aws-iam-authenticator</a> to get a token using cluster data (name, region, CA cert, etc). There hasn't been a release of aws-iam-authenticator for a while, but the contents of <code>master</code> allow for the use of a third-party role cross-account role and external ID to be passed. IMO, this is cleaner than using a <code>ServiceAccount</code> (and <a href="https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html" rel="noreferrer">IRSA</a>) because there are other AWS services the application (the backend API which creates and applies add-ons to these clusters) needs to interact with.</p>

<p>Edit: I have recently found <a href="https://github.com/ericchiang/k8s" rel="noreferrer">https://github.com/ericchiang/k8s</a>. It's definitely simpler to use than client-go, at a high-level, but doesn't support this behavior.</p>

Simon
<p>It sounds like you've figured out how to deserialize YAML files into Kubernetes <code>runtime.Object</code>s, but the problem is dynamically deploying a <code>runtime.Object</code> without writing special code for each Kind.</p>
<p><code>kubectl</code> achieves this by interacting with the <a href="https://godoc.org/k8s.io/client-go/rest" rel="nofollow noreferrer">REST API</a> directly. Specifically, via <a href="https://godoc.org/k8s.io/cli-runtime/pkg/resource#Helper" rel="nofollow noreferrer">resource.Helper</a>.</p>
<p>In my code, I have something like:</p>
<pre><code>import (
    meta &quot;k8s.io/apimachinery/pkg/api/meta&quot;
    &quot;k8s.io/apimachinery/pkg/runtime&quot;
    &quot;k8s.io/apimachinery/pkg/runtime/schema&quot;
    &quot;k8s.io/cli-runtime/pkg/resource&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    &quot;k8s.io/client-go/rest&quot;
    &quot;k8s.io/client-go/restmapper&quot;
)

func createObject(kubeClientset kubernetes.Interface, restConfig rest.Config, obj runtime.Object) (runtime.Object, error) {
    // Create a REST mapper that tracks information about the available resources in the cluster.
    groupResources, err := restmapper.GetAPIGroupResources(kubeClientset.Discovery())
    if err != nil {
        return nil, err
    }
    rm := restmapper.NewDiscoveryRESTMapper(groupResources)

    // Get some metadata needed to make the REST request.
    gvk := obj.GetObjectKind().GroupVersionKind()
    gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
    mapping, err := rm.RESTMapping(gk, gvk.Version)
    if err != nil {
        return nil, err
    }

    namespace, err := meta.NewAccessor().Namespace(obj)
    if err != nil {
        return nil, err
    }

    // Create a client specifically for creating the object.
    restClient, err := newRestClient(restConfig, mapping.GroupVersionKind.GroupVersion())
    if err != nil {
        return nil, err
    }

    // Use the REST helper to create the object in the &quot;default&quot; namespace.
    restHelper := resource.NewHelper(restClient, mapping)
    return restHelper.Create(namespace, false, obj)
}

func newRestClient(restConfig rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
    restConfig.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
    restConfig.GroupVersion = &amp;gv
    if len(gv.Group) == 0 {
        restConfig.APIPath = &quot;/api&quot;
    } else {
        restConfig.APIPath = &quot;/apis&quot;
    }

    return rest.RESTClientFor(&amp;restConfig)
}

</code></pre>
