-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
166 lines (140 loc) · 5.01 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"flag"
"fmt"
"log"
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/helm/pkg/kube"
releaseV2 "k8s.io/helm/pkg/proto/hapi/release"
driverv2 "k8s.io/helm/pkg/storage/driver"
releaseV3 "helm.sh/helm/v3/pkg/release"
driverv3 "helm.sh/helm/v3/pkg/storage/driver"
)
var (
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
stats = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "helm_release_info",
Help: "Helm release information",
}, []string{
"storage_driver",
"chart",
"chart_name",
"chart_version",
"release_namespace",
"release_name",
"release_status",
"helm_version",
})
statusCodeV2V3 = map[releaseV2.Status_Code]releaseV3.Status{
releaseV2.Status_UNKNOWN: releaseV3.StatusUnknown,
releaseV2.Status_DEPLOYED: releaseV3.StatusDeployed,
releaseV2.Status_DELETED: releaseV3.StatusUninstalled,
releaseV2.Status_SUPERSEDED: releaseV3.StatusSuperseded,
releaseV2.Status_FAILED: releaseV3.StatusFailed,
releaseV2.Status_DELETING: releaseV3.StatusUninstalling,
releaseV2.Status_PENDING_INSTALL: releaseV3.StatusPendingInstall,
releaseV2.Status_PENDING_UPGRADE: releaseV3.StatusPendingUpgrade,
releaseV2.Status_PENDING_ROLLBACK: releaseV3.StatusPendingRollback,
}
)
func main() {
flag.Parse()
log.Printf("Starting to listen address %s ...", *addr)
http.HandleFunc("/", redirectHandler)
http.HandleFunc("/metrics", metricsHandler)
http.HandleFunc("/healthz", func(_ http.ResponseWriter, _ *http.Request) {})
_, err := kube.New(nil).KubernetesClientSet()
if err != nil {
log.Fatalf("Cannot initialize Kubernetes connection: %s", err)
}
log.Fatal(http.ListenAndServe(*addr, nil))
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
}
func metricsHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Handling metrics request ...")
stats.Reset()
clientSet, err := kube.New(nil).KubernetesClientSet()
if err != nil {
log.Fatalf("Cannot initialize Kubernetes connection: %s", err)
}
namespaceList, err := clientSet.CoreV1().Namespaces().List(v1.ListOptions{})
if err != nil {
log.Fatalf("Cannot list namespaces: %s", err)
}
var wg sync.WaitGroup
for _, ns := range namespaceList.Items {
namespace := ns.GetName()
cfgmapsv2 := driverv2.NewConfigMaps(clientSet.CoreV1().ConfigMaps(namespace))
secretsv2 := driverv2.NewSecrets(clientSet.CoreV1().Secrets(namespace))
for _, d := range [2]driverv2.Driver{cfgmapsv2, secretsv2} {
wg.Add(1)
go addDriverV2Values(&wg, d, namespace)
}
cfgmapsv3 := driverv3.NewConfigMaps(clientSet.CoreV1().ConfigMaps(namespace))
secretsv3 := driverv3.NewSecrets(clientSet.CoreV1().Secrets(namespace))
for _, d := range [2]driverv3.Driver{cfgmapsv3, secretsv3} {
wg.Add(1)
go addDriverV3Values(&wg, d, namespace)
}
}
wg.Wait()
promhttp.Handler().ServeHTTP(w, r)
log.Println("Handled metrics request.")
}
func addDriverV2Values(wg *sync.WaitGroup, d driverv2.Driver, namespace string) {
defer wg.Done()
lastReleases := make(map[string]*releaseV2.Release)
releaseList, err := d.List(func(_ *releaseV2.Release) bool { return true })
if err != nil {
log.Printf("Cannot list v2 releases in %s namespace stored in %s: %s", namespace, d.Name(), err)
}
for _, r := range releaseList {
if lastRelease, ok := lastReleases[r.Name]; ok {
if lastRelease.Version < r.Version {
lastReleases[r.Name] = r
}
} else {
lastReleases[r.Name] = r
}
}
for _, r := range lastReleases {
statusCode := r.Info.Status.Code
// Map v3 Status_Code to v3 Status
v3status := statusCodeV2V3[statusCode]
// This field is added for convenience
chart := fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version)
stats.WithLabelValues(d.Name(), chart, r.Chart.Metadata.Name, r.Chart.Metadata.Version,
r.Namespace, r.Name, v3status.String(), "v2").Set(float64(r.Version))
}
}
func addDriverV3Values(wg *sync.WaitGroup, d driverv3.Driver, namespace string) {
defer wg.Done()
lastReleases := make(map[string]*releaseV3.Release)
releaseList, err := d.List(func(_ *releaseV3.Release) bool { return true })
if err != nil {
log.Printf("Cannot list v3 releases in %s namespace stored in %s: %s", namespace, d.Name(), err)
}
for _, r := range releaseList {
if lastRelease, ok := lastReleases[r.Name]; ok {
if lastRelease.Version < r.Version {
lastReleases[r.Name] = r
}
} else {
lastReleases[r.Name] = r
}
}
for _, r := range lastReleases {
status := r.Info.Status
// This field is added for convenience
chart := fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version)
stats.WithLabelValues(d.Name(), chart, r.Chart.Metadata.Name, r.Chart.Metadata.Version,
r.Namespace, r.Name, status.String(), "v3").Set(float64(r.Version))
}
}