Skip to content

Commit

Permalink
Refactor config loading and add error handling in autoscaler API
Browse files Browse the repository at this point in the history
 - Replace `io.Reader` with `os` package for file handling
 - Introduce error variables for config file reading and missing server config
 - Change `PublicApiServer` struct field to `Server`
 - Add `defaultConfig` function for initializing default config values
 - Implement `loadYamlFile` and `loadPublicApiServerConfig` for config loading
 - Modify `LoadConfig` to use new helper functions and handle VCAP configuration
 - Update tests to reflect changes and use `testhelpers.BytesToFile` for file operations
 - Add new default JSON files for catalog, config, and info
 - Update build-extension-file.sh to include `skip_ssl_validation` in parameters
  • Loading branch information
bonzofenix committed Oct 18, 2024
1 parent 26afce8 commit e871698
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 487 deletions.
75 changes: 63 additions & 12 deletions src/autoscaler/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package config
import (
"errors"
"fmt"
"io"
"os"
"strings"
"time"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/cf"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/configutil"

"golang.org/x/crypto/bcrypt"

Expand All @@ -33,6 +34,11 @@ const (
DefaultDiskUpperThreshold = 2 * 1024
)

var (
ErrReadYaml = errors.New("failed to read config file")
ErrPublicApiServerConfigNotFound = errors.New("metricsforwarder config service not found")
)

var defaultBrokerServerConfig = helpers.ServerConfig{
Port: 8080,
}
Expand Down Expand Up @@ -92,7 +98,7 @@ type LowerUpperThresholdConfig struct {
type Config struct {
Logging helpers.LoggingConfig `yaml:"logging"`
BrokerServer helpers.ServerConfig `yaml:"broker_server"`
PublicApiServer helpers.ServerConfig `yaml:"public_api_server"`
Server helpers.ServerConfig `yaml:"public_api_server"`
DB map[string]db.DatabaseConfig `yaml:"db"`
BrokerCredentials []BrokerCredentialsConfig `yaml:"broker_credentials"`
APIClientId string `yaml:"api_client_id"`
Expand All @@ -119,13 +125,15 @@ type PlanCheckConfig struct {
PlanDefinitions map[string]PlanDefinition `yaml:"plan_definitions"`
}

func LoadConfig(reader io.Reader) (*Config, error) {
conf := &Config{
Logging: defaultLoggingConfig,
BrokerServer: defaultBrokerServerConfig,
PublicApiServer: defaultPublicApiServerConfig,
func defaultConfig() Config {
return Config{
Logging: defaultLoggingConfig,
BrokerServer: defaultBrokerServerConfig,
Server: defaultPublicApiServerConfig,
CF: cf.Config{
ClientConfig: cf.ClientConfig{SkipSSLValidation: false},
ClientConfig: cf.ClientConfig{
SkipSSLValidation: false,
},
},
RateLimit: models.RateLimitConfig{
MaxAmount: DefaultMaxAmount,
Expand All @@ -151,17 +159,60 @@ func LoadConfig(reader io.Reader) (*Config, error) {
},
}

dec := yaml.NewDecoder(reader)
dec.KnownFields(true)
err := dec.Decode(conf)
}
func loadYamlFile(filepath string, conf *Config) error {
if filepath == "" {
return nil
}
file, err := os.Open(filepath)
if err != nil {
fmt.Fprintf(os.Stdout, "failed to open config file '%s': %s\n", filepath, err)
return ErrReadYaml
}
defer file.Close()

dec := yaml.NewDecoder(file)
dec.KnownFields(true)
if err := dec.Decode(conf); err != nil {
return fmt.Errorf("%w: %v", ErrReadYaml, err)
}
return nil
}
func loadPublicApiServerConfig(conf *Config, vcapReader configutil.VCAPConfigurationReader) error {
data, err := vcapReader.GetServiceCredentialContent("metricsforwarder-config", "metricsforwarder")
if err != nil {
return fmt.Errorf("%w: %v", ErrPublicApiServerConfigNotFound, err)
}
return yaml.Unmarshal(data, conf)
}

func loadVcapConfig(conf *Config, vcapReader configutil.VCAPConfigurationReader) error {
if !vcapReader.IsRunningOnCF() {
return nil
}

conf.Server.Port = vcapReader.GetPort()
if err := loadPublicApiServerConfig(conf, vcapReader); err != nil {
return err
}

return nil
}

func LoadConfig(filepath string, vcapReader configutil.VCAPConfigurationReader) (*Config, error) {
conf := defaultConfig()

if err := loadYamlFile(filepath, &conf); err != nil {
return nil, err
}

if err := loadVcapConfig(&conf, vcapReader); err != nil {
return nil, err
}

conf.Logging.Level = strings.ToLower(conf.Logging.Level)

return conf, nil
return &conf, nil
}

func (c *Config) Validate() error {
Expand Down
Loading

0 comments on commit e871698

Please sign in to comment.