-
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: Sabbir <[email protected]>
- Loading branch information
1 parent
392630b
commit e11cb0a
Showing
77 changed files
with
27,172 additions
and
51 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,93 @@ | ||
package cassandra | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"k8s.io/klog/v2" | ||
health "kmodules.xyz/client-go/tools/healthchecker" | ||
|
||
"github.com/gocql/gocql" | ||
) | ||
|
||
type Client struct { | ||
*gocql.Session | ||
} | ||
|
||
// CreateKeyspace creates a keyspace | ||
func (c *Client) CreateKeyspace() error { | ||
return c.Query(`CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '2'}`).Exec() | ||
} | ||
|
||
// CreateTable creates a table | ||
func (c *Client) CreateTable() error { | ||
return c.Query(`CREATE TABLE IF NOT EXISTS mykeyspace.users ( | ||
id UUID PRIMARY KEY, | ||
name TEXT, | ||
age INT, | ||
email TEXT | ||
)`).Exec() | ||
} | ||
|
||
// InsertUser inserts a user into the table | ||
func (c *Client) InsertUser(id gocql.UUID, name string, age int, email string) error { | ||
return c.Query(`INSERT INTO mykeyspace.users (id, name, age, email) VALUES (?, ?, ?, ?)`, | ||
id, name, age, email).Exec() | ||
} | ||
|
||
func (c *Client) DeleteUser(id gocql.UUID) error { | ||
return c.Query(`DELETE FROM mykeyspace.users WHERE id = ?`, id).Exec() | ||
} | ||
|
||
// QueryUser queries a user by ID | ||
func (c *Client) QueryUser(id gocql.UUID) (string, int, string, error) { | ||
var name string | ||
var age int | ||
var email string | ||
|
||
iter := c.Query(`SELECT name, age, email FROM mykeyspace.users WHERE id = ?`, id).Iter() | ||
if iter.Scan(&name, &age, &email) { | ||
if err := iter.Close(); err != nil { | ||
return "", 0, "", fmt.Errorf("unable to query data: %v", err) | ||
} | ||
return name, age, email, nil | ||
} | ||
return "", 0, "", fmt.Errorf("no data found") | ||
} | ||
|
||
func (c *Client) CheckDbReadWrite() error { | ||
if err := c.CreateKeyspace(); err != nil { | ||
log.Fatal("Unable to create keyspace:", err) | ||
} | ||
if err := c.CreateTable(); err != nil { | ||
log.Fatal("Unable to create table:", err) | ||
} | ||
id := gocql.TimeUUID() | ||
if err := c.InsertUser(id, "John Doe", 30, "[email protected]"); err != nil { | ||
log.Fatal("Unable to insert data:", err) | ||
} | ||
|
||
name, age, email, err := c.QueryUser(id) | ||
if err != nil { | ||
return err | ||
} | ||
klog.Infoln("DB Read Write Successful") | ||
fmt.Printf("Name: %s, Age: %d, Email: %s\n", name, age, email) | ||
err = c.DeleteUser(id) | ||
return err | ||
} | ||
|
||
func (c *Client) PingCassandra() error { | ||
query := c.Query("SELECT now() FROM system.local") | ||
if err := query.Exec(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) CloseCassandraClient(hcf *health.HealthCard) { | ||
if c != nil { | ||
c.Close() | ||
} | ||
hcf.ClientClosed() | ||
} |
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,89 @@ | ||
package cassandra | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
core "k8s.io/api/core/v1" | ||
kerr "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"kubedb.dev/apimachinery/apis/kubedb" | ||
|
||
"github.com/gocql/gocql" | ||
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type KubeDBClientBuilder struct { | ||
kc client.Client | ||
db *api.Cassandra | ||
url string | ||
port *int | ||
ctx context.Context | ||
} | ||
|
||
func NewKubeDBClientBuilder(kc client.Client, db *api.Cassandra) *KubeDBClientBuilder { | ||
return &KubeDBClientBuilder{ | ||
kc: kc, | ||
db: db, | ||
} | ||
} | ||
|
||
func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder { | ||
o.url = url | ||
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) GetCassandraClient(dns string) (*Client, error) { | ||
host := dns | ||
cluster := gocql.NewCluster(host) | ||
cluster.Port = kubedb.CassandraNativeTcpPort | ||
cluster.Keyspace = "system" | ||
if o.db.Spec.Topology == nil { | ||
cluster.Consistency = gocql.One | ||
} else { | ||
cluster.Consistency = gocql.Quorum | ||
} | ||
if !o.db.Spec.DisableSecurity { | ||
if o.db.Spec.AuthSecret == nil { | ||
klog.Error("AuthSecret not set") | ||
return nil, errors.New("auth-secret is not set") | ||
} | ||
|
||
authSecret := &core.Secret{} | ||
err := o.kc.Get(o.ctx, types.NamespacedName{ | ||
Namespace: o.db.Namespace, | ||
Name: o.db.Spec.AuthSecret.Name, | ||
}, authSecret) | ||
if err != nil { | ||
if kerr.IsNotFound(err) { | ||
klog.Error(err, "AuthSecret not found") | ||
return nil, errors.New("auth-secret not found") | ||
} | ||
return nil, err | ||
} | ||
userName := string(authSecret.Data[core.BasicAuthUsernameKey]) | ||
password := string(authSecret.Data[core.BasicAuthPasswordKey]) | ||
cluster.Authenticator = gocql.PasswordAuthenticator{ | ||
Username: userName, | ||
Password: password, | ||
} | ||
} | ||
session, err := cluster.CreateSession() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to connect to Cassandra cluster: %v", err) | ||
} | ||
|
||
return &Client{session}, 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
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
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.