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 Oct 2, 2023
1 parent f9be578 commit b56f677
Show file tree
Hide file tree
Showing 32 changed files with 3,678 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.1
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 @@ -40,7 +43,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 @@ -143,7 +145,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
45 changes: 45 additions & 0 deletions pkg/cmds/remote_replica.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
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 cmds

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

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

var (
desLong = "generate appbinding , secrets for remote replica"
example = "kubectl dba remote-config mysql -n <ns> -u <user_name> -p$<password> -d<dns_name> <db_name>"
)

func NewCmdGenApb(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "remote-config",
Short: desLong,
Long: desLong,
Example: 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{
NewCmdGenApb(f),
},
},
}

filters := []string{"options"}
Expand Down
104 changes: 104 additions & 0 deletions pkg/common/mysql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
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 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 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
}
106 changes: 106 additions & 0 deletions pkg/common/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
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 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 b56f677

Please sign in to comment.