# HG changeset patch
# User Florent Aide <florent.aide@gmail.com>
# Date 1652429481 -7200
#      Fri May 13 10:11:21 2022 +0200
# Node ID a367b04352c786fc2ec2be54d0dfcb0053d88f58
# Parent  00dd885393fa2f611dd6fe246be70453a90b508b
reorge to allow easier chart segregation

diff --git a/runner/config.go b/runner/config.go
--- a/runner/config.go
+++ b/runner/config.go
@@ -22,16 +22,27 @@
 	Value string `mapstructure:"value"`
 }
 
-type Chart struct {
+type HelmChart struct {
 	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 {
-	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,26 +96,26 @@
 	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()
 		}
-		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
 }
-*/
 
-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
--- 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,6 +24,14 @@
     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,
 	)
+
+	// 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
--- 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