From 5e29b27cb5386fd52e84656707c04ec9d4cd5864 Mon Sep 17 00:00:00 2001 From: Christian Gottinger Date: Fri, 26 May 2023 08:25:59 +0200 Subject: [PATCH] feat(info): create environment and cluster info commands --- cmd/copsctl/cli.go | 2 +- cmd/copsctl/cluster_info.go | 19 ------- cmd/copsctl/info.go | 54 +++++++++++++++++++ cmd/copsctl/main.go | 2 +- .../{cluster_info => info}/orchestrator.go | 26 ++++++--- 5 files changed, 75 insertions(+), 28 deletions(-) delete mode 100644 cmd/copsctl/cluster_info.go create mode 100644 cmd/copsctl/info.go rename internal/{cluster_info => info}/orchestrator.go (70%) diff --git a/cmd/copsctl/cli.go b/cmd/copsctl/cli.go index ef38860..1b41f76 100644 --- a/cmd/copsctl/cli.go +++ b/cmd/copsctl/cli.go @@ -4,7 +4,7 @@ import "github.com/conplementag/cops-hq/v2/pkg/hq" func createCommands(hq hq.HQ) { createNamespaceCommand(hq) - createClusterInfoCommand(hq) + createInfoCommands(hq) createConnectCommand(hq) createAzureDevopsCommand(hq) } diff --git a/cmd/copsctl/cluster_info.go b/cmd/copsctl/cluster_info.go deleted file mode 100644 index 7aba35d..0000000 --- a/cmd/copsctl/cluster_info.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "fmt" - "github.com/conplementAG/copsctl/internal/cluster_info" - "github.com/conplementAG/copsctl/internal/cmd/flags" - "github.com/conplementag/cops-hq/v2/pkg/hq" -) - -func createClusterInfoCommand(hq hq.HQ) { - clusterInfoCmdGroup := hq.GetCli().AddBaseCommand("cluster-info", "Command for showing the CoreOps cluster information", - fmt.Sprintf("Use this command to get the cluster info which might be useful for your. For example, if the static outbound IPs are enabled for the cluster, then you can use this command to get these IPs. Make sure you are connected to the cluster first. "+ - "Use the %s flag for automation.", flags.PrintToStdoutSilenceEverythingElse), func() { - cluster_info.New(hq).ShowClusterInfo() - }) - - clusterInfoCmdGroup.AddPersistentParameterBool(flags.PrintToStdoutSilenceEverythingElse, false, false, "q", - "Similar to print-to-stdout, but silences all other logging outputs. Useful for automation.") -} diff --git a/cmd/copsctl/info.go b/cmd/copsctl/info.go new file mode 100644 index 0000000..f43c911 --- /dev/null +++ b/cmd/copsctl/info.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "github.com/conplementAG/copsctl/internal/cmd/flags" + "github.com/conplementAG/copsctl/internal/info" + "github.com/conplementag/cops-hq/v2/pkg/cli" + "github.com/conplementag/cops-hq/v2/pkg/hq" +) + +func createInfoCommands(hq hq.HQ) { + infoCmdGroup := hq.GetCli().AddBaseCommand("info", "Command group for informations of k8s", + "Use this command to get informations on cluster / environment.", nil) + + orchestrator := info.New(hq) + + createClusterInfoCommand(hq, orchestrator) + createInfoClusterCommand(infoCmdGroup, orchestrator) + createInfoEnvironementCommand(infoCmdGroup, orchestrator) +} + +func createClusterInfoCommand(hq hq.HQ, o *info.Orchestrator) { + clusterInfoCmdGroup := hq.GetCli().AddBaseCommand("cluster-info", "[DEPRECATED] Command for showing the CoreOps cluster information", + fmt.Sprintf("[DEPRECATED] Use this command to get the cluster info which might be useful for your. For example, if the static outbound IPs are enabled for the cluster, then you can use this command to get these IPs. Make sure you are connected to the cluster first. "+ + "Use the %s flag for automation.", flags.PrintToStdoutSilenceEverythingElse), func() { + o.ShowEnvironmentInfo() + }) + + addSilenceParam(clusterInfoCmdGroup) +} + +func createInfoClusterCommand(cmd cli.Command, o *info.Orchestrator) { + infoClusterCmd := cmd.AddCommand("cluster", "Get cluster infos", + "Use this command to get informations around cluster which have the same lifecycle as the cluster its self. "+ + "Check 'info environment' for information with common lifecycle. ", func() { + o.ShowClusterInfo() + }) + addSilenceParam(infoClusterCmd) +} + +func createInfoEnvironementCommand(cmd cli.Command, o *info.Orchestrator) { + infoClusterCmd := cmd.AddCommand("environment", "Get environment infos", + "Use this command to get informations around environment with common lifecycle. "+ + "Check 'info cluster' for cluster specific information with cluster lifecycle. ", func() { + o.ShowEnvironmentInfo() + }) + addSilenceParam(infoClusterCmd) +} + +func addSilenceParam(cmd cli.Command) { + cmd.AddPersistentParameterBool(flags.PrintToStdoutSilenceEverythingElse, false, false, "q", + "Similar to print-to-stdout, but silences all other logging outputs. Useful for automation.") + +} diff --git a/cmd/copsctl/main.go b/cmd/copsctl/main.go index 504a1f5..83b7582 100644 --- a/cmd/copsctl/main.go +++ b/cmd/copsctl/main.go @@ -9,7 +9,7 @@ import ( func main() { defer errorhandler() - hq := hq.NewQuiet("copsctl", "0.9.0", "copsctl.log") + hq := hq.NewQuiet("copsctl", "0.10.0", "copsctl.log") createCommands(hq) error_handling.PanicOnAnyError = true diff --git a/internal/cluster_info/orchestrator.go b/internal/info/orchestrator.go similarity index 70% rename from internal/cluster_info/orchestrator.go rename to internal/info/orchestrator.go index 5ed73c0..8b02b29 100644 --- a/internal/cluster_info/orchestrator.go +++ b/internal/info/orchestrator.go @@ -1,4 +1,4 @@ -package cluster_info +package info import ( "encoding/json" @@ -22,28 +22,35 @@ func New(hq hq.HQ) *Orchestrator { executor: hq.GetExecutor(), } } - func (o *Orchestrator) ShowClusterInfo() { + o.showInfo(Cluster) +} + +func (o *Orchestrator) ShowEnvironmentInfo() { + o.showInfo(Environment) +} + +func (o *Orchestrator) showInfo(typeName string) { printConfigSilenceEverythingElse := viper.GetBool(flags.PrintToStdoutSilenceEverythingElse) if !printConfigSilenceEverythingElse { - logrus.Info("Reading the cluster info ...") + logrus.Infof("Reading the %s info ...", typeName) logrus.Info("NOTE: you can use the " + flags.PrintToStdoutSilenceEverythingElse + " flag to silence these outputs (useful for automation)") logrus.Info("===========================================================") - logrus.Info("==================== Cluster Info: =======================") + logrus.Infof("==================== %s Info: =======================", typeName) logrus.Info("===========================================================") } - result, err := o.executor.Execute("kubectl get configmap -n coreops-public -o jsonpath=\"{.data['info\\.json']}\" coreops-cluster-info") + result, err := o.executor.Execute(fmt.Sprintf("kubectl get configmap -n coreops-public -o jsonpath=\"{.data['%s-info\\.json']}\" coreops-info", typeName)) if err != nil { logrus.Errorf(err.Error()) panic(err) } - result = strings.TrimPrefix(result, "'") - result = strings.TrimSuffix(result, "'") + result = strings.Trim(result, "'") + result = strings.Trim(result, "\"") if printConfigSilenceEverythingElse { // no pretty formats or anything is needed if printing for parse purposes @@ -64,3 +71,8 @@ func (o *Orchestrator) ShowClusterInfo() { fmt.Println(string(indented)) } } + +const ( + Cluster string = "cluster" + Environment = "environment" +)