{"record":{"id":"08c3766c7de0f043","repo":"kubernetes/kops","slug":"duplicate-variable-q","errorCode":null,"errorMessage":"duplicate variable: %q","messagePattern":"duplicate variable: %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/cloudup/terraformWriter/writer.go","lineNumber":184,"sourceCode":"\tt.mutex.Lock()\n\tdefer t.mutex.Unlock()\n\n\tt.resources = append(t.resources, res)\n\n\treturn nil\n}\n\nfunc (t *TerraformWriter) AddOutputVariable(key string, literal *Literal) error {\n\tv := &terraformOutputVariable{\n\t\tKey:   key,\n\t\tValue: literal,\n\t}\n\n\tt.mutex.Lock()\n\tdefer t.mutex.Unlock()\n\n\tif t.outputs[key] != nil {\n\t\treturn fmt.Errorf(\"duplicate variable: %q\", key)\n\t}\n\tt.outputs[key] = v\n\n\treturn nil\n}\n\nfunc (t *TerraformWriter) AddOutputVariableArray(key string, literal *Literal) error {\n\tt.mutex.Lock()\n\tdefer t.mutex.Unlock()\n\n\tif t.outputs[key] == nil {\n\t\tv := &terraformOutputVariable{\n\t\t\tKey: key,\n\t\t}\n\t\tt.outputs[key] = v\n\t}\n\tif t.outputs[key].Value != nil {\n\t\treturn fmt.Errorf(\"variable %q is both an array and a scalar\", key)","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/terraformWriter/writer.go#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Identify which two renderers produce the same output name from the duplicate key and report/fix the collision (usually namespaced per resource).","Check for duplicate resources in your cluster spec that map to the same Terraform output name.","If patching kOps, make the output key unique (e.g. prefix with resource name).","Update kOps — such collisions are typically fixed upstream quickly."],"exampleFix":"// before\nAddOutputVariable(\"cluster_name\", lit)\nAddOutputVariable(\"cluster_name\", lit2) // duplicate\n// after\nAddOutputVariable(\"cluster_name\", lit)\nAddOutputVariable(\"secondary_cluster_name\", lit2)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := w.AddOutputVariable(key, lit); err != nil {\n    if strings.Contains(err.Error(), \"duplicate variable\") {\n        return fmt.Errorf(\"output %q registered twice; check renderer naming: %w\", key, err)\n    }\n    return err\n}","preventionTips":["Namespace output keys by resource name when adding new outputs in kOps.","Avoid duplicated resources in cluster specs.","Keep kOps updated to pick up upstream collision fixes.","Search the renderers for the offending key when the error names it."],"tags":["terraform","outputs","kops","internal"],"backgroundTag":"duplicate-output-variable","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"}