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

Do not try to create the environments if they already exist #4008

Merged
merged 2 commits into from
May 20, 2019
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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+v
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/petergtz/pegomock v2.2.0+incompatible h1:UjfRYEyjM8X8lNPuZs8FKakZI1pD7FqMRYc6KfLFNJU=
github.com/petergtz/pegomock v2.2.0+incompatible/go.mod h1:nuBLWZpVyv/fLo56qTwt/AUau7jgouO1h7bEvZCq82o=
github.com/petergtz/pegomock v2.3.0+incompatible h1:kcZy3H5eNyitPhWxrUQOuEoEcKqH/D5SiytA7x9snVI=
github.com/pierrec/lz4 v0.0.0-20181005164709-635575b42742/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
Expand Down
4 changes: 3 additions & 1 deletion pkg/jx/cmd/create_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type CreateEnvOptions struct {
BranchPattern string
Vault bool
PullSecrets string
Update bool
}

// NewCmdCreateEnv creates a command object for the "create" command
Expand Down Expand Up @@ -97,6 +98,7 @@ func NewCmdCreateEnv(commonOpts *opts.CommonOptions) *cobra.Command {

cmd.Flags().StringVarP(&options.Options.Name, kube.OptionName, "n", "", "The Environment resource name. Must follow the Kubernetes name conventions like Services, Namespaces")
cmd.Flags().StringVarP(&options.Options.Spec.Label, "label", "l", "", "The Environment label which is a descriptive string like 'Production' or 'Staging'")
cmd.Flags().BoolVarP(&options.Update, "update", "u", false, "Update environment if already exists")

cmd.Flags().StringVarP(&options.Options.Spec.Namespace, kube.OptionNamespace, "s", "", "The Kubernetes namespace for the Environment")
cmd.Flags().StringVarP(&options.Options.Spec.Cluster, "cluster", "c", "", "The Kubernetes cluster for the Environment. If blank and a namespace is specified assumes the current cluster")
Expand Down Expand Up @@ -185,7 +187,7 @@ func (o *CreateEnvOptions) Run() error {

env := v1.Environment{}
o.Options.Spec.PromotionStrategy = v1.PromotionStrategyType(o.PromotionStrategy)
gitProvider, err := kube.CreateEnvironmentSurvey(o.BatchMode, authConfigSvc, devEnv, &env, &o.Options, o.ForkEnvironmentGitRepo, ns,
gitProvider, err := kube.CreateEnvironmentSurvey(o.BatchMode, authConfigSvc, devEnv, &env, &o.Options, o.Update, o.ForkEnvironmentGitRepo, ns,
jxClient, kubeClient, envDir, &o.GitRepositoryOptions, o.HelmValuesConfig, o.Prefix, o.Git(), o.ResolveChartMuseumURL, o.In, o.Out, o.Err)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/jx/cmd/edit_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (o *EditEnvOptions) Run() error {
return err
}
o.Options.Spec.PromotionStrategy = v1.PromotionStrategyType(o.PromotionStrategy)
gitProvider, err := kube.CreateEnvironmentSurvey(o.BatchMode, authConfigSvc, devEnv, env, &o.Options, o.ForkEnvironmentGitRepo,
gitProvider, err := kube.CreateEnvironmentSurvey(o.BatchMode, authConfigSvc, devEnv, env, &o.Options, true, o.ForkEnvironmentGitRepo,
ns, jxClient, kubeClient, envDir, &o.GitRepositoryOptions, o.HelmValuesConfig, o.Prefix, o.Git(), o.ResolveChartMuseumURL, o.In, o.Out, o.Err)
if err != nil {
return err
Expand Down
17 changes: 3 additions & 14 deletions pkg/jx/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,21 +2568,8 @@ func (options *InstallOptions) createEnvironments(namespace string) error {
options.CreateEnvOptions.GitOpsMode = true
options.CreateEnvOptions.ModifyDevEnvironmentFn = options.ModifyDevEnvironmentFn
options.CreateEnvOptions.ModifyEnvironmentFn = options.ModifyEnvironmentFn
} else {
createEnvironments = false

jxClient, _, err := options.JXClient()
if err != nil {
return errors.Wrap(err, "failed to create the jx client")
}

// lets only recreate the environments if its the first time we run this
_, envNames, err := kube.GetEnvironments(jxClient, namespace)
if err != nil || len(envNames) <= 1 {
createEnvironments = true
}

}

if createEnvironments {
log.Info("Creating default staging and production environments\n")
_, devNamespace, err := options.KubeClientAndDevNamespace()
Expand All @@ -2594,6 +2581,8 @@ func (options *InstallOptions) createEnvironments(namespace string) error {
return errors.Wrap(err, "building the Git repository options for environments")
}
options.CreateEnvOptions.GitRepositoryOptions = *gitRepoOptions
// lets not fail if environments already exist
options.CreateEnvOptions.Update = true

options.CreateEnvOptions.Prefix = options.Flags.DefaultEnvironmentPrefix
options.CreateEnvOptions.Prow = options.Flags.Prow
Expand Down
10 changes: 6 additions & 4 deletions pkg/kube/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ResolveChartMuseumURLFn func() (string, error)
// CreateEnvironmentSurvey creates a Survey on the given environment using the default options
// from the CLI
func CreateEnvironmentSurvey(batchMode bool, authConfigSvc auth.ConfigService, devEnv *v1.Environment, data *v1.Environment,
config *v1.Environment, forkEnvGitURL string, ns string, jxClient versioned.Interface, kubeClient kubernetes.Interface, envDir string,
config *v1.Environment, update bool, forkEnvGitURL string, ns string, jxClient versioned.Interface, kubeClient kubernetes.Interface, envDir string,
gitRepoOptions *gits.GitRepositoryOptions, helmValues config.HelmValuesConfig, prefix string, git gits.Gitter, chartMusemFn ResolveChartMuseumURLFn, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) (gits.GitProvider, error) {
surveyOpts := survey.WithStdio(in, out, errOut)
name := data.Name
Expand All @@ -48,9 +48,11 @@ func CreateEnvironmentSurvey(batchMode bool, authConfigSvc auth.ConfigService, d
if err != nil {
return nil, err
}
err = ValidateEnvironmentDoesNotExist(jxClient, ns, config.Name)
if err != nil {
return nil, err
if !update {
err = ValidateEnvironmentDoesNotExist(jxClient, ns, config.Name)
if err != nil {
return nil, err
}
}
data.Name = config.Name
} else {
Expand Down
1 change: 1 addition & 0 deletions pkg/kube/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func TestCreateEnvironmentSurvey(t *testing.T) {
&devEnv,
&data,
&conf,
true,
forkEnvGitURL,
ns,
versiondInterface,
Expand Down