Skip to content

Commit

Permalink
fix: Turns globals.jx-* template variables into valid variable names
Browse files Browse the repository at this point in the history
In #6427 was added template variables to help templating. Unfortunately, template variables names must be valid go variable names to be used. Tags are still snake-cased.

`jx promote` and `jx preview` now pass the following variables to helm:
- `tags.jx-ns-<namespace>=true` (ex: `tags.jx-ns-jx-production=true`)
- `tags.jx-<env-type>=true` (ex: `tags.jx-preview=true`)
- `tags.jx-env-<env>=true` (ex: `tags.jx-env-production=true`)
- `global.jxNs<namespace>=true` (ex: `global.jxNsJxProduction=true`)
- `global.jx<env-type>=true` (ex: `global.jxPreview=true`)
- `global.jxEnv<env>=true` (ex: `global.jxEnvProduction=true`)
- `global.jxNs=<namespace>` (ex: `global.jxNs=jx-production`)
- `global.jxTypeEnv=<env-type>` (ex: `global.jxTypeEnv=preview`)
- `global.jxEnv=<env>` (ex: `global.jxEnv=production`)
- `global.jxPreviewApp=<preview-app>` (ex: `global.jxPreviewApp=my-app`)
- `global.jxPreviewPr=<preview-pr>` (ex: `global.jxPreviewPr=6`)

`jx step helm install` and `jx step helm apply` now pass the following variables to helm:
- `tags.jx-ns-<namespace>=true` (ex: `tags.jx-ns-jx-production=true`)
- `global.jxNs<namespace>=true` (ex: `global.jxNsJxProduction=true`)
- `global.jxNs=<namespace>` (ex: `global.jxNs=jx-production`)

Signed-off-by: Aurélien Lambert <[email protected]>
  • Loading branch information
aure-olli authored and jenkins-x-bot committed Feb 4, 2020
1 parent 84bd200 commit dce9fc7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
20 changes: 10 additions & 10 deletions pkg/cmd/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,24 +1182,24 @@ func (o *PromoteOptions) SearchForChart(filter string) (string, error) {
}

func (o *PromoteOptions) GetEnvChartValues(targetNS string, env *v1.Environment) ([]string, []string) {
kind := strings.ToLower(string(env.Spec.Kind))
kind := string(env.Spec.Kind)
values := []string{
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
fmt.Sprintf("tags.jx-%s=true", kind),
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
fmt.Sprintf("tags.jx-%s=true", strings.ToLower(kind)),
fmt.Sprintf("tags.jx-env-%s=true", env.ObjectMeta.Name),
fmt.Sprintf("global.jx-%s=true", kind),
fmt.Sprintf("global.jx-env-%s=true", env.ObjectMeta.Name),
fmt.Sprintf("global.jx%s=true", kind),
fmt.Sprintf("global.jxEnv%s=true", util.ToCamelCase(env.ObjectMeta.Name)),
}
valueString := []string{
fmt.Sprintf("global.jx-ns=%s", targetNS),
fmt.Sprintf("global.jx-type-env=%s", kind),
fmt.Sprintf("global.jx-env=%s", env.ObjectMeta.Name),
fmt.Sprintf("global.jxNs=%s", targetNS),
fmt.Sprintf("global.jxTypeEnv=%s", strings.ToLower(kind)),
fmt.Sprintf("global.jxEnv=%s", env.ObjectMeta.Name),
}
if env.Spec.Kind == v1.EnvironmentKindTypePreview {
valueString = append(valueString,
fmt.Sprintf("global.jx-preview-app=%s", env.Spec.PreviewGitSpec.ApplicationName),
fmt.Sprintf("global.jx-preview-pr=%s", env.Spec.PreviewGitSpec.Name),
fmt.Sprintf("global.jxPreviewApp=%s", env.Spec.PreviewGitSpec.ApplicationName),
fmt.Sprintf("global.jxPreviewPr=%s", env.Spec.PreviewGitSpec.Name),
)
}
return values, valueString
Expand Down
40 changes: 20 additions & 20 deletions pkg/cmd/promote/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,16 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-preview=true",
"tags.jx-env-test-preview=true",
"tags.jx-ns-jx-test-preview-pr-6=true",
"global.jx-preview=true",
"global.jx-env-test-preview=true",
"global.jx-ns-jx-test-preview-pr-6=true",
"global.jxPreview=true",
"global.jxEnvTestPreview=true",
"global.jxNsJxTestPreviewPr6=true",
},
[]string{
"global.jx-type-env=preview",
"global.jx-env=test-preview",
"global.jx-ns=jx-test-preview-pr-6",
"global.jx-preview-app=my-app",
"global.jx-preview-pr=6",
"global.jxTypeEnv=preview",
"global.jxEnv=test-preview",
"global.jxNs=jx-test-preview-pr-6",
"global.jxPreviewApp=my-app",
"global.jxPreviewPr=6",
},
}, {
"jx-custom-env",
Expand All @@ -422,14 +422,14 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-permanent=true",
"tags.jx-env-custom-env=true",
"tags.jx-ns-jx-custom-env=true",
"global.jx-permanent=true",
"global.jx-env-custom-env=true",
"global.jx-ns-jx-custom-env=true",
"global.jxPermanent=true",
"global.jxEnvCustomEnv=true",
"global.jxNsJxCustomEnv=true",
},
[]string{
"global.jx-type-env=permanent",
"global.jx-env=custom-env",
"global.jx-ns=jx-custom-env",
"global.jxTypeEnv=permanent",
"global.jxEnv=custom-env",
"global.jxNs=jx-custom-env",
},
}, {
"ns-rand",
Expand Down Expand Up @@ -459,14 +459,14 @@ func TestGetEnvChartValues(t *testing.T) {
"tags.jx-edit=true",
"tags.jx-env-random-env=true",
"tags.jx-ns-ns-rand=true",
"global.jx-edit=true",
"global.jx-env-random-env=true",
"global.jx-ns-ns-rand=true",
"global.jxEdit=true",
"global.jxEnvRandomEnv=true",
"global.jxNsNsRand=true",
},
[]string{
"global.jx-type-env=edit",
"global.jx-env=random-env",
"global.jx-ns=ns-rand",
"global.jxTypeEnv=edit",
"global.jxEnv=random-env",
"global.jxNs=ns-rand",
},
}}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/step/helm/step_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ func (o *StepHelmOptions) overwriteProviderValues(requirements *config.Requireme
func (o *StepHelmOptions) getChartValues(targetNS string) ([]string, []string) {
return []string{
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
}, []string{
fmt.Sprintf("global.jx-ns=%s", targetNS),
fmt.Sprintf("global.jxNs=%s", targetNS),
}
}
6 changes: 6 additions & 0 deletions pkg/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,9 @@ func StripTrailingSlash(url string) string {
func StartsWith(s, substr string) bool {
return strings.Index(s, substr) == 0
}

// ToCamelCase turn "my-super-name" into "MySuperName"
// Usefule for creating valid go-template variable names
func ToCamelCase(s string) string {
return strings.Replace(strings.Title(s), "-", "", -1)
}
4 changes: 4 additions & 0 deletions pkg/util/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ func Test_StartsWith(t *testing.T) {
assert.True(t, util.StartsWith("ML-a-machine-learning-quickstart", "ML-"))
assert.False(t, util.StartsWith("not-a-machine-learning-quickstart", "ML-"))
}

func Test_ToCamelCase(t *testing.T) {
assert.Equal(t, util.ToCamelCase("my-super-name"), "MySuperName")
}

0 comments on commit dce9fc7

Please sign in to comment.