Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing all operator configuration using envconfig #401

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions operator/controllers/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import (
"fmt"
"strconv"

"github.com/kedacore/http-add-on/pkg/env"
"github.com/kelseyhightower/envconfig"
)

// Interceptor holds static configuration info for the interceptor
// Interceptor holds static configuration info for the
// interceptor
type Interceptor struct {
ServiceName string `envconfig:"INTERCEPTOR_SERVICE_NAME" required:"true"`
ProxyPort int32 `envconfig:"INTERCEPTOR_PROXY_PORT" required:"true"`
AdminPort int32 `envconfig:"INTERCEPTOR_ADMIN_PORT" required:"true"`
ServiceName string `envconfig:"SERVICE" required:"true"`
ProxyPort int32 `envconfig:"PROXY_PORT" default:"8091"`
AdminPort int32 `envconfig:"ADMIN_PORT" default:"8090"`
}

// ExternalScaler holds static configuration info for the external scaler
// ExternalScaler holds static configuration info for the
// external scaler
type ExternalScaler struct {
ServiceName string `envconfig:"EXTERNAL_SCALER_SERVICE_NAME" required:"true"`
Port int32 `envconfig:"EXTERNAL_SCALER_PORT" required:"true"`
ServiceName string `envconfig:"SERVICE_NAME" required:"true"`
Port int32 `envconfig:"PORT" required:"true"`
}

// Base contains foundational configuration required for the
// operator to function.
type Base struct {
TargetPendingRequests int32 `envconfig:"TARGET_PENDING_REQUESTS" default:"100"`
// The current namespace in which the operator is running.
Expand All @@ -30,6 +33,11 @@ type Base struct {
WatchNamespace string `envconfig:"WATCH_NAMESPACE" default:""`
}

// NewBaseFromEnv parses appropriate environment variables
// and returns a Base struct to match those values.
//
// Returns nil and an appropriate error if any required
// values were missing or malformed.
func NewBaseFromEnv() (*Base, error) {
ret := new(Base)
if err := envconfig.Process(
Expand All @@ -41,6 +49,11 @@ func NewBaseFromEnv() (*Base, error) {
return ret, nil
}

// HostName returns the Kubernetes in-cluster hostname
// qualified with the given namespace.
// For example, if e.ServiceName is "mysvc", e.Port
// is 8080, and you pass "myns" as the namespace parameter,
// this function will return "mysvc.myns:8080"
func (e ExternalScaler) HostName(namespace string) string {
return fmt.Sprintf(
"%s.%s:%d",
Expand All @@ -60,32 +73,23 @@ func (i Interceptor) AdminPortString() string {
// sensible defaults if values were missing.
// and returns the interceptor struct to match. Returns an error if required values were missing.
func NewInterceptorFromEnv() (*Interceptor, error) {
serviceName, err := env.Get("KEDAHTTP_INTERCEPTOR_SERVICE")
if err != nil {
return nil, fmt.Errorf("missing 'KEDAHTTP_INTERCEPTOR_SERVICE'")
ret := &Interceptor{}
if err := envconfig.Process("KEDAHTTP_INTERCEPTOR", ret); err != nil {
return nil, err
}
adminPort := env.GetInt32Or("KEDAHTTP_INTERCEPTOR_ADMIN_PORT", 8090)
proxyPort := env.GetInt32Or("KEDAHTTP_INTERCEPTOR_PROXY_PORT", 8091)

return &Interceptor{
ServiceName: serviceName,
AdminPort: adminPort,
ProxyPort: proxyPort,
}, nil
return ret, nil
}

// NewExternalScalerFromEnv gets external scaler configuration values from environment variables and/or
// sensible defaults if values were missing.
// and returns the interceptor struct to match. Returns an error if required values were missing.
func NewExternalScalerFromEnv() (*ExternalScaler, error) {
// image, err := env.Get("KEDAHTTP_OPERATOR_EXTERNAL_SCALER_IMAGE")
serviceName, err := env.Get("KEDAHTTP_OPERATOR_EXTERNAL_SCALER_SERVICE")
if err != nil {
return nil, fmt.Errorf("missing KEDAHTTP_EXTERNAL_SCALER_SERVICE")
ret := &ExternalScaler{}
if err := envconfig.Process(
"KEDAHTTP_OPERATOR_EXTERNAL_SCALER",
ret,
); err != nil {
return nil, err
}
port := env.GetInt32Or("KEDAHTTP_OPERATOR_EXTERNAL_SCALER_PORT", 8091)
return &ExternalScaler{
ServiceName: serviceName,
Port: port,
}, nil
return ret, nil
}
42 changes: 4 additions & 38 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,17 @@ package env
import (
"fmt"
"os"
"strconv"
)

// GetOr gets the value of the environment variable called envName. If that variable
// is not set, returns otherwise
func GetOr(envName, otherwise string) string {
fromEnv, err := Get(envName)
if err != nil {
return otherwise
}
return fromEnv
}

// Get gets the value of the environment variable called envName. If that variable
// is not set, returns a non-nil error
func Get(envName string) (string, error) {
fromEnv := os.Getenv(envName)
if fromEnv == "" {
return "", fmt.Errorf("Environnment variable %s not found", envName)
return "", fmt.Errorf(
"environnment variable %s not found",
envName,
)
}
return fromEnv, nil
}

// GetInt32Or returns the int32 value of the environment variable called envName.
// If the environment variable is missing or it's not a valid int32, returns otherwise
func GetInt32Or(envName string, otherwise int32) int32 {
strVal, err := Get(envName)
if err != nil {
return otherwise
}
val, err := strconv.ParseInt(strVal, 10, 32)
if err != nil {
return otherwise
}
return int32(val)
}

func GetIntOr(envName string, otherwise int) int {
strVal, err := Get(envName)
if err != nil {
return otherwise
}
val, err := strconv.Atoi(strVal)
if err != nil {
return otherwise
}
return val
}