forked from kyma-project/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added manage and unmanaged commands (kyma-project#2316)
Co-authored-by: Cortey <[email protected]>
- Loading branch information
Showing
9 changed files
with
388 additions
and
3 deletions.
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,64 @@ | ||
package module | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kyma-project/cli.v3/internal/clierror" | ||
"github.com/kyma-project/cli.v3/internal/cmdcommon" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type manageConfig struct { | ||
*cmdcommon.KymaConfig | ||
|
||
module string | ||
policy string | ||
} | ||
|
||
func newManageCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { | ||
cfg := manageConfig{ | ||
KymaConfig: kymaConfig, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "manage <module>", | ||
Short: "Manage module.", | ||
Long: "Use this command to manage an existing module.", | ||
|
||
PreRun: func(_ *cobra.Command, args []string) { | ||
clierror.Check(cfg.validate()) | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
clierror.Check(runManage(&cfg)) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&cfg.module, "module", "", "Name of the module to manage") | ||
cmd.Flags().StringVar(&cfg.policy, "policy", "CreateAndDelete", "Set custom resource policy. (Possible values: CreateAndDelete, Ignore)") | ||
_ = cmd.MarkFlagRequired("module") | ||
return cmd | ||
} | ||
|
||
func (mc *manageConfig) validate() clierror.Error { | ||
if mc.policy != "CreateAndDelete" && mc.policy != "Ignore" { | ||
return clierror.New(fmt.Sprintf("invalid policy %q, only CreateAndDelete and Ignore are allowed", mc.policy)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runManage(cfg *manageConfig) clierror.Error { | ||
client, clierr := cfg.GetKubeClientWithClierr() | ||
if clierr != nil { | ||
return clierr | ||
} | ||
|
||
err := client.Kyma().ManageModule(cfg.Ctx, cfg.module, cfg.policy) | ||
if err != nil { | ||
return clierror.Wrap(err, clierror.New("failed to set module as managed")) | ||
} | ||
err = client.Kyma().WaitForModuleState(cfg.Ctx, cfg.module, "Ready", "Warning") | ||
if err != nil { | ||
return clierror.Wrap(err, clierror.New("failed to check module state")) | ||
} | ||
return nil | ||
} |
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
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,50 @@ | ||
package module | ||
|
||
import ( | ||
"github.com/kyma-project/cli.v3/internal/clierror" | ||
"github.com/kyma-project/cli.v3/internal/cmdcommon" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type unmanageConfig struct { | ||
*cmdcommon.KymaConfig | ||
|
||
module string | ||
} | ||
|
||
func newUnmanageCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { | ||
cfg := unmanageConfig{ | ||
KymaConfig: kymaConfig, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "unmanage <module>", | ||
Short: "Unmanage module.", | ||
Long: "Use this command to unmanage an existing module.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
clierror.Check(runUnmanage(&cfg)) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&cfg.module, "module", "", "Name of the module to unmanage") | ||
_ = cmd.MarkFlagRequired("module") | ||
return cmd | ||
} | ||
|
||
func runUnmanage(cfg *unmanageConfig) clierror.Error { | ||
client, clierr := cfg.GetKubeClientWithClierr() | ||
if clierr != nil { | ||
return clierr | ||
} | ||
|
||
err := client.Kyma().UnmanageModule(cfg.Ctx, cfg.module) | ||
if err != nil { | ||
return clierror.Wrap(err, clierror.New("failed to set module as unmanaged")) | ||
} | ||
|
||
err = client.Kyma().WaitForModuleState(cfg.Ctx, cfg.module, "Unmanaged") | ||
if err != nil { | ||
return clierror.Wrap(err, clierror.New("failed to check module state")) | ||
} | ||
return nil | ||
} |
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
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
Oops, something went wrong.