{"record":{"id":"8661bcb097525543","repo":"kubernetes/kops","slug":"label-q-not-set-on-v","errorCode":null,"errorMessage":"label %q not set on %v","messagePattern":"label %q not set on (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/controllers/clusterapi/utils.go","lineNumber":66,"sourceCode":"\t\t{\n\t\t\tAPIVersion: apiVersion,\n\t\t\tKind:       kind,\n\t\t\tName:       owner.GetName(),\n\t\t\tUID:        owner.GetUID(),\n\t\t\tController: new(true),\n\t\t},\n\t})\n}\n\nfunc getCAPIClusterFromCAPIObject(ctx context.Context, kube client.Client, obj client.Object) (*unstructured.Unstructured, error) {\n\tid := types.NamespacedName{\n\t\tNamespace: obj.GetNamespace(),\n\t\tName:      obj.GetName(),\n\t}\n\n\tcapiClusterName := obj.GetLabels()[clusterv1.ClusterNameLabel]\n\tif capiClusterName == \"\" {\n\t\treturn nil, fmt.Errorf(\"label %q not set on %v\", clusterv1.ClusterNameLabel, id)\n\t}\n\n\tcapiCluster := &unstructured.Unstructured{}\n\tcapiCluster.SetGroupVersionKind(schema.GroupVersionKind{\n\t\tGroup:   \"cluster.x-k8s.io\",\n\t\tVersion: \"v1beta1\",\n\t\tKind:    \"Cluster\",\n\t})\n\tclusterKey := types.NamespacedName{\n\t\tNamespace: obj.GetNamespace(),\n\t\tName:      capiClusterName,\n\t}\n\tif err := kube.Get(ctx, clusterKey, capiCluster); err != nil {\n\t\treturn nil, fmt.Errorf(\"error fetching cluster %v: %w\", clusterKey, err)\n\t}\n\n\treturn capiCluster, nil\n}","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/controllers/clusterapi/utils.go#L48-L84","documentation":"getCAPIClusterFromCAPIObject resolves the parent cluster.x-k8s.io Cluster for any CAPI object by reading the well-known `cluster.x-k8s.io/cluster-name` label. This error is thrown when the object passed in (e.g. a Machine, MachineDeployment, or KopsControlPlane) has no value for that label, so the owning CAPI Cluster cannot be determined from the object alone.","triggerScenarios":"Reconcile calls getCAPIClusterFromCAPIObject with an object whose GetLabels()[clusterv1.ClusterNameLabel] returns \"\" — the `cluster.x-k8s.io/cluster-name` label is absent or empty on the reconciled resource.","commonSituations":"Resources created manually or by tooling that does not apply CAPI's standard topology labels; resources from an older CAPI version predating the label; hand-copied YAML missing metadata.labels; objects adopted from non-CAPI management.","solutions":["Add the label `cluster.x-k8s.io/cluster-name: <capi-cluster-name>` to the affected object","Recreate the resource through the CAPI Cluster topology (e.g. via the ClusterClass or machine deployment) so CAPI sets the label itself","Check cluster-api version — objects provisioned by very old CAPI releases may lack the label and need upgrading/patching","Verify the reconciler is watching objects that belong to a CAPI-managed cluster at all"],"exampleFix":"// before (object missing label)\nmetadata:\n  name: my-machine\n  kind: Machine\n// after\nmetadata:\n  name: my-machine\n  kind: Machine\n  labels:\n    cluster.x-k8s.io/cluster-name: my-cluster","handlingStrategy":"validation","validationCode":"const ClusterNameLabel = \"cluster.x-k8s.io/cluster-name\"\nfunc hasClusterNameLabel(obj client.Object) bool {\n\treturn obj.GetLabels()[ClusterNameLabel] != \"\"\n}\n// before reconciling:\nif !hasClusterNameLabel(obj) {\n\t// skip / requeue instead of calling getCAPIClusterFromCAPIObject\n}","typeGuard":"func extractCAPIClusterName(obj client.Object) (string, bool) {\n\tname, ok := obj.GetLabels()[\"cluster.x-k8s.io/cluster-name\"]\n\tif !ok || name == \"\" {\n\t\treturn \"\", false\n\t}\n\treturn name, true\n}","tryCatchPattern":null,"preventionTips":["Always create CAPI-managed resources via cluster-api tooling so the cluster-name label is applied automatically","Validate manifests with a policy tool (Kyverno/OPA) requiring cluster.x-k8s.io/cluster-name on Machines and control planes","When copying CAPI object YAML, preserve metadata.labels","Add a CI check that adopted objects carry the required CAPI labels"],"tags":["kubernetes","cluster-api","labels","reconcile"],"backgroundTag":"missing-required-label","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}