Skip to content

Commit

Permalink
App pause (#501)
Browse files Browse the repository at this point in the history
* pause/unpause

* get deployment type

* add suspend check

* add examples

* combine pause/unpause

* add suspend type

* update fake flux

* spelling error

* added wrong file

* rebase

* address comments

Co-authored-by: Justin Thompson <[email protected]>
  • Loading branch information
J-Thompson12 and Justin Thompson authored Jul 27, 2021
1 parent 7bdccd8 commit 0d48ef7
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 12 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/application_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ const (
SourceTypeHelm SourceType = "helm"
)

// SuspendAction defines the command run to pause/unpause an application
type SuspendActionType string

const (
SuspendAction SuspendActionType = "suspend"
ResumeAction SuspendActionType = "resume"
)

// ApplicationStatus defines the observed state of Application
type ApplicationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
14 changes: 12 additions & 2 deletions cmd/wego/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ import (
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/wego/app/add"
"github.com/weaveworks/weave-gitops/cmd/wego/app/list"
"github.com/weaveworks/weave-gitops/cmd/wego/app/pause"
"github.com/weaveworks/weave-gitops/cmd/wego/app/status"
"github.com/weaveworks/weave-gitops/cmd/wego/app/unpause"
)

var ApplicationCmd = &cobra.Command{
Use: "app",
Short: "Manages your applications",
Example:`
Example: `
# Add an application to wego from local git repository
wego app add . --name <app-name>
# Status an application under wego control
wego app status <app-name>
# List applications under wego control
wego app list`,
wego app list
# Pause gitops automation
wego app pause <app-name>
# Unpause gitops automation
wego app unpause <app-name>`,
Args: cobra.MinimumNArgs(1),
}

func init() {
ApplicationCmd.AddCommand(status.Cmd)
ApplicationCmd.AddCommand(add.Cmd)
ApplicationCmd.AddCommand(list.Cmd)
ApplicationCmd.AddCommand(pause.Cmd)
ApplicationCmd.AddCommand(unpause.Cmd)
}
51 changes: 51 additions & 0 deletions cmd/wego/app/pause/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pause

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/wego/version"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)

var params app.PauseParams

var Cmd = &cobra.Command{
Use: "pause <app-name>",
Short: "Pause an application",
Args: cobra.MinimumNArgs(1),
Example: "wego app pause podinfo",
RunE: runCmd,
SilenceUsage: true,
SilenceErrors: true,
PostRun: func(cmd *cobra.Command, args []string) {
version.CheckVersion(version.CheckpointParamsWithFlags(version.CheckpointParams(), cmd))
},
}

func runCmd(cmd *cobra.Command, args []string) error {
params.Namespace, _ = cmd.Parent().Flags().GetString("namespace")
params.Name = args[0]

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
logger := logger.New(os.Stdout)
kubeClient, err := kube.NewKubeHTTPClient()
if err != nil {
return fmt.Errorf("error initializing kube client: %w", err)
}

appService := app.New(logger, nil, fluxClient, kubeClient)

if err := appService.Pause(params); err != nil {
return errors.Wrapf(err, "failed to pause the app %s", params.Name)
}

return nil
}
51 changes: 51 additions & 0 deletions cmd/wego/app/unpause/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package unpause

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/wego/version"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)

var params app.UnpauseParams

var Cmd = &cobra.Command{
Use: "unpause <app-name>",
Short: "Unpause an application",
Args: cobra.MinimumNArgs(1),
Example: "wego app unpause podinfo",
RunE: runCmd,
SilenceUsage: true,
SilenceErrors: true,
PostRun: func(cmd *cobra.Command, args []string) {
version.CheckVersion(version.CheckpointParamsWithFlags(version.CheckpointParams(), cmd))
},
}

func runCmd(cmd *cobra.Command, args []string) error {
params.Namespace, _ = cmd.Parent().Flags().GetString("namespace")
params.Name = args[0]

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
logger := logger.New(os.Stdout)
kubeClient, err := kube.NewKubeHTTPClient()
if err != nil {
return fmt.Errorf("error initializing kube client: %w", err)
}

appService := app.New(logger, nil, fluxClient, kubeClient)

if err := appService.Unpause(params); err != nil {
return errors.Wrapf(err, "failed to unpause the app %s", params.Name)
}

return nil
}
10 changes: 10 additions & 0 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pkg/errors"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/version"
)
Expand All @@ -25,6 +26,7 @@ type Flux interface {
CreateSecretGit(name string, url string, namespace string) ([]byte, error)
GetVersion() (string, error)
GetAllResourcesStatus(name string, namespace string) ([]byte, error)
SuspendOrResumeApp(pause wego.SuspendActionType, name, namespace, deploymentType string) ([]byte, error)
}

type FluxClient struct {
Expand Down Expand Up @@ -252,3 +254,11 @@ func (f *FluxClient) fluxPath() (string, error) {
path := fmt.Sprintf("%v/.wego/bin", homeDir)
return fmt.Sprintf("%v/flux-%v", path, version.FluxVersion), nil
}

func (f *FluxClient) SuspendOrResumeApp(suspend wego.SuspendActionType, name, namespace string, deploymentType string) ([]byte, error) {
args := []string{
string(suspend), deploymentType, name, fmt.Sprintf("--namespace=%s", namespace),
}

return f.runFluxCmd(args...)
}
86 changes: 86 additions & 0 deletions pkg/flux/fluxfakes/fake_flux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0d48ef7

Please sign in to comment.