Skip to content

Commit

Permalink
Add ClickHouse DB Client
Browse files Browse the repository at this point in the history
Signed-off-by: SK Ali Arman <[email protected]>
  • Loading branch information
sheikh-arman committed May 13, 2024
1 parent d3a1eb1 commit aadf90f
Show file tree
Hide file tree
Showing 541 changed files with 310,305 additions and 997 deletions.
9 changes: 9 additions & 0 deletions clickhouse/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package clickhouse_db_client

import (
"database/sql"
)

type Client struct {
*sql.DB
}
125 changes: 125 additions & 0 deletions clickhouse/kubedb_client_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package clickhouse_db_client

import (
"context"
"database/sql"
"fmt"
_ "github.com/ClickHouse/clickhouse-go/v2"
core "k8s.io/api/core/v1"
"k8s.io/klog/v2"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type KubeDBClientBuilder struct {
kc client.Client
db *api.ClickHouse
url string
podName string
port *int
ctx context.Context
}

func NewKubeDBClientBuilder(kc client.Client, db *api.ClickHouse) *KubeDBClientBuilder {
return &KubeDBClientBuilder{
kc: kc,
db: db,
}
}

func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder {
o.url = url
return o
}

func (o *KubeDBClientBuilder) WithPod(podName string) *KubeDBClientBuilder {
o.podName = podName
return o
}

func (o *KubeDBClientBuilder) WithPort(port *int) *KubeDBClientBuilder {
o.port = port
return o
}

func (o *KubeDBClientBuilder) WithContext(ctx context.Context) *KubeDBClientBuilder {
o.ctx = ctx
return o
}

func (o *KubeDBClientBuilder) GetClickHouseClient() (*Client, error) {
if o.ctx == nil {
o.ctx = context.Background()
}

connector, err := o.getConnectionString()
if err != nil {
return nil, err
}

// connect to database
db, err := sql.Open("clickhouse", connector)
if err != nil {
return nil, err
}

// ping to database to check the connection
if err := db.PingContext(o.ctx); err != nil {
closeErr := db.Close()
if closeErr != nil {
klog.Errorf("Failed to close client. error: %v", closeErr)
}
return nil, err
}

return &Client{db}, nil
}

func (o *KubeDBClientBuilder) getURL() string {
return fmt.Sprintf("%s.%s.%s.svc", o.podName, o.db.GoverningServiceName(), o.db.Namespace)
}

func (o *KubeDBClientBuilder) getPort() *int {
chPort := 9000
return &chPort
}

func (o *KubeDBClientBuilder) getMySQLRootCredentials() (string, string, error) {
db := o.db
var secretName string
if db.Spec.AuthSecret != nil {
secretName = db.GetAuthSecretName()
}
var secret core.Secret
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: db.Namespace, Name: secretName}, &secret)
if err != nil {
return "", "", err
}
user, ok := secret.Data[core.BasicAuthUsernameKey]
if !ok {
return "", "", fmt.Errorf("DB root user is not set")
}
pass, ok := secret.Data[core.BasicAuthPasswordKey]
if !ok {
return "", "", fmt.Errorf("DB root password is not set")
}
return string(user), string(pass), nil
}

func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
user, pass, err := o.getMySQLRootCredentials()
if err != nil {
return "", err
}

if o.podName != "" {
o.url = o.getURL()
}

if o.port == nil {
o.port = o.getPort()
}
connector := fmt.Sprintf("clickhouse://%s:%d?username=%s&password=%s", o.url, *o.port, user, pass)

return connector, nil
}
21 changes: 16 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
toolchain go1.22.1

require (
github.com/ClickHouse/clickhouse-go/v2 v2.24.0
github.com/IBM/sarama v1.42.1
github.com/Masterminds/semver/v3 v3.2.1
github.com/elastic/go-elasticsearch/v5 v5.6.1
Expand All @@ -24,14 +25,16 @@ require (
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/klog/v2 v2.120.1
kmodules.xyz/client-go v0.29.13
kmodules.xyz/client-go v0.29.14
kmodules.xyz/custom-resources v0.29.1
kubedb.dev/apimachinery v0.45.1
sigs.k8s.io/controller-runtime v0.17.2
sigs.k8s.io/controller-runtime v0.17.3
xorm.io/xorm v1.3.6
)

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cert-manager/cert-manager v1.13.3 // indirect
Expand All @@ -47,6 +50,8 @@ require (
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -73,7 +78,7 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
Expand All @@ -82,15 +87,18 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.71.2 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand All @@ -100,6 +108,8 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.23.0 // indirect
Expand All @@ -122,7 +132,7 @@ require (
k8s.io/apiserver v0.29.2 // indirect
k8s.io/client-go v0.29.2 // indirect
k8s.io/component-base v0.29.2 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/kube-openapi v0.0.0-20240403164606-bc84c2ddaf99 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
kmodules.xyz/apiversion v0.2.0 // indirect
kmodules.xyz/monitoring-agent-api v0.29.0 // indirect
Expand All @@ -146,3 +156,4 @@ replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.6
replace k8s.io/apiserver => github.com/kmodules/apiserver v0.29.1-0.20240104121741-1fb217d4a573

replace k8s.io/kubernetes => github.com/kmodules/kubernetes v1.30.0-alpha.0.0.20231224075822-3bd9a13c86db

Loading

0 comments on commit aadf90f

Please sign in to comment.