Skip to content

Commit c582bcd

Browse files
Add explicit default cluster defenition with internal_replication=1 and generated cluster secret (#89)
1 parent 75cbacf commit c582bcd

9 files changed

Lines changed: 309 additions & 114 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ linters:
106106
gosec:
107107
excludes:
108108
- G204
109-
godot:
110-
exclude:
111-
- "^ *\\+operator-sdk:"
112109
ireturn:
113110
allow:
114111
- error
@@ -117,6 +114,7 @@ linters:
117114
- generic
118115
- Option$
119116
- github.com\/ClickHouse\/clickhouse-go/v2\.Conn
117+
- github.com\/ClickHouse\/clickhouse-go\/v2\/lib\/driver\.Rows
120118
- k8s.io
121119
wsl_v5:
122120
allow-whole-block: true

internal/controller/clickhouse/config.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,28 @@ type configGeneratorFunc func(tmpl *template.Template, r *clickhouseReconciler,
166166
type baseConfigParams struct {
167167
Path string
168168
Log controller.LoggerConfig
169-
Macros map[string]any
169+
Macros []macro
170+
171+
KeeperNodes []keeperNode
172+
KeeperIdentityEnv string
170173

171-
KeeperNodes []keeperNode
172-
KeeperIdentityEnv string
173174
DistributedDDLPath string
174175
DistributedDDLProfileName string
175176
UsersXMLPath string
176177
UsersZookeeperPath string
177178
UDFZookeeperPath string
178179

180+
ClusterSecretEnv string
181+
ManagementPort uint16
182+
ClusterHosts [][]string
183+
179184
OpenSSL controller.OpenSSLConfig
180185
}
181186

187+
type macro struct {
188+
Name string
189+
Value any
190+
}
182191
type keeperNode struct {
183192
Host string
184193
Port int32
@@ -226,23 +235,38 @@ func baseConfigGenerator(tmpl *template.Template, r *clickhouseReconciler, id v1
226235
openSSL.Client.PreferServerCiphers = true
227236
}
228237

238+
clusterHosts := make([][]string, r.Cluster.Shards())
239+
for shard := range r.Cluster.Shards() {
240+
hosts := make([]string, r.Cluster.Replicas())
241+
for replica := range r.Cluster.Replicas() {
242+
hosts[replica] = r.Cluster.HostnameByID(v1.ClickHouseReplicaID{ShardID: shard, Index: replica})
243+
}
244+
245+
clusterHosts[shard] = hosts
246+
}
247+
229248
params := baseConfigParams{
230249
Path: internal.ClickHouseDataPath,
231250
Log: controller.GenerateLoggerConfig(r.Cluster.Spec.Settings.Logger, LogPath, "clickhouse-server"),
232-
Macros: map[string]any{
233-
"cluster": DefaultClusterName,
234-
"shard": id.ShardID,
235-
"replica": id.Index,
251+
Macros: []macro{
252+
{Name: "cluster", Value: DefaultClusterName},
253+
{Name: "shard", Value: id.ShardID},
254+
{Name: "replica", Value: id.Index},
236255
},
237256

238-
KeeperNodes: keeperNodes,
239-
KeeperIdentityEnv: EnvKeeperIdentity,
257+
KeeperNodes: keeperNodes,
258+
KeeperIdentityEnv: EnvKeeperIdentity,
259+
240260
DistributedDDLPath: KeeperPathDistributedDDL,
241261
DistributedDDLProfileName: DefaultProfileName,
242262
UsersXMLPath: UsersFileName,
243263
UsersZookeeperPath: KeeperPathUsers,
244264
UDFZookeeperPath: KeeperPathUDF,
245265

266+
ClusterSecretEnv: EnvClusterSecret,
267+
ManagementPort: PortManagement,
268+
ClusterHosts: clusterHosts,
269+
246270
OpenSSL: openSSL,
247271
}
248272

@@ -259,13 +283,28 @@ type networkConfigParams struct {
259283
InterserverHTTPUser string
260284
InterserverHTTPPasswordEnvVar string
261285
ManagementPort uint16
262-
Protocols map[string]protocol
286+
Protocols []namedProtocol
287+
}
288+
289+
type namedProtocol struct {
290+
Name string
291+
Protocol protocol
263292
}
264293

265294
func networkConfigGenerator(tmpl *template.Template, r *clickhouseReconciler, _ v1.ClickHouseReplicaID) (string, error) {
266-
protocols := buildProtocols(r.Cluster)
267-
delete(protocols, "interserver")
268-
delete(protocols, "management")
295+
var protocols []namedProtocol
296+
for name, proto := range buildProtocols(r.Cluster) {
297+
if name == "interserver" || name == "management" {
298+
continue
299+
}
300+
301+
protocols = append(protocols, namedProtocol{
302+
Name: name,
303+
Protocol: proto,
304+
})
305+
}
306+
307+
controllerutil.SortKey(protocols, func(p namedProtocol) string { return p.Name })
269308

270309
params := networkConfigParams{
271310
InterserverHTTPPort: PortInterserver,

internal/controller/clickhouse/constants.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ const (
4747
EnvInterserverPassword = "CLICKHOUSE_INTERSERVER_PASSWORD"
4848
EnvDefaultUserPassword = "CLICKHOUSE_DEFAULT_USER_PASSWORD"
4949
EnvKeeperIdentity = "CLICKHOUSE_KEEPER_IDENTITY"
50+
EnvClusterSecret = "CLICKHOUSE_CLUSTER_SECRET"
5051

5152
SecretKeyInterserverPassword = "interserver-password"
5253
SecretKeyManagementPassword = "management-password"
5354
SecretKeyKeeperIdentity = "keeper-identity"
55+
SecretKeyClusterSecret = "cluster-secret"
5456
)
5557

5658
var (
@@ -59,5 +61,14 @@ var (
5961
SecretKeyInterserverPassword: "%s",
6062
SecretKeyManagementPassword: "%s",
6163
SecretKeyKeeperIdentity: "clickhouse:%s",
64+
SecretKeyClusterSecret: "%s",
65+
}
66+
secretsToEnvMapping = []struct {
67+
Key string
68+
Env string
69+
}{
70+
{Key: SecretKeyInterserverPassword, Env: EnvInterserverPassword},
71+
{Key: SecretKeyKeeperIdentity, Env: EnvKeeperIdentity},
72+
{Key: SecretKeyClusterSecret, Env: EnvClusterSecret},
6273
}
6374
)

internal/controller/clickhouse/templates.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"path"
88
"strconv"
99

10-
v1 "github.com/ClickHouse/clickhouse-operator/api/v1alpha1"
11-
"github.com/ClickHouse/clickhouse-operator/internal"
12-
"github.com/ClickHouse/clickhouse-operator/internal/controller"
13-
"github.com/ClickHouse/clickhouse-operator/internal/controllerutil"
14-
1510
appsv1 "k8s.io/api/apps/v1"
1611
corev1 "k8s.io/api/core/v1"
1712
policyv1 "k8s.io/api/policy/v1"
1813
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1914
"k8s.io/apimachinery/pkg/util/intstr"
2015
"k8s.io/utils/ptr"
16+
17+
v1 "github.com/ClickHouse/clickhouse-operator/api/v1alpha1"
18+
"github.com/ClickHouse/clickhouse-operator/internal"
19+
"github.com/ClickHouse/clickhouse-operator/internal/controller"
20+
"github.com/ClickHouse/clickhouse-operator/internal/controllerutil"
2121
)
2222

2323
func templateHeadlessService(cr *v1.ClickHouseCluster) *corev1.Service {
@@ -240,28 +240,6 @@ func templateStatefulSet(r *clickhouseReconciler, id v1.ClickHouseReplicaID) (*a
240240
Name: "CLICKHOUSE_SKIP_USER_SETUP",
241241
Value: "1",
242242
},
243-
{
244-
Name: EnvInterserverPassword,
245-
ValueFrom: &corev1.EnvVarSource{
246-
SecretKeyRef: &corev1.SecretKeySelector{
247-
LocalObjectReference: corev1.LocalObjectReference{
248-
Name: r.Cluster.SecretName(),
249-
},
250-
Key: SecretKeyInterserverPassword,
251-
},
252-
},
253-
},
254-
{
255-
Name: EnvKeeperIdentity,
256-
ValueFrom: &corev1.EnvVarSource{
257-
SecretKeyRef: &corev1.SecretKeySelector{
258-
LocalObjectReference: corev1.LocalObjectReference{
259-
Name: r.Cluster.SecretName(),
260-
},
261-
Key: SecretKeyKeeperIdentity,
262-
},
263-
},
264-
},
265243
}, r.Cluster.Spec.ContainerTemplate.Env...),
266244
Ports: []corev1.ContainerPort{
267245
{
@@ -287,6 +265,20 @@ func templateStatefulSet(r *clickhouseReconciler, id v1.ClickHouseReplicaID) (*a
287265
},
288266
}
289267

268+
for _, secret := range secretsToEnvMapping {
269+
container.Env = append(container.Env, corev1.EnvVar{
270+
Name: secret.Env,
271+
ValueFrom: &corev1.EnvVarSource{
272+
SecretKeyRef: &corev1.SecretKeySelector{
273+
LocalObjectReference: corev1.LocalObjectReference{
274+
Name: r.Cluster.SecretName(),
275+
},
276+
Key: secret.Key,
277+
},
278+
},
279+
})
280+
}
281+
290282
container.Ports = make([]corev1.ContainerPort, 0, len(protocols))
291283
for name, protocol := range protocols {
292284
if protocol.Port == 0 {

internal/controller/clickhouse/templates/base.yaml.tmpl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ path: {{ .Path }}
22
logger:
33
{{ yaml .Log | indent 2 }}
44
macros:
5-
{{- range $key, $value := .Macros }}
6-
{{ $key }}: {{ $value }}
5+
{{- range $i, $m := .Macros }}
6+
{{ $m.Name }}: {{ $m.Value }}
77
{{- end }}
88

99
{{- /* Replication settings */}}
@@ -19,6 +19,21 @@ zookeeper:
1919
identity:
2020
"@from_env": {{ .KeeperIdentityEnv }}
2121

22+
remote_servers:
23+
default:
24+
secret:
25+
"@from_env": {{ .ClusterSecretEnv }}
26+
shard:
27+
{{- $ctx := .}}
28+
{{- range $shard, $replicas := .ClusterHosts }}
29+
- internal_replication: true
30+
replica:
31+
{{- range $i, $replica := $replicas }}
32+
- host: {{ $replica }}
33+
port: {{ $ctx.ManagementPort }}
34+
{{- end }}
35+
{{- end }}
36+
2237
distributed_ddl:
2338
path: {{ .DistributedDDLPath }}
2439
profile: {{ .DistributedDDLProfileName }}

internal/controller/clickhouse/templates/network.yaml.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ interserver_http_credentials:
1212
tcp_port: {{ .ManagementPort }}
1313

1414
protocols:
15-
{{- range $name, $protocol := .Protocols }}
16-
{{ $name }}:
17-
type: {{ $protocol.Type }}
18-
{{- if ne $protocol.Impl "" }}
19-
impl: {{ $protocol.Impl }}
15+
{{- range $protocol := .Protocols }}
16+
{{ $protocol.Name }}:
17+
type: {{ $protocol.Protocol.Type }}
18+
{{- if ne $protocol.Protocol.Impl "" }}
19+
impl: {{ $protocol.Protocol.Impl }}
2020
{{- end }}
21-
{{- if ne $protocol.Port 0 }}
22-
port: {{ $protocol.Port }}
21+
{{- if ne $protocol.Protocol.Port 0 }}
22+
port: {{ $protocol.Protocol.Port }}
2323
{{- end }}
24-
{{- if ne $protocol.Description "" }}
25-
description: {{ $protocol.Description }}
24+
{{- if ne $protocol.Protocol.Description "" }}
25+
description: {{ $protocol.Protocol.Description }}
2626
{{- end }}
2727
{{- end }}
2828

0 commit comments

Comments
 (0)