/
main.go
116 lines (100 loc) · 2.75 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/sapslaj/eks-pricing-exporter/pkg/collector"
"github.com/sapslaj/eks-pricing-exporter/pkg/pricing"
)
func main() {
port := flag.Int("port", 9523, "port to run exporter on")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
go handleSigterm(cancel)
cs := kubernetes.NewForConfigOrDie(ctrl.GetConfigOrDie())
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatalf("loading aws config: %s", err)
}
pricingProvider := pricing.NewAWSProvider(cfg)
// sanity check
_, err = pricingProvider.GetFargatePricing(ctx)
if err != nil {
log.Fatalf("could not load AWS pricing data: %s", err)
}
pricingRepository := pricing.NewRepository(pricingProvider)
log.Printf("updating pricing...")
err = pricingRepository.UpdatePricing(ctx)
if err != nil {
log.Fatalf("could not update pricing repository: %s", err)
}
prometheus.MustRegister(collector.NewCollector(ctx, cs, pricingRepository))
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/admin/pricing/update", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Only POST method is allowed on this endpoint.")
return
}
log.Println("updating pricing via /admin/pricing/update")
err := pricingRepository.UpdatePricing(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error updating pricing: %s", err)
return
}
fmt.Fprintln(w, "success")
})
addr := fmt.Sprintf(":%d", *port)
server := &http.Server{
Addr: addr,
Handler: mux,
BaseContext: func(_ net.Listener) context.Context { return ctx },
ReadTimeout: time.Minute,
}
log.Printf("Starting eks-pricing-exporter/%s on %s", VERSION, addr)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.Tick(1 * time.Hour):
log.Println("updating pricing on schedule")
err := pricingRepository.UpdatePricing(ctx)
if err != nil {
log.Fatalf("could not update pricing repository: %s", err)
}
}
}
}()
err = server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("error running server: %s", err)
}
}
func handleSigterm(cancel func()) {
signals := make(chan os.Signal, 1)
signal.Notify(
signals,
syscall.SIGHUP,
syscall.SIGQUIT,
syscall.SIGTERM,
)
<-signals
log.Println("Received SIGTERM. Terminating.")
cancel()
}