-
Notifications
You must be signed in to change notification settings - Fork 772
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
petar-cvit
merged 21 commits into
cyclops-ui:main
from
ashish111333:cyctl-update_branch
Jul 29, 2024
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9fce378
added update.go
d2f87cf
added update.go
a2d78a9
added modules.go in cyctl/internal
aee252e
provides basic structure for update command
e30b433
added flags for commadn in modules.go
806ccec
Update modules.go
ashish111333 ba3a7e7
Update update.go
ashish111333 aa3001d
Update update.go
ashish111333 7005cf0
added a subcommand to update
52b67b9
added code for updateModule
8fda97f
minor changes
3d26b7a
Merge branch 'cyclops-ui:main' into cyctl-update_branch
ashish111333 8410b78
Update modules.go
ashish111333 f1de7b3
Update modules.go
ashish111333 a0246e9
Update modules.go
ashish111333 de4927c
Update modules.go
ashish111333 2d56401
Update modules.go
ashish111333 0f2e3f0
Update modules.go
ashish111333 825e0c7
Update update.go
ashish111333 405d76f
Update modules.go
ashish111333 e0096c3
Update modules.go
ashish111333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the name to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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