Skip to content

Commit 9e24e6a

Browse files
authored
Change initializer for resource scaler (#20)
1 parent 8eece73 commit 9e24e6a

File tree

9 files changed

+51
-11
lines changed

9 files changed

+51
-11
lines changed

cmd/autoscaler/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.12-rc-alpine3.8
1+
FROM golang:1.10-alpine3.7
22

33
RUN apk --update --no-cache add \
44
git \

cmd/autoscaler/app/autoscaler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func Run(kubeconfigPath string,
4040
if err != nil {
4141
return errors.Wrap(err, "Failed to initialize plugin loader")
4242
}
43-
resourceScaler, err := pluginLoader.Load()
43+
44+
resourceScaler, err := pluginLoader.Load(kubeconfigPath, namespace)
4445
if err != nil {
4546
return errors.Wrap(err, "Failed to load plugin")
4647
}

cmd/autoscaler/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/v3io/scaler/cmd/autoscaler/app"
9+
"github.com/v3io/scaler/pkg/common"
910

1011
"github.com/nuclio/errors"
1112
)
@@ -20,6 +21,8 @@ func main() {
2021
scaleThreshold := flag.Int64("scale-threshold", 0, "Maximum allowed value for metric to be considered below active")
2122
flag.Parse()
2223

24+
*namespace = common.GetNamespace(*namespace)
25+
2326
if err := app.Run(*kubeconfigPath,
2427
*namespace,
2528
*scaleInterval,

cmd/dlx/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.12-rc-alpine3.8
1+
FROM golang:1.10-alpine3.7
22

33
RUN apk --update --no-cache add \
44
git \

cmd/dlx/app/dlx.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ import (
1111
"github.com/v3io/scaler-types"
1212
)
1313

14-
func Run(namespace string, targetNameHeader string, targetPathHeader string, targetPort int, listenAddress string) error {
14+
func Run(kubeconfigPath string,
15+
namespace string,
16+
targetNameHeader string,
17+
targetPathHeader string,
18+
targetPort int,
19+
listenAddress string) error {
1520
pluginLoader, err := pluginloader.New()
1621
if err != nil {
1722
return errors.Wrap(err, "Failed to initialize plugin loader")
1823
}
1924

20-
resourceScaler, err := pluginLoader.Load()
25+
resourceScaler, err := pluginLoader.Load(kubeconfigPath, namespace)
2126
if err != nil {
2227
return errors.Wrap(err, "Failed to load plugin")
2328
}

cmd/dlx/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ import (
55
"os"
66

77
"github.com/v3io/scaler/cmd/dlx/app"
8+
"github.com/v3io/scaler/pkg/common"
89

910
"github.com/nuclio/errors"
1011
)
1112

1213
func main() {
14+
kubeconfigPath := flag.String("kubeconfig-path", os.Getenv("KUBECONFIG"), "Path of kubeconfig file")
1315
namespace := flag.String("namepsace", "", "Kubernetes namespace")
1416
targetNameHeader := flag.String("target-name-header", "", "Name of the header that holds information on target name")
1517
targetPathHeader := flag.String("target-path-header", "", "Name of the header that holds information on target path")
1618
targetPort := flag.Int("target-port", 0, "Name of the header that holds information on target port")
1719
listenAddress := flag.String("listen-address", ":8090", "Address to listen upon for http proxy")
1820
flag.Parse()
1921

20-
if err := app.Run(*namespace, *targetNameHeader, *targetPathHeader, *targetPort, *listenAddress); err != nil {
22+
*namespace = common.GetNamespace(*namespace)
23+
24+
if err := app.Run(*kubeconfigPath, *namespace, *targetNameHeader, *targetPathHeader, *targetPort, *listenAddress); err != nil {
2125
errors.PrintErrorStack(os.Stderr, err, 5)
2226

2327
os.Exit(1)

pkg/common/namespaceresolver.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package common
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
)
7+
8+
func GetNamespace(namespaceArgument string) string {
9+
10+
// if the namespace was passed in the arguments, use that
11+
if namespaceArgument != "" {
12+
return namespaceArgument
13+
}
14+
15+
// if the namespace exists in env, use that
16+
if namespaceEnv := os.Getenv("SCALER_NAMESPACE"); namespaceEnv != "" {
17+
return namespaceEnv
18+
}
19+
20+
// get namespace from within the pod. if found, return that
21+
if namespacePod, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
22+
return string(namespacePod)
23+
}
24+
25+
return "default"
26+
}

pkg/pluginloader/loader.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"github.com/v3io/scaler-types"
99
)
1010

11-
type resourceScalerNewFunc func() (scaler_types.ResourceScaler, error)
11+
type resourceScalerNewFunc func(string, string) (scaler_types.ResourceScaler, error)
1212

1313
type PluginLoader struct{}
1414

1515
func New() (*PluginLoader, error) {
1616
return &PluginLoader{}, nil
1717
}
1818

19-
func (p *PluginLoader) Load() (scaler_types.ResourceScaler, error) {
19+
func (p *PluginLoader) Load(kubeconfigPath string, namespace string) (scaler_types.ResourceScaler, error) {
2020
var funcNew resourceScalerNewFunc
2121
var ok bool
2222

@@ -37,11 +37,12 @@ func (p *PluginLoader) Load() (scaler_types.ResourceScaler, error) {
3737
return nil, errors.Wrap(err, "Failed to find New symbol")
3838
}
3939

40-
funcNew, ok = symbol.(func() (scaler_types.ResourceScaler, error))
40+
// don't use resourceScalerNewFunc as the cast type cause of some bug :(
41+
funcNew, ok = symbol.(func(string, string) (scaler_types.ResourceScaler, error))
4142
if !ok {
4243
return nil, errors.New("Failed to cast New function of plugin")
4344
}
4445
}
4546

46-
return funcNew()
47+
return funcNew(kubeconfigPath, namespace)
4748
}

pkg/resourcescaler/resourcescaler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
type NopResourceScaler struct{}
88

9-
func New() (scaler_types.ResourceScaler, error) { // nolint: deadcode
9+
func New(kubeconfigPath string, namespace string) (scaler_types.ResourceScaler, error) { // nolint: deadcode
1010
return &NopResourceScaler{}, nil
1111
}
1212

0 commit comments

Comments
 (0)