kubernetes/kops · error

error building machine deployments: %w

Error message

error building machine deployments: %w

What it means

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.

Source

Thrown at clusterapi/pkg/builders/machinedeploymentbuilder.go:225

	}

	u := &unstructured.Unstructured{Object: obj}

	b.capiInfra = u
	return nil
}

func (b *MachineDeploymentBuilder) BuildObjects(ctx context.Context) ([]*unstructured.Unstructured, error) {
	if err := b.createKopsConfigTemplate(ctx); err != nil {
		return nil, fmt.Errorf("error creating kops config template: %w", err)
	}

	if err := b.createGCPMachineTemplate(ctx); err != nil {
		return nil, fmt.Errorf("error creating gcp machine template: %w", err)
	}

	if err := b.buildMachineDeployments(); err != nil {
		return nil, fmt.Errorf("error building machine deployments: %w", err)
	}

	var objects []*unstructured.Unstructured
	objects = append(objects, b.capiConfigTemplate)
	objects = append(objects, b.capiInfra)

	var machineDeployments []*unstructured.Unstructured
	for _, md := range b.capiMachineDeployments {
		machineDeployments = append(machineDeployments, md)
	}
	sort.Slice(machineDeployments, func(i, j int) bool {
		return machineDeployments[i].GetName() < machineDeployments[j].GetName()
	})

	objects = append(objects, machineDeployments...)

	return objects, nil
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Ensure Zones contains at least one entry before calling BuildObjects; buildMachineDeployments divides Replicas by zone count.
  2. Set Replicas >= 1; with Replicas < zoneCount some zones get 0 replicas.
  3. Confirm the earlier createKopsConfigTemplate/createGCPMachineTemplate steps succeeded (this step references their outputs).
  4. Check the wrapped cause (%w) for the precise internal failure.

Example fix

// before
b := &builders.MachineDeploymentBuilder{Replicas: 3} // Zones not set -> division by zero
// after
b := &builders.MachineDeploymentBuilder{Replicas: 3, Zones: []string{"us-central1-a", "us-central1-b"}}
Defensive patterns

Strategy: validation

Validate before calling

if len(b.Zones) == 0 {
    return fmt.Errorf("at least one zone required for MachineDeployment")
}
if b.Replicas <= 0 {
    return fmt.Errorf("Replicas must be positive")
}

Try / catch

objs, err := builder.BuildObjects(ctx)
if err != nil {
    if strings.Contains(err.Error(), "error building machine deployments") {
        return fmt.Errorf("machine deployment stage failed (check Zones/Replicas): %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/b6756430e0995a34. Report an issue: GitHub.