-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (100 loc) · 3.39 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Bonial-International-GmbH/spotinst-metrics-exporter/pkg/collectors"
"github.com/Bonial-International-GmbH/spotinst-metrics-exporter/pkg/labels"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spotinst/spotinst-sdk-go/service/mcs"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/aws"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"go.uber.org/zap"
)
var logger logr.Logger
func init() {
// Set up a production logger which will write JSON logs.
zapLog, err := zap.NewProduction()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup logger: %v", err)
os.Exit(1)
}
logger = zapr.NewLogger(zapLog)
}
func main() {
addr := pflag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
var labelMappings labels.Mappings
pflag.Var(
&labelMappings,
"resource-labels",
"Comma-separated list of Kubernetes resource labels (with optional Prometheus label mapping) to propagate onto metrics. E.g. 'mylabel,otherresourcelabel=someprometheuslabel'",
)
pflag.Parse()
logger.Info("propagating resource labels", "mapping", labelMappings)
ctx, cancel := context.WithCancel(context.Background())
go handleSignals(cancel)
sess := session.New()
mcsClient := mcs.New(sess)
oceanAWSClient := ocean.New(sess).CloudProviderAWS()
clusters, err := getOceanAWSClusters(ctx, oceanAWSClient)
if err != nil {
logger.Error(err, "failed to fetch ocean clusters")
os.Exit(1)
}
registry := prometheus.NewRegistry()
registry.MustRegister(collectors.NewOceanAWSClusterCostsCollector(ctx, logger, mcsClient, clusters, labelMappings))
registry.MustRegister(collectors.NewOceanAWSResourceSuggestionsCollector(ctx, logger, oceanAWSClient, clusters))
handler := http.NewServeMux()
handler.HandleFunc("/healthz", healthzHandler)
handler.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{EnableOpenMetrics: true}))
listenAndServe(ctx, handler, *addr)
}
func handleSignals(cancelFunc func()) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM, os.Interrupt)
<-signals
logger.Info("received signal, terminating...")
cancelFunc()
}
func listenAndServe(ctx context.Context, handler http.Handler, addr string) {
srv := &http.Server{
Addr: addr,
Handler: handler,
}
go func() {
logger.Info("starting server", "addr", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error(err, "failed to start server")
os.Exit(1)
}
}()
<-ctx.Done()
logger.Info("shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.Error(err, "failed to shutdown HTTP server")
os.Exit(1)
}
}
func getOceanAWSClusters(ctx context.Context, client aws.Service) ([]*aws.Cluster, error) {
output, err := client.ListClusters(ctx, &aws.ListClustersInput{})
if err != nil {
return nil, err
}
return output.Clusters, nil
}
func healthzHandler(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("ok")); err != nil {
logger.Error(err, "failed to write health check status")
}
}