Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Enable flag to merge answers with exising apps (#42) (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtschreiter authored Jun 17, 2019
1 parent a611910 commit 99ca9a8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 22 deletions.
5 changes: 5 additions & 0 deletions cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
}
4 changes: 4 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions docs/cattlectl_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
```

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type Config interface {
TokenKey() string
ClusterName() string
ClusterID() string
MergeAnswers() bool
}
22 changes: 12 additions & 10 deletions internal/pkg/ctl/cattle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/ctl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions internal/pkg/rancher/client/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/rancher/client/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/rancher/client/project_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func newProjectClient(
name string,
config RancherConfig,
rancherConfig RancherConfig,
clusterClient ClusterClient,
logger *logrus.Entry,
) (ProjectClient, error) {
Expand All @@ -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),
Expand All @@ -54,7 +54,7 @@ func newProjectClient(

type projectClient struct {
resourceClient
config RancherConfig
rancherConfig RancherConfig
clusterClient ClusterClient
_backendProjectClient *backendProjectClient.Client
project projectModel.Project
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -696,3 +696,7 @@ func (client *projectClient) backendProjectClient() (*backendProjectClient.Clien
}
return client._backendProjectClient, nil
}

func (client *projectClient) config() RancherConfig {
return client.rancherConfig
}
11 changes: 6 additions & 5 deletions internal/pkg/rancher/client/rancher_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 99ca9a8

Please sign in to comment.