Skip to content

Commit

Permalink
Add Singlestore db-client (#73)
Browse files Browse the repository at this point in the history
* Add Singlestore db-client

Signed-off-by: ashraful <[email protected]>
Co-authored-by: raihankhan <[email protected]>
  • Loading branch information
AshrafulHaqueToni and raihankhan authored Jan 16, 2024
1 parent 70c5b51 commit 3b88c8f
Show file tree
Hide file tree
Showing 136 changed files with 17,626 additions and 3,112 deletions.
26 changes: 11 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/elastic/go-elasticsearch/v7 v7.15.1
github.com/elastic/go-elasticsearch/v8 v8.4.0
github.com/go-resty/resty/v2 v2.11.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 @@ -21,9 +21,9 @@ require (
k8s.io/apimachinery v0.29.0
k8s.io/klog/v2 v2.110.1
kmodules.xyz/client-go v0.29.6
kubedb.dev/apimachinery v0.41.0-beta.0.0.20240112111342-1ee5ee41dd90
kubedb.dev/apimachinery v0.41.0-beta.0.0.20240115173822-556a36dfe734
sigs.k8s.io/controller-runtime v0.16.3
xorm.io/xorm v1.3.2
xorm.io/xorm v1.3.6
)

require (
Expand All @@ -41,7 +41,7 @@ require (
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand All @@ -67,19 +67,18 @@ 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/v2 v2.0.0 // 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
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/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_golang v1.17.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/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/sergi/go-diff v1.2.0 // indirect
Expand Down Expand Up @@ -119,10 +118,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 All @@ -133,10 +129,10 @@ require (

replace github.com/Masterminds/sprig/v3 => github.com/gomodules/sprig/v3 v3.2.3-0.20220405051441-0a8a99bac1b8

replace sigs.k8s.io/controller-runtime => github.com/kmodules/controller-runtime v0.16.1-0.20240104120228-ef547cbd1b75
replace sigs.k8s.io/controller-runtime => github.com/kmodules/controller-runtime v0.16.1-0.20231224083233-bead154270db

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/apiserver => github.com/kmodules/apiserver v0.29.1-0.20231224075222-50d5bb4aeb26

replace k8s.io/kubernetes => github.com/kmodules/kubernetes v1.30.0-alpha.0.0.20231224075822-3bd9a13c86db
494 changes: 72 additions & 422 deletions go.sum

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions vendor/github.com/go-logr/logr/context.go → singlestore/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 The logr Authors.
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,20 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package logr
package singlestore

// contextKey is how we find Loggers in a context.Context. With Go < 1.21,
// the value is always a Logger value. With Go >= 1.21, the value can be a
// Logger value or a slog.Logger pointer.
type contextKey struct{}
import (
"database/sql"

// notFoundError exists to carry an IsNotFound method.
type notFoundError struct{}
"xorm.io/xorm"
)

func (notFoundError) Error() string {
return "no logr.Logger was present"
type Client struct {
*sql.DB
}

func (notFoundError) IsNotFound() bool {
return true
type XormClient struct {
*xorm.Engine
}
187 changes: 187 additions & 0 deletions singlestore/kubedb_client_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
Copyright AppsCode Inc. and Contributors
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 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/apimachinery/apis/kubedb/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()
if err != nil {
return nil, err
}
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
}
Loading

0 comments on commit 3b88c8f

Please sign in to comment.