<p>Programmatically creating a pod using the Kubernetes client-go gives me the following error:
<code>an empty namespace may not be set during creation</code></p>

<p>Started from this example:
<a href="https://github.com/feiskyer/go-examples/blob/master/kubernetes/pod-create/pod.go" rel="nofollow noreferrer">https://github.com/feiskyer/go-examples/blob/master/kubernetes/pod-create/pod.go</a></p>

<pre><code>#go

handler := clientset.CoreV1().Pods("").PodInterface

pod := apiv1.Pod{
    TypeMeta: metav1.TypeMeta{
        Kind:       "Pod",
        APIVersion: "v1",
    },
    ObjectMeta: metav1.ObjectMeta{
        Name:      "my-pod",
        Namespace: "my-namespace",
    },
    Spec: apiv1.PodSpec{
        Containers: []apiv1.Container{
            {
                Name:  "my-container",
                Image: "my-container",
            },
        },
    },
}

result, err := handler.Create(pod)
</code></pre>

<p><strong>Expectation</strong>: Pod is created.<br>
<strong>Actual</strong>: Creation fails with k8s error: <em>an empty namespace may not be set during creation</em></p>

C. Damoc
<p>To fix the issue above, I had to specify the namespace in the following line:</p>

<pre><code>handler := clientset.CoreV1().Pods("my-namespace").PodInterface
</code></pre>

<p>This fixed the error, because it is not allowed to create a pod outside an namespace. So, even if the namespace was provided in the pod object, it also must be specified 'as a flag'.</p>

<p>So, it should be similar to something like (see the flag --namespace in the command): </p>

<pre><code>#my-pod-file-definition.yaml
----------------------------
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  containers:
  - name: my-container
    image: my-image
</code></pre>

<p><code>kubectl apply -f my-pod-file-definition.yaml --namespace=my-namespace</code></p>
