Skip to content

Commit

Permalink
Sets bindingDB and policyDB from vcap services
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Oct 18, 2024
1 parent 92cfb07 commit 61ab5c4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 26 deletions.
48 changes: 45 additions & 3 deletions src/autoscaler/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type Config struct {
Logging helpers.LoggingConfig `yaml:"logging"`
BrokerServer helpers.ServerConfig `yaml:"broker_server"`
Server helpers.ServerConfig `yaml:"public_api_server"`
DB map[string]db.DatabaseConfig `yaml:"db"`
Db map[string]db.DatabaseConfig `yaml:"db"`
BrokerCredentials []BrokerCredentialsConfig `yaml:"broker_credentials"`
APIClientId string `yaml:"api_client_id"`
PlanCheck *PlanCheckConfig `yaml:"plan_check"`
Expand Down Expand Up @@ -196,6 +196,48 @@ func loadVcapConfig(conf *Config, vcapReader configutil.VCAPConfigurationReader)
return err
}

if conf.Db == nil {
conf.Db = make(map[string]db.DatabaseConfig)
}

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

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

func configurePolicyDb(conf *Config, vcapReader configutil.VCAPConfigurationReader) error {
currentPolicyDb, ok := conf.Db[db.PolicyDb]
if !ok {
conf.Db[db.PolicyDb] = db.DatabaseConfig{}
}

dbURL, err := vcapReader.MaterializeDBFromService(db.PolicyDb)
currentPolicyDb.URL = dbURL
if err != nil {
return err
}
conf.Db[db.PolicyDb] = currentPolicyDb
return nil
}

func configureBindingDb(conf *Config, vcapReader configutil.VCAPConfigurationReader) error {
currentBindingDb, ok := conf.Db[db.BindingDb]
if !ok {
conf.Db[db.BindingDb] = db.DatabaseConfig{}
}

dbURL, err := vcapReader.MaterializeDBFromService(db.BindingDb)
currentBindingDb.URL = dbURL
if err != nil {
return err
}
conf.Db[db.BindingDb] = currentBindingDb

return nil
}

Expand All @@ -221,7 +263,7 @@ func (c *Config) Validate() error {
return err
}

if c.DB[db.PolicyDb].URL == "" {
if c.Db[db.PolicyDb].URL == "" {
return fmt.Errorf("Configuration error: PolicyDB URL is empty")
}
if c.Scheduler.SchedulerURL == "" {
Expand Down Expand Up @@ -261,7 +303,7 @@ func (c *Config) Validate() error {
return fmt.Errorf("Configuration error: ScalingRules.CPU.UpperThreshold is less than zero")
}

if c.DB[db.BindingDb].URL == "" {
if c.Db[db.BindingDb].URL == "" {
return fmt.Errorf("Configuration error: BindingDB URL is empty")
}

Expand Down
46 changes: 37 additions & 9 deletions src/autoscaler/api/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ var _ = Describe("Config", func() {
Expect(err).To(MatchError(MatchRegexp("publicapiserver config service not found")))
})
})

When("VCAP_SERVICES has relational db service bind to app for policy db", func() {
BeforeEach(func() {
mockVCAPConfigurationReader.GetServiceCredentialContentReturns([]byte(`{ "cred_helper_impl": "default" }`), nil) // #nosec G101
expectedDbUrl = "postgres://foo:bar@postgres.example.com:5432/policy_db?sslcert=%2Ftmp%2Fclient_cert.sslcert&sslkey=%2Ftmp%2Fclient_key.sslkey&sslrootcert=%2Ftmp%2Fserver_ca.sslrootcert" // #nosec G101
})

It("loads the db config from VCAP_SERVICES successfully", func() {
Expect(err).NotTo(HaveOccurred())
Expect(conf.Db[db.PolicyDb].URL).To(Equal(expectedDbUrl))
actualDbName := mockVCAPConfigurationReader.MaterializeDBFromServiceArgsForCall(0)
Expect(actualDbName).To(Equal(db.PolicyDb))
})
})

When("VCAP_SERVICES has relational db service bind to app for binding db", func() {
BeforeEach(func() {
mockVCAPConfigurationReader.GetServiceCredentialContentReturns([]byte(`{ "cred_helper_impl": "default" }`), nil) // #nosec G101
expectedDbUrl = "postgres://foo:bar@postgres.example.com:5432/binding_db?sslcert=%2Ftmp%2Fclient_cert.sslcert&sslkey=%2Ftmp%2Fclient_key.sslkey&sslrootcert=%2Ftmp%2Fserver_ca.sslrootcert" // #nosec G101
})

It("loads the db config from VCAP_SERVICES successfully", func() {
Expect(err).NotTo(HaveOccurred())
Expect(conf.Db[db.BindingDb].URL).To(Equal(expectedDbUrl))
actualDbName := mockVCAPConfigurationReader.MaterializeDBFromServiceArgsForCall(1)
Expect(actualDbName).To(Equal(db.BindingDb))
})
})
})

When("config is read from file", func() {
Expand Down Expand Up @@ -105,14 +133,14 @@ var _ = Describe("Config", func() {
CertFile: "/var/vcap/jobs/autoscaler/config/certs/api.crt",
},
))
Expect(conf.DB[db.BindingDb]).To(Equal(
Expect(conf.Db[db.BindingDb]).To(Equal(
db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 10,
MaxIdleConnections: 5,
ConnectionMaxLifetime: 60 * time.Second,
}))
Expect(conf.DB[db.PolicyDb]).To(Equal(
Expect(conf.Db[db.PolicyDb]).To(Equal(
db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 10,
Expand Down Expand Up @@ -175,14 +203,14 @@ var _ = Describe("Config", func() {
Expect(conf.Logging.Level).To(Equal("info"))
Expect(conf.BrokerServer.Port).To(Equal(8080))
Expect(conf.Server.Port).To(Equal(8081))
Expect(conf.DB[db.BindingDb]).To(Equal(
Expect(conf.Db[db.BindingDb]).To(Equal(
db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 0,
MaxIdleConnections: 0,
ConnectionMaxLifetime: 0 * time.Second,
}))
Expect(conf.DB[db.PolicyDb]).To(Equal(
Expect(conf.Db[db.PolicyDb]).To(Equal(
db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 0,
Expand Down Expand Up @@ -230,14 +258,14 @@ rate_limit:
Describe("Validate", func() {
BeforeEach(func() {
conf = &Config{}
conf.DB = make(map[string]db.DatabaseConfig)
conf.DB[db.BindingDb] = db.DatabaseConfig{
conf.Db = make(map[string]db.DatabaseConfig)
conf.Db[db.BindingDb] = db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 10,
MaxIdleConnections: 5,
ConnectionMaxLifetime: 60 * time.Second,
}
conf.DB[db.PolicyDb] = db.DatabaseConfig{
conf.Db[db.PolicyDb] = db.DatabaseConfig{
URL: "postgres://postgres:postgres@localhost/autoscaler?sslmode=disable",
MaxOpenConnections: 10,
MaxIdleConnections: 5,
Expand Down Expand Up @@ -285,7 +313,7 @@ rate_limit:

Context("when bindingdb url is not set", func() {
BeforeEach(func() {
conf.DB[db.BindingDb] = db.DatabaseConfig{URL: ""}
conf.Db[db.BindingDb] = db.DatabaseConfig{URL: ""}
})
It("should err", func() {
Expect(err).To(MatchError(MatchRegexp("Configuration error: BindingDB URL is empty")))
Expand All @@ -294,7 +322,7 @@ rate_limit:

Context("when policydb url is not set", func() {
BeforeEach(func() {
conf.DB[db.PolicyDb] = db.DatabaseConfig{URL: ""}
conf.Db[db.PolicyDb] = db.DatabaseConfig{URL: ""}
})
It("should err", func() {
Expect(err).To(MatchError(MatchRegexp("Configuration error: PolicyDB URL is empty")))
Expand Down
14 changes: 0 additions & 14 deletions src/autoscaler/api/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@
"port": 1080
}
},
"db": {
"policy_db": {
"url": "REPLACEME_WITH_SERVICE",
"max_open_connections": 100,
"max_idle_connections": 10,
"connection_max_lifetime": "60s"
},
"binding_db": {
"url": "REPLACEME_WITH_SERVICE",
"max_open_connections": 20,
"max_idle_connections": 10,
"connection_max_lifetime": "60s"
}
},
"scaling_engine": {
"scaling_engine_url": "https://autoscaler-3119.scalingengine.service.cf.internal:6104",
"tls": {
Expand Down

0 comments on commit 61ab5c4

Please sign in to comment.