Skip to content

Commit

Permalink
Add group by annotation processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Fisher authored and Nathan Fisher committed Nov 2, 2023
1 parent 81cdd5d commit 7d0206b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/envcheckctl/envcheckctl_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Exec(config EnvcheckConfig) {
type EnvcheckConfig struct {
AgentNamespace string
AgentName string
Annotation string
Kubeconfig string
PingerHost string
PingerNamespace string
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
51 changes: 51 additions & 0 deletions cmd/envcheckctl/exec_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7d0206b

Please sign in to comment.