Skip to content

Commit cfac700

Browse files
authored
resource/cluster: prefer custom node-tls or client-tls secrets (#909)
1 parent d84779c commit cfac700

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

pkg/resource/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ go_library(
5757
go_test(
5858
name = "go_default_test",
5959
srcs = [
60+
"cluster_test.go",
6061
"certificate_test.go",
6162
"discovery_service_test.go",
6263
"pod_distruption_budget_test.go",

pkg/resource/cluster.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,18 @@ func (cluster Cluster) GetImagePullSecret() *string {
289289
}
290290

291291
func (cluster Cluster) NodeTLSSecretName() string {
292+
if cluster.Spec().NodeTLSSecret != "" {
293+
return cluster.Spec().NodeTLSSecret
294+
}
295+
292296
return fmt.Sprintf("%s-node", cluster.Name())
293297
}
294298

295299
func (cluster Cluster) ClientTLSSecretName() string {
300+
if cluster.Spec().ClientTLSSecret != "" {
301+
return cluster.Spec().ClientTLSSecret
302+
}
303+
296304
return fmt.Sprintf("%s-root", cluster.Name())
297305
}
298306
func (cluster Cluster) CASecretName() string {

pkg/resource/cluster_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
package resource_test
17+
18+
import (
19+
"testing"
20+
21+
"fmt"
22+
23+
"github.com/cockroachdb/cockroach-operator/pkg/resource"
24+
"github.com/cockroachdb/cockroach-operator/pkg/testutil"
25+
"github.com/google/go-cmp/cmp"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func TestClusterTLSSecrets(t *testing.T) {
30+
var (
31+
testCluster = "test-cluster"
32+
testNS = "test-ns"
33+
34+
customNodeTLS = "custom-node-tls"
35+
customClientTLS = "custom-client-tls"
36+
)
37+
38+
clusterBuilder := testutil.NewBuilder(testCluster).Namespaced(testNS)
39+
40+
for _, tt := range []struct {
41+
name string
42+
cluster *resource.Cluster
43+
nodeTLSSecretName string
44+
clientTLSSecretName string
45+
}{
46+
{
47+
name: "verify default node tls cert",
48+
cluster: clusterBuilder.Cluster(),
49+
nodeTLSSecretName: "test-cluster-node",
50+
},
51+
{
52+
name: "verify default client tls cert",
53+
cluster: clusterBuilder.Cluster(),
54+
clientTLSSecretName: "test-cluster-root",
55+
},
56+
{
57+
name: "verify custom node tls cert",
58+
cluster: clusterBuilder.WithNodeTLS(customNodeTLS).Cluster(),
59+
nodeTLSSecretName: customNodeTLS,
60+
},
61+
{
62+
name: "verify custom client tls cert",
63+
cluster: clusterBuilder.WithClientTLS(customClientTLS).Cluster(),
64+
clientTLSSecretName: customClientTLS,
65+
},
66+
} {
67+
t.Run(tt.name, func(t *testing.T) {
68+
var expected, actual string
69+
70+
if tt.nodeTLSSecretName != "" {
71+
expected = tt.nodeTLSSecretName
72+
actual = tt.cluster.NodeTLSSecretName()
73+
74+
}
75+
76+
if tt.clientTLSSecretName != "" {
77+
expected = tt.clientTLSSecretName
78+
actual = tt.cluster.ClientTLSSecretName()
79+
}
80+
81+
diff := cmp.Diff(expected, actual, testutil.RuntimeObjCmpOpts...)
82+
if diff != "" {
83+
assert.Fail(t, fmt.Sprintf("unexpected result (-want +got):\n%v", diff))
84+
}
85+
})
86+
}
87+
}

pkg/testutil/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (b ClusterBuilder) WithTLS() ClusterBuilder {
9292
return b
9393
}
9494

95+
func (b ClusterBuilder) WithClientTLS(secret string) ClusterBuilder {
96+
b.cluster.Spec.ClientTLSSecret = secret
97+
return b
98+
}
99+
95100
func (b ClusterBuilder) WithNodeTLS(secret string) ClusterBuilder {
96101
b.cluster.Spec.NodeTLSSecret = secret
97102
return b

0 commit comments

Comments
 (0)