Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: sayedppqq <[email protected]>
  • Loading branch information
sayedppqq committed Jan 30, 2024
1 parent a67dadc commit 7f9fd7f
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 132 deletions.
59 changes: 0 additions & 59 deletions pkg/cmds/alert.go

This file was deleted.

65 changes: 0 additions & 65 deletions pkg/cmds/grafana_dashboard.go

This file was deleted.

172 changes: 172 additions & 0 deletions pkg/cmds/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
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/alerts"
"kubedb.dev/cli/pkg/connection"
"kubedb.dev/cli/pkg/dashboard"

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

var monitorLong = templates.LongDesc(`
All monitoring related commands from AppsCode.
`)

var monitorExample = templates.Examples(`
# Check triggered alerts for a specific database
kubectl dba monitor get-alerts mongodb -n demo sample-mongodb --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring
# Check availability of mongodb-summary-dashboard grafana dashboard of mongodb
kubectl dba monitor dashboard mongodb mongodb-summary-dashboard
# Check connection status of target with prometheus server
kubectl dba monitor check-connection mongodb
Valid sub command include:
* get-alerts
* dashboard
* check-connection
`)

func NewCmdMonitor(f cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "monitor",
Short: i18n.T("get-alerts,grafana dashboard check and target connection check for monitoring"),
Long: monitorLong,
Example: monitorExample,
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}

cmd.AddCommand(DashboardCMD(f))
cmd.AddCommand(AlertCMD(f))
cmd.AddCommand(ConnectionCMD(f))

return cmd
}

// alert
var alertLong = templates.LongDesc(`
Get the prometheus alerts for a specific database in just one command
`)
var alertExample = templates.Examples(`
kubectl dba monitor get-alerts mongodb -n demo sample-mongodb --prom-svc-name=prometheus-kube-prometheus-prometheus --prom-svc-namespace=monitoring
Valid resource types include:
* elasticsearch
* mongodb
* mariadb
* mysql
* postgres
* redis
`)

func AlertCMD(f cmdutil.Factory) *cobra.Command {
var prom alerts.PromSvc
cmd := &cobra.Command{
Use: "get-alerts",
Short: i18n.T("Alerts associated with a database"),
Long: alertLong,
Example: alertExample,
Run: func(cmd *cobra.Command, args []string) {
alerts.Run(f, args, prom)
},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&prom.Name, "prom-svc-name", "", "", "name of the prometheus service")
cmd.Flags().StringVarP(&prom.Namespace, "prom-svc-namespace", "", "", "namespace of the prometheus service")
cmd.Flags().IntVarP(&prom.Port, "prom-svc-port", "", 9090, "port of the prometheus service")
return cmd
}

// dashboard
var dashboardLong = templates.LongDesc(`
Check availability of metrics in prometheus server used in a grafana dashboard.
`)

var dashboardExample = templates.Examples(`
# Check availability of mongodb-summary-dashboard grafana dashboard of mongodb
kubectl dba monitor dashboard mongodb mongodb-summary-dashboard
Valid dashboards include:
* elasticsearch
* mongodb
* mariadb
* mysql
* postgres
* redis
`)

func DashboardCMD(f cmdutil.Factory) *cobra.Command {
var branch string
var prom dashboard.PromSvc
cmd := &cobra.Command{
Use: "dashboard",
Short: i18n.T("Check availability of a grafana dashboard"),
Long: dashboardLong,

Run: func(cmd *cobra.Command, args []string) {
dashboard.Run(f, args, branch, prom)
},
Example: dashboardExample,
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&branch, "branch", "b", "master", "branch name of the github repo")
cmd.Flags().StringVarP(&prom.Name, "prom-svc-name", "", "", "name of the prometheus service")
cmd.Flags().StringVarP(&prom.Namespace, "prom-svc-namespace", "", "", "namespace of the prometheus service")
cmd.Flags().IntVarP(&prom.Port, "prom-svc-port", "", 9090, "port of the prometheus service")
return cmd
}

// check-connection
// TODO
var connectionLong = templates.LongDesc(`
`)

// TODO
var connectionExample = templates.Examples(`
# kubectl dba monitor check-connection mongodb -n demo sample_mg -> all connection check and report
`)

func ConnectionCMD(f cmdutil.Factory) *cobra.Command {
var prom connection.PromSvc
cmd := &cobra.Command{
Use: "check-connection",
Short: i18n.T("Check connection status of prometheus target with server"),
Long: connectionLong,
Example: connectionExample,
Run: func(cmd *cobra.Command, args []string) {
connection.Run(f, args, prom)
},
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
}
cmd.Flags().StringVarP(&prom.Name, "prom-svc-name", "", "", "name of the prometheus service")
cmd.Flags().StringVarP(&prom.Namespace, "prom-svc-namespace", "", "", "namespace of the prometheus service")
cmd.Flags().IntVarP(&prom.Port, "prom-svc-port", "", 9090, "port of the prometheus service")
return cmd
}
10 changes: 2 additions & 8 deletions pkg/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ func NewKubeDBCommand(in io.Reader, out, err io.Writer) *cobra.Command {
NewCmdExec(f),
},
},
{
Message: "Get Alerts for specific database",
Commands: []*cobra.Command{
NewCmdAlert(f),
},
},
{
Message: "Insert and Verify data in Database",
Commands: []*cobra.Command{
Expand All @@ -114,9 +108,9 @@ func NewKubeDBCommand(in io.Reader, out, err io.Writer) *cobra.Command {
},
},
{
Message: "Check availability of metrics",
Message: "Metric related CMDs",
Commands: []*cobra.Command{
NewCmdDashboard(f),
NewCmdMonitor(f),
},
},
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/connection/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package connection

import (
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"log"
)

// kubectl dba monitor check-connection mongodb -n demo sample_mg -> all connection check and report

type PromSvc struct {
Name string
Namespace string
Port int
}

func Run(f cmdutil.Factory, args []string, prom PromSvc) {
if len(args) < 2 {
log.Fatal("Enter database and specific database name as argument")
}

database := args[0]
databaseName := args[1]
namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
klog.Error(err, "failed to get current namespace")
}

}

0 comments on commit 7f9fd7f

Please sign in to comment.