Skip to content

Commit bb4a5e3

Browse files
committed
fix: Turns globals.jx-* template variables into valid variable names
In jenkins-x#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 <aure@olli-ai.com>
1 parent a93d931 commit bb4a5e3

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

pkg/cmd/promote/promote.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1178,24 +1178,25 @@ func (o *PromoteOptions) SearchForChart(filter string) (string, error) {
11781178
}
11791179

11801180
func (o *PromoteOptions) GetEnvChartValues(targetNS string, env *v1.Environment) ([]string, []string) {
1181-
kind := strings.ToLower(string(env.Spec.Kind))
1181+
kind := string(env.Spec.Kind)
1182+
// camel case
11821183
values := []string{
11831184
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
1184-
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
1185-
fmt.Sprintf("tags.jx-%s=true", kind),
1185+
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
1186+
fmt.Sprintf("tags.jx-%s=true", strings.ToLower(kind)),
11861187
fmt.Sprintf("tags.jx-env-%s=true", env.ObjectMeta.Name),
1187-
fmt.Sprintf("global.jx-%s=true", kind),
1188-
fmt.Sprintf("global.jx-env-%s=true", env.ObjectMeta.Name),
1188+
fmt.Sprintf("global.jx%s=true", kind),
1189+
fmt.Sprintf("global.jxEnv%s=true", util.ToCamelCase(env.ObjectMeta.Name)),
11891190
}
11901191
valueString := []string{
1191-
fmt.Sprintf("global.jx-ns=%s", targetNS),
1192-
fmt.Sprintf("global.jx-type-env=%s", kind),
1193-
fmt.Sprintf("global.jx-env=%s", env.ObjectMeta.Name),
1192+
fmt.Sprintf("global.jxNs=%s", targetNS),
1193+
fmt.Sprintf("global.jxTypeEnv=%s", strings.ToLower(kind)),
1194+
fmt.Sprintf("global.jxEnv=%s", env.ObjectMeta.Name),
11941195
}
11951196
if env.Spec.Kind == v1.EnvironmentKindTypePreview {
11961197
valueString = append(valueString,
1197-
fmt.Sprintf("global.jx-preview-app=%s", env.Spec.PreviewGitSpec.ApplicationName),
1198-
fmt.Sprintf("global.jx-preview-pr=%s", env.Spec.PreviewGitSpec.Name),
1198+
fmt.Sprintf("global.jxPreviewApp=%s", env.Spec.PreviewGitSpec.ApplicationName),
1199+
fmt.Sprintf("global.jxPreviewPr=%s", env.Spec.PreviewGitSpec.Name),
11991200
)
12001201
}
12011202
return values, valueString

pkg/cmd/promote/promote_test.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,16 @@ func TestGetEnvChartValues(t *testing.T) {
386386
"tags.jx-preview=true",
387387
"tags.jx-env-test-preview=true",
388388
"tags.jx-ns-jx-test-preview-pr-6=true",
389-
"global.jx-preview=true",
390-
"global.jx-env-test-preview=true",
391-
"global.jx-ns-jx-test-preview-pr-6=true",
389+
"global.jxPreview=true",
390+
"global.jxEnvTestPreview=true",
391+
"global.jxNsJxTestPreviewPr6=true",
392392
},
393393
[]string{
394-
"global.jx-type-env=preview",
395-
"global.jx-env=test-preview",
396-
"global.jx-ns=jx-test-preview-pr-6",
397-
"global.jx-preview-app=my-app",
398-
"global.jx-preview-pr=6",
394+
"global.jxTypeEnv=preview",
395+
"global.jxEnv=test-preview",
396+
"global.jxNs=jx-test-preview-pr-6",
397+
"global.jxPreviewApp=my-app",
398+
"global.jxPreview-pr=6",
399399
},
400400
}, {
401401
"jx-custom-env",
@@ -420,14 +420,14 @@ func TestGetEnvChartValues(t *testing.T) {
420420
"tags.jx-permanent=true",
421421
"tags.jx-env-custom-env=true",
422422
"tags.jx-ns-jx-custom-env=true",
423-
"global.jx-permanent=true",
424-
"global.jx-env-custom-env=true",
425-
"global.jx-ns-jx-custom-env=true",
423+
"global.jxPermanent=true",
424+
"global.jxEnvCustom-env=true",
425+
"global.jxNsJxCustomEnv=true",
426426
},
427427
[]string{
428-
"global.jx-type-env=permanent",
429-
"global.jx-env=custom-env",
430-
"global.jx-ns=jx-custom-env",
428+
"global.jxTypeEnv=permanent",
429+
"global.jxEnv=custom-env",
430+
"global.jxNs=jx-custom-env",
431431
},
432432
}, {
433433
"ns-rand",
@@ -457,14 +457,14 @@ func TestGetEnvChartValues(t *testing.T) {
457457
"tags.jx-edit=true",
458458
"tags.jx-env-random-env=true",
459459
"tags.jx-ns-ns-rand=true",
460-
"global.jx-edit=true",
461-
"global.jx-env-random-env=true",
462-
"global.jx-ns-ns-rand=true",
460+
"global.jxEdit=true",
461+
"global.jxEnvRandomEnv=true",
462+
"global.jxNsNsRand=true",
463463
},
464464
[]string{
465-
"global.jx-type-env=edit",
466-
"global.jx-env=random-env",
467-
"global.jx-ns=ns-rand",
465+
"global.jxTypeEnv=edit",
466+
"global.jxEnv=random-env",
467+
"global.jxNs=ns-rand",
468468
},
469469
}}
470470

pkg/cmd/step/helm/step_helm.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ func (o *StepHelmOptions) overwriteProviderValues(requirements *config.Requireme
391391
func (o *StepHelmOptions) getChartValues(targetNS string) ([]string, []string) {
392392
return []string{
393393
fmt.Sprintf("tags.jx-ns-%s=true", targetNS),
394-
fmt.Sprintf("global.jx-ns-%s=true", targetNS),
394+
fmt.Sprintf("global.jxNs%s=true", util.ToCamelCase(targetNS)),
395395
}, []string{
396-
fmt.Sprintf("global.jx-ns=%s", targetNS),
396+
fmt.Sprintf("global.jxNs=%s", targetNS),
397397
}
398398
}

pkg/util/strings.go

+6
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,9 @@ func StripTrailingSlash(url string) string {
280280
func StartsWith(s, substr string) bool {
281281
return strings.Index(s, substr) == 0
282282
}
283+
284+
// ToCamelCase turn "my-super-name" into "MySuperName"
285+
// Usefule for creating valid go-template variable names
286+
func ToCamelCase(s string) string {
287+
return strings.Replace(strings.Title(s), "-", "", -1)
288+
}

pkg/util/strings_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,7 @@ func Test_StartsWith(t *testing.T) {
156156
assert.True(t, util.StartsWith("ML-a-machine-learning-quickstart", "ML-"))
157157
assert.False(t, util.StartsWith("not-a-machine-learning-quickstart", "ML-"))
158158
}
159+
160+
func Test_ToCamelCase(t *testing.T) {
161+
assert.Equal(t, util.ToCamelCase("my-super-name", "MySuperName"))
162+
}

0 commit comments

Comments
 (0)