-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (53 loc) · 1.63 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
package main
import (
"flag"
"net/http"
"os"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
semaphoreProvider "github.com/semaphoreci/k8s-metrics-apiserver/pkg/provider"
"github.com/semaphoreci/k8s-metrics-apiserver/pkg/semaphore"
basecmd "sigs.k8s.io/custom-metrics-apiserver/pkg/cmd"
)
type SemaphoreAdapter struct {
basecmd.AdapterBase
Message string
}
func (a *SemaphoreAdapter) makeProviderOrDie() *semaphoreProvider.SemaphoreMetricsProvider {
client, err := a.DynamicClient()
if err != nil {
klog.Fatalf("unable to construct dynamic client: %v", err)
}
mapper, err := a.RESTMapper()
if err != nil {
klog.Fatalf("unable to construct discovery REST mapper: %v", err)
}
provider, err := semaphoreProvider.New(semaphoreProvider.Config{
Client: client,
Mapper: mapper,
SemaphoreClient: semaphore.NewClient(http.DefaultClient, false),
})
if err != nil {
klog.Fatalf("unable to construct Semaphore provider: %v", err)
}
return provider
}
func main() {
logs.InitLogs()
defer logs.FlushLogs()
// initialize the flags, with one custom flag for the message
cmd := &SemaphoreAdapter{}
cmd.Flags().StringVar(&cmd.Message, "msg", "starting semaphore metrics adapter...", "startup message")
// make sure you get the klog flags
logs.AddFlags(cmd.Flags())
cmd.Flags().AddGoFlagSet(flag.CommandLine)
cmd.Flags().Parse(os.Args)
provider := cmd.makeProviderOrDie()
cmd.WithExternalMetrics(provider)
klog.Infof(cmd.Message)
go provider.Collect()
if err := cmd.Run(wait.NeverStop); err != nil {
klog.Fatalf("unable to run custom metrics adapter: %v", err)
}
}