<p>I'm not sure if the title is the correct terminology..But I basically want to use the go-client and get kubectl compliant objects (yamls).</p>

<p>i.e a deployment resource would be:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.4
        ports:
        - containerPort: 80
</code></pre>

<p>I can get deployments from my k8s cluster via go-client like such:</p>

<pre><code>    Deployments, err := clientset.AppsV1().Deployments().List(metav1.ListOptions{})
    //and then loop through each deployment:
    for _, deploy := range Deployments.Items{
     //deploy is type v1.Deployment
    }
</code></pre>

<p>if I was to marshal deployment and save to file, the struct is:</p>

<pre><code>type Deployment struct {
    v1.TypeMeta    `json:",inline"`
    v1.ObjectMeta  `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
    Spec              DeploymentSpec    `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
    Status            DeploymentStatus  `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
</code></pre>

<p>obviously much different from what kubectl file is expecting. </p>

<p>(Though i can use go-client deployment.create(obj) to create that deployment).</p>

<p>If I wanted to create a kubectl valid resource, I <strong><em>could</em></strong> create a custom struct that adheres to that type, and then manually fill in the values.</p>

<p>Is there any way to do this automatically? or some helper functions that is there currently?</p>

<p>in essence i want to convert the v1.Deployment struct to the generic kubectl yaml resource.</p>

<p>Following is the way I use to encode objects to json with kubectl like structure. As shown below this is specifically for objects from CoreV1 but you can easily register other apis with the used scheme.</p>
<pre><code>scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
codec := serializer.NewCodecFactory(scheme).LegacyCodec(corev1.SchemeGroupVersion)
output, _ := runtime.Encode(codec, stripped)
</code></pre>
