forked from Azure/azure-k8s-metrics-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·119 lines (96 loc) · 4.03 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
/*
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/custommetrics"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/azure/externalmetrics"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/azure/instancemetadata"
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"
"github.com/Azure/azure-k8s-metrics-adapter/pkg/metriccache"
azureprovider "github.com/Azure/azure-k8s-metrics-adapter/pkg/provider"
"k8s.io/klog"
basecmd "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/cmd"
"k8s.io/component-base/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 {
klog.Fatalf("Unable to run Azure metrics adapter: %v", err)
}
}
func setupAzureProvider(cmd *basecmd.AdapterBase, metricsCache *metriccache.MetricCache) {
mapper, err := cmd.RESTMapper()
if err != nil {
klog.Fatalf("unable to construct discovery REST mapper: %v", err)
}
dynamicClient, err := cmd.DynamicClient()
if err != nil {
klog.Fatalf("unable to construct dynamic k8s client: %v", err)
}
defaultSubscriptionID := getDefaultSubscriptionID()
customMetricsClient := custommetrics.NewClient()
azureExternalClientFactory := externalmetrics.AzureExternalMetricClientFactory{
DefaultSubscriptionID: defaultSubscriptionID,
}
azureProvider := azureprovider.NewAzureProvider(defaultSubscriptionID, mapper, dynamicClient, customMetricsClient, azureExternalClientFactory, 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 {
klog.Fatalf("unable to construct client config: %s", err)
}
adapterClientSet, err := clientset.NewForConfig(clientConfig)
if err != nil {
klog.Fatalf("unable to construct lister client to initialize provider: %v", err)
}
adapterInformerFactory := informers.NewSharedInformerFactory(adapterClientSet, time.Second*30)
handler := controller.NewHandler(adapterInformerFactory.Azure().V1alpha2().ExternalMetrics().Lister(),
adapterInformerFactory.Azure().V1alpha2().CustomMetrics().Lister(),
metricsCache)
controller := controller.NewController(adapterInformerFactory.Azure().V1alpha2().ExternalMetrics(),
adapterInformerFactory.Azure().V1alpha2().CustomMetrics(), &handler)
return controller, adapterInformerFactory
}
func getDefaultSubscriptionID() string {
// if the user explicitly sets we should use that
subscriptionID := os.Getenv("SUBSCRIPTION_ID")
if subscriptionID == "" {
klog.V(2).Info("Looking up subscription ID via instance metadata")
//fallback to trying azure instance meta data
azureConfig, err := instancemetadata.GetAzureConfig()
if err != nil {
klog.Errorf("Unable to get azure config from MSI: %v", err)
}
subscriptionID = azureConfig.SubscriptionID
}
if subscriptionID == "" {
klog.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
}