{"record":{"id":"348086d866e84cb8","repo":"kubernetes/kops","slug":"cluster-is-required","errorCode":null,"errorMessage":"cluster is required","messagePattern":"cluster is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/toolbox/clusterapi/generate_machinedeployment.go","lineNumber":98,"sourceCode":"\t\t},\n\t}\n\n\tcmd.Flags().StringVar(&options.ClusterName, \"cluster\", options.ClusterName, \"Name of cluster to join\")\n\tcmd.Flags().StringVar(&options.Name, \"name\", options.Name, \"Name of MachineDeployment (and other objects) to create\")\n\tcmd.Flags().StringVar(&options.Namespace, \"namespace\", options.Namespace, \"Namespace for objects\")\n\tcmd.Flags().IntVar(&options.Replicas, \"replicas\", options.Replicas, \"Number of replicas for MachineDeployment\")\n\tcmd.Flags().StringArrayVar(&options.Zones, \"zones\", options.Zones, \"Zones for the MachineDeployment (if not specified, will be deployed to all discovered zones in the cluster)\")\n\tcmd.Flags().StringVar(&options.InstanceType, \"instance-type\", options.InstanceType, \"Instance type for the MachineDeployment (if not specified, an arbitrary instance type from the instance groups will be used)\")\n\tcmd.Flags().StringVar(&options.Subnet, \"subnet\", options.Subnet, \"Subnet for the MachineDeployment (if not specified, an arbitrary subnet from the instance groups will be used)\")\n\tcmd.Flags().StringVar(&options.Image, \"image\", options.Image, \"Image for the MachineDeployment (if not specified, an arbitrary image from the instance groups will be used)\")\n\treturn cmd\n}\n\nfunc RunGenerateMachineDeployment(ctx context.Context, f commandutils.Factory, out io.Writer, options *GenerateMachineDeploymentOptions) error {\n\tlog := klog.FromContext(ctx)\n\n\tif options.ClusterName == \"\" {\n\t\treturn fmt.Errorf(\"cluster is required\")\n\t}\n\tif options.Name == \"\" {\n\t\treturn fmt.Errorf(\"name is required\")\n\t}\n\n\tclientset, err := f.KopsClient()\n\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 == \"\" {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/toolbox/clusterapi/generate_machinedeployment.go#L80-L116","documentation":"RunGenerateMachineDeployment validates options before doing any work and returns 'cluster is required' when options.ClusterName is empty. The toolbox clusterapi generator needs to know which cluster's subnets/zones/instance types to mirror into the MachineDeployment.","triggerScenarios":"Running `kops toolbox cluster-events`/clusterapi generate machinedeployment without --cluster (or programmatically passing GenerateMachineDeploymentOptions with ClusterName unset).","commonSituations":"Forgetting the --cluster flag on the CLI; scripting the command with an empty variable; calling the exported function directly in tests/tools without initializing options.","solutions":["Pass --cluster <name> (e.g. --cluster mycluster.k8s.local) to the generate machinedeployment command.","Verify the option value is non-empty in scripts before invoking (echo \"$CLUSTER\").","When calling programmatically, set options.ClusterName before RunGenerateMachineDeployment."],"exampleFix":"// before\nopts := &GenerateMachineDeploymentOptions{Name: \"workers\"}\nRunGenerateMachineDeployment(ctx, f, out, opts) // cluster is required\n// after\nopts := &GenerateMachineDeploymentOptions{Name: \"workers\", ClusterName: \"mycluster.k8s.local\"}\nRunGenerateMachineDeployment(ctx, f, out, opts)","handlingStrategy":"validation","validationCode":"if options == nil || options.ClusterName == \"\" {\n\treturn fmt.Errorf(\"cluster is required: pass --cluster <name>\")\n}","typeGuard":"func optsReady(o *GenerateMachineDeploymentOptions) bool {\n\treturn o != nil && o.ClusterName != \"\" && o.Name != \"\"\n}","tryCatchPattern":"if err := RunGenerateMachineDeployment(ctx, f, out, options); err != nil {\n\tswitch err.Error() {\n\tcase \"cluster is required\":\n\t\treturn flag.ErrHelp // trigger usage output\n\tdefault:\n\t\treturn err\n\t}\n}","preventionTips":["Always pass --cluster in scripts; fail fast if the variable is empty: : \"${CLUSTER:?cluster required}\".","Pre-validate options structs before calling the exported function.","Wire cobra Required flags (cmd.MarkFlagRequired(\"cluster\")) so the CLI blocks earlier."],"tags":["kops","cluster-api","cli-validation"],"backgroundTag":"missing-required-flag","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"}