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

Support patching dependencies for local charts #94

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
25 changes: 19 additions & 6 deletions pkg/charts/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package charts

import (
"fmt"
"path/filepath"

"github.com/blang/semver"
"github.com/go-git/go-billy/v5"
Expand All @@ -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
Expand All @@ -35,14 +35,16 @@ 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
}
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 {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pkg/charts/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down