Skip to content

Commit

Permalink
Merge pull request #367 from fluxcd/fix-include
Browse files Browse the repository at this point in the history
Fix GitRepository include for nested paths
  • Loading branch information
stefanprodan authored May 28, 2021
2 parents fd636d6 + add5444 commit 48efbd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
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

0 comments on commit 48efbd1

Please sign in to comment.