Skip to content

Commit 59d0f4b

Browse files
author
Ye Ji
committed
add support for auto detecting cluster domain
Previously, the cluster domain is hardcoded to `cluster.local`. This PR adds support for auto detecting the domain by running a DNS query.
1 parent d4760ac commit 59d0f4b

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
# [Unreleased](https://github.com/cockroachdb/cockroach-operator/compare/v2.6.0...master)
99

10+
## Changed
11+
* Cluster domain for cert generation is now autodetected by running a DNS query
12+
1013
## Fixed
1114

1215
* Grant operator deletecollection permissions to fix fullcluster restart flow

pkg/resource/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ go_library(
2828
"//pkg/labels:go_default_library",
2929
"//pkg/ptr:go_default_library",
3030
"//pkg/security:go_default_library",
31+
"//pkg/util:go_default_library",
3132
"//pkg/utilfeature:go_default_library",
3233
"@com_github_cockroachdb_errors//:go_default_library",
3334
"@com_github_go_logr_logr//:go_default_library",

pkg/resource/cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package resource
1818

1919
import (
2020
"fmt"
21+
"github.com/cockroachdb/cockroach-operator/pkg/util"
2122
"os"
2223
"strings"
2324
"time"
@@ -300,7 +301,7 @@ func (cluster Cluster) CASecretName() string {
300301
}
301302

302303
func (cluster Cluster) Domain() string {
303-
return "svc.cluster.local"
304+
return fmt.Sprintf("svc.%s", util.GetClusterDomain())
304305
}
305306

306307
func (cluster Cluster) SecureMode() string {

pkg/resource/webhook_certificates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package resource
1919
import (
2020
"context"
2121
"fmt"
22+
"github.com/cockroachdb/cockroach-operator/pkg/util"
2223

2324
"github.com/cockroachdb/cockroach-operator/pkg/security"
2425
"github.com/cockroachdb/errors"
@@ -97,7 +98,7 @@ func CreateWebhookCertificate(ctx context.Context, api SecretsInterface, ns stri
9798
webhookService,
9899
fmt.Sprintf("%s.%s", webhookService, ns),
99100
fmt.Sprintf("%s.%s.svc", webhookService, ns),
100-
fmt.Sprintf("%s.%s.svc.cluster.local", webhookService, ns),
101+
fmt.Sprintf("%s.%s.svc.%s", webhookService, ns, util.GetClusterDomain()),
101102
))
102103

103104
if err != nil {

pkg/util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_library(
44
name = "go_default_library",
55
srcs = [
66
"api_kind_checker.go",
7+
"cluster_domain.go",
78
"tmp_dir.go",
89
],
910
importpath = "github.com/cockroachdb/cockroach-operator/pkg/util",

pkg/util/cluster_domain.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package util
2+
3+
import (
4+
"context"
5+
"net"
6+
"time"
7+
)
8+
9+
10+
var clusterDomain = "cluster.local"
11+
12+
func init() {
13+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
14+
defer cancel()
15+
16+
const host = "kubernetes.default.svc"
17+
cname, err := net.DefaultResolver.LookupCNAME(ctx, host)
18+
if err == nil {
19+
clusterDomain = cname[len(host)+1:len(cname)-1]
20+
}
21+
}
22+
23+
// GetClusterDomain returns the cluster domain of the k8s cluster.
24+
// It is auto-detected by running a DNS query when the controller starts up.
25+
// It defaults to "cluster.local" if we cannot determine the domain.
26+
func GetClusterDomain() string {
27+
return clusterDomain
28+
}

0 commit comments

Comments
 (0)