{"record":{"id":"4ebe9723cfc757e6","repo":"kubernetes/kops","slug":"no-subnets-found-in-cluster-q","errorCode":null,"errorMessage":"no subnets found in cluster %q","messagePattern":"no subnets found in cluster %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox/clusterapi/generate_machinedeployment.go","lineNumber":123,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\tcluster, err := clientset.GetCluster(ctx, options.ClusterName)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// TODO: Sync with LinkToSubnet and support ID\n\tsubnet := options.Subnet\n\tif subnet == \"\" {\n\t\tsubnets := sets.New[string]()\n\t\tfor _, subnet := range cluster.Spec.Networking.Subnets {\n\t\t\tsubnets.Insert(subnet.Name)\n\t\t}\n\n\t\tif subnets.Len() == 0 {\n\t\t\treturn fmt.Errorf(\"no subnets found in cluster %q\", options.ClusterName)\n\t\t}\n\t\tif subnets.Len() > 1 {\n\t\t\tklog.Warningf(\"multiple subnets found in cluster %q; using an arbitrary subnet\", options.ClusterName)\n\t\t}\n\t\tsubnet = sets.List(subnets)[0]\n\t\tlog.Info(\"selected subnet\", \"subnet\", subnet)\n\t}\n\n\tzones := options.Zones\n\n\t// TODO: When to use cluster zones, when to use IG zones?\n\t// if zone == \"\" {\n\t// \tfor _, subnetCandidate := range cluster.Spec.Networking.Subnets {\n\t// \t\tif subnetCandidate.Name == subnet {\n\t// \t\t\tzone = subnetCandidate.Zone\n\t// \t\t}\n\t// \t}\n\t// \tif zone == \"\" {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox/clusterapi/generate_machinedeployment.go#L105-L141","documentation":"When auto-selecting a subnet for the MachineDeployment, the command collects cluster.Spec.Networking.Subnets into a set; if the set is empty it returns 'no subnets found in cluster %q'. A cluster spec with no subnets cannot host worker machines.","triggerScenarios":"Generating a MachineDeployment for a cluster whose spec has an empty Spec.Networking.Subnets list — e.g. a partially created or hand-edited cluster config, or a spec that failed population.","commonSituations":"Manually stripped subnets in `kops edit cluster`; clusters created from incomplete templates; spec fetched from a statestore before initial update populated networking.","solutions":["Add subnets to the cluster spec: `kops edit cluster` and define subnets under spec.networking.subnets, then `kops update cluster`.","Re-run `kops create cluster` with valid --zones so subnets are generated.","Verify with `kops get cluster -oyaml | grep -A5 subnets` that subnets exist before generating."],"exampleFix":"// before: cluster spec\nnetworking:\n  subnets: []\n// after: kops edit cluster\nnetworking:\n  subnets:\n  - name: us-east-1a\n    type: Public\n    zone: us-east-1a","handlingStrategy":"validation","validationCode":"cluster, err := clientset.GetCluster(ctx, options.ClusterName)\nif err != nil { return err }\nif cluster.Spec.Networking == nil || len(cluster.Spec.Networking.Subnets) == 0 {\n\treturn fmt.Errorf(\"cluster %q has no subnets; run kops edit cluster first\", options.ClusterName)\n}","typeGuard":"func clusterHasSubnets(c *api.Cluster) bool {\n\treturn c != nil && c.Spec.Networking != nil && len(c.Spec.Networking.Subnets) > 0\n}","tryCatchPattern":"if err := RunGenerateMachineDeployment(ctx, f, out, options); err != nil {\n\tif strings.Contains(err.Error(), \"no subnets found in cluster\") {\n\t\treturn fmt.Errorf(\"fix cluster spec (add spec.networking.subnets) then retry: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Check `kops get cluster -oyaml` for populated subnets before cluster-api generation.","Never hand-strip subnets from the cluster spec.","Run `kops update cluster` after creating a cluster so the spec is fully populated.","Guard automation with a spec-validation preflight step."],"tags":["kops","cluster-api","subnets","cluster-spec"],"backgroundTag":"empty-cluster-subnets","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"}