<p>I have a folder with name "myspec" which has some kube-spec files , let's say</p>

<ul>
<li>pod.yaml , service.yaml,  secret.yaml</li>
</ul>

<p>when I run the command <strong>"kubectl create -f myspec"</strong> it creates everything , pod , service and secret.</p>

<p>Now I wish to perform the same thing using kubernetes go client library.</p>

<p>I believe the previous poster, meant to post This: </p>

<p>1) You first convert the string to bytes.</p>

<p>2) Then serialize it to a pod.</p>

<p>3) Then create the pod like any other object.</p>

<p>This can be done, without loss of generality, for Pods, Services, ReplicationControllers, Deployments, ConfigMaps, Secrets, and any other kubernetes API object.</p>

<p><strong>example</strong></p>

<pre><code>func CreatePodFromYaml(podAsYaml string, ns string) error {
    var p v1.Pod
    err := json.Unmarshal([]byte(podAsYaml), &amp;p)
    if err != nil {
        return err
    }
    pod, poderr := kubernetesConfig().CoreV1().Pods(ns).Create(&amp;p)
    if poderr != nil {
        return poderr
    }
    fmt.Printf("Created Pod %q.\n", pod.GetObjectMeta().GetName())
    return nil
}
</code></pre>

<p>To compile this code, you'll also need to make the kubernetesConfig object:</p>

<pre><code>func kubernetesConfig() *kubernetes.Clientset {
    config, err := clientcmd.BuildConfigFromFlags("", "/$HOME/.kube/config")
    if err != nil {
        fmt.Println(err.Error())
    }
    clientset, err := kubernetes.NewForConfig(config)

    if err != nil {
        fmt.Println(err.Error())
    }
    return clientset
}
</code></pre>
