Skip to content

Commit

Permalink
Collect all generated resources (both applied to cluster and written …
Browse files Browse the repository at this point in the history
…to git) for removal (#529)

* rebase-on-main

* tests mostly working except for resources that are both written and applied

* resource and path collection working with tests

* fixes after rebasing on main

* remove shims and handles that are no longer used

* remove unused code

* Add dependencies dependency to the "coverage/merged.lcov" target to ensure flux is available for unit tests

* move dependency to correct target

* get more info from flux errors

* stop fake installing flux for tests

* force update

* explicitly download dependencies to initialize flux

* add full path to dep file

* address review comment

Co-authored-by: Jerry Jackson <[email protected]>
  • Loading branch information
jrryjcksn and weave-e2e-quickstart authored Jul 28, 2021
1 parent ecd846f commit f626800
Show file tree
Hide file tree
Showing 27 changed files with 1,143 additions and 729 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ jobs:
run: make cmd/ui/dist/main.js
- name: Set up kubebuilder
uses: fluxcd/pkg/actions/kubebuilder@main
- name: Fake Install flux
run: mkdir -p pkg/flux/bin && touch pkg/flux/bin/flux
- name: Set up flux dir but let dependencies install flux
run: mkdir -p pkg/flux/bin && tools/download-deps.sh $PWD/tools/dependencies.toml
- name: Unit Tests with Coverage
run: make coverage/merged.lcov
env:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ coverage/lcov.info:
npm run test -- --coverage

# Golang gocov data. Not compatible with coveralls at this point.
coverage.out:
coverage.out: dependencies
go get github.com/ory/go-acc
go-acc --ignore fakes,acceptance,pkg/api,api -o coverage.out ./... -- -v --timeout=496s -tags test
@go mod tidy
Expand All @@ -120,7 +120,7 @@ proto-deps:

proto:
buf generate
# This job is complaining about a missing plugin and error-ing out
# This job is complaining about a missing plugin and error-ing out
# oapi-codegen -config oapi-codegen.config.yaml api/applications/applications.swagger.json

api-dev:
Expand Down
6 changes: 4 additions & 2 deletions cmd/wego/app/add/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/weaveworks/weave-gitops/pkg/git"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
"github.com/weaveworks/weave-gitops/pkg/utils"
Expand Down Expand Up @@ -114,12 +115,13 @@ func runCmd(cmd *cobra.Command, args []string) error {
}

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
kubeClient := kube.New(cliRunner)
gitClient := git.New(authMethod)
logger := logger.New(os.Stdout)

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

utils.SetCommmitMessageFromArgs("wego app add", params.Url, params.Path, params.Name)

Expand Down
6 changes: 4 additions & 2 deletions cmd/wego/app/pause/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)
Expand All @@ -34,14 +35,15 @@ func runCmd(cmd *cobra.Command, args []string) error {
params.Name = args[0]

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, 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)
appService := app.New(logger, nil, fluxClient, kubeClient, osysClient)

if err := appService.Pause(params); err != nil {
return errors.Wrapf(err, "failed to pause the app %s", params.Name)
Expand Down
6 changes: 4 additions & 2 deletions cmd/wego/app/status/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/weaveworks/weave-gitops/pkg/git"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/logger"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)
Expand All @@ -27,7 +28,8 @@ var Cmd = &cobra.Command{
params.Namespace, _ = cmd.Parent().Parent().Flags().GetString("namespace")

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
kubeClient, err := kube.NewKubeHTTPClient()
if err != nil {
return fmt.Errorf("error initializing kube client: %w", err)
Expand All @@ -36,7 +38,7 @@ var Cmd = &cobra.Command{
gitClient := git.New(nil)
logger := logger.New(os.Stdout)

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

fluxOutput, lastSuccessReconciliation, err := appService.Status(params)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions cmd/wego/app/unpause/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)
Expand All @@ -34,14 +35,15 @@ func runCmd(cmd *cobra.Command, args []string) error {
params.Name = args[0]

cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, 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)
appService := app.New(logger, nil, fluxClient, kubeClient, osysClient)

if err := appService.Unpause(params); err != nil {
return errors.Wrapf(err, "failed to unpause the app %s", params.Name)
Expand Down
24 changes: 15 additions & 9 deletions cmd/wego/flux/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package flux

import (
"fmt"
"os"
"os/exec"

"github.com/spf13/cobra"
fluxBin "github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/shims"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
)

var Cmd = &cobra.Command{
Expand All @@ -30,10 +30,13 @@ func init() {

// Example flux command with flags 'wego flux -- install -h'
func runCmd(cmd *cobra.Command, args []string) {
exePath, err := fluxBin.GetFluxExePath()
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
exePath, err := fluxClient.GetExePath()
if err != nil {
fmt.Fprintf(shims.Stderr(), "Error: %v\n", err)
os.Exit(1)
fmt.Fprintf(osysClient.Stderr(), "Error: %v\n", err)
osysClient.Exit(1)
}

c := exec.Command(exePath, args...)
Expand All @@ -44,10 +47,13 @@ func runCmd(cmd *cobra.Command, args []string) {
}

func runStatusCmd(cmd *cobra.Command, args []string) {
status, err := fluxBin.GetLatestStatusAllNamespaces()
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
status, err := fluxClient.GetLatestStatusAllNamespaces()
if err != nil {
fmt.Fprintf(shims.Stderr(), "Error: %v\n", err)
os.Exit(1)
fmt.Fprintf(osysClient.Stderr(), "Error: %v\n", err)
osysClient.Exit(1)
}
fmt.Printf("Status: %s\n", status)
}
7 changes: 5 additions & 2 deletions cmd/wego/gitops/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"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/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services/gitops"
)
Expand Down Expand Up @@ -72,7 +73,8 @@ func init() {

func installRunCmd(cmd *cobra.Command, args []string) error {
cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
kubeClient := kube.New(cliRunner)

gitopsService := gitops.New(logger.New(os.Stdout), fluxClient, kubeClient)
Expand All @@ -94,7 +96,8 @@ func installRunCmd(cmd *cobra.Command, args []string) error {

func uninstallRunCmd(cmd *cobra.Command, args []string) error {
cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
kubeClient := kube.New(cliRunner)

gitopsService := gitops.New(logger.New(os.Stdout), fluxClient, kubeClient)
Expand Down
19 changes: 12 additions & 7 deletions cmd/wego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ import (
"github.com/weaveworks/weave-gitops/cmd/wego/gitops"
"github.com/weaveworks/weave-gitops/cmd/wego/version"
fluxBin "github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
)

var options struct {
verbose bool
}

var rootCmd = &cobra.Command{
Use: "wego",
Use: "wego",
SilenceUsage: true,
SilenceErrors: true,
Short: "Weave GitOps",
Long: "Command line utility for managing Kubernetes applications via GitOps.",
Example:`
Example: `
# Get verbose output for any wego command
wego [command] -v, --verbose
Expand All @@ -37,10 +39,10 @@ var rootCmd = &cobra.Command{
# Add application to wego control from a github repository
wego app add \
--name <myapp> \
--url [email protected]:myorg/<myapp> \
--private-key ${HOME}/.ssh/<SSH key for myapp> \
--branch prod-<myapp>
--name <myapp> \
--url [email protected]:myorg/<myapp> \
--private-key ${HOME}/.ssh/<SSH key for myapp> \
--branch prod-<myapp>
# Get status of application under wego control
wego app status podinfo
Expand Down Expand Up @@ -73,7 +75,10 @@ func configureLogger() {
}

func main() {
fluxBin.SetupFluxBin()
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := fluxBin.New(osysClient, cliRunner)
fluxClient.SetupBin()
rootCmd.PersistentFlags().BoolVarP(&options.verbose, "verbose", "v", false, "Enable verbose output")
rootCmd.PersistentFlags().String("namespace", "wego-system", "gitops runtime namespace")

Expand Down
4 changes: 3 additions & 1 deletion cmd/wego/version/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/weaveworks/go-checkpoint"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -73,6 +74,7 @@ func CheckpointParamsWithFlags(params *checkpoint.CheckParams, c *cobra.Command)
}
func CheckFluxVersion() (string, error) {
cliRunner := &runner.CLIRunner{}
fluxClient := flux.New(cliRunner)
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
return fluxClient.GetVersion()
}
26 changes: 13 additions & 13 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package flux

import (
"bytes"
"fmt"
"os"
"strings"

"github.com/pkg/errors"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/version"
)
Expand All @@ -16,6 +15,9 @@ import (

//counterfeiter:generate . Flux
type Flux interface {
SetupBin()
GetBinPath() (string, error)
GetExePath() (string, error)
Install(namespace string, export bool) ([]byte, error)
Uninstall(namespace string, export bool) error
CreateSourceGit(name string, url string, branch string, secretRef string, namespace string) ([]byte, error)
Expand All @@ -27,14 +29,17 @@ type Flux interface {
GetVersion() (string, error)
GetAllResourcesStatus(name string, namespace string) ([]byte, error)
SuspendOrResumeApp(pause wego.SuspendActionType, name, namespace, deploymentType string) ([]byte, error)
GetLatestStatusAllNamespaces() ([]string, error)
}

type FluxClient struct {
osys osys.Osys
runner runner.Runner
}

func New(cliRunner runner.Runner) *FluxClient {
func New(osysClient osys.Osys, cliRunner runner.Runner) *FluxClient {
return &FluxClient{
osys: osysClient,
runner: cliRunner,
}
}
Expand Down Expand Up @@ -181,20 +186,15 @@ func (f *FluxClient) CreateSecretGit(name string, url string, namespace string)
"create", "secret", "git", name,
"--url", url,
"--namespace", namespace,
"--export",
}

out, err := f.runFluxCmd(args...)
if err != nil {
return out, fmt.Errorf("failed to create secret git: %w", err)
}

deployKeyBody := bytes.TrimPrefix(out, []byte("✚ deploy key: "))
deployKeyLines := bytes.Split(deployKeyBody, []byte("\n"))
if len(deployKeyBody) == 0 {
return nil, fmt.Errorf("error getting deploy key from flux output: %s", string(out))
}

return deployKeyLines[0], nil
return out, nil
}

func (f *FluxClient) GetAllResourcesStatus(name string, namespace string) ([]byte, error) {
Expand Down Expand Up @@ -227,7 +227,7 @@ func (f *FluxClient) runFluxCmd(args ...string) ([]byte, error) {
}
out, err := f.runner.Run(fluxPath, args...)
if err != nil {
return []byte{}, fmt.Errorf("failed to run flux with output: %s", string(out))
return []byte{}, fmt.Errorf("failed to run flux with output: %s and error: %w", string(out), err)
}

return out, nil
Expand All @@ -240,14 +240,14 @@ func (f *FluxClient) runFluxCmdOutputStream(args ...string) ([]byte, error) {
}
out, err := f.runner.RunWithOutputStream(fluxPath, args...)
if err != nil {
return []byte{}, fmt.Errorf("failed to run flux with output: %s", string(out))
return []byte{}, fmt.Errorf("failed to run flux with output: %s and error: %w", string(out), err)
}

return out, nil
}

func (f *FluxClient) fluxPath() (string, error) {
homeDir, err := os.UserHomeDir()
homeDir, err := f.osys.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "failed getting user home directory")
}
Expand Down
Loading

0 comments on commit f626800

Please sign in to comment.