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 GitRepository include for nested paths #367

Merged
merged 1 commit into from
May 28, 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
6 changes: 6 additions & 0 deletions controllers/gitrepository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,12 @@ var _ = Describe("GitRepositoryReconciler", func() {
createFiles: []string{"dir1", "dir2"},
checkFiles: []string{"sub/dir1", "sub/dir2"},
}),
Entry("to nested path", includeTestCase{
fromPath: "",
toPath: "sub/nested",
createFiles: []string{"dir1", "dir2"},
checkFiles: []string{"sub/nested/dir1", "sub/nested/dir2"},
}),
Entry("from and to path", includeTestCase{
fromPath: "nested",
toPath: "sub",
Expand Down
16 changes: 12 additions & 4 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,35 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
return s.Copy(artifact, f)
}

// CopyToPath copies the contents of the given atrifact to the path.
func (s *Storage) CopyToPath(atrifact *sourcev1.Artifact, subPath, toPath string) error {
// CopyToPath copies the contents of the given artifact to the path.
func (s *Storage) CopyToPath(artifact *sourcev1.Artifact, subPath, toPath string) error {
// create a tmp directory to store artifact
tmp, err := ioutil.TempDir("", "flux-include")
if err != nil {
return err
}
defer os.RemoveAll(tmp)

// read artifact file content
localPath := s.LocalPath(*atrifact)
localPath := s.LocalPath(*artifact)
f, err := os.Open(localPath)
if err != nil {
return err
}
defer f.Close()

// untar the artifact
untarPath := filepath.Join(tmp, "tar")
if _, err = untar.Untar(f, untarPath); err != nil {
return err
}
// copy the folder to the path

// create the destination parent dir
if err = os.MkdirAll(filepath.Dir(toPath), os.ModePerm); err != nil {
return err
}

// copy the artifact content to the destination dir
fromPath := filepath.Join(untarPath, subPath)
if err := fs.RenameWithFallback(fromPath, toPath); err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions docs/spec/v1beta1/gitrepositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type GitRepositorySpec struct {
// This option is available only when using the 'go-git' GitImplementation.
// +optional
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`

// Extra git repositories to map into the repository
Include []GitRepositoryInclude `json:"include,omitempty"`
}
```

Expand Down Expand Up @@ -529,8 +532,8 @@ spec:
include:
- repository:
name: app-repo
from: deploy/kubernetes
to: base/app
fromPath: deploy/kubernetes
toPath: base/app
---
apiVersion: v1
kind: Secret
Expand All @@ -543,9 +546,9 @@ data:
password: <GitHub Token>
```

The `from` and `to` parameters allows you to limit the files included and where they will be
copied to in the main repository. If you do not specify a value for `from` all files in the
repository will be included. The `to` value will default to the name of the repository.
The `fromPath` and `toPath` parameters allows you to limit the files included and where they will be
copied to in the main repository. If you do not specify a value for `fromPath` all files in the
repository will be included. The `toPath` value will default to the name of the repository.

## Status examples

Expand Down