kubernetes/kops · error

duplicate variable: %q

Error message

duplicate variable: %q

What it means

TerraformWriter.AddOutputVariable registers a Terraform output variable keyed by name, protected by a mutex. Terraform allows only one definition per output name, so registering a second variable with the same key would generate invalid config; kOps rejects it with this error. It detects internal collisions between renderers producing identically named outputs.

Source

Thrown at upup/pkg/fi/cloudup/terraformWriter/writer.go:184

	t.mutex.Lock()
	defer t.mutex.Unlock()

	t.resources = append(t.resources, res)

	return nil
}

func (t *TerraformWriter) AddOutputVariable(key string, literal *Literal) error {
	v := &terraformOutputVariable{
		Key:   key,
		Value: literal,
	}

	t.mutex.Lock()
	defer t.mutex.Unlock()

	if t.outputs[key] != nil {
		return fmt.Errorf("duplicate variable: %q", key)
	}
	t.outputs[key] = v

	return nil
}

func (t *TerraformWriter) AddOutputVariableArray(key string, literal *Literal) error {
	t.mutex.Lock()
	defer t.mutex.Unlock()

	if t.outputs[key] == nil {
		v := &terraformOutputVariable{
			Key: key,
		}
		t.outputs[key] = v
	}
	if t.outputs[key].Value != nil {
		return fmt.Errorf("variable %q is both an array and a scalar", key)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Identify which two renderers produce the same output name from the duplicate key and report/fix the collision (usually namespaced per resource).
  2. Check for duplicate resources in your cluster spec that map to the same Terraform output name.
  3. If patching kOps, make the output key unique (e.g. prefix with resource name).
  4. Update kOps — such collisions are typically fixed upstream quickly.

Example fix

// before
AddOutputVariable("cluster_name", lit)
AddOutputVariable("cluster_name", lit2) // duplicate
// after
AddOutputVariable("cluster_name", lit)
AddOutputVariable("secondary_cluster_name", lit2)
Defensive patterns

Strategy: try-catch

Try / catch

if err := w.AddOutputVariable(key, lit); err != nil {
    if strings.Contains(err.Error(), "duplicate variable") {
        return fmt.Errorf("output %q registered twice; check renderer naming: %w", key, err)
    }
    return err
}

Prevention

When it happens

Trigger: Two renderers call AddOutputVariable with the same key in one TerraformWriter — e.g. two resources both trying to expose an output with the same name, or a re-run of a renderer that already registered the output.

Common situations: kOps internal bug after adding a new output without namespacing it; duplicated resources in the cluster spec leading to duplicated output registration; custom fork/patch code reusing an existing output name.

Related errors


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