Skip to content

Commit

Permalink
Use system flux instead of an embedded flux
Browse files Browse the repository at this point in the history
This just calls "flux" instead of a specific path - it has the neat
side-effect that instead of using WEAVE_GITOPS_FLUX_BIN_PATH, you can
just set your own flux bin path using the PATH variable.

Right now, there are only two things we do with this flux: creating a
secret, and wrapping `flux check` in `gitops check`.

The gitops check should end up removing the flux check.

The secret is part of accessing private git repos, which is called by
auth which is called by both upgrade and profiles.

There's still a flux installed by `make dependencies` - I've adapted
the auth tests to use the one installed by that, but as it's not added
to your path, it's not going to be used by the local CLI. Flux is now
a dependency I expect you to install yourself (will not document right
now, but note to self to add to the documentation #1532 is adding).
  • Loading branch information
Robin Sonefors committed Mar 3, 2022
1 parent 53ff716 commit 11b6670
Show file tree
Hide file tree
Showing 22 changed files with 15 additions and 1,274 deletions.
3 changes: 1 addition & 2 deletions cmd/gitops/add/profiles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/models"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/server"
"github.com/weaveworks/weave-gitops/pkg/services"
Expand Down Expand Up @@ -64,7 +63,7 @@ func addProfileCmdRunE() func(*cobra.Command, []string) error {
rand.Seed(time.Now().UnixNano())

log := internal.NewCLILogger(os.Stdout)
fluxClient := flux.New(osys.New(), &runner.CLIRunner{})
fluxClient := flux.New(&runner.CLIRunner{})
factory := services.NewFactory(fluxClient, log)
providerClient := internal.NewGitProviderClient(os.Stdout, os.LookupEnv, auth.NewAuthCLIHandler, log)

Expand Down
3 changes: 1 addition & 2 deletions cmd/gitops/check/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/weaveworks/weave-gitops/pkg/services/check"
"github.com/weaveworks/weave-gitops/pkg/version"

"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"

"github.com/weaveworks/weave-gitops/pkg/flux"
Expand Down Expand Up @@ -37,7 +36,7 @@ func init() {
func runCmd(_ *cobra.Command, _ []string) error {
ctx := context.Background()

fluxClient := flux.New(osys.New(), &runner.CLIRunner{})
fluxClient := flux.New(&runner.CLIRunner{})

rest, clusterName, err := kube.RestConfig()
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions cmd/gitops/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import (
"github.com/weaveworks/weave-gitops/cmd/gitops/ui"
"github.com/weaveworks/weave-gitops/cmd/gitops/upgrade"
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
fluxBin "github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/utils"
"k8s.io/client-go/rest"
)
Expand All @@ -46,11 +43,6 @@ func init() {
}

func RootCmd(client *resty.Client) *cobra.Command {
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := fluxBin.New(osysClient, cliRunner)
fluxClient.SetupBin()

var rootCmd = &cobra.Command{
Use: "gitops",
SilenceUsage: true,
Expand Down
3 changes: 1 addition & 2 deletions cmd/gitops/upgrade/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/weaveworks/weave-gitops/cmd/internal"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services"
"github.com/weaveworks/weave-gitops/pkg/services/auth"
Expand Down Expand Up @@ -65,7 +64,7 @@ func upgradeCmdRunE() func(*cobra.Command, []string) error {
upgradeCmdFlags.Namespace = namespace

log := internal.NewCLILogger(os.Stdout)
fluxClient := flux.New(osys.New(), &runner.CLIRunner{})
fluxClient := flux.New(&runner.CLIRunner{})
factory := services.NewFactory(fluxClient, log)

wegoConfig, err := kubeClient.GetWegoConfig(ctx, namespace)
Expand Down
2 changes: 1 addition & 1 deletion gitops.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ RUN ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
COPY Makefile /app/
COPY tools /app/tools
WORKDIR /app
COPY --from=flux /usr/local/bin/flux /app/pkg/flux/bin/flux
COPY go.* /app/
RUN go mod download
COPY . /app
RUN make gitops

# Distroless
FROM gcr.io/distroless/base as runtime
COPY --from=flux /usr/local/bin/flux /usr/local/bin/flux
COPY --from=go-build /app/bin/gitops /gitops
COPY --from=go-build /root/.ssh/known_hosts /root/.ssh/known_hosts

Expand Down
3 changes: 0 additions & 3 deletions pkg/flux/bin/go_embed_fs_cannot_be_empty.txt

This file was deleted.

42 changes: 2 additions & 40 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ package flux
import (
"fmt"

"github.com/pkg/errors"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/version"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

//counterfeiter:generate . Flux
type Flux interface {
SetupBin()
CreateSecretGit(name string, repoUrl gitproviders.RepoURL, namespace string) ([]byte, error)
GetAllResourcesStatus(name string, namespace string) ([]byte, error)
PreCheck() (string, error)
}

Expand All @@ -26,16 +21,12 @@ const (
VersionLabelKey = "app.kubernetes.io/version"
)

const fluxBinaryPathEnvVar = "WEAVE_GITOPS_FLUX_BIN_PATH"

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

func New(osysClient osys.Osys, cliRunner runner.Runner) *FluxClient {
func New(cliRunner runner.Runner) *FluxClient {
return &FluxClient{
osys: osysClient,
runner: cliRunner,
}
}
Expand All @@ -59,44 +50,15 @@ func (f *FluxClient) CreateSecretGit(name string, repoUrl gitproviders.RepoURL,
return out, nil
}

func (f *FluxClient) GetAllResourcesStatus(name string, namespace string) ([]byte, error) {
args := []string{
"get", "all", "--namespace", namespace, name,
}

out, err := f.runFluxCmd(args...)
if err != nil {
return out, fmt.Errorf("failed to get flux resources status: %w", err)
}

return out, nil
}

func (f *FluxClient) runFluxCmd(args ...string) ([]byte, error) {
fluxPath, err := f.fluxPath()
if err != nil {
return []byte{}, errors.Wrap(err, "error getting flux binary path")
}

out, err := f.runner.Run(fluxPath, args...)
out, err := f.runner.Run("flux", args...)
if err != nil {
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 := f.osys.UserHomeDir()
if err != nil {
return "", errors.Wrap(err, "failed getting user home directory")
}

path := fmt.Sprintf("%v/.wego/bin", homeDir)

return fmt.Sprintf("%v/flux-%v", path, version.FluxVersion), nil
}

func (f *FluxClient) PreCheck() (string, error) {
args := []string{
"check",
Expand Down
9 changes: 2 additions & 7 deletions pkg/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flux_test

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -11,7 +10,6 @@ import (
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner/runnerfakes"
)

Expand All @@ -22,7 +20,7 @@ var (

var _ = BeforeEach(func() {
runner = &runnerfakes.FakeRunner{}
fluxClient = flux.New(osys.New(), runner)
fluxClient = flux.New(runner)
})

var _ = Describe("CreateSecretGit", func() {
Expand All @@ -47,8 +45,5 @@ var _ = Describe("CreateSecretGit", func() {
})

func fluxPath() string {
homeDir, err := os.UserHomeDir()
Expect(err).ShouldNot(HaveOccurred())

return filepath.Join(homeDir, ".wego", "bin", "flux-0.12.0")
return filepath.Join("flux")
}
111 changes: 0 additions & 111 deletions pkg/flux/fluxfakes/fake_flux.go

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

0 comments on commit 11b6670

Please sign in to comment.