{"record":{"id":"330099d572b89852","repo":"kubernetes/kubernetes","slug":"unable-to-create-pods-no-labels","errorCode":null,"errorMessage":"unable to create pods, no labels","messagePattern":"unable to create pods, no labels","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/controller/controller_utils.go","lineNumber":594,"sourceCode":"\n\tpod := &v1.Pod{\n\t\tObjectMeta: metav1.ObjectMeta{\n\t\t\tLabels:       desiredLabels,\n\t\t\tAnnotations:  desiredAnnotations,\n\t\t\tGenerateName: prefix,\n\t\t\tFinalizers:   desiredFinalizers,\n\t\t},\n\t}\n\tif controllerRef != nil {\n\t\tpod.OwnerReferences = append(pod.OwnerReferences, *controllerRef)\n\t}\n\tpod.Spec = *template.Spec.DeepCopy()\n\treturn pod, nil\n}\n\nfunc (r RealPodControl) createPods(ctx context.Context, namespace string, pod *v1.Pod, object runtime.Object, controllerRef *metav1.OwnerReference) error {\n\tif len(labels.Set(pod.Labels)) == 0 {\n\t\treturn fmt.Errorf(\"unable to create pods, no labels\")\n\t}\n\tnewPod, err := r.KubeClient.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})\n\tif err != nil {\n\t\t// only send an event if the namespace isn't terminating\n\t\tif !apierrors.HasStatusCause(err, v1.NamespaceTerminatingCause) {\n\t\t\tr.Recorder.Eventf(object, v1.EventTypeWarning, FailedCreatePodReason, \"Error creating: %v\", err)\n\t\t}\n\t\treturn err\n\t}\n\tlogger := klog.FromContext(ctx)\n\tif r.OnWrite != nil {\n\t\tr.OnWrite(newPod, controllerRef)\n\t}\n\taccessor, err := meta.Accessor(object)\n\tif err != nil {\n\t\tlogger.Error(err, \"parentObject does not have ObjectMeta\")\n\t\treturn nil\n\t}","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/kubernetes/kubernetes/blob/94c136764292cc5fac976c0de6587daaea56410f/pkg/controller/controller_utils.go#L576-L612","documentation":"createPods checks that the pod being created has at least one label before making the API Create call. Labels are required because controllers use label selectors to find and manage their pods. Without labels, the controller would create pods it can never reconcile, leading to orphaned resources. The pod's labels are copied from PodTemplateSpec.Labels via GetPodFromTemplate.","triggerScenarios":"RealPodControl.createPods is called with a pod whose .Labels map is empty or nil. The pod is built from GetPodFromTemplate which copies labels from PodTemplateSpec.Labels. If the workload spec's template has no labels, this triggers.","commonSituations":"A StatefulSet/ReplicaSet/DaemonSet/Job spec with spec.template.metadata.labels omitted or empty. A template where labels were stripped by a mutating admission webhook. A misconfigured manifest where the selector references labels that don't exist in the template.","solutions":["Ensure the workload spec's spec.template.metadata.labels has at least one label.","Verify labels in the template satisfy spec.selector.matchLabels.","Check for admission webhooks that strip labels from pod templates."],"exampleFix":"# before\nspec:\n  template:\n    metadata: {}\n    spec:\n      containers:\n        - name: app\n\n# after\nspec:\n  selector:\n    matchLabels:\n      app: my-app\n  template:\n    metadata:\n      labels:\n        app: my-app\n    spec:\n      containers:\n        - name: app","handlingStrategy":"validation","validationCode":"// Before creating pods, verify the template has labels\nif len(template.Labels) == 0 {\n    return fmt.Errorf(\"pod template must have at least one label\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always define spec.template.metadata.labels on workload resources.","Ensure labels in the template satisfy spec.selector.matchLabels.","Test pod templates with kubectl apply --dry-run=server before applying.","Check for mutating admission webhooks that might strip labels."],"tags":["controller","pod-control","labels","pod-template","validation"],"backgroundTag":null,"analyzedSha":"94c136764292cc5fac976c0de6587daaea56410f","analyzedAt":"2026-08-08T23:58:27.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}