Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Switch to BOSH manifest style variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Aug 31, 2018
1 parent 91eee25 commit 4c3f655
Show file tree
Hide file tree
Showing 28 changed files with 466 additions and 369 deletions.
2 changes: 1 addition & 1 deletion app/fissile.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ func (f *Fissile) GenerateKube(roleManifestPath string, defaultFiles []string, s

cvs := model.MakeMapOfVariables(settings.RoleManifest)
for key, value := range cvs {
if !value.Secret {
if !value.CVOptions.Secret {
delete(cvs, key)
}
}
Expand Down
16 changes: 8 additions & 8 deletions kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func getEnvVars(role *model.InstanceGroup, settings ExportSettings) (helm.Node,
return getEnvVarsFromConfigs(configs, settings)
}

func getEnvVarsFromConfigs(configs model.ConfigurationVariableSlice, settings ExportSettings) (helm.Node, error) {
func getEnvVarsFromConfigs(configs model.Variables, settings ExportSettings) (helm.Node, error) {
sizingCountRegexp := regexp.MustCompile("^KUBE_SIZING_([A-Z][A-Z_]*)_COUNT$")
sizingPortsRegexp := regexp.MustCompile("^KUBE_SIZING_([A-Z][A-Z_]*)_PORTS_([A-Z][A-Z_]*)_(MIN|MAX)$")

Expand All @@ -344,7 +344,7 @@ func getEnvVarsFromConfigs(configs model.ConfigurationVariableSlice, settings Ex
if role == nil {
return nil, fmt.Errorf("Role %s for %s not found", roleName, config.Name)
}
if config.Secret {
if config.CVOptions.Secret {
return nil, fmt.Errorf("%s must not be a secret variable", config.Name)
}
if settings.CreateHelmChart {
Expand All @@ -366,7 +366,7 @@ func getEnvVarsFromConfigs(configs model.ConfigurationVariableSlice, settings Ex
if role == nil {
return nil, fmt.Errorf("Role %s for %s not found", roleName, config.Name)
}
if config.Secret {
if config.CVOptions.Secret {
return nil, fmt.Errorf("%s must not be a secret variable", config.Name)
}

Expand Down Expand Up @@ -434,14 +434,14 @@ func getEnvVarsFromConfigs(configs model.ConfigurationVariableSlice, settings Ex
continue
}

if config.Secret {
if config.CVOptions.Secret {
if !settings.CreateHelmChart {
env = append(env, makeSecretVar(config.Name, false))
} else {
if config.Immutable && config.Generator != nil {
if config.CVOptions.Immutable && config.Type != "" {
// Users cannot override immutable secrets that are generated
env = append(env, makeSecretVar(config.Name, true))
} else if config.Generator == nil {
} else if config.Type == "" {
env = append(env, makeSecretVar(config.Name, false))
} else {
// Generated secrets can be overridden by the user (unless immutable)
Expand All @@ -456,9 +456,9 @@ func getEnvVarsFromConfigs(configs model.ConfigurationVariableSlice, settings Ex
}

var stringifiedValue string
if settings.CreateHelmChart && config.Type == model.CVTypeUser {
if settings.CreateHelmChart && config.CVOptions.Type == model.CVTypeUser {
required := `""`
if config.Required {
if config.CVOptions.Required {
required = fmt.Sprintf(`{{fail "env.%s has not been set"}}`, config.Name)
}
name := ".Values.env." + config.Name
Expand Down
117 changes: 63 additions & 54 deletions kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ func podTemplateTestLoadRole(assert *assert.Assertions) *model.InstanceGroup {
}

// Force a broadcast SECRET_VAR into the manifest, to see in all roles
manifest.Configuration.Variables =
append(manifest.Configuration.Variables,
&model.ConfigurationVariable{
Name: "SECRET_VAR",
manifest.Variables = append(manifest.Variables,
&model.VariableDefinition{
Name: "SECRET_VAR",
CVOptions: model.CVOptions{
Type: model.CVTypeUser,
Secret: true,
Internal: true,
})
},
})
return instanceGroup
}

Expand Down Expand Up @@ -390,8 +391,8 @@ func TestPodGetEnvVarsFromConfigSizingCountKube(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_COUNT",
},
}, ExportSettings{
Expand Down Expand Up @@ -427,8 +428,8 @@ func TestPodGetEnvVarsFromConfigSizingCountHelm(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_COUNT",
},
}, ExportSettings{
Expand Down Expand Up @@ -464,11 +465,11 @@ func TestPodGetEnvVarsFromConfigSizingPortsKube(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_PORTS_STORE_MIN",
},
&model.ConfigurationVariable{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_PORTS_STORE_MAX",
},
}, ExportSettings{
Expand Down Expand Up @@ -511,11 +512,11 @@ func TestPodGetEnvVarsFromConfigSizingPortsHelm(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_PORTS_STORE_MIN",
},
&model.ConfigurationVariable{
&model.VariableDefinition{
Name: "KUBE_SIZING_FOO_PORTS_STORE_MAX",
},
}, ExportSettings{
Expand Down Expand Up @@ -562,8 +563,8 @@ func TestPodGetEnvVarsFromConfigGenerationCounterKube(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SECRETS_GENERATION_COUNTER",
},
}, ExportSettings{
Expand Down Expand Up @@ -594,8 +595,8 @@ func TestPodGetEnvVarsFromConfigGenerationCounterHelm(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SECRETS_GENERATION_COUNTER",
},
}, ExportSettings{
Expand Down Expand Up @@ -631,8 +632,8 @@ func TestPodGetEnvVarsFromConfigGenerationNameKube(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SECRETS_GENERATION_NAME",
},
}, ExportSettings{
Expand Down Expand Up @@ -663,8 +664,8 @@ func TestPodGetEnvVarsFromConfigGenerationNameHelm(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "KUBE_SECRETS_GENERATION_NAME",
},
}, ExportSettings{
Expand Down Expand Up @@ -700,10 +701,12 @@ func TestPodGetEnvVarsFromConfigGenerationNameHelm(t *testing.T) {
func TestPodGetEnvVarsFromConfigSecretsKube(t *testing.T) {
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
Name: "A_SECRET",
Secret: true,
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "A_SECRET",
CVOptions: model.CVOptions{
Secret: true,
},
},
}, ExportSettings{
RoleManifest: &model.RoleManifest{
Expand Down Expand Up @@ -749,10 +752,12 @@ func TestPodGetEnvVarsFromConfigSecretsHelm(t *testing.T) {

t.Run("Plain", func(t *testing.T) {
t.Parallel()
ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
Name: "A_SECRET",
Secret: true,
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "A_SECRET",
CVOptions: model.CVOptions{
Secret: true,
},
},
}, settings)
if !assert.NoError(err) {
Expand All @@ -779,14 +784,12 @@ func TestPodGetEnvVarsFromConfigSecretsHelm(t *testing.T) {

t.Run("Generated", func(t *testing.T) {
t.Parallel()
cv := []*model.ConfigurationVariable{
&model.ConfigurationVariable{
Name: "A_SECRET",
Secret: true,
Generator: &model.ConfigurationVariableGenerator{
ID: "no",
Type: model.GeneratorTypePassword,
ValueType: "foo-login",
cv := model.Variables{
&model.VariableDefinition{
Name: "A_SECRET",
Type: "password",
CVOptions: model.CVOptions{
Secret: true,
},
},
}
Expand Down Expand Up @@ -846,7 +849,7 @@ func TestPodGetEnvVarsFromConfigSecretsHelm(t *testing.T) {
`, actual)
})

cv[0].Immutable = true
cv[0].CVOptions.Immutable = true
ev, err = getEnvVarsFromConfigs(cv, settings)
if !assert.NoError(err) {
return
Expand Down Expand Up @@ -888,10 +891,12 @@ func TestPodGetEnvVarsFromConfigNonSecretKube(t *testing.T) {

t.Run("Present", func(t *testing.T) {
t.Parallel()
ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
Name: "SOMETHING",
Default: []string{"or", "other"},
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "SOMETHING",
CVOptions: model.CVOptions{
Default: []string{"or", "other"},
},
},
}, settings)

Expand All @@ -912,8 +917,8 @@ func TestPodGetEnvVarsFromConfigNonSecretKube(t *testing.T) {

t.Run("Missing", func(t *testing.T) {
t.Parallel()
ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "SOMETHING",
},
}, settings)
Expand All @@ -935,10 +940,12 @@ func TestPodGetEnvVarsFromConfigNonSecretHelmUserOptional(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "SOMETHING",
Type: model.CVTypeUser,
CVOptions: model.CVOptions{
Type: model.CVTypeUser,
},
},
}, ExportSettings{
CreateHelmChart: true,
Expand Down Expand Up @@ -1000,11 +1007,13 @@ func TestPodGetEnvVarsFromConfigNonSecretHelmUserRequired(t *testing.T) {
t.Parallel()
assert := assert.New(t)

ev, err := getEnvVarsFromConfigs([]*model.ConfigurationVariable{
&model.ConfigurationVariable{
Name: "SOMETHING",
Type: model.CVTypeUser,
Required: true,
ev, err := getEnvVarsFromConfigs(model.Variables{
&model.VariableDefinition{
Name: "SOMETHING",
CVOptions: model.CVOptions{
Type: model.CVTypeUser,
Required: true,
},
},
}, ExportSettings{
CreateHelmChart: true,
Expand Down
16 changes: 8 additions & 8 deletions kube/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ func MakeSecrets(secrets model.CVMap, settings ExportSettings) (helm.Node, error
for name, cv := range secrets {
key := util.ConvertNameToKey(name)
var value interface{}
comment := cv.Description
comment := cv.CVOptions.Description

if settings.CreateHelmChart {
if cv.Generator == nil {
if cv.Immutable {
if cv.Type == "" {
if cv.CVOptions.Immutable {
comment += "\nThis value is immutable and must not be changed once set."
}
comment += formattedExample(cv.Example, value)
comment += formattedExample(cv.CVOptions.Example, value)
required := `{{"" | b64enc | quote}}`
if cv.Required {
if cv.CVOptions.Required {
required = fmt.Sprintf(`{{fail "secrets.%s has not been set"}}`, cv.Name)
}
name := ".Values.secrets." + cv.Name
tmpl := `{{if ne (typeOf %s) "<nil>"}}{{if has (kindOf %s) (list "map" "slice")}}` +
`{{%s | toJson | b64enc | quote}}{{else}}{{%s | b64enc | quote}}{{end}}{{else}}%s{{end}}`
value = fmt.Sprintf(tmpl, name, name, name, name, required)
data.Add(key, helm.NewNode(value, helm.Comment(comment)))
} else if !cv.Immutable {
comment += formattedExample(cv.Example, value)
} else if !cv.CVOptions.Immutable {
comment += formattedExample(cv.CVOptions.Example, value)
comment += "\nThis value uses a generated default."
value = fmt.Sprintf(`{{ default "" .Values.secrets.%s | b64enc | quote }}`, cv.Name)
generated.Add(key, helm.NewNode(value, helm.Comment(comment)))
Expand All @@ -48,7 +48,7 @@ func MakeSecrets(secrets model.CVMap, settings ExportSettings) (helm.Node, error
value = ""
}
value = base64.StdEncoding.EncodeToString([]byte(value))
comment += formattedExample(cv.Example, value)
comment += formattedExample(cv.CVOptions.Example, value)
data.Add(key, helm.NewNode(value, helm.Comment(comment)))
}
}
Expand Down
Loading

0 comments on commit 4c3f655

Please sign in to comment.