8000 feat(library): split pkg/chop config machinery from the kube client wiring [stacked on #2160] by sks · Pull Request #2161 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Open
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
2 changes: 1 addition & 1 deletion THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The main project license is Apache-2.0; see [`LICENSE`](LICENSE) and [`NOTICE`](
| `github.com/google/go-cmp/cmp` | BSD-3-Clause | `vendor/github.com/google/go-cmp/LICENSE` |
| `github.com/google/gofuzz` | Apache-2.0 | `vendor/github.com/google/gofuzz/LICENSE` |
| `github.com/google/uuid` | BSD-3-Clause | `vendor/github.com/google/uuid/LICENSE` |
| `github.com/imdario/mergo` | BSD-3-Clause | `vendor/github.com/imdario/mergo/LICENSE` |
| `github.com/imdario/mergo` (sunsingerus fork, vendored at `pkg/util/mergo`) | BSD-3-Clause | `pkg/util/mergo/LICENSE` |
| `github.com/josharian/intern` | MIT | `vendor/github.com/josharian/intern/license.md` |
| `github.com/json-iterator/go` | MIT | `vendor/github.com/json-iterator/go/LICENSE` |
| `github.com/juliangruber/go-intersect` | MIT | [`LICENSE.md`](https://github.com/juliangruber/go-intersect/blob/v1.0.0/LICENSE.md) |
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.26.8
replace (
github.com/emicklei/go-restful/v3 => github.com/emicklei/go-restful/v3 v3.10.0
github.com/golang/glog => github.com/sunsingerus/glog v1.0.1-0.20220103184348-48242e35873d
github.com/imdario/mergo => github.com/sunsingerus/mergo v0.0.0-20230507185449-fc6fffa94450
)

require github.com/altinity/queue v0.0.0-20210114142043-ddb7da66064f
Expand All @@ -25,7 +24,6 @@ require (
github.com/go-zookeeper/zk v1.0.3
github.com/golang/glog v1.1.0
github.com/google/uuid v1.6.0
github.com/imdario/mergo v0.3.15
github.com/juliangruber/go-intersect v1.0.0
github.com/kubernetes-sigs/yaml v1.1.0
github.com/mailru/go-clickhouse/v2 v2.1.0
Expand Down Expand Up @@ -68,6 +66,7 @@ require (
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gookit/color v1.4.2 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huandu/xstrings v1.0.0/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbcucJdbSo=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jhump/protoreflect v1.6.1/go.mod h1:RZQ/lnuN+zqeRVpQigTwO6o0AJUkxbnSnpuG7toUTG4=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down Expand Up @@ -431,8 +434,6 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/sunsingerus/glog v1.0.1-0.20220103184348-48242e35873d h1:0e/CoQX3ZCy3rXDTflbXcj1ReYvRNpKONzr+8eDF3Hc=
github.com/sunsingerus/glog v1.0.1-0.20220103184348-48242e35873d/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/sunsingerus/mergo v0.0.0-20230507185449-fc6fffa94450 h1:PvdDV9N8PrsoL3ToXH6bId0/OPyt/ExMziouOeyEhco=
github.com/sunsingerus/mergo v0.0.0-20230507185449-fc6fffa94450/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/clickhouse-keeper.altinity.com/v1/type_chk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"github.com/altinity/clickhouse-operator/pkg/apis/swversion"

"github.com/imdario/mergo"
"github.com/altinity/clickhouse-operator/pkg/util/mergo"
"gopkg.in/yaml.v3"

apiChi "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/clickhouse.altinity.com/v1/type_chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/altinity/clickhouse-operator/pkg/apis/swversion"

"github.com/imdario/mergo"
"github.com/altinity/clickhouse-operator/pkg/util/mergo"
"gopkg.in/yaml.v3"

"github.com/altinity/clickhouse-operator/pkg/apis/common/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"sync"
"time"

"github.com/altinity/clickhouse-operator/pkg/util/mergo"
log "github.com/golang/glog"
"github.com/imdario/mergo"
"gopkg.in/yaml.v3"

meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
1798
2 changes: 1 addition & 1 deletion pkg/apis/clickhouse.altinity.com/v1/type_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package v1

import (
"github.com/imdario/mergo"
"github.com/altinity/clickhouse-operator/pkg/util/mergo"

core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
62 changes: 55 additions & 7 deletions pkg/chop/chop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ import (
"flag"
"fmt"

"context"

kube "k8s.io/client-go/kubernetes"

log "github.com/altinity/clickhouse-operator/pkg/announcer"
api "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
chopclientset "github.com/altinity/clickhouse-operator/pkg/client/clientset/versioned"
chopconfig "github.com/altinity/clickhouse-operator/pkg/chop/config"
"github.com/altinity/clickhouse-operator/pkg/client/clientset/versioned"
"github.com/altinity/clickhouse-operator/pkg/controller"
)

// CHOp defines ClickHouse Operator
Expand All @@ -34,7 +38,7 @@ type CHOp struct {
// Date specified date when operator was built
Date string
// ConfigManager specifies configuration manager in charge of operator's configuration
ConfigManager *ConfigManager
ConfigManager *chopconfig.ConfigManager
}

// newCHOp creates new CHOp
Expand All @@ -43,14 +47,58 @@ func newCHOp(
commit string,
date string,
kubeClient *kube.Clientset,
chopClient *chopclientset.Clientset,
chopClient *versioned.Clientset,
initConfigFilePath string,
) *CHOp {
return &CHOp{
Version: version,
Commit: commit,
Date: date,
ConfigManager: newConfigManager(kubeClient, chopClient, initConfigFilePath),
Version: version,
Commit: commit,
Date: date,
ConfigManager: chopconfig.NewManager(
crConfigListerFrom(chopClient),
secretGetterFrom(kubeClient),
lastKubeConfigInsecure,
initConfigFilePath,
),
}
}

// crConfigListerFrom adapts the operator's custom-resource clientset to
// the config package's CRConfigLister seam — the only place the CR
// listing touches the k8s wire.
func crConfigListerFrom(chopClient *versioned.Clientset) chopconfig.CRConfigLister {
if chopClient == nil {
return nil
}
return func(namespace string) ([]*api.OperatorConfig, map[string]string, error) {
list, err := chopClient.ClickhouseV1().ClickHouseOperatorConfigurations(namespace).List(context.TODO(), controller.NewListOptions())
if err != nil {
return nil, nil, err
}
configs := make([]*api.OperatorConfig, 0, len(list.Items))
versions := make(map[string]string, len(list.Items))
for i := range list.Items {
cr := &list.Items[i]
cr.Spec.Runtime.ConfigCRName = cr.Name
configs = append(configs, &cr.Spec)
versions[cr.Namespace+"/"+cr.Name] = cr.ResourceVersion
}
return configs, versions, nil
}
}

// secretGetterFrom adapts the operator's kube clientset to the config
// package's secretDataGetter seam.
func secretGetterFrom(kubeClient *kube.Clientset) func(namespace, name string) (map[string][]byte, error) {
if kubeClient == nil {
return nil
}
return func(namespace, name string) (map[string][]byte, error) {
secret, err := kubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), name, controller.NewGetOptions())
if err != nil {
return nil, err
}
return secret.Data, nil
}
}

Expand Down
80 changes: 80 additions & 0 deletions pkg/chop/config/ipc_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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 chop

import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"

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

// ipcTokenBytes is the random-byte length of the operator↔exporter shared
// bearer token. 32 bytes = 256 bits → 64 hex characters in the file.
// Comfortably above the brute-force-resistance threshold; not so large that
// it stresses /etc tmpfs.
const ipcTokenBytes = 32

// ProvisionIPCToken generates a fresh random token and writes it to the
// configured TokenPath when chopconf has IPC Mode=Secure. Plain mode is a
// no-op so existing deployments are unaffected.
//
// provisionIPCToken is the test-accessible inner implementation. Splitting
// the IPC-config read from the file-write logic lets unit tests drive the
// behavior with a synthetic *api.OperatorConfigSecurityIPC rather
// than spinning up a full chop subsystem.
//
// Idempotent across restarts: if the path already holds a non-empty token,
// the existing value is reused (avoids invalidating the token if the operator
// container restarts while the exporter container keeps holding the old
// value).
// ProvisionIPCToken is the exported inner implementation (pkg/chop's
// wrapper reads the global config and hands the IPC section here).
func ProvisionIPCToken(ipc *api.OperatorConfigSecurityIPC) error {
if (ipc == nil) || (ipc.GetMode() != api.IPCModeSecure) {
return nil
}

path := ipc.GetTokenPath()
if path == "" {
path = api.IPCDefaultTokenPath
}

// Reuse non-empty existing token across operator-container restarts.
if data, err := os.ReadFile(path); (err == nil) && (len(data) > 0) {
log.V(1).Info("IPC: reusing existing token at %s", path)
return nil
}

if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("IPC: cannot create token dir %s: %w", filepath.Dir(path), err)
}

raw := make([]byte, ipcTokenBytes)
if _, err := rand.Read(raw); err != nil {
return fmt.Errorf("IPC: crypto/rand failure generating token: %w", err)
}
encoded := hex.EncodeToString(raw)

if err := os.WriteFile(path, []byte(encoded), 0o400); err != nil {
return fmt.Errorf("IPC: cannot write token to %s: %w", path, err)
}
log.Info("IPC: Secure mode — provisioned token at %s (%d bytes)", path, len(encoded))
return nil
}
12 changes: 6 additions & 6 deletions pkg/chop/ipc_token_test.go → pkg/chop/config/ipc_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ func TestProvisionIPCTokenPlainModeIsNoop(t *testing.T) {
dir := t.TempDir()
tokenPath := filepath.Join(dir, "token")

require.NoError(t, provisionIPCToken(ipcCfg(api.IPCModePlain, tokenPath)))
require.NoError(t, ProvisionIPCToken(ipcCfg(api.IPCModePlain, tokenPath)))

_, err := os.Stat(tokenPath)
require.True(t, os.IsNotExist(err), "Plain mode must not write any token file")
}

func TestProvisionIPCTokenNilIPCIsNoop(t *testing.T) {
require.NoError(t, provisionIPCToken(nil))
require.NoError(t, ProvisionIPCToken(nil))
}

func TestProvisionIPCTokenSecureModeWritesToken(t *testing.T) {
dir := t.TempDir()
// Path under a subdirectory that doesn't exist yet — exercises MkdirAll.
tokenPath := filepath.Join(dir, "subdir", "token")

require.NoError(t, provisionIPCToken(ipcCfg(api.IPCModeSecure, tokenPath)))
require.NoError(t, ProvisionIPCToken(ipcCfg(api.IPCModeSecure, tokenPath)))

data, err := os.ReadFile(tokenPath)
require.NoError(t, err)
Expand All @@ -71,7 +71,7 @@ func TestProvisionIPCTokenReuseExisting(t *testing.T) {
tokenPath := filepath.Join(dir, "token")
require.NoError(t, os.WriteFile(tokenPath, []byte("preexisting-token-value"), 0o400))

require.NoError(t, provisionIPCToken(ipcCfg(api.IPCModeSecure, tokenPath)))
require.NoError(t, ProvisionIPCToken(ipcCfg(api.IPCModeSecure, tokenPath)))

data, err := os.ReadFile(tokenPath)
require.NoError(t, err)
Expand All @@ -86,12 +86,12 @@ func TestProvisionIPCTokenTwoCallsSameToken(t *testing.T) {
tokenPath := filepath.Join(dir, "token")
cfg := ipcCfg(api.IPCModeSecure, tokenPath)

require.NoError(t, provisionIPCToken(cfg))
require.NoError(t, ProvisionIPCToken(cfg))
first, err := os.ReadFile(tokenPath)
require.NoError(t, err)
require.NotEmpty(t, first)

require.NoError(t, provisionIPCToken(cfg))
require.NoError(t, ProvisionIPCToken(cfg))
sec 5326 ond, err := os.ReadFile(tokenPath)
require.NoError(t, err)
require.Equal(t, first, second, "second call must reuse the existing token")
Expand Down
Loading
0