Skip to content

Commit

Permalink
feat(info): create environment and cluster info commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianGottinger committed Jun 6, 2023
1 parent d1fb6e1 commit 5e29b27
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/copsctl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 0 additions & 19 deletions cmd/copsctl/cluster_info.go

This file was deleted.

54 changes: 54 additions & 0 deletions cmd/copsctl/info.go
Original file line number Diff line number Diff line change
@@ -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.")

}
2 changes: 1 addition & 1 deletion cmd/copsctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cluster_info
package info

import (
"encoding/json"
Expand All @@ -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
Expand All @@ -64,3 +71,8 @@ func (o *Orchestrator) ShowClusterInfo() {
fmt.Println(string(indented))
}
}

const (
Cluster string = "cluster"
Environment = "environment"
)

0 comments on commit 5e29b27

Please sign in to comment.