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

Support multiple values module update #521

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
6 changes: 3 additions & 3 deletions cyctl/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

var (
updateExample = `# updates the given module
cyctl update module <module-name> --key=<key> --value=<value>
cyctl update module <module-name> --value="<key>=<value>" --value="<key>=<value>"

# to update replicas for a module named test,updates number of replicas to 3
cyctl update module <module-name> test --key="scaling.replicas" --value=3`
# to update replicas and version for a module named test, with 3 replicas and version 1.27.1
cyctl update module test --value="scaling.replicas=3" --value="general.version=1.27.1"`
)

var (
Expand Down
51 changes: 24 additions & 27 deletions cyctl/internal/update/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import (
)

var (
updateModuleExample = `# updates module values; takes module name as an argument with flags --key and --value
# to update replicas for a module named test
cyctl update module test --key="scaling.replicas" --value=3
updateModuleExample = `# updates module values; takes module name as an argument with flag --value
# to update replicas and version for a module named test
cyctl update module test --value="scaling.replicas=3" --value="general.version=1.27.1"
`
)

// updates the given module from cyclops API
func updateModule(clientset *client.CyclopsV1Alpha1Client, moduleName, key string, value interface{}) {
if key == "" {
fmt.Println("Error: key cannot be an empty string")
return
}

func updateModule(clientset *client.CyclopsV1Alpha1Client, moduleName string, values []string) {
module, err := clientset.Modules("cyclops").Get(moduleName)
if err != nil {
fmt.Println("Failed to fetch module ", err)
Expand All @@ -40,10 +35,20 @@ func updateModule(clientset *client.CyclopsV1Alpha1Client, moduleName, key strin
return
}

err = unstructured.SetNestedField(specValuesMap, value, strings.Split(key, ".")...)
if err != nil {
fmt.Println(err)
return
for _, v := range values {
keyValue := strings.Split(v, "=")
if len(keyValue) != 2 {
fmt.Println("invalid key value pair: ", v)
return
}
key := keyValue[0]
value := keyValue[1]

err = unstructured.SetNestedField(specValuesMap, value, strings.Split(key, ".")...)
if err != nil {
fmt.Println(err)
return
}
}

updatedSpecValues, err := json.Marshal(specValuesMap)
Expand All @@ -70,31 +75,23 @@ func updateModule(clientset *client.CyclopsV1Alpha1Client, moduleName, key strin
var (
UpdateModuleCMD = &cobra.Command{
Use: "module",
Short: "updates module values; takes module name as an argument with flags --key and --value",
Long: "updates module values; takes module name as an argument with flags --key and --value",
Short: "updates module values; takes module name as an argument with flag --value",
Long: "updates module values; takes module name as an argument with flag --value",
Example: updateModuleExample,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
key, err := cmd.Flags().GetString("key")
if err != nil {
fmt.Println("failed to get value of flag --key: ", err)
return
}

value, err := cmd.Flags().GetString("value")
values, err := cmd.Flags().GetStringArray("value")
if err != nil {
fmt.Println("failed to get value of flag --value ")
fmt.Println("failed to get value of flag --value: ", err)
return
}

updateModule(kubeconfig.Moduleset, args[0], key, value)
updateModule(kubeconfig.Moduleset, args[0], values)
},
}
)

func init() {
UpdateModuleCMD.Flags().StringP("key", "k", "", "the field to update")
UpdateModuleCMD.Flags().StringP("value", "v", "", "field value")
UpdateModuleCMD.MarkFlagRequired("key")
UpdateModuleCMD.Flags().StringArrayP("value", "v", []string{}, "key value pair to update module")
UpdateModuleCMD.MarkFlagRequired("value")
}
Loading