8000 add zookeeper configuration in ChiCluster by qixiaogang · Pull Request #288 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
39 changes: 39 additions & 0 deletions docs/chi-examples/04-replication-zookeeper-06-on-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: "clickhouse.altinity.com/v1"
kind: "ClickHouseInstallation"
metadata:
name: "repl-06"
spec:
configuration:
zookeeper:
nodes:
- host: zookeeper-0.zookeepers.zoo3ns
port: 2181
- host: zookeeper-1.zookeepers.zoo3ns
port: 2181
- host: zookeeper-2.zookeepers.zoo3ns
port: 2181
session_timeout_ms: 30000
operation_timeout_ms: 10000
root: /path/to/zookeeper/node
identity: user:password
clusters:
- name: replcluster1
layout:
shardsCount: 3
replicasCount: 2
- name: replcluster2
zookeeper:
nodes:
- host: zookeeper-0.zookeepers.zoo5ns
port: 2181
- host: zookeeper-1.zookeepers.zoo5ns
port: 2181
- host: zookeeper-2.zookeepers.zoo5ns
port: 2181
- host: zookeeper-3.zookeepers.zoo5ns
port: 2181
- host: zookeeper-4.zookeepers.zoo5ns
port: 2181
layout:
shardsCount: 3
replicasCount: 2
7 changes: 4 additions & 3 deletions pkg/apis/clickhouse.altinity.com/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ type ChiConfiguration struct {

// ChiCluster defines item of a clusters section of .configuration
type ChiCluster struct {
Name string `json:"name"`
Layout ChiClusterLayout `json:"layout"`
Templates ChiTemplateNames `json:"templates,omitempty"`
Name string `json:"name"`
Zookeeper ChiZookeeperConfig `json:"zookeeper,omitempty" yaml:"zookeeper"`
Layout ChiClusterLayout `json:"layout"`
Templates ChiTemplateNames `json:"templates,omitempty"`

// Internal data
Address ChiClusterAddress `json:"address"`
Expand Down
8 changes: 7 additions & 1 deletion pkg/model/ch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ func (c *ClickHouseConfigGenerator) GetFiles() map[string]string {
}

// GetZookeeper creates data for "zookeeper.xml"
func (c *ClickHouseConfigGenerator) GetZookeeper() string {
func (c *ClickHouseConfigGenerator) GetZookeeper(host *chiv1.ChiHost) string {
// Convenience wrapper
zk := &c.chi.Spec.Configuration.Zookeeper
for index := range host.CHI.Spec.Configuration.Clusters {
if host.Address.ClusterName == host.CHI.Spec.Configuration.Clusters[index].Name &&
len(host.CHI.Spec.Configuration.Clusters[index].Zookeeper.Nodes) > 0 {
zk = &host.CHI.Spec.Configuration.Clusters[index].Zookeeper
}
}

if len(zk.Nodes) == 0 {
// No Zookeeper nodes provided
Expand Down
7 changes: 3 additions & 4 deletions pkg/model/ch_config_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ func NewConfigSections(chConfigGenerator *ClickHouseConfigGenerator, chopConfig
func (c *configSections) CreateConfigsCommon() {
// commonConfigSections maps section name to section XML chopConfig of the following sections:
// 1. remote servers
// 2. zookeeper
// 3. settings
// 4. files
// 2. settings
// 3. files
util.IncludeNonEmpty(c.commonConfigSections, filenameRemoteServersXML, c.chConfigGenerator.GetRemoteServers())
util.IncludeNonEmpty(c.commonConfigSections, filenameZookeeperXML, c.chConfigGenerator.GetZookeeper())
util.IncludeNonEmpty(c.commonConfigSections, filenameSettingsXML, c.chConfigGenerator.GetSettings())
util.MergeStringMaps(c.commonConfigSections, c.chConfigGenerator.GetFiles())
// Extra user-specified config files
Expand All @@ -71,6 +69,7 @@ func (c *configSections) CreateConfigsHost(host *chi.ChiHost) map[string]string
hostConfigSections := make(map[string]string)
util.IncludeNonEmpty(hostConfigSections, filenameMacrosXML, c.chConfigGenerator.GetHostMacros(host))
util.IncludeNonEmpty(hostConfigSections, filenamePortsXML, c.chConfigGenerator.GetHostPorts(host))
util.IncludeNonEmpty(hostConfigSections, filenameZookeeperXML, c.chConfigGenerator.GetZookeeper(host))
// Extra user-specified config files
util.MergeStringMaps(hostConfigSections, c.chopConfig.CHHostConfigs)

Expand Down
130 changes: 130 additions & 0 deletions pkg/model/ch_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package model

import (
"testing"

chiv1 "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/chop"
"github.com/kubernetes-sigs/yaml"
"github.com/stretchr/testify/require"
)

var ZookeeperOnClusterData = `
apiVersion: "clickhouse.altinity.com/v1"
kind: "ClickHouseInstallation"
metadata:
name: "repl-06"
spec:
configuration:
zookeeper:
nodes:
- host: zookeeper-0.zookeepers.zoo3ns
port: 2181
- host: zookeeper-1.zookeepers.zoo3ns
port: 2181
- host: zookeeper-2.zookeepers.zoo3ns
port: 2181
session_timeout_ms: 30000
operation_timeout_ms: 10000
root: /path/to/zookeeper/node
identity: user:password
clusters:
- name: replcluster1
layout:
shardsCount: 3
replicasCount: 2
- name: replcluster2
zookeeper:
nodes:
- host: zookeeper-0.zookeepers.zoo5ns
port: 2181
- host: zookeeper-1.zookeepers.zoo5ns
port: 2181
- host: zookeeper-2.zookeepers.zoo5ns
port: 2181
- host: zookeeper-3.zookeepers.zoo5ns
port: 2181
- host: zookeeper-4.zookeepers.zoo5ns
port: 2181
layout:
shardsCount: 3
replicasCount: 2
`

var Cluster1ZookeeperConfigurationData = `<yandex>
<zookeeper>
<node>
<host>zookeeper-0.zookeepers.zoo3ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-1.zookeepers.zoo3ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-2.zookeepers.zoo3ns</host>
<port>2181</port>
</node>
<session_timeout_ms>30000</session_timeout_ms>
<operation_timeout_ms>10000</operation_timeout_ms>
<root>/path/to/zookeeper/node</root>
<identity>user:password</identity>
</zookeeper>
<distributed_ddl>
<path>/clickhouse/repl-06/task_queue/ddl</path>
</distributed_ddl>
</yandex>
`

var Cluster2ZookeeperConfigurationData = `<yandex>
<zookeeper>
<node>
<host>zookeeper-0.zookeepers.zoo5ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-1.zookeepers.zoo5ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-2.zookeepers.zoo5ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-3.zookeepers.zoo5ns</host>
<port>2181</port>
</node>
<node>
<host>zookeeper-4.zookeepers.zoo5ns</host>
<port>2181</port>
</node>
</zookeeper>
<distributed_ddl>
<path>/clickhouse/repl-06/task_queue/ddl</path>
</distributed_ddl>
</yandex>
`

func TestGetZookeeper(t *testing.T) {
chi := new(chiv1.ClickHouseInstallation)
err := yaml.Unmarshal([]byte(ZookeeperOnClusterData), chi)
require.Nil(t, err, "failed to unmarshal chi")

CHOp := chop.NewCHOp("", nil, "")
CHOp.Init()
normalizer := NewNormalizer(CHOp)
chi, err = normalizer.NormalizeChi(chi)
require.Nil(t, err, "failed to normalize chi")

creator := NewCreator(CHOp, chi)
chi.WalkHostsTillError(func(host *chiv1.ChiHost) error {
str := creator.chConfigGenerator.GetZookeeper(host)
if host.Address.ClusterName == "replcluster2" {
require.Equal(t, Cluster2ZookeeperConfigurationData, str, "unexpected zookeeper configuration")
} else {
require.Equal(t, Cluster1ZookeeperConfigurationData, str, "unexpected zookeeper configuration")
}

return nil
})
}
0