in custom-metrics-stackdriver-adapter/adapter.go [172:260]
func main() {
logs.InitLogs()
defer logs.FlushLogs()
cmd := &StackdriverAdapter{
basecmd.AdapterBase{
Name: "custom-metrics-stackdriver-adapter",
},
}
if cmd.OpenAPIConfig == nil {
cmd.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(generatedopenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(api.Scheme, customexternalmetrics.Scheme))
cmd.OpenAPIConfig.Info.Title = "custom-metrics-stackdriver-adapter"
cmd.OpenAPIConfig.Info.Version = "1.0.0"
}
flags := cmd.Flags()
flags.AddGoFlagSet(flag.CommandLine) // make sure we get the klog flags
serverOptions := stackdriverAdapterServerOptions{
UseNewResourceModel: false,
EnableCustomMetricsAPI: true,
EnableExternalMetricsAPI: true,
FallbackForContainerMetrics: false,
EnableCoreMetricsAPI: false,
EnableDistributionSupport: false,
ListFullCustomMetrics: false,
ExternalMetricsCacheSize: defaultExternalMetricsCacheSize,
}
flags.BoolVar(&serverOptions.UseNewResourceModel, "use-new-resource-model", serverOptions.UseNewResourceModel,
"whether to use new Stackdriver resource model")
flags.BoolVar(&serverOptions.EnableCustomMetricsAPI, "enable-custom-metrics-api", serverOptions.EnableCustomMetricsAPI,
"whether to enable Custom Metrics API")
flags.BoolVar(&serverOptions.EnableExternalMetricsAPI, "enable-external-metrics-api", serverOptions.EnableExternalMetricsAPI,
"whether to enable External Metrics API")
flags.BoolVar(&serverOptions.FallbackForContainerMetrics, "fallback-for-container-metrics", serverOptions.FallbackForContainerMetrics,
"If true, fallbacks to k8s_container resource when given metric is not present on k8s_pod. At most one container with given metric is allowed for each pod.")
flags.BoolVar(&serverOptions.EnableCoreMetricsAPI, "enable-core-metrics-api", serverOptions.EnableCoreMetricsAPI,
"Experimental, do not use. Whether to enable Core Metrics API.")
flags.BoolVar(&serverOptions.ListFullCustomMetrics, "list-full-custom-metrics", serverOptions.ListFullCustomMetrics,
"whether to supporting list full custom metrics. This is a featuragate to list full custom metrics back, which should keep as false to return only 1 metric. Otherwise, it would have high memory usage issue.")
flags.StringVar(&serverOptions.MetricsAddress, "metrics-address", "",
"Endpoint with port on which Prometheus metrics server should be enabled. Example: localhost:8080. If there is no flag, Prometheus metric server is disabled and monitoring metrics are not collected.")
flags.StringVar(&serverOptions.StackdriverEndpoint, "stackdriver-endpoint", "",
"Stackdriver Endpoint used by adapter. Default is https://monitoring.googleapis.com/")
flags.BoolVar(&serverOptions.EnableDistributionSupport, "enable-distribution-support", serverOptions.EnableDistributionSupport,
"enables support for scaling based on distribution values")
flags.DurationVar(&serverOptions.ExternalMetricsCacheTTL, "external-metric-cache-ttl", serverOptions.ExternalMetricsCacheTTL,
"The duration (e.g., 1m, 5s) for which external metric values are cached.")
flags.IntVar(&serverOptions.ExternalMetricsCacheSize, "external-metric-cache-size", serverOptions.ExternalMetricsCacheSize,
"The maximum number of entries in the external metric cache.")
flags.Parse(os.Args)
klog.Info("serverOptions: ", serverOptions)
if !serverOptions.UseNewResourceModel && serverOptions.FallbackForContainerMetrics {
klog.Fatalf("Container metrics work only with new resource model")
}
if !serverOptions.UseNewResourceModel && serverOptions.EnableCoreMetricsAPI {
klog.Fatalf("Core metrics work only with new resource model")
}
if serverOptions.ListFullCustomMetrics {
klog.Infof("ListFullCustomMetrics is enabled, which would increase memory usage a lot. Please keep it as false, unless have to.")
} else {
klog.Infof("ListFullCustomMetrics is disabled, which would only list 1 metric resource to reduce memory usage. Add --list-full-custom-metrics to list full metric resources for debugging.")
}
// TODO(holubwicz): move duration config to server options
metricsProvider, translator := cmd.makeProviderOrDie(&serverOptions, 5*time.Minute, 1*time.Minute)
if serverOptions.EnableCustomMetricsAPI {
cmd.WithCustomMetrics(metricsProvider)
}
if serverOptions.EnableExternalMetricsAPI {
cmd.WithExternalMetrics(metricsProvider)
}
if serverOptions.EnableCoreMetricsAPI {
if err := cmd.withCoreMetrics(translator); err != nil {
klog.Fatalf("unable to install resource metrics API: %v", err)
}
}
if serverOptions.MetricsAddress != "" {
go runPrometheusMetricsServer(serverOptions.MetricsAddress)
}
if err := cmd.Run(wait.NeverStop); err != nil {
klog.Fatalf("unable to run custom metrics adapter: %v", err)
}
}