diff --git a/pkg/charts/chart.go b/pkg/charts/chart.go index 54321f92..854ebdb6 100644 --- a/pkg/charts/chart.go +++ b/pkg/charts/chart.go @@ -2,6 +2,7 @@ package charts import ( "fmt" + "path/filepath" "github.com/blang/semver" "github.com/go-git/go-billy/v5" @@ -10,7 +11,6 @@ import ( "github.com/rancher/charts-build-scripts/pkg/helm" "github.com/rancher/charts-build-scripts/pkg/path" "github.com/rancher/charts-build-scripts/pkg/puller" - "github.com/sirupsen/logrus" ) // Chart represents the main chart in a given package @@ -35,7 +35,6 @@ func (c *Chart) Prepare(rootFs, pkgFs billy.Filesystem) error { upstreamChartVersion := "" defer func() { c.upstreamChartVersion = &upstreamChartVersion }() if c.Upstream.IsWithinPackage() { - logrus.Infof("Local chart does not need to be prepared") // Ensure local charts standardize the Chart.yaml on prepare if err := helm.StandardizeChartYaml(pkgFs, c.WorkingDir); err != nil { return err @@ -43,6 +42,9 @@ func (c *Chart) Prepare(rootFs, pkgFs billy.Filesystem) error { if err := PrepareDependencies(rootFs, pkgFs, c.WorkingDir, c.GeneratedChangesRootDir(), c.IgnoreDependencies); err != nil { return fmt.Errorf("encountered error while trying to prepare dependencies in %s: %s", c.WorkingDir, err) } + if err := change.ApplyChanges(pkgFs, c.WorkingDir, c.GeneratedChangesRootDir()); err != nil { + return fmt.Errorf("encountered error while trying to apply changes to %s: %s", c.WorkingDir, err) + } return nil } if err := filesystem.RemoveAll(pkgFs, c.WorkingDir); err != nil { @@ -72,15 +74,26 @@ func (c *Chart) Prepare(rootFs, pkgFs billy.Filesystem) error { // GeneratePatch generates a patch on a forked Helm chart based on local changes func (c *Chart) GeneratePatch(rootFs, pkgFs billy.Filesystem) error { - if c.Upstream.IsWithinPackage() { - logrus.Infof("Local chart does not need to be patched") - return nil - } + // Check if working directory exists if exists, err := filesystem.PathExists(pkgFs, c.WorkingDir); err != nil { return fmt.Errorf("encountered error while checking if %s exist: %s", c.WorkingDir, err) } else if !exists { return fmt.Errorf("working directory %s has not been prepared yet", c.WorkingDir) } + // Check if dependencies exist + depMap, err := GetDependencyMap(pkgFs, c.GeneratedChangesRootDir()) + if err != nil { + return fmt.Errorf("encountered error while finding dependencies: %s", err) + } + if len(depMap) > 0 { + // Check if expected dependencies have been prepared + subchartDir := filepath.Join(c.WorkingDir, path.ChartDependenciesDir) + if exists, err := filesystem.PathExists(pkgFs, subchartDir); err != nil { + return fmt.Errorf("encountered error while checking if %s exist: %s", subchartDir, err) + } else if !exists { + return fmt.Errorf("subcharts directory %s has not been prepared yet", subchartDir) + } + } // Standardize the local copy of the Chart.yaml before trying to compare the patch if err := helm.StandardizeChartYaml(pkgFs, c.WorkingDir); err != nil { return err diff --git a/pkg/charts/puller.go b/pkg/charts/puller.go index 3743c151..8e5de02e 100644 --- a/pkg/charts/puller.go +++ b/pkg/charts/puller.go @@ -7,6 +7,7 @@ import ( "github.com/go-git/go-billy/v5" "github.com/rancher/charts-build-scripts/pkg/filesystem" "github.com/rancher/charts-build-scripts/pkg/options" + cbsPath "github.com/rancher/charts-build-scripts/pkg/path" "github.com/sirupsen/logrus" ) @@ -83,6 +84,28 @@ type Local struct{} // Pull grabs the Helm chart by preparing the package itself func (u Local) Pull(rootFs, fs billy.Filesystem, path string) error { + packageName := strings.SplitN(fs.Root(), cbsPath.RepositoryPackagesDir, 2)[1] + pkg, err := GetPackage(rootFs, packageName) + if err != nil { + return err + } + if pkg == nil { + return fmt.Errorf("could not find local package %s", packageName) + } + repositoryPackageWorkingDir, err := filesystem.GetRelativePath(rootFs, filesystem.GetAbsPath(pkg.fs, pkg.Chart.WorkingDir)) + if err != nil { + return err + } + if repositoryPackageWorkingDir == path { + return nil + } + repositoryPath, err := filesystem.GetRelativePath(rootFs, filesystem.GetAbsPath(fs, path)) + if err != nil { + return err + } + if err := filesystem.CopyDir(rootFs, repositoryPackageWorkingDir, repositoryPath); err != nil { + return fmt.Errorf("encountered error while copying prepared package into path: %s", err) + } return nil } diff --git a/pkg/path/path.go b/pkg/path/path.go index 79f44a5b..19796da0 100644 --- a/pkg/path/path.go +++ b/pkg/path/path.go @@ -37,6 +37,8 @@ const ( // ChartCRDDir represents the directory that we expect to contain CRDs within the chart ChartCRDDir = "crds" + // ChartCRDDir represents the directory that we expect to contain subcharts + ChartDependenciesDir = "charts" // ChartExtraFileDir represents the directory that contains non-YAML files ChartExtraFileDir = "files" // ChartCRDTgzFilename represents the filename of the crd's tgz file