Skip to content

Commit

Permalink
dev: generalize confgile files generator - take 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed May 16, 2024
1 parent 597c2f7 commit eaa6d29
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 44 deletions.
8 changes: 8 additions & 0 deletions pkg/model/chi/config/ch_config_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import (
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
)

type FilesGroupType string

const (
ConfigFilesGroupCommon FilesGroupType = "FilesGroupType common"
ConfigFilesGroupUsers FilesGroupType = "FilesGroupType users"
ConfigFilesGroupHost FilesGroupType = "FilesGroupType host"
)

const (
xmlTagYandex = "yandex"
)
Expand Down
65 changes: 26 additions & 39 deletions pkg/model/chi/config/ch_config_files_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,28 @@ func NewClickHouseConfigFilesGenerator(chi *api.ClickHouseInstallation, opts *Cl
}
}

// CreateConfigFilesGroupCommon creates common config files
func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupCommon(options *ClickHouseConfigFilesGeneratorOptions) map[string]string {
func (c *ClickHouseConfigFilesGenerator) CreateConfigFiles(what FilesGroupType, params ...any) map[string]string {
switch what {
case ConfigFilesGroupCommon:
var options *ClickHouseConfigFilesGeneratorOptions
if len(params) > 0 {
options = params[0].(*ClickHouseConfigFilesGeneratorOptions)
return c.createConfigFilesGroupCommon(options)
}
case ConfigFilesGroupUsers:
return c.createConfigFilesGroupUsers()
case ConfigFilesGroupHost:
var host *api.Host
if len(params) > 0 {
host = params[0].(*api.Host)
return c.createConfigFilesGroupHost(host)
}
}
return nil
}

// createConfigFilesGroupCommon creates common config files
func (c *ClickHouseConfigFilesGenerator) createConfigFilesGroupCommon(options *ClickHouseConfigFilesGeneratorOptions) map[string]string {
if options == nil {
options = defaultClickHouseConfigFilesGeneratorOptions()
}
Expand All @@ -55,8 +75,8 @@ func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupCommon(options *C
return commonConfigSections
}

// CreateConfigFilesGroupUsers creates users config files
func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupUsers() map[string]string {
// createConfigFilesGroupUsers creates users config files
func (c *ClickHouseConfigFilesGenerator) createConfigFilesGroupUsers() map[string]string {
commonUsersConfigSections := make(map[string]string)
// commonUsersConfigSections maps section name to section XML chopConfig of the following sections:
// 1. users
Expand All @@ -73,8 +93,8 @@ func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupUsers() map[strin
return commonUsersConfigSections
}

// CreateConfigFilesGroupHost creates host config files
func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupHost(host *api.Host) map[string]string {
// createConfigFilesGroupHost creates host config files
func (c *ClickHouseConfigFilesGenerator) createConfigFilesGroupHost(host *api.Host) map[string]string {
// Prepare for this replica deployment chopConfig files map as filename->content
hostConfigSections := make(map[string]string)
util.IncludeNonEmpty(hostConfigSections, createConfigSectionFilename(configMacros), c.configGenerator.getHostMacros(host))
Expand All @@ -93,36 +113,3 @@ func (c *ClickHouseConfigFilesGenerator) CreateConfigFilesGroupHost(host *api.Ho
func createConfigSectionFilename(section string) string {
return "chop-generated-" + section + ".xml"
}

// ClickHouseConfigFilesGeneratorOptions specifies options for clickhouse configuration generator
type ClickHouseConfigFilesGeneratorOptions struct {
RemoteServersGeneratorOptions *RemoteServersGeneratorOptions
}

// NewClickHouseConfigFilesGeneratorOptions creates new options for clickhouse configuration generator
func NewClickHouseConfigFilesGeneratorOptions() *ClickHouseConfigFilesGeneratorOptions {
return &ClickHouseConfigFilesGeneratorOptions{}
}

// GetRemoteServersGeneratorOptions gets remote-servers generator options
func (o *ClickHouseConfigFilesGeneratorOptions) GetRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
if o == nil {
return nil
}
return o.RemoteServersGeneratorOptions
}

// SetRemoteServersGeneratorOptions sets remote-servers generator options
func (o *ClickHouseConfigFilesGeneratorOptions) SetRemoteServersGeneratorOptions(opts *RemoteServersGeneratorOptions) *ClickHouseConfigFilesGeneratorOptions {
if o == nil {
return nil
}
o.RemoteServersGeneratorOptions = opts

return o
}

// defaultClickHouseConfigFilesGeneratorOptions creates new default options for clickhouse config generator
func defaultClickHouseConfigFilesGeneratorOptions() *ClickHouseConfigFilesGeneratorOptions {
return NewClickHouseConfigFilesGeneratorOptions()
}
48 changes: 48 additions & 0 deletions pkg/model/chi/config/ch_config_files_generator_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// 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
//
// http://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 config

// ClickHouseConfigFilesGeneratorOptions specifies options for clickhouse configuration generator
type ClickHouseConfigFilesGeneratorOptions struct {
RemoteServersGeneratorOptions *RemoteServersGeneratorOptions
}

// NewClickHouseConfigFilesGeneratorOptions creates new options for clickhouse configuration generator
func NewClickHouseConfigFilesGeneratorOptions() *ClickHouseConfigFilesGeneratorOptions {
return &ClickHouseConfigFilesGeneratorOptions{}
}

// GetRemoteServersGeneratorOptions gets remote-servers generator options
func (o *ClickHouseConfigFilesGeneratorOptions) GetRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
if o == nil {
return nil
}
return o.RemoteServersGeneratorOptions
}

// SetRemoteServersGeneratorOptions sets remote-servers generator options
func (o *ClickHouseConfigFilesGeneratorOptions) SetRemoteServersGeneratorOptions(opts *RemoteServersGeneratorOptions) *ClickHouseConfigFilesGeneratorOptions {
if o == nil {
return nil
}
o.RemoteServersGeneratorOptions = opts

return o
}

// defaultClickHouseConfigFilesGeneratorOptions creates new default options for clickhouse config generator
func defaultClickHouseConfigFilesGeneratorOptions() *ClickHouseConfigFilesGeneratorOptions {
return NewClickHouseConfigFilesGeneratorOptions()
}
6 changes: 3 additions & 3 deletions pkg/model/chi/creator/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *Creator) createConfigMapCHICommon(options *config.ClickHouseConfigFiles
OwnerReferences: createOwnerReferences(c.chi),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFilesGroupCommon(options),
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupCommon, options),
}
// And after the object is ready we can put version label
tags.MakeObjectVersion(cm.GetObjectMeta(), cm)
Expand All @@ -82,7 +82,7 @@ func (c *Creator) createConfigMapCHICommonUsers() *core.ConfigMap {
OwnerReferences: createOwnerReferences(c.chi),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFilesGroupUsers(),
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupUsers),
}
// And after the object is ready we can put version label
tags.MakeObjectVersion(cm.GetObjectMeta(), cm)
Expand All @@ -100,7 +100,7 @@ func (c *Creator) createConfigMapCHIHost(host *api.Host) *core.ConfigMap {
OwnerReferences: createOwnerReferences(c.chi),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFilesGroupHost(host),
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupHost, host),
}
// And after the object is ready we can put version label
tags.MakeObjectVersion(cm.GetObjectMeta(), cm)
Expand Down
8 changes: 6 additions & 2 deletions pkg/model/chi/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ type tagger interface {
Selector(what tags.SelectorType, params ...any) map[string]string
}

type configFileGenerator interface {
CreateConfigFiles(what config.FilesGroupType, params ...any) map[string]string
}

// Creator specifies creator object
type Creator struct {
chi api.IChi
configFilesGenerator *config.ClickHouseConfigFilesGenerator
configFilesGenerator configFileGenerator
tagger tagger
a log.Announcer
// namer
Expand All @@ -43,7 +47,7 @@ type Creator struct {
}

// NewCreator creates new Creator object
func NewCreator(chi api.IChi, configFilesGenerator *config.ClickHouseConfigFilesGenerator) *Creator {
func NewCreator(chi api.IChi, configFilesGenerator configFileGenerator) *Creator {
return &Creator{
chi: chi,
configFilesGenerator: configFilesGenerator,
Expand Down

0 comments on commit eaa6d29

Please sign in to comment.