Skip to content

Commit

Permalink
Becoming stateless + supporting multiple metrics (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedingber authored Nov 3, 2019
1 parent 368367c commit 18ddecf
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 388 deletions.
49 changes: 5 additions & 44 deletions cmd/autoscaler/app/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,11 @@ import (
func Run(kubeconfigPath string,
namespace string,
scaleInterval time.Duration,
scaleWindow time.Duration,
metricName string,
scaleThreshold int64,
metricsInterval time.Duration,
metricsGroupKind string) error {
autoScalerOptions := scaler_types.AutoScalerOptions{
Namespace: namespace,
ScaleInterval: scaleInterval,
ScaleWindow: scaleWindow,
Threshold: scaleThreshold,
MetricName: metricName,
}

pollerOptions := scaler_types.PollerOptions{
Namespace: namespace,
MetricName: metricName,
MetricInterval: metricsInterval,
GroupKind: metricsGroupKind,
GroupKind: metricsGroupKind,
}

pluginLoader, err := pluginloader.New()
Expand All @@ -56,7 +43,6 @@ func Run(kubeconfigPath string,
if resourceScalerConfig != nil {
kubeconfigPath = resourceScalerConfig.KubeconfigPath
autoScalerOptions = resourceScalerConfig.AutoScalerOptions
pollerOptions = resourceScalerConfig.PollerOptions
}

restConfig, err := getClientConfig(kubeconfigPath)
Expand All @@ -73,15 +59,6 @@ func Run(kubeconfigPath string,
return errors.Wrap(err, "Failed to start scaler")
}

newPoller, err := createPoller(restConfig, newScaler, pollerOptions)
if err != nil {
return errors.Wrap(err, "Failed to create poller")
}

if err := newPoller.Start(); err != nil {
return errors.Wrap(err, "Failed to start poller")
}

select {}
}

Expand All @@ -93,34 +70,18 @@ func createAutoScaler(restConfig *rest.Config,
return nil, errors.Wrap(err, "Failed to initialize root logger")
}

newScaler, err := autoscaler.NewAutoScaler(rootLogger, resourceScaler, options)

if err != nil {
return nil, err
}

return newScaler, nil
}

func createPoller(restConfig *rest.Config,
reporter autoscaler.MetricReporter,
options scaler_types.PollerOptions) (*autoscaler.MetricsPoller, error) {
rootLogger, err := nucliozap.NewNuclioZap("autoscaler", "console", os.Stdout, os.Stderr, nucliozap.DebugLevel)
if err != nil {
return nil, errors.Wrap(err, "Failed to initialize root logger")
}

customMetricsClient, err := custommetricsv1.NewForConfig(restConfig)
if err != nil {
return nil, errors.Wrap(err, "Failed create custom metrics client set")
}

newPoller, err := autoscaler.NewMetricsPoller(rootLogger, reporter, customMetricsClient, options)
newScaler, err := autoscaler.NewAutoScaler(rootLogger, resourceScaler, customMetricsClient, options)

if err != nil {
return nil, errors.Wrap(err, "Failed to create metrics poller")
return nil, err
}

return newPoller, err
return newScaler, nil
}

func getClientConfig(kubeconfigPath string) (*rest.Config, error) {
Expand Down
8 changes: 0 additions & 8 deletions cmd/autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ func main() {
kubeconfigPath := flag.String("kubeconfig-path", os.Getenv("KUBECONFIG"), "Path of kubeconfig file")
namespace := flag.String("namespace", "", "Namespace to listen on, or * for all")
scaleInterval := flag.Duration("scale-interval", time.Minute, "Interval to call check scale function")
scaleWindow := flag.Duration("scale-window", time.Minute, "Time window after initial value to act upon")
metricsInterval := flag.Duration("metrics-poll-interval", time.Minute, "Interval to poll custom metrics")
metricsGroupKind := flag.String("metrics-group-kind", "", "Metrics resource kind")
metricName := flag.String("metric-name", "", "Metric name from custom metrics")
scaleThreshold := flag.Int64("scale-threshold", 0, "Maximum allowed value for metric to be considered below active")
flag.Parse()

*namespace = common.GetNamespace(*namespace)

if err := app.Run(*kubeconfigPath,
*namespace,
*scaleInterval,
*scaleWindow,
*metricName,
*scaleThreshold,
*metricsInterval,
*metricsGroupKind); err != nil {
errors.PrintErrorStack(os.Stderr, err, 5)

Expand Down
Loading

0 comments on commit 18ddecf

Please sign in to comment.