Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load env from host into Flux exec #3608

Merged
merged 3 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/actions/flux/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ func New(k8sClientSet kubeclient.Interface, opts *api.GitOps) (*Installer, error
return nil, errors.New("expected gitops.flux in cluster configuration but found nil")
}

fluxClient, err := flux.NewClient(opts.Flux)
if err != nil {
return nil, err
}

installer := &Installer{
opts: opts.Flux,
kubeClient: k8sClientSet,
fluxClient: flux.NewClient(opts.Flux),
fluxClient: fluxClient,
}

return installer, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (e ShellExecutor) ExecInDir(command string, dir string, args ...string) err
func (e ShellExecutor) buildCmd(command string, args ...string) *exec.Cmd {
cmd := exec.Command(command, args...)

cmd.Env = os.Environ()
for k, v := range e.envVars {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
Expand Down
45 changes: 17 additions & 28 deletions pkg/flux/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"

Expand All @@ -20,11 +19,16 @@ type Client struct {
opts *api.Flux
}

func NewClient(opts *api.Flux) *Client {
func NewClient(opts *api.Flux) (*Client, error) {
env, err := setTokenEnv(opts.AuthTokenPath, opts.GitProvider)
if err != nil {
return nil, err
}

return &Client{
executor: executor.NewShellExecutor(setEnvVars(opts.AuthTokenPath, opts.GitProvider)),
executor: executor.NewShellExecutor(env),
opts: opts,
}
}, nil
}

func (c *Client) PreFlight() error {
Expand Down Expand Up @@ -69,40 +73,25 @@ func (c *Client) runFluxCmd(args ...string) error {
return c.executor.Exec(fluxBin, args...)
}

func setEnvVars(tokenPath, gitProvider string) executor.EnvVars {
envVars := executor.EnvVars{
"PATH": os.Getenv("PATH"),
"HOME": os.Getenv("HOME"),
"AWS_ACCESS_KEY_ID": os.Getenv("AWS_ACCESS_KEY_ID"),
"AWS_SECRET_ACCESS_KEY": os.Getenv("AWS_SECRET_ACCESS_KEY"),
func setTokenEnv(tokenPath, gitProvider string) (executor.EnvVars, error) {
if tokenPath == "" {
return executor.EnvVars{}, nil
}

var token string
if tokenPath != "" {
data, err := ioutil.ReadFile(tokenPath)
if err != nil {
logger.Warning("reading auth token file %s", err)
}

token = strings.Replace(string(data), "\n", "", -1)
data, err := ioutil.ReadFile(tokenPath)
if err != nil {
return executor.EnvVars{}, fmt.Errorf("reading auth token file %w", err)
}

token = strings.Replace(string(data), "\n", "", -1)
envVars := executor.EnvVars{}
switch gitProvider {
case "github":
if token == "" {
if githubToken, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
token = githubToken
}
}
envVars["GITHUB_TOKEN"] = token
case "gitlab":
if token == "" {
if gitlabToken, ok := os.LookupEnv("GITLAB_TOKEN"); ok {
token = gitlabToken
}
}
envVars["GITLAB_TOKEN"] = token
}

return envVars
return envVars, nil
}
17 changes: 16 additions & 1 deletion pkg/flux/flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ var _ = Describe("Flux", func() {
}

fakeExecutor = new(fakes.FakeExecutor)
fluxClient = flux.NewClient(opts)
var err error
fluxClient, err = flux.NewClient(opts)
Expect(err).NotTo(HaveOccurred())
fluxClient.SetExecutor(fakeExecutor)

binDir, err := ioutil.TempDir("", "bin")
Expand Down Expand Up @@ -180,4 +182,17 @@ var _ = Describe("Flux", func() {
})
})
})

Context("setting token env", func() {
When("given PAT file does not exist", func() {
BeforeEach(func() {
opts.AuthTokenPath = "/road/to/nowhere"
})

It("creating the client should fail", func() {
_, err := flux.NewClient(opts)
Expect(err).To(MatchError(ContainSubstring("reading auth token file open /road/to/nowhere: no such file or directory")))
})
})
})
})