/
main.go
145 lines (122 loc) · 3.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/olekukonko/tablewriter"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sandipb/prom-k8s-versions/pkg/prom"
"gopkg.in/alecthomas/kingpin.v2"
)
const appName = "prom-k8s-versions"
var (
version = "unset"
commit = "unset"
date = "unset"
)
var (
showVersion = kingpin.Flag("version", "Show version").Short('v').Bool()
debugLevel = kingpin.Flag("debug", "Debug level logging").Short('d').Bool()
promServer = kingpin.Flag("prom-api", "URL to API server").Short('p').Default("localhost:9090").String()
namespace = kingpin.Flag("namespace", "Namespace for the app").Short('n').Default("default").String()
clusters = kingpin.Flag("clusters", "(Optional) Regex of clusters to select. Can be repeated.").Short('c').Strings()
timeoutSeconds = kingpin.Flag("timeout", "Timeout in seconds for the query").Short('t').Default("10").Int()
showPods = kingpin.Flag("pods", "Show pods").Bool()
showDeploys = kingpin.Flag("deploys", "Show deployments,daemonsets and statefulsets").Bool()
showAll bool
)
const helpText = `
Shows a table of pods with their image versions and a table of deployment-like
objects with chart versions.
NOTE: By default, both "--pods" and "--deploys" are implied. But if any one of
them is specified, the other is not shown unless specifically specified.
`
func setup() {
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.CommandLine.Help = helpText
kingpin.Parse()
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
if *debugLevel {
log.Logger = log.Logger.Level(zerolog.DebugLevel)
} else {
log.Logger = log.Logger.Level(zerolog.InfoLevel)
}
if !*showPods && !*showDeploys {
showAll = true
}
}
func printVersion() {
fmt.Printf("%s %s, commit %s, built %s\n", appName, version, commit, date)
os.Exit(0)
}
func main() {
setup()
if *showVersion {
printVersion()
}
if !strings.HasPrefix(*promServer, "http") {
*promServer = "http://" + *promServer
}
log.Debug().Msgf("Using prometheus server: %#v", *promServer)
log.Debug().Msgf("Searching in namespace: %#v", *namespace)
clusterFilter := prom.ClusterFilter{}
if len(*clusters) > 0 {
log.Debug().Msgf("Filtering by clusters: %#v", *clusters)
for _, c := range *clusters {
clusterFilter.Add(c)
}
}
client := prom.NewClient(*promServer).SetTimeout(*timeoutSeconds)
data := client.GetInfo(*namespace, clusterFilter)
if data.HasPods() && (showAll || *showPods) {
printPods(data)
fmt.Println()
}
if data.HasDeploys() && (showAll || *showDeploys) {
printDeployables(data)
}
}
func printPods(data prom.ClusterResultSet) {
fmt.Printf("PODS\n\n")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Cluster", "Pod", "Container", "Image"})
for _, cluster_name := range data.SortedByCluster() {
cluster_info := data[cluster_name]
idx := 0
for _, e := range cluster_info.Entries {
// Only handle pods
if e.Type != prom.EntityPod {
continue
}
idx += 1
cname := cluster_name
if idx > 1 {
cname = ""
}
table.Append([]string{cname, e.Name, e.ContainerName, e.ContainerImage})
}
}
table.Render()
}
func printDeployables(data prom.ClusterResultSet) {
fmt.Printf("DEPLOYS\n\n")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Cluster", "Type", "Name", "Chart"})
for cluster_name, cluster_info := range data {
idx := 0
for _, e := range cluster_info.Entries {
if e.Type == prom.EntityPod {
continue
}
idx += 1
cname := cluster_name
if idx > 1 {
cname = ""
}
table.Append([]string{cname, string(e.Type), e.Name, e.ChartName})
}
}
table.Render()
}