forked from deckhouse/k8s-image-availability-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (80 loc) · 2.98 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/flant/k8s-image-availability-exporter/pkg/handlers"
"github.com/flant/k8s-image-availability-exporter/pkg/logging"
"github.com/flant/k8s-image-availability-exporter/pkg/registry_checker"
"github.com/google/go-containerregistry/pkg/name"
"github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/sample-controller/pkg/signals"
_ "sigs.k8s.io/controller-runtime/pkg/metrics"
)
func main() {
imageCheckInterval := flag.Duration("check-interval", time.Minute, "image re-check interval")
ignoredImagesStr := flag.String("ignored-images", "", "tilde-separated image regexes to ignore, each image will be checked against this list of regexes")
bindAddr := flag.String("bind-address", ":8080", "address:port to bind /metrics endpoint to")
namespaceLabels := flag.String("namespace-label", "", "namespace label for checks")
insecureSkipVerify := flag.Bool("skip-registry-cert-verification", false, "whether to skip registries' certificate verification")
defaultRegistry := flag.String("default-registry", "",
fmt.Sprintf("default registry to use in absence of a fully qualified image name, defaults to %q", name.DefaultRegistry))
flag.Parse()
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logrus.AddHook(logging.NewPrometheusHook())
// set up signals, so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
logrus.Fatalf("Couldn't get Kubernetes default config: %s", err)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
logrus.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
liveTicksCounter := prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "k8s_image_availability_exporter",
Name: "completed_rechecks_total",
Help: "Number of image rechecks completed.",
},
)
prometheus.MustRegister(liveTicksCounter)
var regexes []regexp.Regexp
if *ignoredImagesStr != "" {
regexStrings := strings.Split(*ignoredImagesStr, "~")
for _, regexStr := range regexStrings {
regexes = append(regexes, *regexp.MustCompile(regexStr))
}
}
registryChecker := registry_checker.NewRegistryChecker(
stopCh,
kubeClient,
*insecureSkipVerify,
regexes,
*defaultRegistry,
*namespaceLabels,
)
prometheus.MustRegister(registryChecker)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", handlers.Healthz)
go func() {
log.Fatal(http.ListenAndServe(*bindAddr, nil))
}()
handlers.UpdateHealth(true)
wait.Until(func() {
registryChecker.Tick()
liveTicksCounter.Inc()
}, *imageCheckInterval, stopCh)
}