Skip to content

Commit

Permalink
Add Singlestore db-client
Browse files Browse the repository at this point in the history
Signed-off-by: ashraful <[email protected]>

# Conflicts:
#	go.mod
#	go.sum
#	vendor/github.com/go-logr/logr/logr.go
#	vendor/github.com/klauspost/compress/.goreleaser.yml
#	vendor/github.com/klauspost/compress/README.md
#	vendor/github.com/klauspost/compress/zstd/decoder_options.go
#	vendor/github.com/klauspost/compress/zstd/dict.go
#	vendor/github.com/klauspost/compress/zstd/zstd.go
#	vendor/golang.org/x/net/idna/tables13.0.0.go
#	vendor/golang.org/x/net/idna/tables15.0.0.go
#	vendor/golang.org/x/net/idna/trie12.0.0.go
#	vendor/golang.org/x/net/idna/trie13.0.0.go
#	vendor/golang.org/x/sys/unix/mkerrors.sh
#	vendor/golang.org/x/sys/unix/mmap_nomremap.go
#	vendor/golang.org/x/sys/unix/mremap.go
#	vendor/golang.org/x/sys/unix/syscall_linux.go
#	vendor/golang.org/x/sys/unix/syscall_openbsd.go
#	vendor/golang.org/x/sys/unix/zerrors_linux_loong64.go
#	vendor/golang.org/x/sys/unix/zsyscall_linux.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_386.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_amd64.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_arm.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_arm64.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_mips64.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_ppc64.go
#	vendor/golang.org/x/sys/unix/zsyscall_openbsd_riscv64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_386.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_amd64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_arm.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_arm64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_loong64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_mips.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_mips64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_mips64le.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_mipsle.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_ppc.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_ppc64le.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_riscv64.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_s390x.go
#	vendor/golang.org/x/sys/unix/zsysnum_linux_sparc64.go
#	vendor/golang.org/x/sys/unix/ztypes_linux.go
#	vendor/golang.org/x/text/unicode/bidi/tables13.0.0.go
#	vendor/golang.org/x/text/unicode/bidi/tables15.0.0.go
#	vendor/golang.org/x/text/unicode/norm/tables13.0.0.go
#	vendor/golang.org/x/text/unicode/norm/tables15.0.0.go
#	vendor/kmodules.xyz/client-go/core/v1/sa-token.go
#	vendor/kmodules.xyz/monitoring-agent-api/api/v1/helpers.go
#	vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/constants.go
#	vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/kafka_types.go
#	vendor/kubedb.dev/apimachinery/apis/kubedb/v1alpha2/openapi_generated.go
#	vendor/kubedb.dev/apimachinery/crds/ops.kubedb.com_kafkaopsrequests.yaml
#	vendor/modules.txt

Signed-off-by: ashraful <[email protected]>
  • Loading branch information
AshrafulHaqueToni committed Jan 2, 2024
1 parent 986266b commit a72b9db
Show file tree
Hide file tree
Showing 104 changed files with 5,452 additions and 2,472 deletions.
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/elastic/go-elasticsearch/v6 v6.8.10
github.com/elastic/go-elasticsearch/v7 v7.15.1
github.com/elastic/go-elasticsearch/v8 v8.4.0
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.4
github.com/go-sql-driver/mysql v1.7.0
github.com/lib/pq v1.10.7
github.com/opensearch-project/opensearch-go v1.1.0
github.com/opensearch-project/opensearch-go/v2 v2.2.0
github.com/pkg/errors v0.9.1
Expand All @@ -19,8 +19,9 @@ require (
k8s.io/klog/v2 v2.110.1
kmodules.xyz/client-go v0.29.4
kubedb.dev/apimachinery v0.40.1-0.20240101000103-032b27211164
kubedb.dev/singlestore v0.0.0-20231229114848-09611a2eb558
sigs.k8s.io/controller-runtime v0.16.3
xorm.io/xorm v1.3.2
xorm.io/xorm v1.3.6
)

require (
Expand Down Expand Up @@ -53,7 +54,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down Expand Up @@ -104,10 +104,7 @@ require (
kmodules.xyz/custom-resources v0.29.0 // indirect
kmodules.xyz/monitoring-agent-api v0.29.0 // indirect
kmodules.xyz/offshoot-api v0.29.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.18.2 // indirect
modernc.org/token v1.1.0 // indirect
sigs.k8s.io/gateway-api v0.8.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
476 changes: 66 additions & 410 deletions go.sum

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions singlestore/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package singlestore

import (
"database/sql"

"xorm.io/xorm"
)

type Client struct {
*sql.DB
}

type XormClient struct {
*xorm.Engine
}
172 changes: 172 additions & 0 deletions singlestore/kubedb_client_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package singlestore

import (
"context"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
core "k8s.io/api/core/v1"
"k8s.io/klog/v2"
api "kubedb.dev/singlestore/api/v1alpha2"
"sigs.k8s.io/controller-runtime/pkg/client"
"xorm.io/xorm"
)

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

func NewKubeDBClientBuilder(kc client.Client, db *api.Singlestore) *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) WithContext(ctx context.Context) *KubeDBClientBuilder {
o.ctx = ctx
return o
}

func (o *KubeDBClientBuilder) GetSinglestoreClient() (*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("mysql", 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) GetSinglestoreXormClient() (*XormClient, error) {
if o.ctx == nil {
o.ctx = context.Background()
}
connector, err := o.getConnectionString()
klog.Infoln("-----------connector: ", connector)
if err != nil {
return nil, err
}
//klog.Infoln("------------ hi--------------------")
engine, err := xorm.NewEngine("mysql", connector)
if err != nil {
return nil, err
}
_, err = engine.Query("SELECT 1")
if err != nil {
return nil, err
}

engine.SetDefaultContext(o.ctx)
return &XormClient{
engine,
}, nil
}

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

func (o *KubeDBClientBuilder) getSinglestoreRootCredentials() (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.getSinglestoreRootCredentials()
if err != nil {
return "", err
}

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

tlsConfig := ""
/*if o.db.Spec.RequireSSL && o.db.Spec.TLS != nil {
// get client-secret
var clientSecret core.Secret
err := o.kc.Get(o.ctx, client.ObjectKey{Namespace: o.db.GetNamespace(), Name: o.db.GetCertSecretName(api.MySQLClientCert)}, &clientSecret)
if err != nil {
return "", err
}
cacrt := clientSecret.Data["ca.crt"]
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cacrt)
crt := clientSecret.Data["tls.crt"]
key := clientSecret.Data["tls.key"]
cert, err := tls.X509KeyPair(crt, key)
if err != nil {
return "", err
}
var clientCert []tls.Certificate
clientCert = append(clientCert, cert)
// tls custom setup
if o.db.Spec.RequireSSL {
err = sql_driver.RegisterTLSConfig(api.MySQLTLSConfigCustom, &tls.Config{
RootCAs: certPool,
Certificates: clientCert,
})
if err != nil {
return "", err
}
tlsConfig = fmt.Sprintf("tls=%s", api.MySQLTLSConfigCustom)
} else {
tlsConfig = fmt.Sprintf("tls=%s", api.MySQLTLSConfigSkipVerify)
}
}*/

connector := fmt.Sprintf("%v:%v@tcp(%s:%d)/%s?%s", user, pass, o.url, 3306, "memsql", tlsConfig)
return connector, nil
}
7 changes: 7 additions & 0 deletions vendor/github.com/go-sql-driver/mysql/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/go-sql-driver/mysql/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions vendor/github.com/go-sql-driver/mysql/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/go-sql-driver/mysql/atomic_bool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a72b9db

Please sign in to comment.