Skip to content

Commit

Permalink
add support for remote-config
Browse files Browse the repository at this point in the history
Signed-off-by: Mehedi Hasan <[email protected]>
  • Loading branch information
heheh13 committed Sep 25, 2023
1 parent bbe4b2e commit ccda007
Show file tree
Hide file tree
Showing 32 changed files with 3,578 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/cert-manager/cert-manager v1.9.1
github.com/fatih/camelcase v1.0.0
github.com/go-sql-driver/mysql v1.6.0
github.com/spf13/cobra v1.7.0
Expand All @@ -21,11 +22,13 @@ require (
k8s.io/component-base v0.25.3
k8s.io/klog/v2 v2.80.1
k8s.io/kubectl v0.25.1
kmodules.xyz/cert-manager-util v0.25.0
kmodules.xyz/client-go v0.25.30
kmodules.xyz/custom-resources v0.25.2
kmodules.xyz/monitoring-agent-api v0.25.1
kubedb.dev/apimachinery v0.35.0
kubedb.dev/db-client-go v0.0.8-0.20230818101900-6ddd035705ef
sigs.k8s.io/yaml v1.3.0
stash.appscode.dev/apimachinery v0.31.0
)

Expand All @@ -42,7 +45,6 @@ require (
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cert-manager/cert-manager v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
Expand Down Expand Up @@ -144,7 +146,6 @@ require (
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/Masterminds/sprig/v3 => github.com/gomodules/sprig/v3 v3.2.3-0.20220405051441-0a8a99bac1b8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,8 @@ k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85 h1:cTdVh7LYu82xeClmfzGtgyspNh6Ux
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk=
kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80=
kmodules.xyz/cert-manager-util v0.25.0 h1:B/1sYzdrXatzDnpB+F9IIi6DdwJs2sPBm46VPAlDtxc=
kmodules.xyz/cert-manager-util v0.25.0/go.mod h1:Zi1ZLUsrzsYs6ibdZddQCn+9VUqCuO9YqAq9QLI9RNM=
kmodules.xyz/client-go v0.25.30 h1:4qOhs1kX0oNl9tcY98PEtiUlD6TZqK9CRfqzfhZApsk=
kmodules.xyz/client-go v0.25.30/go.mod h1:r/Va2Y6t1G8X1sPRjrQC6FWB3oh/i6rjssmlfJnbCmg=
kmodules.xyz/custom-resources v0.25.2 h1:+PJgUZvbbSgyNT7EX9gUZ3PIzY2LAW03TDW8cevvXqo=
Expand Down
24 changes: 24 additions & 0 deletions pkg/cmds/remote_replica.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmds

import (
"kubedb.dev/cli/pkg/remote_replica"

"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

func NewCmdApp(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "remote-config",
Short: "",
Long: "",
Example: "",
Run: func(cmd *cobra.Command, args []string) {
},
DisableAutoGenTag: false,
DisableFlagsInUseLine: false,
}
cmd.AddCommand(remote_replica.MysqlAPP(f))
cmd.AddCommand(remote_replica.PostgreSQlAPP(f))
return cmd
}
6 changes: 6 additions & 0 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ func NewKubeDBCommand(in io.Reader, out, err io.Writer) *cobra.Command {
NewCmdData(f),
},
},
{
Message: "Generate appbinding and secrets for remote Replica",
Commands: []*cobra.Command{
NewCmdApp(f),
},
},
}

filters := []string{"options"}
Expand Down
92 changes: 92 additions & 0 deletions pkg/common/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package common

import (
"bytes"
"context"
"fmt"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
cs "kubedb.dev/apimachinery/client/clientset/versioned"

cm "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
as "kmodules.xyz/custom-resources/client/clientset/versioned"
)

type MySQLOpts struct {
DB *api.MySQL
DBImage string
Config *rest.Config
Client *kubernetes.Clientset
DBClient *cs.Clientset
AppcatClient *as.Clientset
CertManagerClient *cm.Clientset
Username string
Pass string

ErrWriter *bytes.Buffer
}

func (x MySQLOpts) ishello() bool {
return true
}

func NewMySQLOpts(f cmdutil.Factory, dbName, namespace string) (*MySQLOpts, error) {
config, err := f.ToRESTConfig()
if err != nil {
return nil, err
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

dbClient, err := cs.NewForConfig(config)
if err != nil {
return nil, err
}
appCatClient, err := as.NewForConfig(config)
if err != nil {
return nil, err
}
certmanagerClient, err := cm.NewForConfig(config)
if err != nil {
return nil, err
}
db, err := dbClient.KubedbV1alpha2().MySQLs(namespace).Get(context.TODO(), dbName, metav1.GetOptions{})
if err != nil {
return nil, err
}

if db.Status.Phase != api.DatabasePhaseReady {
return nil, fmt.Errorf("MySQL %s/%s is not ready", namespace, dbName)
}

dbVersion, err := dbClient.CatalogV1alpha1().MySQLVersions().Get(context.TODO(), db.Spec.Version, metav1.GetOptions{})
if err != nil {
return nil, err
}

secret, err := client.CoreV1().Secrets(db.Namespace).Get(context.TODO(), db.Spec.AuthSecret.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}

return &MySQLOpts{
DB: db,
DBImage: dbVersion.Spec.DB.Image,
Config: config,
Client: client,
DBClient: dbClient,
AppcatClient: appCatClient,
CertManagerClient: certmanagerClient,
Username: string(secret.Data[corev1.BasicAuthUsernameKey]),
Pass: string(secret.Data[corev1.BasicAuthPasswordKey]),
ErrWriter: &bytes.Buffer{},
}, nil
}
90 changes: 90 additions & 0 deletions pkg/common/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package common

import (
"bytes"
"context"
"fmt"

api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
cs "kubedb.dev/apimachinery/client/clientset/versioned"

cm "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
as "kmodules.xyz/custom-resources/client/clientset/versioned"
)

type PostgresOpts struct {
DB *api.Postgres
DBImage string
Config *rest.Config
Client *kubernetes.Clientset
DBClient *cs.Clientset
AppcatClient *as.Clientset
CertManagerClient *cm.Clientset
PostgresDBName string

Username string
Pass string

ErrWriter *bytes.Buffer
}

func NewPostgresOpts(f cmdutil.Factory, dbName, namespace string) (*PostgresOpts, error) {
config, err := f.ToRESTConfig()
if err != nil {
return nil, err
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

dbClient, err := cs.NewForConfig(config)
if err != nil {
return nil, err
}
appCatClient, err := as.NewForConfig(config)
if err != nil {
return nil, err
}
certmanagerClient, err := cm.NewForConfig(config)
if err != nil {
return nil, err
}
db, err := dbClient.KubedbV1alpha2().Postgreses(namespace).Get(context.TODO(), dbName, metav1.GetOptions{})
if err != nil {
return nil, err
}

if db.Status.Phase != api.DatabasePhaseReady {
return nil, fmt.Errorf("postgres %s/%s is not ready", namespace, dbName)
}

dbVersion, err := dbClient.CatalogV1alpha1().PostgresVersions().Get(context.TODO(), db.Spec.Version, metav1.GetOptions{})
if err != nil {
return nil, err
}

secret, err := client.CoreV1().Secrets(db.Namespace).Get(context.TODO(), db.Spec.AuthSecret.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}

return &PostgresOpts{
DB: db,
DBImage: dbVersion.Spec.DB.Image,
Config: config,
Client: client,
DBClient: dbClient,
AppcatClient: appCatClient,
CertManagerClient: certmanagerClient,
Username: string(secret.Data[corev1.BasicAuthUsernameKey]),
Pass: string(secret.Data[corev1.BasicAuthPasswordKey]),
ErrWriter: &bytes.Buffer{},
}, nil
}
Loading

0 comments on commit ccda007

Please sign in to comment.