{"record":{"id":"e428f5fd1125b6aa","repo":"kubernetes/kops","slug":"duplicate-scope-q","errorCode":null,"errorMessage":"duplicate scope: %q","messagePattern":"duplicate scope: %q","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dns-controller/pkg/dns/dnscontroller.go","lineNumber":664,"sourceCode":"\t}\n\tfor i := range l {\n\t\tif l[i] != r[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n// CreateScope creates a scope object.\nfunc (c *DNSController) CreateScope(scopeName string) (Scope, error) {\n\tc.mutex.Lock()\n\tdefer c.mutex.Unlock()\n\n\ts := c.scopes[scopeName]\n\tif s != nil {\n\t\t// We can't support this then we would need to change Ready to a counter\n\t\t// (OK, so we could, but it's probably an error anyway)\n\t\treturn nil, fmt.Errorf(\"duplicate scope: %q\", scopeName)\n\t}\n\n\ts = &DNSControllerScope{\n\t\tScopeName: scopeName,\n\t\tRecords:   make(map[string][]Record),\n\t\tparent:    c,\n\t\tReady:     false,\n\t}\n\tc.scopes[scopeName] = s\n\treturn s, nil\n}\n","sourceCodeStart":646,"sourceCodeEnd":676,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/dns-controller/pkg/dns/dnscontroller.go#L646-L676","documentation":"CreateScope refuses to create a second scope with the same name. Scopes are singletons in DNSController.scopes and each carries its own Ready flag and record map, so re-creating one would break ready-tracking semantics; the controller returns this error instead.","triggerScenarios":"Calling dns.CreateScope(\"name\") twice with the same scopeName on the same DNSController instance — e.g. initializeWatchers invoked twice, or two watcher constructors both requesting the same scope name.","commonSituations":"Running initializeWatchers more than once in tests or on SIGHUP reload; a custom watcher reusing the built-in \"node\", \"ingress\", or \"service\" scope name; accidental double-start of the controller.","solutions":["Create each scope exactly once and pass the Scope to the watcher instead of calling CreateScope again","Use unique scope names for custom watchers (avoid \"ingress\", \"node\", \"service\")","Fix control flow so the constructor (e.g. NewNodeController/NewIngressController) is not called twice for the same controller instance","If restart semantics are needed, construct a new DNSController rather than reusing scopes"],"exampleFix":"// before (second init reuses name)\nscope1, _ := dns.CreateScope(\"node\")\nscope2, err := dns.CreateScope(\"node\") // duplicate scope\n// after\nscope, err := dns.CreateScope(\"node\")\nnodeCtl := NewNodeController(client, dns, internalTypes) // controller owns its scope; don't recreate","handlingStrategy":"validation","validationCode":"if c.scopes != nil {\n    if _, exists := existingScopes[scopeName]; exists {\n        return fmt.Errorf(\"refusing to initialize watchers twice for scope %q\", scopeName)\n    }\n}","typeGuard":"func scopeExists(c *dns.DNSController, name string) bool {\n    // expose via AllScopes()/lookup in tests; treat as guard before CreateScope\n    return scopes[name] != nil\n}","tryCatchPattern":"scope, err := dns.CreateScope(\"node\")\nif err != nil {\n    if strings.Contains(err.Error(), \"duplicate scope\") {\n        klog.Warning(\"watchers already initialized; skipping\")\n        return nil\n    }\n    return err\n}","preventionTips":["Create each scope exactly once at controller startup","Never reuse reserved scope names (node, ingress, service) in custom code","Guard initialization with sync.Once in tests and reload paths"],"tags":["dns","scope","lifecycle","programming-error"],"backgroundTag":"duplicate-scope-creation","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"}