Skip to content

Commit

Permalink
Merge pull request #574 from MUzairS15/MUzairS15/improve/helm-dryrun
Browse files Browse the repository at this point in the history
peform helm dry run based on the k8s model available in the registry
  • Loading branch information
MUzairS15 authored Aug 21, 2024
2 parents 836d0eb + 9dac74d commit 305c646
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion generators/github/git_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func fileInterceptor(br *bufio.Writer) walker.FileInterceptor {
// Add more calrifying commment and entry inside docs.
func dirInterceptor(br *bufio.Writer) walker.DirInterceptor {
return func(d walker.Directory) error {
err := helm.ConvertToK8sManifest(d.Path, br)
err := helm.ConvertToK8sManifest(d.Path, "", br)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion generators/github/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ProcessContent(w io.Writer, downloadDirPath, downloadfilePath string) error
}

err = utils.ProcessContent(downloadDirPath, func(path string) error {
err = helm.ConvertToK8sManifest(path, w)
err = helm.ConvertToK8sManifest(path, "", w)
if err != nil {
return err
}
Expand Down
26 changes: 14 additions & 12 deletions utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func extractSemVer(versionConstraint string) string {
}

// DryRun a given helm chart to convert into k8s manifest
func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
func DryRunHelmChart(chart *chart.Chart, kubernetesVersion string) ([]byte, error) {
actconfig := new(action.Configuration)
act := action.NewInstall(actconfig)
act.ReleaseName = chart.Metadata.Name
Expand All @@ -35,19 +35,21 @@ func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
act.IncludeCRDs = true
act.ClientOnly = true

version := "v1.29.5" // Default version

kubeVersion := kubernetesVersion
if chart.Metadata.KubeVersion != "" {
extractedVersion := extractSemVer(chart.Metadata.KubeVersion)

if extractedVersion != "" {
version = extractedVersion
}
kubeVersion = extractedVersion
}
}

act.KubeVersion = &chartutil.KubeVersion{
Version: version,
if kubeVersion != "" {
act.KubeVersion = &chartutil.KubeVersion{
Version: kubeVersion,
}
}

rel, err := act.Run(chart, nil)
if err != nil {
return nil, ErrDryRunHelmChart(err, chart.Name())
Expand All @@ -61,7 +63,7 @@ func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
}

// Takes in the directory and converts HelmCharts/multiple manifests into a single K8s manifest
func ConvertToK8sManifest(path string, w io.Writer) error {
func ConvertToK8sManifest(path, kubeVersion string, w io.Writer) error {
info, err := os.Stat(path)
if err != nil {
return utils.ErrReadDir(err, path)
Expand All @@ -71,7 +73,7 @@ func ConvertToK8sManifest(path string, w io.Writer) error {
helmChartPath, _ = strings.CutSuffix(path, filepath.Base(path))
}
if IsHelmChart(helmChartPath) {
err := LoadHelmChart(helmChartPath, w, true)
err := LoadHelmChart(helmChartPath, w, true, kubeVersion)
if err != nil {
return err
}
Expand Down Expand Up @@ -130,7 +132,7 @@ func IsHelmChart(dirPath string) bool {
return true
}

func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool) error {
func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool, kubeVersion string) error {
var errs []error
chart, err := loader.Load(path)
if err != nil {
Expand All @@ -151,7 +153,7 @@ func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool) error {
_, _ = w.Write([]byte("\n---\n"))
}
} else {
manifests, err := DryRunHelmChart(chart)
manifests, err := DryRunHelmChart(chart, kubeVersion)
if err != nil {
return ErrLoadHelmChart(err, path)
}
Expand Down
4 changes: 4 additions & 0 deletions utils/kubernetes/apply-helm-chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type ApplyHelmChartConfig struct {
// SkipCRDs while installation
SkipCRDs bool

// Kubernetes version against which the DryRun is performed
KubernetesVersion string

// Upgrade the release if already installed
UpgradeIfInstalled bool

Expand Down Expand Up @@ -451,6 +454,7 @@ func generateAction(actionConfig *action.Configuration, cfg ApplyHelmChartConfig
case UNINSTALL:
return func(c *chart.Chart) error {
act := action.NewUninstall(actionConfig)

act.DryRun = cfg.DryRun
if _, err := act.Run(cfg.ReleaseName); err != nil {
return ErrApplyHelmChart(err)
Expand Down
2 changes: 1 addition & 1 deletion utils/kubernetes/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func ConvertHelmChartToK8sManifest(cfg ApplyHelmChartConfig) (manifest []byte, e
return nil, ErrApplyHelmChart(err)
}

return helm.DryRunHelmChart(helmChart)
return helm.DryRunHelmChart(helmChart, cfg.KubernetesVersion)
}

0 comments on commit 305c646

Please sign in to comment.