Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass tags and global chart variables when deploying an application #6427

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pkg/cmd/add/add_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func TestAddAppWithSecrets(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())).
Expand Down Expand Up @@ -350,6 +351,7 @@ func TestAddAppWithSecrets(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -436,6 +438,7 @@ func TestAddAppWithDefaults(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())).
Expand Down Expand Up @@ -480,6 +483,7 @@ func TestAddAppWithDefaults(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -715,6 +719,7 @@ func TestAddApp(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -786,6 +791,7 @@ func TestAddAppWithShortName(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -845,6 +851,7 @@ func TestAddAppFromPath(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyString(),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -906,6 +913,7 @@ func TestAddLatestApp(t *testing.T) {
pegomock.AnyBool(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.AnyStringSlice(),
pegomock.EqString(kube.DefaultChartMuseumURL),
pegomock.AnyString(),
pegomock.AnyString())
Expand Down Expand Up @@ -1231,16 +1239,16 @@ func TestAddAppIncludingConditionalQuestionsForGitOps(t *testing.T) {
"description": "test values.yaml",
"type": "object",
"properties": {
"enablePersistentStorage": {
"enablePersistentStorage": {
"type": "boolean"
}
},
"if": {
"properties": { "enablePersistentStorage": { "const": "true", "type": "boolean" } }
},
"then": {
"properties": { "databaseConnectionUrl": { "type": "string" },
"databaseUsername": { "type": "string"},
"properties": { "databaseConnectionUrl": { "type": "string" },
"databaseUsername": { "type": "string"},
"databasePassword": { "type": "string", "format" : "password"} }
}}`),
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,14 @@ func (o *PreviewOptions) Run() error {
return err
}

setValues, setStrings := o.GetEnvChartValues(o.Namespace, env)

helmOptions := helm.InstallChartOptions{
Chart: ".",
ReleaseName: o.ReleaseName,
Ns: o.Namespace,
SetValues: setValues,
SetStrings: setStrings,
ValueFiles: []string{configFileName},
Wait: true,
}
Expand Down
31 changes: 30 additions & 1 deletion pkg/cmd/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var (
`)

promote_example = templates.Examples(`
# Promote a version of the current application to staging
# Promote a version of the current application to staging
# discovering the application name from the source code
jx promote --version 1.2.3 --env staging

Expand Down Expand Up @@ -504,14 +504,19 @@ func (o *PromoteOptions) Promote(targetNS string, env *v1.Environment, warnIfAut
}
promoteKey.OnPromoteUpdate(kubeClient, jxClient, o.Namespace, startPromote)

setValues, setStrings := o.GetEnvChartValues(o.Namespace, env)

helmOptions := helm.InstallChartOptions{
Chart: fullAppName,
ReleaseName: releaseName,
Ns: targetNS,
Version: version,
SetValues: setValues,
SetStrings: setStrings,
NoForce: true,
Wait: true,
}

err = o.InstallChartWithOptions(helmOptions)
if err == nil {
err = o.CommentOnIssues(targetNS, env, promoteKey)
Expand Down Expand Up @@ -1171,3 +1176,27 @@ func (o *PromoteOptions) SearchForChart(filter string) (string, error) {
o.HelmRepositoryURL = repoUrl
return appName, nil
}

func (o *PromoteOptions) GetEnvChartValues(targetNS string, env *v1.Environment) ([]string, []string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method PromoteOptions.GetEnvChartValues should have comment or be unexported

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method PromoteOptions.GetEnvChartValues should have comment or be unexported

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method PromoteOptions.GetEnvChartValues should have comment or be unexported

kind := strings.ToLower(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("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),
}
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),
}
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),
)
}
return values, valueString
}
131 changes: 131 additions & 0 deletions pkg/cmd/promote/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"testing"

"github.com/jenkins-x/jx/pkg/cmd/clients/fake"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/stretchr/testify/assert"

resources_mock "github.com/jenkins-x/jx/pkg/kube/resources/mocks"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -349,6 +351,135 @@ func TestEnsureApplicationNameIsDefinedWithoutApplicationFlagUserSaysNo(t *testi
assert.Equal(t, "", promoteOptions.Application)
}

func TestGetEnvChartValues(t *testing.T) {
tests := []struct {
ns string
env v1.Environment
values []string
valueStrings []string
}{{
"jx-test-preview-pr-6",
v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: "test-preview",
},
Spec: v1.EnvironmentSpec{
Namespace: "jx-test-preview-pr-6",
Label: "Test preview",
Kind: v1.EnvironmentKindTypePreview,
PromotionStrategy: v1.PromotionStrategyTypeAutomatic,
PullRequestURL: "https://github.com/my-project/my-app/pull/6",
Order: 999,
Source: v1.EnvironmentRepository{
Kind: v1.EnvironmentRepositoryTypeGit,
URL: "https://github.com/my-project/my-app",
Ref: "my-branch",
},
PreviewGitSpec: v1.PreviewGitSpec{
ApplicationName: "my-app",
Name: "6",
URL: "https://github.com/my-project/my-app/pull/6",
},
},
},
[]string{
"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",
},
[]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",
},
}, {
"jx-custom-env",
v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: "custom-env",
},
Spec: v1.EnvironmentSpec{
Namespace: "jx-custom-env",
Label: "Custom environment",
Kind: v1.EnvironmentKindTypePermanent,
PromotionStrategy: v1.PromotionStrategyTypeManual,
Order: 5,
Source: v1.EnvironmentRepository{
Kind: v1.EnvironmentRepositoryTypeGit,
URL: "https://github.com/my-project/jx-environment-custom-env",
Ref: "master",
},
},
},
[]string{
"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",
},
[]string{
"global.jx-type-env=permanent",
"global.jx-env=custom-env",
"global.jx-ns=jx-custom-env",
},
}, {
"ns-rand",
v1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: "random-env",
},
Spec: v1.EnvironmentSpec{
Namespace: "ns-other",
Label: "Random environment",
Kind: v1.EnvironmentKindTypeEdit,
PromotionStrategy: v1.PromotionStrategyTypeNever,
Order: 666,
Source: v1.EnvironmentRepository{
Kind: v1.EnvironmentRepositoryTypeGit,
URL: "https://github.com/my-project/random",
Ref: "master",
},
PreviewGitSpec: v1.PreviewGitSpec{
ApplicationName: "random",
Name: "2",
URL: "https://github.com/my-project/random/pull/6",
},
},
},
[]string{
"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",
},
[]string{
"global.jx-type-env=edit",
"global.jx-env=random-env",
"global.jx-ns=ns-rand",
},
}}

for _, test := range tests {
promoteOptions := &promote.PromoteOptions{}
values, valueStrings := promoteOptions.GetEnvChartValues(test.ns, &test.env)
sort.Strings(values)
sort.Strings(test.values)
assert.Equal(t, values, test.values)
sort.Strings(valueStrings)
sort.Strings(test.valueStrings)
assert.Equal(t, valueStrings, test.valueStrings)
}
}

// Contains all useful data from the test environment initialized by `prepareInitialPromotionEnv`
type TestEnv struct {
Activity *v1.PipelineActivity
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/step/helm/step_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,12 @@ func (o *StepHelmOptions) overwriteProviderValues(requirements *config.Requireme
data, err := yaml.Marshal(values)
return data, err
}

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),
}, []string{
fmt.Sprintf("global.jx-ns=%s", targetNS),
}
}
6 changes: 5 additions & 1 deletion pkg/cmd/step/helm/step_helm_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
`)

StepHelmApplyExample = templates.Examples(`
# apply the chart in the env folder to namespace jx-staging
# apply the chart in the env folder to namespace jx-staging
jx step helm apply --dir env --namespace jx-staging

`)
Expand Down Expand Up @@ -356,11 +356,15 @@ func (o *StepHelmApplyOptions) Run() error {
return errors.Wrap(err, "applying chart overrides")
}

setValues, setStrings := o.getChartValues(ns)

helmOptions := helm.InstallChartOptions{
Chart: chartName,
ReleaseName: releaseName,
Ns: ns,
NoForce: !o.Force,
SetValues: setValues,
SetStrings: setStrings,
ValueFiles: valueFiles,
Dir: dir,
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/cmd/step/helm/step_helm_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
type StepHelmInstallOptions struct {
StepHelmOptions

Name string
Namespace string
Version string
Values []string
ValuesFiles []string
Name string
Namespace string
Version string
Values []string
ValueStrings []string
ValuesFiles []string
}

var (
Expand Down Expand Up @@ -66,6 +67,7 @@ func NewCmdStepHelmInstall(commonOpts *opts.CommonOptions) *cobra.Command {
cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The version to install. Defaults to the latest")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "", "", "The namespace to install into. Defaults to the current namespace")
cmd.Flags().StringArrayVarP(&options.Values, "set", "", []string{}, "The values to override in the helm chart")
cmd.Flags().StringArrayVarP(&options.ValueStrings, "set-string", "", []string{}, "The STRING values to override in the helm chart")
cmd.Flags().StringArrayVarP(&options.ValuesFiles, "set-file", "", []string{}, "The values files to override values in the helm chart")

return cmd
Expand Down Expand Up @@ -94,12 +96,16 @@ func (o *StepHelmInstallOptions) Run() error {
if o.Version == "" {
version = ""
}

SetValues, setStrings := o.getChartValues(ns)

helmOptions := helm.InstallChartOptions{
Chart: chart,
ReleaseName: releaseName,
Version: version,
Ns: ns,
SetValues: o.Values,
SetValues: append(SetValues, o.Values...),
SetStrings: append(setStrings, o.ValueStrings...),
ValueFiles: o.ValuesFiles,
}
err = o.InstallChartWithOptions(helmOptions)
Expand Down
2 changes: 1 addition & 1 deletion pkg/environments/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func LocateAppResource(helmer helm.Helmer, chartDir string, appName string) (*je
},
Spec: jenkinsv1.AppSpec{},
}
err = helmer.Template(chartDir, appName, "", templateWorkDir, false, make([]string, 0), make([]string, 0))
err = helmer.Template(chartDir, appName, "", templateWorkDir, false, make([]string, 0), make([]string, 0), make([]string, 0))
if err != nil {
templateWorkDir = chartDir
}
Expand Down
Loading