{"record":{"id":"b6756430e0995a34","repo":"kubernetes/kops","slug":"error-building-machine-deployments-w","errorCode":null,"errorMessage":"error building machine deployments: %w","messagePattern":"error building machine deployments: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clusterapi/pkg/builders/machinedeploymentbuilder.go","lineNumber":225,"sourceCode":"\t}\n\n\tu := &unstructured.Unstructured{Object: obj}\n\n\tb.capiInfra = u\n\treturn nil\n}\n\nfunc (b *MachineDeploymentBuilder) BuildObjects(ctx context.Context) ([]*unstructured.Unstructured, error) {\n\tif err := b.createKopsConfigTemplate(ctx); err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating kops config template: %w\", err)\n\t}\n\n\tif err := b.createGCPMachineTemplate(ctx); err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating gcp machine template: %w\", err)\n\t}\n\n\tif err := b.buildMachineDeployments(); err != nil {\n\t\treturn nil, fmt.Errorf(\"error building machine deployments: %w\", err)\n\t}\n\n\tvar objects []*unstructured.Unstructured\n\tobjects = append(objects, b.capiConfigTemplate)\n\tobjects = append(objects, b.capiInfra)\n\n\tvar machineDeployments []*unstructured.Unstructured\n\tfor _, md := range b.capiMachineDeployments {\n\t\tmachineDeployments = append(machineDeployments, md)\n\t}\n\tsort.Slice(machineDeployments, func(i, j int) bool {\n\t\treturn machineDeployments[i].GetName() < machineDeployments[j].GetName()\n\t})\n\n\tobjects = append(objects, machineDeployments...)\n\n\treturn objects, nil\n}","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/clusterapi/pkg/builders/machinedeploymentbuilder.go#L207-L243","documentation":"MachineDeploymentBuilder.BuildObjects wraps any failure from buildMachineDeployments with this message. buildMachineDeployments divides Replicas across Zones and creates one cluster.x-k8s.io/v1beta1 MachineDeployment per zone, referencing the previously built KopsConfigTemplate and GCPMachineTemplate. The underlying error is preserved via %w.","triggerScenarios":"Calling BuildObjects when buildMachineDeployments returns an error — notably len(b.Zones) == 0, which causes a division by zero when computing replicas per zone, or a nil capiConfigTemplate/capiInfra from earlier steps.","commonSituations":"Constructing the builder without populating Zones (an empty instance-group zone list), Replicas less than the zone count, or reusing a builder whose BuildObjects already failed in an earlier step leaving template refs unset.","solutions":["Ensure Zones contains at least one entry before calling BuildObjects; buildMachineDeployments divides Replicas by zone count.","Set Replicas >= 1; with Replicas < zoneCount some zones get 0 replicas.","Confirm the earlier createKopsConfigTemplate/createGCPMachineTemplate steps succeeded (this step references their outputs).","Check the wrapped cause (%w) for the precise internal failure."],"exampleFix":"// before\nb := &builders.MachineDeploymentBuilder{Replicas: 3} // Zones not set -> division by zero\n// after\nb := &builders.MachineDeploymentBuilder{Replicas: 3, Zones: []string{\"us-central1-a\", \"us-central1-b\"}}","handlingStrategy":"validation","validationCode":"if len(b.Zones) == 0 {\n    return fmt.Errorf(\"at least one zone required for MachineDeployment\")\n}\nif b.Replicas <= 0 {\n    return fmt.Errorf(\"Replicas must be positive\")\n}","typeGuard":null,"tryCatchPattern":"objs, err := builder.BuildObjects(ctx)\nif err != nil {\n    if strings.Contains(err.Error(), \"error building machine deployments\") {\n        return fmt.Errorf(\"machine deployment stage failed (check Zones/Replicas): %w\", err)\n    }\n    return err\n}","preventionTips":["Never construct the builder with an empty Zones slice — replicas are divided by zone count.","Set Replicas >= number of zones if every zone must get a replica.","Reuse a fresh builder per BuildObjects call to avoid stale template refs."],"tags":["kops","cluster-api","gcp","machine-deployment"],"backgroundTag":"machine-deployment-build-failed","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"}