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

cyclops cli: support for update command #475

Merged
merged 21 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
32 changes: 32 additions & 0 deletions cyctl/cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/cyclops-ui/cycops-cyctl/internal/update"
"github.com/spf13/cobra"
)

var (
updateExample = `# updates the given module
cyctl update module <module-name> --key=<key> --value=<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`
)

var (
updateCMD = &cobra.Command{

Use: "update",
Short: "updates the given module",
Long: "updates the given module",
Example: updateExample,
Args: cobra.NoArgs,
}
)

func init() {

RootCmd.AddCommand(updateCMD)
updateCMD.AddCommand(update.UpdateModule)

}
99 changes: 99 additions & 0 deletions cyctl/internal/update/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package update

import (
"encoding/json"
"fmt"
"strings"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/spf13/cobra"
apiextensionv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

)

var (
updateModuleExample = `# updates the module,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
`
)

// 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
}

module, err := clientset.Modules("cyclops").Get(moduleName)
if err != nil {
fmt.Println("Failed to fetch module ", err)
return

}
specValuesMap := make(map[string]interface{})
err = json.Unmarshal(module.Spec.Values.Raw, &specValuesMap)
if err != nil {

fmt.Println("failed to decode json data:", err)
return

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

fmt.Println(err)
return
}
updatedSpecValues, err := json.Marshal(specValuesMap)
if err != nil {
fmt.Println("failed to encode to json: ", err)
return
}
module.Spec.Values = apiextensionv1.JSON{Raw: updatedSpecValues}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also have to populate typeMeta before updating

Suggested change
module.Spec.Values = apiextensionv1.JSON{Raw: updatedSpecValues}
module.Spec.Values = apiextensionv1.JSON{Raw: updatedSpecValues}
module.TypeMeta = v1.TypeMeta{
APIVersion: "cyclops-ui.com/v1alpha1",
Kind: "Module",
}

module.TypeMeta = v1.TypeMeta{
APIVersion: "cyclops-ui.com/v1alpha1",
Kind: "Module",
}
updatedModule, err := clientset.Modules("cyclops").Update(module)
if err != nil {
fmt.Println("failed to update module: ", err)
return
}
fmt.Printf("successfully updated %v", moduleName)
}

var (
UpdateModule = &cobra.Command{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the name to updateModuleCMD. Update command name in init func as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah


Use: "module",
Short: "updates the module,takes module-name as an argument with flags --key and --value",
Long: "updates the module,takes module-name as an argument with flags --key and --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)
}
value, err := cmd.Flags().GetString("value")
if err != nil {
fmt.Println("failed to get value of flag --value ")
}

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

func init() {
UpdateModule.Flags().StringP("key", "k", "", "the field to update")
UpdateModule.Flags().StringP("value", "v", "", "field value")
UpdateModule.MarkFlagRequired("key")
UpdateModule.MarkFlagRequired("value")
}
Loading