From 7d0206bff75ac98bbdeac1cf1fdd86b16f3eac73 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 2 Nov 2023 16:00:13 -0400 Subject: [PATCH] Add group by annotation processing --- cmd/envcheckctl/envcheckctl_main.go | 6 ++++ cmd/envcheckctl/exec_inspect.go | 51 +++++++++++++++++++++++++++++ go.mod | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/cmd/envcheckctl/envcheckctl_main.go b/cmd/envcheckctl/envcheckctl_main.go index aaa751c..6112e03 100644 --- a/cmd/envcheckctl/envcheckctl_main.go +++ b/cmd/envcheckctl/envcheckctl_main.go @@ -42,6 +42,7 @@ func Exec(config EnvcheckConfig) { type EnvcheckConfig struct { AgentNamespace string AgentName string + Annotation string Kubeconfig string PingerHost string PingerNamespace string @@ -56,6 +57,10 @@ func (c *EnvcheckConfig) IsLive() bool { return c.Podfile == "" } +func (c *EnvcheckConfig) CheckAnnotation() bool { + return c.Annotation != "" +} + const ( // Agent is the subcommand flag to indicate the agent debug to be executed. Agent int = iota @@ -93,6 +98,7 @@ func Parse(args []string, kubepath string, w io.Writer) (*EnvcheckConfig, error) flags, config = cmdFlags.FlagSet("inspect", InspectCluster) flags.StringVar(&config.Podfile, "podfile", "", "read from podfile instead of live cluster query") flags.StringVar(&config.Kubeconfig, "kubeconfig", kubepath, "absolute path to the kubeconfig file") + flags.StringVar(&config.Annotation, "annotation", "", "group by annotation value") flags, config = cmdFlags.FlagSet("leader", Leader) flags.StringVar(&config.Kubeconfig, "kubeconfig", kubepath, "absolute path to the kubeconfig file") diff --git a/cmd/envcheckctl/exec_inspect.go b/cmd/envcheckctl/exec_inspect.go index 10bb3da..358889a 100644 --- a/cmd/envcheckctl/exec_inspect.go +++ b/cmd/envcheckctl/exec_inspect.go @@ -86,8 +86,59 @@ func ExecInspect(config EnvcheckConfig) { PrintCounter("zones", index.Zones) PrintCounter("linkedConfigMaps", index.LinkedConfigMaps) PrintCounter("owners", index.Owners) + + if config.CheckAnnotation() { + grouping := NewGrouping(config.Annotation) + info.Apply(grouping) + PrintGroup(config.Annotation, grouping) + } +} + +func PrintGroup(header string, ag *AnnotationGrouping) { + log.Println("") + log.Println(header) + for k, v := range ag.group { + if k == "" { + k = "~~~ NOT PRESENT ~~~" + } + log.Println("-", k) + sort.Strings(v) + m := make(map[string]bool) + for _, n := range v { + if !m[n] { + log.Println(" -", n) + } + m[n] = true + } + } } +func NewGrouping(annotation string) *AnnotationGrouping { + return &AnnotationGrouping{ + annotation: annotation, + group: make(map[string][]string), + } +} + +type AnnotationGrouping struct { + annotation string + group map[string][]string +} + +func (a AnnotationGrouping) EachPod(pod cluster.PodInfo) { + v := pod.Annotations[a.annotation] + ag := a.group[v] + name := pod.Name + // use the owner name if present... if more than one present assigned oh well. + for owner := range pod.Owners { + name = owner + } + ag = append(ag, pod.Namespace+"/"+name) + a.group[v] = ag +} + +func (a AnnotationGrouping) EachNode(_ cluster.NodeInfo) {} + func PrintKind(version string) { dist := ExtractDistribution(version) log.Println("serverDistribution") diff --git a/go.mod b/go.mod index e40e452..083d6e6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/instana/envcheck -go 1.19 +go 1.21 require ( github.com/gogunit/gunit v0.0.0-20230317150752-6f4e88d129c6