From 1d952a443bae8abebf02ccdac1a067e9206ec87b Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Tue, 13 Aug 2024 09:36:41 +0800 Subject: [PATCH] Add unpush subcommand to undo the submission --- README.md | 24 +++++++++++++++++++++++- cmd/root.go | 1 + cmd/unpush.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/cloud/cloud.go | 13 +++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cmd/unpush.go diff --git a/README.md b/README.md index e9c89ad..0c42256 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ Login Succeeded ### Push and submit the extension -Use the `ksbuilder push` subcommand to submit the plugin to KubeSphere Cloud. The `push` subcommand is similar to `publish` and the target can be either a directory or a packaged `.tgz` file: +Use the `ksbuilder push` subcommand to submit the extension to KubeSphere Cloud. The `push` subcommand is similar to `publish` and the target can be either a directory or a packaged `.tgz` file: ``` $ ksbuilder push tower @@ -185,4 +185,26 @@ SNAPSHOT ID VERSION STATUS UPDATE TIME 515518094316217510 1.0.0 submitted 2024-05-27 09:37:05 ``` +### Unpush a snapshot + +Use the `ksbuilder unpush` subcommand to cancel the submission of a snapshot in KubeSphere Cloud. + +When we submit an extension, its snapshot status is marked as `submitted`. If we want to make some new changes to the extension at this point, we can use this command to undo the submission. After undoing, the status of the extension will revert to `draft`, allowing it to be pushed again. + +To undo the submission, we first need to use the `get` subcommand to obtain the corresponding snapshot ID: + +``` +$ ksbuilder get tower +Name: tower +ID: 515518094316151974 +Status: draft + +SNAPSHOT ID VERSION STATUS UPDATE TIME +515518094316217510 1.0.0 submitted 2024-05-27 17:37:05 CST + +$ ksbuilder unpush 515518094316217510 +unpush snapshot 515518094316217510 +Snapshot 515518094316217510 has been unsubmitted and reverted to draft state +``` + Please refer to [KubeSphere extension development guide](https://dev-guide.kubesphere.io/extension-dev-guide/en/packaging-and-release/) for more details on extension packaging and releasing. diff --git a/cmd/root.go b/cmd/root.go index 521cae5..eb78c07 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,7 @@ func NewRootCmd(version string) *cobra.Command { cmd.AddCommand(pushCmd()) cmd.AddCommand(getCmd()) cmd.AddCommand(listCmd()) + cmd.AddCommand(unpushCmd()) return cmd } diff --git a/cmd/unpush.go b/cmd/unpush.go new file mode 100644 index 0000000..b9b10de --- /dev/null +++ b/cmd/unpush.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/kubesphere/ksbuilder/pkg/cloud" +) + +type unpushOptions struct{} + +func unpushCmd() *cobra.Command { + o := unpushOptions{} + + return &cobra.Command{ + Use: "unpush", + Short: "Unpush a snapshot of an extension", + Args: cobra.ExactArgs(1), + RunE: o.unpush, + } +} + +func (o *unpushOptions) unpush(_ *cobra.Command, args []string) error { + snapshot := args[0] + fmt.Printf("unpush snapshot %s\n", snapshot) + + client, err := cloud.NewClient() + if err != nil { + return fmt.Errorf("login failed: %v", err) + } + + if err = client.CancelSubmitExtension(snapshot); err != nil { + return err + } + fmt.Printf("Snapshot %s has been unsubmitted and reverted to draft state\n", snapshot) + return nil +} diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 22339e7..6fe8a48 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -207,6 +207,19 @@ func (c *Client) SubmitExtension(snapshotID string) error { ) } +func (c *Client) CancelSubmitExtension(snapshotID string) error { + body := &bytes.Buffer{} + body.WriteString(`{"message": "cancel submit"}`) + + return c.sendRequest( + http.MethodPost, + fmt.Sprintf("/apis/extension/v1/users/%s/snapshots/%s/action:cancel-submit", c.userID, snapshotID), + body, + map[string]string{"Content-Type": "application/json"}, + nil, + ) +} + func (c *Client) ListExtensions() (*ListExtensionsResponse, error) { body := &bytes.Buffer{} body.WriteString(fmt.Sprintf(`{"developer_ids": ["%s"], "statuses": ["*"]}`, c.userID))