{"record":{"id":"694805b4b52a64e8","repo":"cilium/cilium","slug":"failed-to-compile-regex-for-kubernetes-plugin-matc","errorCode":null,"errorMessage":"Failed to compile regex for kubernetes plugin matching: %w","messagePattern":"Failed to compile regex for kubernetes plugin matching: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"clustermesh-apiserver/mcsapi-coredns-cfg/root.go","lineNumber":145,"sourceCode":"\t\terr = restartCoreDNS(ctx, client, deployment)\n\t\tif err != nil {\n\t\t\tlogger.Error(\"Failed to restart CoreDNS Deployment\", logfields.Error, err)\n\t\t} else {\n\t\t\tlogger.Info(\"CoreDNS is rolling out with the new configuration\")\n\t\t}\n\t\treturn\n\t}))\n}\n\nfunc updateCorefile(clusterDomain, clustersetDomain string, corefile string) (string, error) {\n\tif strings.Contains(corefile, clustersetDomain) || strings.Contains(corefile, \"multicluster\") {\n\t\treturn \"\", nil // This is not an error as this command might have already been executed\n\t}\n\n\tclusterDomainEscaped := strings.ReplaceAll(clusterDomain, \".\", \"\\\\.\")\n\tkubernetesMatchRegex, err := regexp.Compile(fmt.Sprintf(`(?m)^\\s*kubernetes.*%s.*\\{`, clusterDomainEscaped))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Failed to compile regex for kubernetes plugin matching: %w\", err)\n\t}\n\tif !kubernetesMatchRegex.MatchString(corefile) {\n\t\treturn \"\", fmt.Errorf(\"CoreDNS not configured with kubernetes plugin and the domain '%s'\", clusterDomain)\n\t}\n\n\tcorefile = strings.ReplaceAll(\n\t\tcorefile,\n\t\tclusterDomain,\n\t\tclusterDomain+\" \"+clustersetDomain,\n\t)\n\tkubernetesReplaceRegex := regexp.MustCompile(`(?m)^(\\s*)kubernetes(.*)\\{`)\n\tcorefile = kubernetesReplaceRegex.ReplaceAllString(\n\t\tcorefile,\n\t\tfmt.Sprintf(\"${1}kubernetes${2}{\\n${1}   multicluster %s\", clustersetDomain),\n\t)\n\treturn corefile, nil\n}\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/clustermesh-apiserver/mcsapi-coredns-cfg/root.go#L127-L163","documentation":"Inside updateCorefile, the controller builds a multiline regex `(?m)^\\s*kubernetes.*<domain>.*\\{` to locate the kubernetes plugin block in the Corefile. If `regexp.Compile` fails, this wrapped error is returned. Compile failure here is practically impossible since the pattern is constructed from static text plus an escaped domain, so it usually indicates a pathological cluster domain value.","triggerScenarios":"updateCorefile is called with a clusterDomain that, even after dot-escaping, forms an invalid regex (e.g. containing stray regexp metacharacters beyond dots, or an invalid UTF-8/oversized pattern from misconfiguration).","commonSituations":"A mistyped --coredns-cluster-domain containing unbalanced parentheses/brackets or control characters, causing regexp.Compile to reject the composed pattern.","solutions":["Fix the clusterDomain flag so it is a plain DNS domain (letters, digits, dots, hyphens only)","Check the wrapped `%w` cause in the error message for the exact regexp syntax problem","If running a modified build, validate the domain with a `^[a-z0-9.-]+$` check before composing the regex"],"exampleFix":"// before\nclusterDomain = \"cluster.local))\"\n// after\nclusterDomain = \"cluster.local\"","handlingStrategy":"validation","validationCode":"var domainRe = regexp.MustCompile(`^[a-zA-Z0-9.-]+$`)\nif !domainRe.MatchString(clusterDomain) {\n    return fmt.Errorf(\"invalid cluster domain %q\", clusterDomain)\n}","typeGuard":null,"tryCatchPattern":"corefile, err := updateCorefile(domain, clustersetDomain, corefile)\nif err != nil {\n    if strings.Contains(err.Error(), \"Failed to compile regex\") {\n        log.Fatalf(\"bad cluster domain %q: %v\", domain, err)\n    }\n}","preventionTips":["Keep --coredns-cluster-domain limited to valid DNS characters","Quote/escape domains before composing regexes","Fail fast on flag validation at startup"],"tags":["regex","coredns","config","cilium"],"backgroundTag":"regex-compile-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}