-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (77 loc) · 1.61 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
package main
import (
"context"
"errors"
"flag"
"log/slog"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
)
type App interface {
Start()
}
type app struct {
ctx context.Context
client *kubernetes.Clientset
config *viper.Viper
deployedNamespace string
namespace string
targetService string
}
func newApp(
ctx context.Context,
client *kubernetes.Clientset,
config *viper.Viper,
) App {
return &app{
ctx: ctx,
client: client,
config: config,
deployedNamespace: getDeployedNamespace(),
namespace: getVaultNamespace(),
targetService: getTargetService(),
}
}
func (a *app) Start() {
go func() {
r := mux.NewRouter()
r.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
srv := &http.Server{
Addr: ":8080",
Handler: r,
}
slog.Info("Starting metrics server")
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("Error starting metrics server", slog.String(loggingKeyError, err.Error()))
os.Exit(1)
}
}()
for {
select {
case <-a.ctx.Done():
return
default:
a.watchNewPods()
slog.Debug("Watch new pods ended, trying to reconnect")
}
}
}
func init() {
flag.Parse()
initializeLogger()
}
func main() {
a, err := InitializeApp()
if err != nil {
slog.Error("Error initializing app", slog.String(loggingKeyError, err.Error()))
os.Exit(1)
} else if a == nil {
slog.Error("App is nil")
os.Exit(1)
}
a.Start()
}