From 99ca9a82d5bc84bc74202b42da42e86de104447b Mon Sep 17 00:00:00 2001 From: wtschreiter Date: Mon, 17 Jun 2019 10:34:28 +0200 Subject: [PATCH] Enable flag to merge answers with exising apps (#42) (#43) --- cmd/apply/apply.go | 5 +++++ cmd/config.go | 4 ++++ docs/cattlectl_apply.md | 1 + internal/pkg/config/config.go | 1 + internal/pkg/ctl/cattle.go | 22 ++++++++++--------- internal/pkg/ctl/version.go | 2 +- internal/pkg/rancher/client/app_client.go | 13 +++++++++-- internal/pkg/rancher/client/clients.go | 1 + internal/pkg/rancher/client/project_client.go | 12 ++++++---- internal/pkg/rancher/client/rancher_client.go | 11 +++++----- 10 files changed, 50 insertions(+), 22 deletions(-) diff --git a/cmd/apply/apply.go b/cmd/apply/apply.go index 8bf5718..36d3c78 100644 --- a/cmd/apply/apply.go +++ b/cmd/apply/apply.go @@ -25,6 +25,7 @@ import ( "github.com/bitgrip/cattlectl/internal/pkg/template" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var ( @@ -83,4 +84,8 @@ func apply(cmd *cobra.Command, args []string) { func init() { applyCmd.Flags().StringVarP(&applyFile, "file", "f", "project.yaml", "project file to apply") applyCmd.Flags().StringSliceVar(&valuesFiles, "values", []string{"values.yaml"}, "values file(s) to apply") + + applyCmd.Flags().Bool("merge-answers", false, "If answers of existing apps should be merged with the new apply answers") + viper.BindPFlag("rancher.merge_answers", applyCmd.Flags().Lookup("merge-answers")) + viper.BindEnv("rancher.merge_answers", "RANCHER_MERGE_ANSWERS") } diff --git a/cmd/config.go b/cmd/config.go index 2eae6d5..3caf13d 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -56,3 +56,7 @@ func (config) ClusterName() string { func (config) ClusterID() string { return viper.GetString("rancher.cluster_id") } + +func (config) MergeAnswers() bool { + return viper.GetBool("rancher.merge_answers") +} diff --git a/docs/cattlectl_apply.md b/docs/cattlectl_apply.md index 08bc33d..ae0b35d 100644 --- a/docs/cattlectl_apply.md +++ b/docs/cattlectl_apply.md @@ -26,6 +26,7 @@ cattlectl apply [flags] ``` -f, --file string project file to apply (default "project.yaml") -h, --help help for apply + --merge-answers If answers of existing apps should be merged with the new apply answers --values strings values file(s) to apply (default [values.yaml]) ``` diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index b02897d..a766a2d 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -24,4 +24,5 @@ type Config interface { TokenKey() string ClusterName() string ClusterID() string + MergeAnswers() bool } diff --git a/internal/pkg/ctl/cattle.go b/internal/pkg/ctl/cattle.go index 6c71394..829539b 100644 --- a/internal/pkg/ctl/cattle.go +++ b/internal/pkg/ctl/cattle.go @@ -275,11 +275,12 @@ func fillWorkloadMetadata(metadata *projectModel.WorkloadMetadata, config config } rancherClient, err := newRancherClient(rancher_client.RancherConfig{ - RancherURL: metadata.RancherURL, - AccessKey: metadata.AccessKey, - SecretKey: metadata.SecretKey, - Insecure: config.InsecureAPI(), - CACerts: config.CACerts(), + RancherURL: metadata.RancherURL, + AccessKey: metadata.AccessKey, + SecretKey: metadata.SecretKey, + Insecure: config.InsecureAPI(), + CACerts: config.CACerts(), + MergeAnswers: config.MergeAnswers(), }) if err != nil { return nil, nil, nil, err @@ -316,11 +317,12 @@ func fillProjectMetadata(metadata *projectModel.ProjectMetadata, config config.C } rancherClient, err := newRancherClient(rancher_client.RancherConfig{ - RancherURL: metadata.RancherURL, - AccessKey: metadata.AccessKey, - SecretKey: metadata.SecretKey, - Insecure: config.InsecureAPI(), - CACerts: config.CACerts(), + RancherURL: metadata.RancherURL, + AccessKey: metadata.AccessKey, + SecretKey: metadata.SecretKey, + Insecure: config.InsecureAPI(), + CACerts: config.CACerts(), + MergeAnswers: config.MergeAnswers(), }) if err != nil { return nil, nil, err diff --git a/internal/pkg/ctl/version.go b/internal/pkg/ctl/version.go index 9e56d5c..6f6a8bc 100644 --- a/internal/pkg/ctl/version.go +++ b/internal/pkg/ctl/version.go @@ -21,7 +21,7 @@ import ( ) // Version is the current build version -var Version = "v1.2.0-local" +var Version = "v1.3.0-local" func isSupportedAPIVersion(apiVersion string) bool { v, err := semver.NewVersion(Version) diff --git a/internal/pkg/rancher/client/app_client.go b/internal/pkg/rancher/client/app_client.go index aa88a7a..e7f1d80 100644 --- a/internal/pkg/rancher/client/app_client.go +++ b/internal/pkg/rancher/client/app_client.go @@ -155,11 +155,20 @@ func (client *appClient) Upgrade() error { } au.ValuesYaml = client.app.ValuesYaml } else { - if reflect.DeepEqual(installedApp.Answers, client.app.Answers) { + resultAnswers := map[string]string{} + if client.projectClient.config().MergeAnswers { + for key, value := range installedApp.Answers { + resultAnswers[key] = value + } + } + for key, value := range client.app.Answers { + resultAnswers[key] = value + } + if reflect.DeepEqual(installedApp.Answers, resultAnswers) { client.logger.Debug("Skip upgrade app - no changes") return nil } - au.Answers = client.app.Answers + au.Answers = resultAnswers } if client.app.SkipUpgrade { client.logger.Info("Suppress upgrade app - by config") diff --git a/internal/pkg/rancher/client/clients.go b/internal/pkg/rancher/client/clients.go index fd31e41..8afa412 100644 --- a/internal/pkg/rancher/client/clients.go +++ b/internal/pkg/rancher/client/clients.go @@ -95,6 +95,7 @@ type ProjectClient interface { StatefulSets(namespaceName string) ([]StatefulSetClient, error) backendProjectClient() (*backendProjectClient.Client, error) + config() RancherConfig } // NamespaceClient interacts with a Rancher namespace resource diff --git a/internal/pkg/rancher/client/project_client.go b/internal/pkg/rancher/client/project_client.go index 9241f67..2a4b6b8 100644 --- a/internal/pkg/rancher/client/project_client.go +++ b/internal/pkg/rancher/client/project_client.go @@ -26,7 +26,7 @@ import ( func newProjectClient( name string, - config RancherConfig, + rancherConfig RancherConfig, clusterClient ClusterClient, logger *logrus.Entry, ) (ProjectClient, error) { @@ -37,7 +37,7 @@ func newProjectClient( name: name, logger: projectLogger, }, - config: config, + rancherConfig: rancherConfig, clusterClient: clusterClient, certificateClients: make(map[string]CertificateClient), configMapClients: make(map[string]ConfigMapClient), @@ -54,7 +54,7 @@ func newProjectClient( type projectClient struct { resourceClient - config RancherConfig + rancherConfig RancherConfig clusterClient ClusterClient _backendProjectClient *backendProjectClient.Client project projectModel.Project @@ -85,7 +85,7 @@ func (client *projectClient) init() error { if clusterID, err = client.clusterClient.ID(); err != nil { return err } - client._backendProjectClient, err = createProjectClient(client.config, clusterID, projectID) + client._backendProjectClient, err = createProjectClient(client.rancherConfig, clusterID, projectID) return err } @@ -696,3 +696,7 @@ func (client *projectClient) backendProjectClient() (*backendProjectClient.Clien } return client._backendProjectClient, nil } + +func (client *projectClient) config() RancherConfig { + return client.rancherConfig +} diff --git a/internal/pkg/rancher/client/rancher_client.go b/internal/pkg/rancher/client/rancher_client.go index a25cc7e..d88641c 100644 --- a/internal/pkg/rancher/client/rancher_client.go +++ b/internal/pkg/rancher/client/rancher_client.go @@ -31,11 +31,12 @@ func NewRancherClient(config RancherConfig) (RancherClient, error) { // RancherConfig holds the configuration data to interact with a rancher server type RancherConfig struct { - RancherURL string - AccessKey string - SecretKey string - Insecure bool - CACerts string + RancherURL string + AccessKey string + SecretKey string + Insecure bool + CACerts string + MergeAnswers bool } type rancherClient struct {