Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement debug command for all dbs #727

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
kmodules.xyz/monitoring-agent-api v0.25.5
kubedb.dev/apimachinery v0.36.0
kubedb.dev/db-client-go v0.0.8-0.20230818101900-6ddd035705ef
sigs.k8s.io/controller-runtime v0.13.1
sigs.k8s.io/yaml v1.3.0
stash.appscode.dev/apimachinery v0.32.0
)
Expand Down Expand Up @@ -139,7 +140,6 @@ require (
kmodules.xyz/offshoot-api v0.25.4 // indirect
kmodules.xyz/prober v0.25.0 // indirect
kubeops.dev/sidekick v0.0.2-0.20230113102427-9848f83b2f0f // indirect
sigs.k8s.io/controller-runtime v0.13.1 // indirect
sigs.k8s.io/gateway-api v0.4.3 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/kustomize/api v0.12.1 // indirect
Expand Down
61 changes: 61 additions & 0 deletions pkg/cmds/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
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/debug"

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

var (
debugLong = templates.LongDesc(`
Collect all the logs and yamls of a specific database in just one command
`)
debugExample = templates.Examples(`
kubectl dba debug mongodb -n demo sample-mongodb --operator-namespace kubedb

Valid resource types include:
* elasticsearch
* mongodb
* mariadb
* mysql
* postgres
* redis
`)
)

func NewCmdDebug(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: i18n.T("Debug any Database issue"),
Long: debugLong,
Example: debugExample,
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}

cmd.AddCommand(debug.ElasticsearchDebugCMD(f))
cmd.AddCommand(debug.MariaDBDebugCMD(f))
cmd.AddCommand(debug.MongoDBDebugCMD(f))
cmd.AddCommand(debug.MySQLDebugCMD(f))
cmd.AddCommand(debug.PostgresDebugCMD(f))
cmd.AddCommand(debug.RedisDebugCMD(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: "Debug any Database issue",
Commands: []*cobra.Command{
NewCmdDebug(f),
},
},
{
Message: "Generate appbinding and secrets for remote Replica",
Commands: []*cobra.Command{
Expand Down
26 changes: 26 additions & 0 deletions pkg/debug/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
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 debug

const (
logsDir = "logs"
yamlsDir = "yamls"
dirPerm = 0o755
filePerm = 0o644

operatorContainerName = "operator"
)
256 changes: 256 additions & 0 deletions pkg/debug/elasticsearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/*
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 debug

import (
"bytes"
"context"
"log"
"os"
"path"

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

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

type elasticsearchOpts struct {
db *api.Elasticsearch
dbClient *cs.Clientset
podClient *kubernetes.Clientset

operatorNamespace string
dir string
errWriter *bytes.Buffer
}

func ElasticsearchDebugCMD(f cmdutil.Factory) *cobra.Command {
var (
dbName string
operatorNamespace string
)

esDebugCmd := &cobra.Command{
Use: "elasticsearch",
Aliases: []string{
"es",
},
Short: "Debug helper for elasticsearch database",
Example: `kubectl dba debug elasticsearch -n demo sample-elasticsearch --operator-namespace kubedb`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Enter elasticsearch object's name as an argument")
}
dbName = args[0]

namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
klog.Error(err, "failed to get current namespace")
}

opts, err := newElasticsearchOpts(f, dbName, namespace, operatorNamespace)
if err != nil {
log.Fatalln(err)
}

err = opts.collectOperatorLogs()
if err != nil {
log.Fatal(err)
}

err = opts.collectForAllDBPods()
if err != nil {
log.Fatal(err)
}

err = opts.collectOtherYamls()
if err != nil {
log.Fatal(err)
}
},
}
esDebugCmd.Flags().StringVarP(&operatorNamespace, "operator-namespace", "o", "kubedb", "the namespace where the kubedb operator is installed")

return esDebugCmd
}

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

dbClient, err := cs.NewForConfig(config)
if err != nil {
return nil, err
}

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

db, err := dbClient.KubedbV1alpha2().Elasticsearches(namespace).Get(context.TODO(), dbName, metav1.GetOptions{})
if err != nil {
return nil, err
}

pwd, _ := os.Getwd()
dir := path.Join(pwd, db.Name)
err = os.MkdirAll(path.Join(dir, logsDir), dirPerm)
if err != nil {
return nil, err
}
err = os.MkdirAll(path.Join(dir, yamlsDir), dirPerm)
if err != nil {
return nil, err
}

opts := &elasticsearchOpts{
db: db,
dbClient: dbClient,
podClient: podClient,
operatorNamespace: operatorNS,
dir: dir,
errWriter: &bytes.Buffer{},
}
return opts, writeYaml(db, path.Join(opts.dir, yamlsDir))
}

func (opts *elasticsearchOpts) collectOperatorLogs() error {
pods, err := opts.podClient.CoreV1().Pods(opts.operatorNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, pod := range pods.Items {
err = opts.writeLogs(pod.Name, pod.Namespace, operatorContainerName)
if err != nil {
return err
}
}
return nil
}

func (opts *elasticsearchOpts) collectForAllDBPods() error {
dbLabels := labels.SelectorFromSet(opts.db.GetLabels()).String()
pods, err := opts.podClient.CoreV1().Pods(opts.db.Namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: dbLabels,
})
if err != nil {
return err
}

podYamlDir := path.Join(opts.dir, yamlsDir)
for _, pod := range pods.Items {
err = opts.writeLogsForSinglePod(pod)
if err != nil {
return err
}

err = writeYaml(&pod, podYamlDir)
if err != nil {
return err
}

}
return nil
}

func (opts *elasticsearchOpts) writeLogsForSinglePod(pod corev1.Pod) error {
for _, c := range pod.Spec.Containers {
err := opts.writeLogs(pod.Name, pod.Namespace, c.Name)
if err != nil {
return err
}
}
return nil
}

func (opts *elasticsearchOpts) writeLogs(podName, ns, container string) error {
req := opts.podClient.CoreV1().Pods(ns).GetLogs(podName, &corev1.PodLogOptions{
Container: container,
})

podLogs, err := req.Stream(context.TODO())
if err != nil {
return err
}
defer podLogs.Close()

logFile, err := os.Create(path.Join(opts.dir, logsDir, podName+"_"+container+".log"))
if err != nil {
return err
}
defer logFile.Close()

buf := make([]byte, 1024)
for {
bytesRead, err := podLogs.Read(buf)
if err != nil {
break
}
_, _ = logFile.Write(buf[:bytesRead])
}
return nil
}

func (opts *elasticsearchOpts) collectOtherYamls() error {
opsReqs, err := opts.dbClient.OpsV1alpha1().ElasticsearchOpsRequests(opts.db.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
opsYamlDir := path.Join(opts.dir, yamlsDir, "ops")
err = os.MkdirAll(opsYamlDir, dirPerm)
if err != nil {
return err
}
for _, ops := range opsReqs.Items {
if ops.Spec.DatabaseRef.Name == opts.db.Name {
err = writeYaml(&ops, opsYamlDir)
if err != nil {
return err
}
}
}

scalers, err := opts.dbClient.AutoscalingV1alpha1().ElasticsearchAutoscalers(opts.db.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
scalerYamlDir := path.Join(opts.dir, yamlsDir, "scaler")
err = os.MkdirAll(scalerYamlDir, dirPerm)
if err != nil {
return err
}
for _, sc := range scalers.Items {
if sc.Spec.DatabaseRef.Name == opts.db.Name {
err = writeYaml(&sc, scalerYamlDir)
if err != nil {
return err
}
}
}
return nil
}
Loading
Loading