Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for auto detecting cluster domain #894

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* Correctly detect failed version checker Pods
* retry cluster status updates, reducing test flakes

## Changed
* Cluster domain for cert generation is now autodetected by running a DNS query

# [v2.7.0](https://github.com/cockroachdb/cockroach-operator/compare/v2.6.0...v2.7.0)

## Fixed
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_library(
"//pkg/labels:go_default_library",
"//pkg/ptr:go_default_library",
"//pkg/security:go_default_library",
"//pkg/util:go_default_library",
"//pkg/utilfeature:go_default_library",
"@com_github_cockroachdb_errors//:go_default_library",
"@com_github_go_logr_logr//:go_default_library",
Expand Down
7 changes: 4 additions & 3 deletions pkg/resource/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
api "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
"github.com/cockroachdb/cockroach-operator/pkg/clusterstatus"
"github.com/cockroachdb/cockroach-operator/pkg/condition"
"github.com/cockroachdb/cockroach-operator/pkg/util"
"github.com/cockroachdb/errors"
"github.com/gosimple/slug"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -199,7 +200,7 @@ func (cluster Cluster) LookupSupportedVersion(version string) (string, bool) {
return "", false
}

//GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
// GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
func (cluster Cluster) GetVersionAnnotation() string {
return cluster.getAnnotation(CrdbVersionAnnotation)
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func (cluster Cluster) GetCockroachDBImageName() string {
}
return NotSupportedVersion
}
//we validate the version after the job runs with exec
// we validate the version after the job runs with exec
return cluster.Spec().Image.Name
}

Expand Down Expand Up @@ -308,7 +309,7 @@ func (cluster Cluster) CASecretName() string {
}

func (cluster Cluster) Domain() string {
return "svc.cluster.local"
return fmt.Sprintf("svc.%s", util.GetClusterDomain())
}

func (cluster Cluster) SecureMode() string {
Expand Down
3 changes: 2 additions & 1 deletion pkg/resource/webhook_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/cockroachdb/cockroach-operator/pkg/security"
"github.com/cockroachdb/cockroach-operator/pkg/util"
"github.com/cockroachdb/errors"
"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -97,7 +98,7 @@ func CreateWebhookCertificate(ctx context.Context, api SecretsInterface, ns stri
webhookService,
fmt.Sprintf("%s.%s", webhookService, ns),
fmt.Sprintf("%s.%s.svc", webhookService, ns),
fmt.Sprintf("%s.%s.svc.cluster.local", webhookService, ns),
fmt.Sprintf("%s.%s.svc.%s", webhookService, ns, util.GetClusterDomain()),
))

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"api_kind_checker.go",
"cluster_domain.go",
"tmp_dir.go",
],
importpath = "github.com/cockroachdb/cockroach-operator/pkg/util",
Expand Down
49 changes: 49 additions & 0 deletions pkg/util/cluster_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2022 The Cockroach Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"context"
"net"
"sync"
"time"
)

var (
once = &sync.Once{}
clusterDomain = "cluster.local"
)

// GetClusterDomain returns the cluster domain of the k8s cluster.
// It is auto-detected by lazily running a DNS query.
// It defaults to "cluster.local" if we cannot determine the domain.
func GetClusterDomain() string {
once.Do(func() {
// We try to lookup a non-FQDN that *should* always exist in the
// k8s's domain.
// Reference: https://stackoverflow.com/a/59162874
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

const host = "kubernetes.default.svc"
cname, err := net.DefaultResolver.LookupCNAME(ctx, host)
if err == nil {
clusterDomain = cname[len(host)+1 : len(cname)-1]
}
})
return clusterDomain
}