Skip to content

Commit 54a8e46

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 9ceebb1 commit 54a8e46

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1919
* Correctly detect failed version checker Pods
2020
* retry cluster status updates, reducing test flakes
2121

22+
## Changed
23+
* Cluster domain for cert generation is now autodetected by running a DNS query
24+
2225
# [v2.7.0](https://github.com/cockroachdb/cockroach-operator/compare/v2.6.0...v2.7.0)
2326

2427
## Fixed

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
api "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
2727
"github.com/cockroachdb/cockroach-operator/pkg/clusterstatus"
2828
"github.com/cockroachdb/cockroach-operator/pkg/condition"
29+
"github.com/cockroachdb/cockroach-operator/pkg/util"
2930
"github.com/cockroachdb/errors"
3031
"github.com/gosimple/slug"
3132
corev1 "k8s.io/api/core/v1"
@@ -199,7 +200,7 @@ func (cluster Cluster) LookupSupportedVersion(version string) (string, bool) {
199200
return "", false
200201
}
201202

202-
//GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
203+
// GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
203204
func (cluster Cluster) GetVersionAnnotation() string {
204205
return cluster.getAnnotation(CrdbVersionAnnotation)
205206
}
@@ -270,7 +271,7 @@ func (cluster Cluster) GetCockroachDBImageName() string {
270271
}
271272
return NotSupportedVersion
272273
}
273-
//we validate the version after the job runs with exec
274+
// we validate the version after the job runs with exec
274275
return cluster.Spec().Image.Name
275276
}
276277

@@ -308,7 +309,7 @@ func (cluster Cluster) CASecretName() string {
308309
}
309310

310311
func (cluster Cluster) Domain() string {
311-
return "svc.cluster.local"
312+
return fmt.Sprintf("svc.%s", util.GetClusterDomain())
312313
}
313314

314315
func (cluster Cluster) SecureMode() string {

pkg/resource/webhook_certificates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/cockroachdb/cockroach-operator/pkg/security"
24+
"github.com/cockroachdb/cockroach-operator/pkg/util"
2425
"github.com/cockroachdb/errors"
2526
"github.com/go-logr/logr"
2627
"go.uber.org/zap/zapcore"
@@ -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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"sync"
23+
"time"
24+
)
25+
26+
var (
27+
once = &sync.Once{}
28+
clusterDomain = "cluster.local"
29+
)
30+
31+
// GetClusterDomain returns the cluster domain of the k8s cluster.
32+
// It is auto-detected by lazily running a DNS query.
33+
// It defaults to "cluster.local" if we cannot determine the domain.
34+
func GetClusterDomain() string {
35+
once.Do(func() {
36+
// We try to lookup a non-FQDN that *should* always exist in the
37+
// k8s's domain.
38+
// Reference: https://stackoverflow.com/a/59162874
39+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40+
defer cancel()
41+
42+
const host = "kubernetes.default.svc"
43+
cname, err := net.DefaultResolver.LookupCNAME(ctx, host)
44+
if err == nil {
45+
clusterDomain = cname[len(host)+1 : len(cname)-1]
46+
}
47+
})
48+
return clusterDomain
49+
}

0 commit comments

Comments
 (0)