diff --git a/runner/config.go b/runner/config.go index 00dd885393fa2f611dd6fe246be70453a90b508b_cnVubmVyL2NvbmZpZy5nbw==..a367b04352c786fc2ec2be54d0dfcb0053d88f58_cnVubmVyL2NvbmZpZy5nbw== 100644 --- a/runner/config.go +++ b/runner/config.go @@ -22,9 +22,9 @@ Value string `mapstructure:"value"` } -type Chart struct { +type HelmChart struct { Type string `mapstructure:"type"` Name string `mapstructure:"name"` Values interface{} `mapstructure:"values"` } @@ -26,7 +26,18 @@ Type string `mapstructure:"type"` Name string `mapstructure:"name"` Values interface{} `mapstructure:"values"` } +type YttChart struct { + Type string `mapstructure:"type"` + Name string `mapstructure:"name"` + Values []Value `mapstructure:"values"` +} + +type Charts struct { + Helm map[string]HelmChart `mapstructure:"helm"` + Ytt map[string]YttChart `mapstructure:"ytt"` +} + // Spec ... type Spec struct { @@ -31,7 +42,7 @@ // Spec ... type Spec struct { - Variables []Variable `mapstructure:"variables"` - Charts map[string]Chart `mapstructure:"charts"` + Variables []Variable `mapstructure:"variables"` + Charts Charts `mapstructure:"charts"` } // Config is the configuration we get after parsing our beaver.yml file @@ -67,7 +78,10 @@ // hydrate expands templated variables in our config with concrete values func (c *Config) hydrate() error { - if err := c.hydrateCharts(); err != nil { + if err := c.hydrateHelmCharts(); err != nil { + return err + } + if err := c.hydrateYttCharts(); err != nil { return err } return nil @@ -82,10 +96,17 @@ return variables } -/* -func (c *Config) hydrateYtt() error { - for index, entry := range c.Spec.Ytt { - valueTmpl, err := template.New("entry").Parse(entry.Value) - if err != nil { - return fmt.Errorf("failed to parse ytt entry value as template: %q, %w", entry.Value, err) +func (c *Config) hydrateYttCharts() error { + for entryFileName, entry := range c.Spec.Charts.Ytt { + for valIndex, val := range entry.Values { + valueTmpl, err := template.New("ytt entry value").Parse(val.Value) + if err != nil { + return fmt.Errorf("failed to parse ytt entry value as template: %q, %w", val.Value, err) + } + buf := new(bytes.Buffer) + if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil { + return fmt.Errorf("failed to hydrate ytt entry: %q, %w", val.Value, err) + } + // replace original content with hydrated version + c.Spec.Charts.Ytt[entryFileName].Values[valIndex].Value = buf.String() } @@ -91,10 +112,4 @@ } - buf := new(bytes.Buffer) - if err := valueTmpl.Execute(buf, c.prepareVariables(c.Spec.Variables)); err != nil { - return fmt.Errorf("failed to hydrate ytt entry: %q, %w", entry.Value, err) - } - // replace original content with hydrated version - c.Spec.Ytt[index].Value = buf.String() } return nil } @@ -98,5 +113,4 @@ } return nil } -*/ @@ -102,6 +116,6 @@ -func (c *Config) hydrateCharts() error { - for name, chart := range c.Spec.Charts { +func (c *Config) hydrateHelmCharts() error { + for name, chart := range c.Spec.Charts.Helm { rawChartValues, err := yaml.Marshal(chart.Values) if err != nil { return fmt.Errorf("failed to get chart values as string: %w", err) @@ -117,7 +131,7 @@ // replace original content with hydrated version hydratedChart := chart hydratedChart.Values = buf.String() - c.Spec.Charts[name] = hydratedChart + c.Spec.Charts.Helm[name] = hydratedChart } return nil } diff --git a/runner/config_test.go b/runner/config_test.go index 00dd885393fa2f611dd6fe246be70453a90b508b_cnVubmVyL2NvbmZpZ190ZXN0Lmdv..a367b04352c786fc2ec2be54d0dfcb0053d88f58_cnVubmVyL2NvbmZpZ190ZXN0Lmdv 100644 --- a/runner/config_test.go +++ b/runner/config_test.go @@ -11,7 +11,8 @@ func TestConfig(t *testing.T) { logger := testutils.GetLogger(t) configDir := "fixtures/" - config, err := NewConfig(logger, configDir, "ns1") + testNS := "ns1" + config, err := NewConfig(logger, configDir, testNS) require.NoError(t, err) // first config.spec.variables entry name should be VAULT_KV in our test file assert.Equal(t, "VAULT_KV", config.Spec.Variables[0].Name) @@ -23,5 +24,5 @@ password: <path:cnpp.k8s.cloudcrane.io/data/ns1/postgres#password> fullnameoverride: pg-exporter-ns1 `, - config.Spec.Charts["postgres"].Values, + config.Spec.Charts.Helm["postgres"].Values, ) @@ -27,2 +28,10 @@ ) + + // verify ytt entries + assert.Equal(t, "cnpp.k8s.cloudcrane.io", config.Spec.Charts.Ytt["odoo"].Values[0].Value) + assert.Equal(t, testNS, config.Spec.Charts.Ytt["odoo"].Values[1].Value) + + // yaml support should let toto entry use DEFAULT_VALUES defined in odoo + assert.Equal(t, "cnpp.k8s.cloudcrane.io", config.Spec.Charts.Ytt["toto"].Values[0].Value) + assert.Equal(t, testNS, config.Spec.Charts.Ytt["toto"].Values[1].Value) } diff --git a/runner/fixtures/beaver.yml b/runner/fixtures/beaver.yml index 00dd885393fa2f611dd6fe246be70453a90b508b_cnVubmVyL2ZpeHR1cmVzL2JlYXZlci55bWw=..a367b04352c786fc2ec2be54d0dfcb0053d88f58_cnVubmVyL2ZpeHR1cmVzL2JlYXZlci55bWw= 100644 --- a/runner/fixtures/beaver.yml +++ b/runner/fixtures/beaver.yml @@ -5,23 +5,22 @@ - name: VAULT_KV value: cnpp.k8s.cloudcrane.io charts: - postgres: - type: helm - name: postgresql - values: - config: - datasource: - password: "<path:{{.VAULT_KV}}/data/{{.namespace}}/postgres#password>" - fullnameOverride: "pg-exporter-{{.namespace}}" - odoo: - type: ytt - name: odoo - values: &DEFAULT_VALUES - - key: vault.kv - value: "{{.VAULT_KV}}" - - key: namespace - value: "{{.namespace}}" - toto: - type: ytt - name: odoo - values: *DEFAULT_VALUES + helm: + postgres: + name: postgresql + values: + config: + datasource: + password: "<path:{{.VAULT_KV}}/data/{{.namespace}}/postgres#password>" + fullnameOverride: "pg-exporter-{{.namespace}}" + ytt: + odoo: + name: odoo + values: &DEFAULT_VALUES + - key: vault.kv + value: "{{.VAULT_KV}}" + - key: namespace + value: "{{.namespace}}" + toto: + name: toto + values: *DEFAULT_VALUES