Skip to content

Commit a8fb565

Browse files
committed
Add httpAddr, sqlAddr, listenAddr in CrdbCluster API and migrated ports to addresses
1 parent a3078de commit a8fb565

18 files changed

+168
-56
lines changed

apis/v1alpha1/cluster_types.go

+16
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,31 @@ type CrdbClusterSpec struct {
4343
// (Optional) The database port (`--listen-addr` CLI parameter when starting the service)
4444
// Default: 26258
4545
// +optional
46+
// Deprecated: Use ListenAddr instead of GRPCPort
4647
GRPCPort *int32 `json:"grpcPort,omitempty"`
48+
// (Optional) The database port (`--listen-addr` CLI parameter when starting the service)
49+
// Default: ":26258"
50+
// +optional
51+
ListenAddr *string `json:"listenAddr,omitempty"`
4752
// (Optional) The web UI port (`--http-addr` CLI parameter when starting the service)
4853
// Default: 8080
4954
// +optional
55+
// Deprecated: Use HTTPAddr instead of HTTPPort
5056
HTTPPort *int32 `json:"httpPort,omitempty"`
57+
// (Optional) The IP address/hostname and port on which to listen for DB Console HTTP requests.
58+
// (`--http-addr` CLI parameter when starting the service)
59+
// Default: ":8080"
60+
// +optional
61+
HTTPAddr *string `json:"httpAddr,omitempty"`
5162
// (Optional) The SQL Port number
5263
// Default: 26257
5364
// +optional
65+
// Deprecated: Use SQLAddr instead of SQLPort
5466
SQLPort *int32 `json:"sqlPort,omitempty"`
67+
// (Optional) The IP address/hostname and port on which to listen for SQL connections from clients.
68+
// Default: ":26257"
69+
// +optional
70+
SQLAddr *string `json:"sqlAddr,omitempty"`
5571
// (Optional) TLSEnabled determines if TLS is enabled for your CockroachDB Cluster
5672
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="TLS Enabled",xDescriptors="urn:alm:descriptor:com.tectonic.ui:booleanSwitch"
5773
// +optional

apis/v1alpha1/webhook.go

+24-12
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import (
2929
)
3030

3131
var (
32-
// DefaultGRPCPort is the default port used for GRPC communication
33-
DefaultGRPCPort int32 = 26258
34-
// DefaultSQLPort is the default port used for SQL connections
35-
DefaultSQLPort int32 = 26257
36-
// DefaultHTTPPort is the default port for the Web UI
37-
DefaultHTTPPort int32 = 8080
32+
// DefaultGRPCAddr is the default grpc address used for GRPC communication
33+
DefaultGRPCAddr string = ":26258"
34+
// DefaultSQLAddr is the default sql address used for SQL connections
35+
DefaultSQLAddr string = ":26257"
36+
// DefaultHTTPAddr is the default http address for the Web UI
37+
DefaultHTTPAddr string = ":8080"
3838
// DefaultMaxUnavailable is the default max unavailable nodes during a rollout
3939
DefaultMaxUnavailable int32 = 1
4040
)
@@ -59,16 +59,28 @@ func (r *CrdbCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
5959
func (r *CrdbCluster) Default() {
6060
webhookLog.Info("default", "name", r.Name)
6161

62-
if r.Spec.GRPCPort == nil {
63-
r.Spec.GRPCPort = &DefaultGRPCPort
62+
if r.Spec.GRPCPort == nil && r.Spec.ListenAddr == nil {
63+
r.Spec.ListenAddr = &DefaultGRPCAddr
64+
} else if r.Spec.GRPCPort != nil && r.Spec.ListenAddr == nil {
65+
listenAddr := fmt.Sprintf(":%d", *r.Spec.GRPCPort)
66+
r.Spec.ListenAddr = &listenAddr
67+
r.Spec.GRPCPort = nil
6468
}
6569

66-
if r.Spec.SQLPort == nil {
67-
r.Spec.SQLPort = &DefaultSQLPort
70+
if r.Spec.SQLPort == nil && r.Spec.SQLAddr == nil {
71+
r.Spec.SQLAddr = &DefaultSQLAddr
72+
} else if r.Spec.SQLPort != nil && r.Spec.SQLAddr == nil {
73+
sqlAddr := fmt.Sprintf(":%d", *r.Spec.SQLPort)
74+
r.Spec.SQLAddr = &sqlAddr
75+
r.Spec.SQLPort = nil
6876
}
6977

70-
if r.Spec.HTTPPort == nil {
71-
r.Spec.HTTPPort = &DefaultHTTPPort
78+
if r.Spec.HTTPPort == nil && r.Spec.HTTPAddr == nil {
79+
r.Spec.HTTPAddr = &DefaultHTTPAddr
80+
} else if r.Spec.HTTPPort != nil && r.Spec.HTTPAddr == nil {
81+
httpAddr := fmt.Sprintf(":%d", *r.Spec.HTTPPort)
82+
r.Spec.HTTPAddr = &httpAddr
83+
r.Spec.HTTPPort = nil
7284
}
7385

7486
if r.Spec.MaxUnavailable == nil && r.Spec.MinAvailable == nil {

apis/v1alpha1/webhook_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func TestCrdbClusterDefault(t *testing.T) {
3636
maxUnavailable := int32(1)
3737
policy := v1.PullIfNotPresent
3838
expected := CrdbClusterSpec{
39-
GRPCPort: &DefaultGRPCPort,
40-
HTTPPort: &DefaultHTTPPort,
41-
SQLPort: &DefaultSQLPort,
39+
ListenAddr: &DefaultGRPCAddr,
40+
SQLAddr: &DefaultSQLAddr,
41+
HTTPAddr: &DefaultHTTPAddr,
4242
MaxUnavailable: &maxUnavailable,
4343
Image: &PodImage{PullPolicyName: &policy},
4444
}

apis/v1alpha1/zz_generated.deepcopy.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/crdb.cockroachlabs.com_crdbclusters.yaml

+19-3
Original file line numberDiff line numberDiff line change
@@ -1087,12 +1087,19 @@ spec:
10871087
type: object
10881088
grpcPort:
10891089
description: '(Optional) The database port (`--listen-addr` CLI parameter
1090-
when starting the service) Default: 26258'
1090+
when starting the service) Default: 26258 Deprecated: Use ListenAddr
1091+
instead of GRPCPort'
10911092
format: int32
10921093
type: integer
1094+
httpAddr:
1095+
description: '(Optional) The IP address/hostname and port on which
1096+
to listen for DB Console HTTP requests. (`--http-addr` CLI parameter
1097+
when starting the service) Default: ":8080"'
1098+
type: string
10931099
httpPort:
10941100
description: '(Optional) The web UI port (`--http-addr` CLI parameter
1095-
when starting the service) Default: 8080'
1101+
when starting the service) Default: 8080 Deprecated: Use HTTPAddr
1102+
instead of HTTPPort'
10961103
format: int32
10971104
type: integer
10981105
image:
@@ -1214,6 +1221,10 @@ spec:
12141221
- host
12151222
type: object
12161223
type: object
1224+
listenAddr:
1225+
description: '(Optional) The database port (`--listen-addr` CLI parameter
1226+
when starting the service) Default: ":26258"'
1227+
type: string
12171228
logConfigMap:
12181229
description: '(Optional) LogConfigMap define the config map which
12191230
contains log configuration used to send the logs through the proper
@@ -1389,8 +1400,13 @@ spec:
13891400
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
13901401
type: object
13911402
type: object
1403+
sqlAddr:
1404+
description: '(Optional) The IP address/hostname and port on which
1405+
to listen for SQL connections from clients. Default: ":26257"'
1406+
type: string
13921407
sqlPort:
1393-
description: '(Optional) The SQL Port number Default: 26257'
1408+
description: '(Optional) The SQL Port number Default: 26257 Deprecated:
1409+
Use SQLAddr instead of SQLPort'
13941410
format: int32
13951411
type: integer
13961412
tlsEnabled:

pkg/actor/decommission.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ func (d decommission) Act(ctx context.Context, cluster *resource.Cluster, log lo
9393

9494
// The connection needs to use the discovery service name because of the
9595
// hostnames in the SSL certificates
96+
sqlPort := cluster.GetSQLPort()
9697
conn := &database.DBConnection{
9798
Ctx: ctx,
9899
Client: d.client,
99100
RestConfig: d.config,
100101
ServiceName: serviceName,
101102
Namespace: cluster.Namespace(),
102103
DatabaseName: "system", // TODO we need to use variable instead of string
103-
Port: cluster.Spec().SQLPort,
104+
Port: &sqlPort,
104105
RunningInsideK8s: runningInsideK8s,
105106
}
106107

@@ -140,7 +141,7 @@ func (d decommission) Act(ctx context.Context, cluster *resource.Cluster, log lo
140141
Drainer: drainer,
141142
PVCPruner: &pvcPruner,
142143
}
143-
if err := scaler.EnsureScale(ctx, nodes, *cluster.Spec().GRPCPort, utilfeature.DefaultMutableFeatureGate.Enabled(features.AutoPrunePVC)); err != nil {
144+
if err := scaler.EnsureScale(ctx, nodes, *cluster.Spec().ListenAddr, utilfeature.DefaultMutableFeatureGate.Enabled(features.AutoPrunePVC)); err != nil {
144145
/// now check if the decommissionStaleErr and update status
145146
log.Error(err, "decommission failed")
146147
cluster.SetFalse(api.DecommissionCondition)

pkg/actor/initialize.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"github.com/go-logr/logr"
2323
"k8s.io/client-go/util/retry"
24-
"strconv"
2524
"strings"
2625

2726
api "github.com/cockroachdb/cockroach-operator/apis/v1alpha1"
@@ -92,12 +91,12 @@ func (init initialize) Act(ctx context.Context, cluster *resource.Cluster, log l
9291

9392
log.V(DEBUGLEVEL).Info("Pod is ready")
9493

95-
port := strconv.FormatInt(int64(*cluster.Spec().GRPCPort), 10)
94+
listenAddr := cluster.GetListenAddr()
9695
cmd := []string{
9796
"/cockroach/cockroach.sh",
9897
"init",
9998
cluster.SecureMode(),
100-
"--host=localhost:" + port,
99+
"--host=" + listenAddr,
101100
}
102101

103102
log.V(DEBUGLEVEL).Info(fmt.Sprintf("Executing init in pod %s with phase %s", podName, phase))

pkg/actor/partitioned_update.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ func (up *partitionedUpdate) Act(ctx context.Context, cluster *resource.Cluster,
140140

141141
// The connection needs to use the discovery service name because of the
142142
// hostnames in the SSL certificates
143+
sqlPort := cluster.GetSQLPort()
143144
conn := &database.DBConnection{
144145
Ctx: ctx,
145146
Client: up.client,
146147
RestConfig: up.config,
147148
ServiceName: serviceName,
148149
Namespace: cluster.Namespace(),
149150
DatabaseName: "system", // TODO we need to use variable instead of string
150-
Port: cluster.Spec().SQLPort,
151+
Port: &sqlPort,
151152
RunningInsideK8s: runningInsideK8s,
152153
}
153154

pkg/healthchecker/healthchecker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (hc *HealthCheckerImpl) waitUntilUnderReplicatedMetricIsZero(ctx context.Co
119119
// ranges_underreplicated{store="1"} 0
120120
func (hc *HealthCheckerImpl) checkUnderReplicatedMetric(ctx context.Context, l logr.Logger, logSuffix, podname, stsname, stsnamespace string, partition int32) error {
121121
l.V(int(zapcore.DebugLevel)).Info("checkUnderReplicatedMetric", "label", logSuffix, "podname", podname, "partition", partition)
122-
port := strconv.FormatInt(int64(*hc.cluster.Spec().HTTPPort), 10)
122+
port := strconv.FormatInt(int64(hc.cluster.GetHTTPPort()), 10)
123123
url := fmt.Sprintf("https://%s.%s.%s:%s/_status/vars", podname, stsname, stsnamespace, port)
124124

125125
runningInsideK8s := inK8s("/var/run/secrets/kubernetes.io/serviceaccount/token")

pkg/resource/cluster.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package resource
1919
import (
2020
"fmt"
2121
"os"
22+
"strconv"
2223
"strings"
2324
"time"
2425

@@ -48,7 +49,6 @@ const (
4849

4950
func NewCluster(original *api.CrdbCluster) Cluster {
5051
cr := original.DeepCopy()
51-
cr.Default()
5252

5353
timeNow := metav1.Now()
5454
condition.InitConditionsIfNeeded(&cr.Status, timeNow)
@@ -415,3 +415,44 @@ func (cluster Cluster) IsUIIngressEnabled() bool {
415415
func (cluster Cluster) IsSQLIngressEnabled() bool {
416416
return cluster.Spec().Ingress != nil && cluster.Spec().Ingress.SQL != nil
417417
}
418+
419+
func (cluster Cluster) GetListenAddr() string {
420+
if cluster.Spec().ListenAddr != nil {
421+
return *cluster.Spec().ListenAddr
422+
}
423+
return fmt.Sprintf(":%d", cluster.GetGRPCPort())
424+
}
425+
426+
func (cluster Cluster) GetSQLAddr() string {
427+
if cluster.Spec().SQLAddr != nil {
428+
return *cluster.Spec().SQLAddr
429+
}
430+
return fmt.Sprintf(":%d", cluster.GetSQLPort())
431+
}
432+
433+
func (cluster Cluster) GetGRPCPort() int32 {
434+
if cluster.Spec().GRPCPort != nil {
435+
return *cluster.Spec().GRPCPort
436+
}
437+
addr := strings.Split(*cluster.Spec().ListenAddr, ":")
438+
i, _ := strconv.ParseInt(addr[1], 10, 32)
439+
return int32(i)
440+
}
441+
442+
func (cluster Cluster) GetSQLPort() int32 {
443+
if cluster.Spec().SQLPort != nil {
444+
return *cluster.Spec().SQLPort
445+
}
446+
addr := strings.Split(*cluster.Spec().SQLAddr, ":")
447+
i, _ := strconv.ParseInt(addr[1], 10, 32)
448+
return int32(i)
449+
}
450+
451+
func (cluster Cluster) GetHTTPPort() int32 {
452+
if cluster.Spec().HTTPPort != nil {
453+
return *cluster.Spec().HTTPPort
454+
}
455+
addr := strings.Split(*cluster.Spec().HTTPAddr, ":")
456+
i, _ := strconv.ParseInt(addr[1], 10, 32)
457+
return int32(i)
458+
}

pkg/resource/discovery_service.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func (b DiscoveryServiceBuilder) Build(obj client.Object) error {
6767
ClusterIP: "None",
6868
PublishNotReadyAddresses: true,
6969
Ports: []corev1.ServicePort{
70-
{Name: "grpc", Port: *b.Cluster.Spec().GRPCPort},
71-
{Name: "http", Port: *b.Cluster.Spec().HTTPPort},
72-
{Name: "sql", Port: *b.Cluster.Spec().SQLPort},
70+
{Name: "grpc", Port: b.Cluster.GetGRPCPort()},
71+
{Name: "http", Port: b.Cluster.GetHTTPPort()},
72+
{Name: "sql", Port: b.Cluster.GetSQLPort()},
7373
},
7474
Selector: b.Selector,
7575
}
@@ -89,6 +89,6 @@ func (b *DiscoveryServiceBuilder) monitoringAnnotations() map[string]string {
8989
return map[string]string{
9090
"prometheus.io/scrape": "true",
9191
"prometheus.io/path": "_status/vars",
92-
"prometheus.io/port": fmt.Sprint(*(b.Cluster.Spec().HTTPPort)),
92+
"prometheus.io/port": fmt.Sprint(b.Cluster.GetHTTPPort()),
9393
}
9494
}

pkg/resource/public_service.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (b PublicServiceBuilder) Build(obj client.Object) error {
5454
service.Spec = corev1.ServiceSpec{
5555
Type: corev1.ServiceTypeClusterIP,
5656
Ports: []corev1.ServicePort{
57-
{Name: "grpc", Port: *b.Cluster.Spec().GRPCPort},
58-
{Name: "http", Port: *b.Cluster.Spec().HTTPPort},
59-
{Name: "sql", Port: *b.Cluster.Spec().SQLPort},
57+
{Name: "grpc", Port: b.Cluster.GetGRPCPort()},
58+
{Name: "http", Port: b.Cluster.GetHTTPPort()},
59+
{Name: "sql", Port: b.Cluster.GetSQLPort()},
6060
},
6161
}
6262
}

pkg/resource/resource_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestReconcile(t *testing.T) {
5757
{
5858
name: "updates object when its spec is different",
5959
cluster: testutil.NewBuilder("test-cluster").Namespaced("default").
60-
WithUID("test-cluster-uid").WithHTTPPort(8443).Cluster(),
60+
WithUID("test-cluster-uid").WithHTTPAddr(":8443").Cluster(),
6161
existingObjs: []runtime.Object{makeTestService()},
6262
wantUpserted: true,
6363
expected: modifyHTTPPort(8443, makeTestService()),

0 commit comments

Comments
 (0)