Skip to content

Commit

Permalink
Merge pull request #302 from PennyScissors/m-answers-set-str-support
Browse files Browse the repository at this point in the history
Add support for answersSetString
  • Loading branch information
Steven Crespo authored Jun 25, 2021
2 parents 244c898 + 7f9f974 commit 4deb7e6
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 455 deletions.
6 changes: 3 additions & 3 deletions cliclient/cliclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/rancher/cli/config"
"github.com/rancher/norman/clientbase"
ntypes "github.com/rancher/norman/types"
clusterClient "github.com/rancher/types/client/cluster/v3"
managementClient "github.com/rancher/types/client/management/v3"
projectClient "github.com/rancher/types/client/project/v3"
clusterClient "github.com/rancher/rancher/pkg/client/generated/cluster/v3"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
Expand Down
71 changes: 47 additions & 24 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"github.com/pkg/errors"
"github.com/rancher/cli/cliclient"
"github.com/rancher/norman/clientbase"
clusterClient "github.com/rancher/types/client/cluster/v3"
managementClient "github.com/rancher/types/client/management/v3"
projectClient "github.com/rancher/types/client/project/v3"
clusterClient "github.com/rancher/rancher/pkg/client/generated/cluster/v3"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
Expand All @@ -46,14 +46,14 @@ Example:
$ rancher app install --answers /example/answers.yaml redis appFoo
# Install the redis template and set multiple answers and the version to install
$ rancher app install --set foo=bar --set baz=bunk --version 1.0.1 redis appFoo
$ rancher app install --set foo=bar --set-string baz=bunk --version 1.0.1 redis appFoo
# Install the redis template and specify the namespace for the app
$ rancher app install --namespace bar redis appFoo
`
upgradeAppDescription = `
Upgrade an existing app to a newer version via app template or app version in the current Rancher server.
Example:
# Upgrade the 'appFoo' app to latest version without any options
$ rancher app upgrade appFoo latest
Expand All @@ -62,7 +62,7 @@ Example:
$ rancher app upgrade appFoo ./redis
# Upgrade the 'appFoo' app and set multiple answers and the 0.2.0 version to install
$ rancher app upgrade --set foo=bar --set baz=bunk appFoo 0.2.0
$ rancher app upgrade --set foo=bar --set-string baz=bunk appFoo 0.2.0
`
)

Expand Down Expand Up @@ -171,6 +171,10 @@ func AppCommand() cli.Command {
Name: "set",
Usage: "Set answers for the template, can be used multiple times. Example: --set foo=bar",
},
cli.StringSliceFlag{
Name: "set-string",
Usage: "Set string answers for the template (Skips Helm's type conversion), can be used multiple times. Example: --set-string foo=bar",
},
cli.StringFlag{
Name: "version",
Usage: "Version of the template to use",
Expand Down Expand Up @@ -225,6 +229,10 @@ func AppCommand() cli.Command {
Name: "set",
Usage: "Set answers for the template, can be used multiple times. Example: --set foo=bar",
},
cli.StringSliceFlag{
Name: "set-string",
Usage: "Set string answers for the template (Skips Helm's type conversion), can be used multiple times. Example: --set-string foo=bar",
},
cli.BoolFlag{
Name: "show-versions,v",
Usage: "Display versions available to upgrade to",
Expand Down Expand Up @@ -432,8 +440,9 @@ func appUpgrade(ctx *cli.Context) error {
}

answers := app.Answers
answersSetString := app.AnswersSetString
values := app.ValuesYaml
answers, err = processAnswerUpdates(ctx, answers)
answers, answersSetString, err = processAnswerUpdates(ctx, answers, answersSetString)
if err != nil {
return err
}
Expand All @@ -445,9 +454,10 @@ func appUpgrade(ctx *cli.Context) error {
force := ctx.Bool("force")

au := &projectClient.AppUpgradeConfig{
Answers: answers,
ForceUpgrade: force,
ValuesYaml: values,
Answers: answers,
AnswersSetString: answersSetString,
ForceUpgrade: force,
ValuesYaml: values,
}

if resolveTemplatePath(appVersionOrLocalTemplatePath) {
Expand Down Expand Up @@ -634,7 +644,7 @@ func templateInstall(ctx *cli.Context) error {
if err != nil {
return err
}
answers, err := processAnswerInstall(ctx, nil, nil, false, false)
answers, answersSetString, err := processAnswerInstall(ctx, nil, nil, nil, false, false)
if err != nil {
return err
}
Expand All @@ -645,6 +655,7 @@ func templateInstall(ctx *cli.Context) error {

app.Files = files
app.Answers = answers
app.AnswersSetString = answersSetString
app.ValuesYaml = values
namespace := ctx.String("namespace")
if namespace == "" {
Expand Down Expand Up @@ -692,7 +703,7 @@ func templateInstall(ctx *cli.Context) error {
}

interactive := !ctx.Bool("no-prompt")
answers, err := processAnswerInstall(ctx, templateVersion, nil, interactive, false)
answers, answersSetString, err := processAnswerInstall(ctx, templateVersion, nil, nil, interactive, false)
if err != nil {
return err
}
Expand All @@ -709,6 +720,7 @@ func templateInstall(ctx *cli.Context) error {
return err
}
app.Answers = answers
app.AnswersSetString = answersSetString
app.ValuesYaml = values
app.ExternalID = templateVersion.ExternalID
app.TargetNamespace = namespace
Expand Down Expand Up @@ -1153,53 +1165,64 @@ func processValues(ctx *cli.Context, existingValues string) (map[string]interfac
func processAnswerInstall(
ctx *cli.Context,
tv *managementClient.TemplateVersion,
answers map[string]string,
answers,
answersSetString map[string]string,
interactive bool,
multicluster bool,
) (map[string]string, error) {
) (map[string]string, map[string]string, error) {
var err error
answers, err = processAnswerUpdates(ctx, answers)
answers, answersSetString, err = processAnswerUpdates(ctx, answers, answersSetString)
if err != nil {
return answers, err
return answers, answersSetString, err
}
// interactive occurs before adding defaults to ensure all questions are asked
if interactive {
// answers to questions will be added to map
err := askQuestions(tv, answers)
if err != nil {
return answers, err
return answers, answersSetString, err
}
}
if multicluster && !interactive {
// add default values if answers missing from map
err = fillInDefaultAnswersStringMap(tv, answers)
if err != nil {
return answers, err
return answers, answersSetString, err
}
}
return answers, nil
return answers, answersSetString, nil
}

func processAnswerUpdates(ctx *cli.Context, answers map[string]string) (map[string]string, error) {
func processAnswerUpdates(ctx *cli.Context, answers, answersSetString map[string]string) (map[string]string, map[string]string, error) {
logrus.Println("ok")
if answers == nil || ctx.Bool("reset") {
// this would not be possible without returning a map
answers = make(map[string]string)
}

if answersSetString == nil || ctx.Bool("reset") {
// this would not be possible without returning a map
answersSetString = make(map[string]string)
}
if ctx.String("answers") != "" {
err := parseAnswersFile(ctx.String("answers"), answers)
if err != nil {
return answers, err
return answers, answersSetString, err
}
}

for _, answer := range ctx.StringSlice("set") {
parts := strings.SplitN(answer, "=", 2)
if len(parts) == 2 {
answers[parts[0]] = parts[1]
}
}
return answers, nil
for _, answer := range ctx.StringSlice("set-string") {
parts := strings.SplitN(answer, "=", 2)
logrus.Printf("%v\n", parts)
if len(parts) == 2 {
answersSetString[parts[0]] = parts[1]
}
}
return answers, answersSetString, nil
}

// parseMapToYamlString create yaml string from answers map
Expand Down
2 changes: 1 addition & 1 deletion cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/pkg/errors"
managementClient "github.com/rancher/types/client/management/v3"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/rancher/cli/cliclient"
managementClient "github.com/rancher/types/client/management/v3"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/rancher/norman/clientbase"
ntypes "github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
managementClient "github.com/rancher/types/client/management/v3"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/client-go/tools/clientcmd/api"
Expand Down
Loading

0 comments on commit 4deb7e6

Please sign in to comment.