-
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.
* Add Singlestore db-client Signed-off-by: ashraful <[email protected]> Co-authored-by: raihankhan <[email protected]>
- Loading branch information
1 parent
70c5b51
commit 3b88c8f
Showing
136 changed files
with
17,626 additions
and
3,112 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
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,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 | ||
} |
Oops, something went wrong.