Skip to content

Commit 66ec87d

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 66ec87d

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2022 The Cockroach Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"context"
21+
"net"
22+
"time"
23+
)
24+
25+
26+
var clusterDomain = "cluster.local"
27+
28+
func init() {
29+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
30+
defer cancel()
31+
32+
const host = "kubernetes.default.svc"
33+
cname, err := net.DefaultResolver.LookupCNAME(ctx, host)
34+
if err == nil {
35+
clusterDomain = cname[len(host)+1:len(cname)-1]
36+
}
37+
}
38+
39+
// GetClusterDomain returns the cluster domain of the k8s cluster.
40+
// It is auto-detected by running a DNS query when the controller starts up.
41+
// It defaults to "cluster.local" if we cannot determine the domain.
42+
func GetClusterDomain() string {
43+
return clusterDomain
44+
}

0 commit comments

Comments
 (0)