{"record":{"id":"4d59b0523fb9cae2","repo":"kubernetes/kops","slug":"cluster-q-already-exists","errorCode":null,"errorMessage":"cluster %q already exists","messagePattern":"cluster %q already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/create.go","lineNumber":148,"sourceCode":"\t\t\t}\n\n\t\t\tswitch v := o.(type) {\n\t\t\tcase *kopsapi.Cluster:\n\t\t\t\tcloud, err := cloudup.BuildCloud(v)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\n\t\t\t\t// Adding a PerformAssignments() call here as the user might be trying to use\n\t\t\t\t// the new `-f` feature, with an old cluster definition.\n\t\t\t\terr = cloudup.PerformAssignments(v, vfsContext, cloud)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"error populating configuration: %v\", err)\n\t\t\t\t}\n\t\t\t\t_, err = clientset.CreateCluster(ctx, v)\n\t\t\t\tif err != nil {\n\t\t\t\t\tif apierrors.IsAlreadyExists(err) {\n\t\t\t\t\t\treturn fmt.Errorf(\"cluster %q already exists\", v.ObjectMeta.Name)\n\t\t\t\t\t}\n\t\t\t\t\treturn fmt.Errorf(\"error creating cluster: %v\", err)\n\t\t\t\t}\n\t\t\t\tfmt.Fprintf(&sb, \"Created cluster/%s\\n\", v.ObjectMeta.Name)\n\t\t\t\tclusters = append(clusters, v)\n\t\t\t\t// cSpec = true\n\n\t\t\tcase *kopsapi.InstanceGroup:\n\t\t\t\tclusterName = v.ObjectMeta.Labels[kopsapi.LabelClusterName]\n\t\t\t\tif clusterName == \"\" {\n\t\t\t\t\treturn fmt.Errorf(\"must specify %q label with cluster name to create instanceGroup\", kopsapi.LabelClusterName)\n\t\t\t\t}\n\t\t\t\tcluster, err := clientset.GetCluster(ctx, clusterName)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"error querying cluster %q: %v\", clusterName, err)\n\t\t\t\t}\n\n\t\t\t\tif cluster == nil {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/create.go#L130-L166","documentation":"After populating config, RunCreate calls clientset.CreateCluster to persist the cluster. When the registry reports the cluster already exists (apierrors.IsAlreadyExists), the CLI returns \"cluster %q already exists\" instead of the generic create error.","triggerScenarios":"`kops create -f cluster.yaml` with a cluster whose metadata.name matches a Cluster already registered in the kOps state store.","commonSituations":"Re-running create for an existing cluster (instead of `kops replace -f` or `kops update`); multiple manifests defining the same cluster name; stale state store pointing at a previously created cluster.","solutions":["Use `kops replace -f cluster.yaml` to update the existing cluster spec instead of create","Delete the existing cluster with `kops delete cluster --name <name> --yes` if it should be recreated","Choose a different cluster name if this is a new cluster","Verify state store (KOPS_STATE_STORE) points where you expect"],"exampleFix":"// before\nkops create -f cluster.yaml\n// after\nkops replace -f cluster.yaml","handlingStrategy":"validation","validationCode":"// check existence before create\nexisting, err := clientset.ListClusters(ctx, nil)\nif err == nil {\n    for _, c := range existing.Items {\n        if c.Name == cluster.Name {\n            return fmt.Errorf(\"cluster %s exists; use kops replace\", c.Name)\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"_, err := clientset.CreateCluster(ctx, v)\nif err != nil {\n    if apierrors.IsAlreadyExists(err) {\n        return fmt.Errorf(\"cluster %q already exists\", v.ObjectMeta.Name)\n    }\n    return fmt.Errorf(\"error creating cluster: %v\", err)\n}","preventionTips":["Use `kops replace -f` for updates, create only for new clusters","Ensure unique cluster names across manifests","Confirm KOPS_STATE_STORE points to the intended store before create","Script idempotently: check-then-create or use replace"],"tags":["cli","cluster-lifecycle","duplicate-resource"],"backgroundTag":"resource-already-exists","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"}