Skip to content

Commit

Permalink
dev: structurise sts creator
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsingerus committed May 16, 2024
1 parent eaa6d29 commit 7ce3458
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 342 deletions.
116 changes: 1 addition & 115 deletions pkg/model/chi/config/ch_config_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ package config
import (
"bytes"
"fmt"
"github.com/altinity/clickhouse-operator/pkg/model/chi/namer"
"strings"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/model/chi"
"github.com/altinity/clickhouse-operator/pkg/model/chi/namer"
"github.com/altinity/clickhouse-operator/pkg/util"
"github.com/altinity/clickhouse-operator/pkg/xml"
)
Expand All @@ -47,15 +45,6 @@ type ClickHouseConfigGenerator struct {
opts *ClickHouseConfigGeneratorOptions
}

type ClickHouseConfigGeneratorOptions struct {
DistributedDDL *api.ChiDistributedDDL
Users *api.Settings
Profiles *api.Settings
Quotas *api.Settings
Settings *api.Settings
Files *api.Settings
}

// newClickHouseConfigGenerator returns new ClickHouseConfigGenerator struct
func newClickHouseConfigGenerator(chi api.IChi, opts *ClickHouseConfigGeneratorOptions) *ClickHouseConfigGenerator {
return &ClickHouseConfigGenerator{
Expand Down Expand Up @@ -185,109 +174,6 @@ func (c *ClickHouseConfigGenerator) getHostZookeeper(host *api.Host) string {
return b.String()
}

// RemoteServersGeneratorOptions specifies options for remote-servers generator
type RemoteServersGeneratorOptions struct {
exclude struct {
attributes *api.HostReconcileAttributes
hosts []*api.Host
}
}

// NewRemoteServersGeneratorOptions creates new remote-servers generator options
func NewRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
return &RemoteServersGeneratorOptions{}
}

// ExcludeHost specifies to exclude a host
func (o *RemoteServersGeneratorOptions) ExcludeHost(host *api.Host) *RemoteServersGeneratorOptions {
if (o == nil) || (host == nil) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, host)
return o
}

// ExcludeHosts specifies to exclude list of hosts
func (o *RemoteServersGeneratorOptions) ExcludeHosts(hosts ...*api.Host) *RemoteServersGeneratorOptions {
if (o == nil) || (len(hosts) == 0) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, hosts...)
return o
}

// ExcludeReconcileAttributes specifies to exclude reconcile attributes
func (o *RemoteServersGeneratorOptions) ExcludeReconcileAttributes(attrs *api.HostReconcileAttributes) *RemoteServersGeneratorOptions {
if (o == nil) || (attrs == nil) {
return o
}

o.exclude.attributes = attrs
return o
}

// Exclude tells whether to exclude the host
func (o *RemoteServersGeneratorOptions) Exclude(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return true
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return true
}
}

return false
}

// Include tells whether to include the host
func (o *RemoteServersGeneratorOptions) Include(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return false
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return false
}
}

return true
}

// String returns string representation
func (o *RemoteServersGeneratorOptions) String() string {
if o == nil {
return "(nil)"
}

var hostnames []string
for _, host := range o.exclude.hosts {
hostnames = append(hostnames, host.Name)
}
return fmt.Sprintf("exclude hosts: %s, attributes: %s", "["+strings.Join(hostnames, ",")+"]", o.exclude.attributes)
}

// defaultRemoteServersGeneratorOptions
func defaultRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
return NewRemoteServersGeneratorOptions()
}

// chiHostsNum count hosts according to the options
func (c *ClickHouseConfigGenerator) chiHostsNum(options *RemoteServersGeneratorOptions) int {
num := 0
Expand Down
134 changes: 134 additions & 0 deletions pkg/model/chi/config/ch_config_generator_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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

import (
"fmt"
"strings"

api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
)

type ClickHouseConfigGeneratorOptions struct {
DistributedDDL *api.ChiDistributedDDL
Users *api.Settings
Profiles *api.Settings
Quotas *api.Settings
Settings *api.Settings
Files *api.Settings
}

// RemoteServersGeneratorOptions specifies options for remote-servers generator
type RemoteServersGeneratorOptions struct {
exclude struct {
attributes *api.HostReconcileAttributes
hosts []*api.Host
}
}

// NewRemoteServersGeneratorOptions creates new remote-servers generator options
func NewRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
return &RemoteServersGeneratorOptions{}
}

// ExcludeHost specifies to exclude a host
func (o *RemoteServersGeneratorOptions) ExcludeHost(host *api.Host) *RemoteServersGeneratorOptions {
if (o == nil) || (host == nil) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, host)
return o
}

// ExcludeHosts specifies to exclude list of hosts
func (o *RemoteServersGeneratorOptions) ExcludeHosts(hosts ...*api.Host) *RemoteServersGeneratorOptions {
if (o == nil) || (len(hosts) == 0) {
return o
}

o.exclude.hosts = append(o.exclude.hosts, hosts...)
return o
}

// ExcludeReconcileAttributes specifies to exclude reconcile attributes
func (o *RemoteServersGeneratorOptions) ExcludeReconcileAttributes(attrs *api.HostReconcileAttributes) *RemoteServersGeneratorOptions {
if (o == nil) || (attrs == nil) {
return o
}

o.exclude.attributes = attrs
return o
}

// Exclude tells whether to exclude the host
func (o *RemoteServersGeneratorOptions) Exclude(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return true
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return true
}
}

return false
}

// Include tells whether to include the host
func (o *RemoteServersGeneratorOptions) Include(host *api.Host) bool {
if o == nil {
return false
}

if o.exclude.attributes.Any(host.GetReconcileAttributes()) {
// Reconcile attributes specify to exclude this host
return false
}

for _, val := range o.exclude.hosts {
// Host is in the list to be excluded
if val == host {
return false
}
}

return true
}

// String returns string representation
func (o *RemoteServersGeneratorOptions) String() string {
if o == nil {
return "(nil)"
}

var hostnames []string
for _, host := range o.exclude.hosts {
hostnames = append(hostnames, host.Name)
}
return fmt.Sprintf("exclude hosts: %s, attributes: %s", "["+strings.Join(hostnames, ",")+"]", o.exclude.attributes)
}

// defaultRemoteServersGeneratorOptions
func defaultRemoteServersGeneratorOptions() *RemoteServersGeneratorOptions {
return NewRemoteServersGeneratorOptions()
}
17 changes: 16 additions & 1 deletion pkg/model/chi/creator/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ import (
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
)

func CreateCHI() *api.ClickHouseInstallation {
type CustomResourceType string

const (
CustomResourceCHI CustomResourceType = "chi"
)

func CreateCustomResource(what CustomResourceType) *api.ClickHouseInstallation {
switch what {
case CustomResourceCHI:
return createCHI()
default:
return nil
}
}

func createCHI() *api.ClickHouseInstallation {
return &api.ClickHouseInstallation{
TypeMeta: meta.TypeMeta{
Kind: api.ClickHouseInstallationCRDResourceKind,
Expand Down
22 changes: 11 additions & 11 deletions pkg/model/chi/creator/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func (c *Creator) CreateConfigMap(what ConfigMapType, params ...any) *core.Confi
func (c *Creator) createConfigMapCHICommon(options *config.ClickHouseConfigFilesGeneratorOptions) *core.ConfigMap {
cm := &core.ConfigMap{
ObjectMeta: meta.ObjectMeta{
Name: namer.CreateConfigMapCommonName(c.chi),
Namespace: c.chi.GetNamespace(),
Labels: namer.Macro(c.chi).Map(c.tagger.Label(tags.LabelConfigMapCommon)),
Annotations: namer.Macro(c.chi).Map(c.tagger.Annotate(tags.AnnotateConfigMapCommon)),
OwnerReferences: createOwnerReferences(c.chi),
Name: namer.CreateConfigMapCommonName(c.cr),
Namespace: c.cr.GetNamespace(),
Labels: namer.Macro(c.cr).Map(c.tagger.Label(tags.LabelConfigMapCommon)),
Annotations: namer.Macro(c.cr).Map(c.tagger.Annotate(tags.AnnotateConfigMapCommon)),
OwnerReferences: createOwnerReferences(c.cr),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupCommon, options),
Expand All @@ -75,11 +75,11 @@ func (c *Creator) createConfigMapCHICommon(options *config.ClickHouseConfigFiles
func (c *Creator) createConfigMapCHICommonUsers() *core.ConfigMap {
cm := &core.ConfigMap{
ObjectMeta: meta.ObjectMeta{
Name: namer.CreateConfigMapCommonUsersName(c.chi),
Namespace: c.chi.GetNamespace(),
Labels: namer.Macro(c.chi).Map(c.tagger.Label(tags.LabelConfigMapCommonUsers)),
Annotations: namer.Macro(c.chi).Map(c.tagger.Annotate(tags.AnnotateConfigMapCommonUsers)),
OwnerReferences: createOwnerReferences(c.chi),
Name: namer.CreateConfigMapCommonUsersName(c.cr),
Namespace: c.cr.GetNamespace(),
Labels: namer.Macro(c.cr).Map(c.tagger.Label(tags.LabelConfigMapCommonUsers)),
Annotations: namer.Macro(c.cr).Map(c.tagger.Annotate(tags.AnnotateConfigMapCommonUsers)),
OwnerReferences: createOwnerReferences(c.cr),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupUsers),
Expand All @@ -97,7 +97,7 @@ func (c *Creator) createConfigMapCHIHost(host *api.Host) *core.ConfigMap {
Namespace: host.GetRuntime().GetAddress().GetNamespace(),
Labels: namer.Macro(host).Map(c.tagger.Label(tags.LabelConfigMapHost, host)),
Annotations: namer.Macro(host).Map(c.tagger.Annotate(tags.AnnotateConfigMapHost, host)),
OwnerReferences: createOwnerReferences(c.chi),
OwnerReferences: createOwnerReferences(c.cr),
},
// Data contains several sections which are to be several xml chopConfig files
Data: c.configFilesGenerator.CreateConfigFiles(config.ConfigFilesGroupHost, host),
Expand Down

0 comments on commit 7ce3458

Please sign in to comment.