-
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: SK Ali Arman <[email protected]>
- Loading branch information
1 parent
d3a1eb1
commit aadf90f
Showing
541 changed files
with
310,305 additions
and
997 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
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 | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.