-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
986266b
commit a72b9db
Showing
104 changed files
with
5,452 additions
and
2,472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.