forked from Azure/azure-k8s-metrics-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·117 lines (93 loc) · 3.88 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
/*
Uses base classes and Provider interfaces from https://github.com/kubernetes-incubator/custom-metrics-apiserver to build
a metric server for Azure based services.
*/
package main
import (
"flag"
"os"
"runtime"
"time"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/azure/appinsights"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/azure/instancemetadata"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/azure/monitor"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/metriccache"
clientset "github.com/Azure/azure-k8s-metrics-adapter/pkg/client/clientset/versioned"
informers "github.com/Azure/azure-k8s-metrics-adapter/pkg/client/informers/externalversions"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/controller"
azureprovider "github.com/Azure/azure-k8s-metrics-adapter/pkg/provider"
"github.com/golang/glog"
basecmd "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/cmd"
"k8s.io/apiserver/pkg/util/logs"
)
func main() {
logs.InitLogs()
defer logs.FlushLogs()
if len(os.Getenv("GOMAXPROCS")) == 0 {
runtime.GOMAXPROCS(runtime.NumCPU())
}
cmd := &basecmd.AdapterBase{}
cmd.Flags().AddGoFlagSet(flag.CommandLine)
cmd.Flags().Parse(os.Args)
stopCh := make(chan struct{})
defer close(stopCh)
metriccache := metriccache.NewMetricCache()
// start and run contoller components
controller, adapterInformerFactory := newController(cmd, metriccache)
go adapterInformerFactory.Start(stopCh)
go controller.Run(2, time.Second, stopCh)
//setup and run metric server
setupAzureProvider(cmd, metriccache)
if err := cmd.Run(stopCh); err != nil {
glog.Fatalf("Unable to run Azure metrics adapter: %v", err)
}
}
func setupAzureProvider(cmd *basecmd.AdapterBase, metricsCache *metriccache.MetricCache) {
mapper, err := cmd.RESTMapper()
if err != nil {
glog.Fatalf("unable to construct discovery REST mapper: %v", err)
}
dynamicClient, err := cmd.DynamicClient()
if err != nil {
glog.Fatalf("unable to construct dynamic k8s client: %v", err)
}
defaultSubscriptionID := getDefaultSubscriptionID()
monitorClient := monitor.NewClient(defaultSubscriptionID)
appinsightsClient := appinsights.NewClient()
azureProvider := azureprovider.NewAzureProvider(defaultSubscriptionID, mapper, dynamicClient, appinsightsClient, monitorClient, metricsCache)
cmd.WithCustomMetrics(azureProvider)
cmd.WithExternalMetrics(azureProvider)
}
func newController(cmd *basecmd.AdapterBase, metricsCache *metriccache.MetricCache) (*controller.Controller, informers.SharedInformerFactory) {
clientConfig, err := cmd.ClientConfig()
if err != nil {
glog.Fatalf("unable to construct client config: %s", err)
}
adapterClientSet, err := clientset.NewForConfig(clientConfig)
if err != nil {
glog.Fatalf("unable to construct lister client to initialize provider: %v", err)
}
adapterInformerFactory := informers.NewSharedInformerFactory(adapterClientSet, time.Second*30)
handler := controller.NewHandler(adapterInformerFactory.Azure().V1alpha1().ExternalMetrics().Lister(),
adapterInformerFactory.Azure().V1alpha1().CustomMetrics().Lister(),
metricsCache)
controller := controller.NewController(adapterInformerFactory.Azure().V1alpha1().ExternalMetrics(),
adapterInformerFactory.Azure().V1alpha1().CustomMetrics(), &handler)
return controller, adapterInformerFactory
}
func getDefaultSubscriptionID() string {
// if the user explicitly sets we should use that
subscriptionID := os.Getenv("SUBSCRIPTION_ID")
if subscriptionID == "" {
//fallback to trying azure instance meta data
azureConfig, err := instancemetadata.GetAzureConfig()
if err != nil {
glog.Errorf("Unable to get azure config from MSI: %v", err)
}
subscriptionID = azureConfig.SubscriptionID
}
if subscriptionID == "" {
glog.V(0).Info("Default Azure Subscription is not set. You must provide subscription id via HPA lables, set an environment variable, or enable MSI. See docs for more details")
}
return subscriptionID
}