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

fix: Fix filepath setup #198

Merged
merged 1 commit into from
May 28, 2024
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
4 changes: 3 additions & 1 deletion dockerfiles/harness/base.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ COPY --chown=nonroot --from=environment /bin/sh /bin/sh
COPY --chown=nonroot --from=environment /bin/sleep /bin/sleep
COPY --from=builder /plural/harness /harness

ENTRYPOINT ["/harness", "--working-dir=plural"]
WORKDIR /plural

ENTRYPOINT ["/harness", "--working-dir=/plural"]
2 changes: 1 addition & 1 deletion internal/helpers/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func EnsureFileOrDie(file string) string {
return f.Name()
}

func IsExists(file string) bool {
func Exists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
5 changes: 5 additions & 0 deletions internal/helpers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"os"
"path/filepath"
)

var (
Expand All @@ -19,5 +20,9 @@ func File() FileClient {
type fileClient struct{}

func (in *fileClient) Create(path, content string) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}

return os.WriteFile(path, []byte(content), 0644)
}
2 changes: 1 addition & 1 deletion pkg/harness/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (in *stackRunController) prepare() error {
env := environment.New(
environment.WithStackRun(in.stackRun),
environment.WithWorkingDir(in.dir),
environment.WithFilesDir(in.execWorkDir()),
environment.WithFilesDir(in.dir),
environment.WithFetchClient(in.fetchClient),
)

Expand Down
4 changes: 1 addition & 3 deletions pkg/harness/environment/environment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package environment

import (
"path"

"k8s.io/klog/v2"

"github.com/pluralsh/deployment-operator/internal/helpers"
Expand Down Expand Up @@ -38,7 +36,7 @@ func (in *environment) prepareFiles() error {
}

for _, fragment := range in.stackRun.Files {
destination := path.Join(in.filesDir, fragment.Path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC it will require user to provide full path including harness working dir, i.e. /plural/tf-test/.... Isn't it more prone to errors? Any other root level dir than /plural will not work currently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to specify . as the mount path, and then it would be something like ./creds.json. I do think we should probably make some other folders writable though i just can't think of which at the moment (also think there probably will need to be a home folder for the user).

The problem I faced though was I didn't know the path at config-time at first, so it was a bit tricky to set things like GOOGLE_APPLICATION_CREDENTIALS properly, if you have full understanding of the path that's more straightforward.

destination := fragment.Path
if err := helpers.File().Create(destination, fragment.Content); err != nil {
klog.ErrorS(err, "failed preparing files", "path", destination)
return err
Expand Down
16 changes: 7 additions & 9 deletions pkg/harness/tool/terraform/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"path"

"github.com/pluralsh/polly/algorithms"
"github.com/samber/lo"

"github.com/pluralsh/deployment-operator/internal/helpers"
)
Expand All @@ -20,9 +20,7 @@ func NewInitModifier() *InitModifier {

// Args implements exec.ArgsModifier type.
func (in *PlanModifier) Args(args []string) []string {
if algorithms.Index(args, func(a string) bool {
return a == "plan"
}) < 0 {
if !lo.Contains(args, "plan") {
return args
}

Expand All @@ -34,11 +32,11 @@ func NewPlanModifier(planFileName string) *PlanModifier {
}

func (in *ApplyModifier) Args(args []string) []string {
if !helpers.IsExists(path.Join(in.dir, in.planFileName)) ||
// This is to avoid doubling plan file arg if API already adds it
algorithms.Index(args, func(a string) bool {
return a == in.planFileName
}) >= 0 {
if !lo.Contains(args, "apply") {
return args
}

if !helpers.Exists(path.Join(in.dir, in.planFileName)) || lo.Contains(args, in.planFileName) {
return args
}

Expand Down
Loading