Skip to content

Commit

Permalink
Merge kptfiles from newly added subpackages in upstream (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Mar 22, 2021
1 parent f56f41f commit 57e578e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
29 changes: 29 additions & 0 deletions internal/util/update/resource-merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ func MergeSubPackages(localRoot, updatedRoot, originalRoot string) error {
return err
}
}

// there could be subpackage which are not present in local but added in upstream
// we should copy the Kptfiles from newly added subpackages in upstream to local

// get the Kptfile paths from the subdirectories of updated package recursively
updatedPkgPaths, err := pathutil.DirsWithFile(updatedRoot, kptfile.KptFileName, true)
if err != nil {
return err
}

for _, updatedPkgPath := range updatedPkgPaths {
cleanUpdatedPkgPath := filepath.Clean(updatedPkgPath)
relativePkgPath, err := filepath.Rel(updatedRoot, cleanUpdatedPkgPath)
if err != nil {
return err
}

if !fileExists(filepath.Join(localRoot, relativePkgPath, kptfile.KptFileName)) {
err = os.MkdirAll(filepath.Join(localRoot, relativePkgPath), 0700)
if err != nil {
return err
}
err = copyutil.SyncFile(filepath.Join(updatedPkgPath, kptfile.KptFileName),
filepath.Join(localRoot, relativePkgPath, kptfile.KptFileName))
if err != nil {
return err
}
}
}
return nil
}

Expand Down
10 changes: 6 additions & 4 deletions internal/util/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ func TestCommand_Run_subpackages(t *testing.T) {
expectedLocal *pkgbuilder.Pkg
}{
{
// TODO(mortent): This does not handle Kptfiles correctly.
name: "update fetches any new subpackages",
initialUpstream: pkgbuilder.NewPackage("foo").
WithKptfile().
Expand All @@ -727,9 +726,11 @@ func TestCommand_Run_subpackages(t *testing.T) {
pkgbuilder.NewPackage("bar").
WithKptfile().
WithSubPackages(
pkgbuilder.NewPackage("nestedbar"),
pkgbuilder.NewPackage("nestedbar").
WithKptfile(),
),
pkgbuilder.NewPackage("zork"),
pkgbuilder.NewPackage("zork").
WithKptfile(),
),
},
{
Expand Down Expand Up @@ -786,7 +787,8 @@ func TestCommand_Run_subpackages(t *testing.T) {
WithSubPackages(
pkgbuilder.NewPackage("bar").
WithKptfile(),
pkgbuilder.NewPackage("zork"),
pkgbuilder.NewPackage("zork").
WithKptfile(),
pkgbuilder.NewPackage("abc").
WithKptfile(),
),
Expand Down

0 comments on commit 57e578e

Please sign in to comment.