-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (72 loc) · 1.77 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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"time"
)
var (
port int
interval time.Duration
)
func main() {
app := cli.NewApp()
app.Name = "Image Availability Prometheus Exporter"
app.Usage = "Expose Prometheus metrics about image availability in your Kubernetes cluster"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "port",
Value: 8080,
Usage: "Port to expose metrics on",
Destination: &port,
EnvVar: "PORT",
},
cli.DurationFlag{
Name: "interval",
Value: 12 * time.Hour,
Usage: "Interval to check images",
Destination: &interval,
EnvVar: "INTERVAL",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
go func() {
err := startMetricsServer(port)
if err != nil {
log.Fatalf("error starting metrics server %v", err)
}
}()
log.Infof("Starting image availability check every %s", interval)
for {
log.Info("Getting images in pods")
imageList, err := getImagesFromAllPods()
if err != nil {
log.Fatalf("error getting images from pods %v", err)
}
imagesCount := len(imageList)
for i, image := range imageList {
log.WithFields(map[string]interface{}{
"progress": fmt.Sprintf("%d/%d", i+1, imagesCount),
"image": image,
}).Info("Checking image")
exists, err := imageExistsInRegistry(image)
if err != nil {
log.Errorf("Error checking image %s: %v", image, err)
}
if !exists {
log.Errorf("Image %s not found in registry", image)
resolveMissingTotal.WithLabelValues(image).Set(1)
} else {
log.Infof("Image %s found in registry", image)
resolveMissingTotal.WithLabelValues(image).Set(0)
}
}
time.Sleep(interval)
}
}