Skip to content

Commit

Permalink
add availability_zones field to ClusterConfiguration
Browse files Browse the repository at this point in the history
I was also going to use it, but then I noticed that test.Setup does
not validate the test configurations, and then the commit got rather
large as it is.
  • Loading branch information
majewsky committed Oct 11, 2023
1 parent d33ec37 commit 0b9d8c1
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 25 deletions.
5 changes: 5 additions & 0 deletions docs/operators/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Limes logs all quota changes at the domain and project level in an Open Standard
A configuration file in YAML format must be provided that describes things like the set of available backend services and the quota/capacity scraping behavior. A minimal config file could look like this:

```yaml
availability_zones:
- east-1
- west-1
- west-2
services:
- type: compute
- type: network
Expand All @@ -75,6 +79,7 @@ The following fields and sections are supported:
| Field | Required | Description |
| --- | --- | --- |
| `availability_zones` | yes | List of availability zones in this cluster. |
| `catalog_url` | no | URL of Limes API service as it appears in the Keystone service catalog for this cluster. This is only used for version advertisements, and can be omitted if no client relies on the URLs in these version advertisements. |
| `discovery.method` | no | Defines which method to use to discover Keystone domains and projects in this cluster. If not given, the default value is `list`. |
| `discovery.except_domains` | no | May contain a regex. Domains whose names match the regex will not be considered by Limes. |
Expand Down
1 change: 1 addition & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
// as possible
const (
testConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
services:
Expand Down
1 change: 1 addition & 0 deletions internal/api/inconsistencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

const (
inconsistenciesTestConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
services:
Expand Down
1 change: 1 addition & 0 deletions internal/api/removed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
func TestForbidClusterIDHeader(t *testing.T) {
s := test.NewSetup(t,
test.WithConfig(`
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
services:
Expand Down
1 change: 1 addition & 0 deletions internal/collector/capacity_scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

const (
testScanCapacityConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
services:
Expand Down
1 change: 1 addition & 0 deletions internal/collector/keystone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

const (
testKeystoneConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
services:
Expand Down
1 change: 1 addition & 0 deletions internal/collector/ratescrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

const (
testRateScrapeBasicConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
params:
Expand Down
4 changes: 4 additions & 0 deletions internal/collector/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func prepareDomainsAndProjectsForScrape(t *testing.T, s test.Setup) {

const (
testScrapeBasicConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
params:
Expand Down Expand Up @@ -436,6 +437,7 @@ func Test_ScrapeFailure(t *testing.T) {

const (
testScrapeCentralizedConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
params:
Expand Down Expand Up @@ -547,6 +549,7 @@ func Test_ScrapeCentralized(t *testing.T) {

const (
testAutoApprovalConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
params:
Expand Down Expand Up @@ -611,6 +614,7 @@ func Test_AutoApproveInitialQuota(t *testing.T) {

const (
testNoopConfigYAML = `
availability_zones: [ az-one, az-two ]
discovery:
method: --test-static
params:
Expand Down
30 changes: 13 additions & 17 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package core

import (
"fmt"
"os"

"github.com/sapcc/go-api-declarations/limes"
limesrates "github.com/sapcc/go-api-declarations/limes/rates"
Expand All @@ -37,10 +36,11 @@ import (
// cluster. It is instantiated from YAML and then transformed into type
// Cluster during the startup phase.
type ClusterConfiguration struct {
CatalogURL string `yaml:"catalog_url"`
Discovery DiscoveryConfiguration `yaml:"discovery"`
Services []ServiceConfiguration `yaml:"services"`
Capacitors []CapacitorConfiguration `yaml:"capacitors"`
AvailabilityZones []string `yaml:"availability_zones"`
CatalogURL string `yaml:"catalog_url"`
Discovery DiscoveryConfiguration `yaml:"discovery"`
Services []ServiceConfiguration `yaml:"services"`
Capacitors []CapacitorConfiguration `yaml:"capacitors"`
//^ Sorry for the stupid pun. Not.
Subresources map[string][]string `yaml:"subresources"`
Subcapacities map[string][]string `yaml:"subcapacities"`
Expand Down Expand Up @@ -158,20 +158,13 @@ func (c QuotaDistributionConfiguration) InitialProjectQuota() uint64 {
}
}

// NewConfiguration reads and validates the given configuration file.
// Errors are logged and will result in
// program termination, causing the function to not return.
func NewConfiguration(path string) (cluster *Cluster, errs errext.ErrorSet) {
//read config file
configBytes, err := os.ReadFile(path)
if err != nil {
errs.Addf("read configuration file: %s", err.Error())
return nil, errs
}
// NewClusterFromYAML reads and validates the configuration in the given YAML document.
// Errors are logged and will result in program termination, causing the function to not return.
func NewClusterFromYAML(configBytes []byte) (cluster *Cluster, errs errext.ErrorSet) {
var config ClusterConfiguration
err = yaml.UnmarshalStrict(configBytes, &config)
err := yaml.UnmarshalStrict(configBytes, &config)
if err != nil {
errs.Addf("parse configuration: %s", err.Error())
errs.Addf("parse configuration: %w", err)
return nil, errs
}

Expand All @@ -196,6 +189,9 @@ func (cluster ClusterConfiguration) validateConfig() (errs errext.ErrorSet) {
errs.Addf("missing configuration value: %s", key)
}

if len(cluster.AvailabilityZones) == 0 {
missing("availability_zones[]")
}
if len(cluster.Services) == 0 {
missing("services[]")
}
Expand Down
8 changes: 1 addition & 7 deletions internal/test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,7 @@ func initDatabase(t *testing.T, fixtureFile string) *gorp.DbMap {
}

func initCluster(t *testing.T, configYAML string) *core.Cluster {
var cfg core.ClusterConfiguration
err := yaml.UnmarshalStrict([]byte(configYAML), &cfg)
if err != nil {
t.Fatal(err)
}

cluster, errs := core.NewCluster(cfg)
cluster, errs := core.NewClusterFromYAML([]byte(configYAML))
if errs.IsEmpty() {
errs = cluster.Connect(nil, gophercloud.EndpointOpts{})
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func main() {
}

//load configuration and connect to cluster
cluster, errs := core.NewConfiguration(configPath)
cluster, errs := core.NewClusterFromYAML(must.Return(os.ReadFile(configPath)))
errs.LogFatalIfError()
errs = cluster.Connect(provider, eo)
errs.LogFatalIfError()
Expand Down

0 comments on commit 0b9d8c1

Please sign in to comment.