-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathapp.go
More file actions
138 lines (112 loc) · 3.84 KB
/
app.go
File metadata and controls
138 lines (112 loc) · 3.84 KB
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
package app
import (
"context"
"fmt"
"net/http"
logrusr "github.com/bombsimon/logrusr/v4"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/go-chi/transport"
"github.com/hashicorp/go-cleanhttp"
_ "k8s.io/client-go/plugin/pkg/client/auth" // Load all auth plugins
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
ctrmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client"
"github.com/jetstack/version-checker/pkg/controller"
"github.com/jetstack/version-checker/pkg/metrics"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
const (
helpOutput = "Kubernetes utility for exposing used image versions compared to the latest version, as metrics."
)
func NewCommand(ctx context.Context) *cobra.Command {
opts := new(Options)
cmd := &cobra.Command{
Use: "version-checker",
Short: helpOutput,
Long: helpOutput,
RunE: func(_ *cobra.Command, _ []string) error {
opts.complete()
logLevel, err := logrus.ParseLevel(opts.LogLevel)
if err != nil {
return fmt.Errorf("failed to parse --log-level %q: %s",
opts.LogLevel, err)
}
log := newLogger(logLevel).WithField("component", "controller")
ctrl.SetLogger(logrusr.New(log.WithField("controller", "manager").Logger))
defaultTestAllInfoMsg := fmt.Sprintf(`only containers with the annotation "%s/${my-container}=true" will be parsed`, api.EnableAnnotationKey)
if opts.DefaultTestAll {
defaultTestAllInfoMsg = fmt.Sprintf(`all containers will be tested, unless they have the annotation "%s/${my-container}=false"`, api.EnableAnnotationKey)
}
restConfig, err := opts.kubeConfigFlags.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to build kubernetes rest config: %s", err)
}
log.Warnf("flag --test-all-containers=%t %s", opts.DefaultTestAll, defaultTestAllInfoMsg)
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
LeaderElection: false,
Metrics: server.Options{
BindAddress: opts.MetricsServingAddress,
SecureServing: false,
},
GracefulShutdownTimeout: &opts.GracefulShutdownTimeout,
Cache: cache.Options{SyncPeriod: &opts.CacheSyncPeriod},
PprofBindAddress: opts.PprofBindAddress,
})
if err != nil {
return err
}
// Liveness probe
if err := mgr.AddMetricsServerExtraHandler("/healthz",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})); err != nil {
log.Fatal("Unable to set up health check:", err)
}
// Readiness probe
if err := mgr.AddMetricsServerExtraHandler("/readyz",
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if mgr.GetCache().WaitForCacheSync(context.Background()) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ready"))
} else {
http.Error(w, "cache not synced", http.StatusServiceUnavailable)
}
}),
); err != nil {
log.Fatal("Unable to set up ready check:", err)
}
metricsServer := metrics.New(log, ctrmetrics.Registry, mgr.GetCache())
opts.Client.Transport = transport.Chain(
cleanhttp.DefaultTransport(),
metricsServer.RoundTripper,
)
client, err := client.New(ctx, log, opts.Client)
if err != nil {
return fmt.Errorf("failed to setup image registry clients: %s", err)
}
c := controller.NewPodReconciler(opts.CacheTimeout,
metricsServer,
client,
mgr.GetClient(),
log,
opts.RequeueDuration,
opts.DefaultTestAll,
)
if err := c.SetupWithManager(mgr); err != nil {
return err
}
// Start the manager and all controllers
log.Info("Starting controller manager")
if err := mgr.Start(ctx); err != nil {
return err
}
return nil
},
}
opts.addFlags(cmd)
return cmd
}