Skip to content

Commit 305c646

Browse files
author
Mohd Uzair
authored
Merge pull request #574 from MUzairS15/MUzairS15/improve/helm-dryrun
peform helm dry run based on the k8s model available in the registry
2 parents 836d0eb + 9dac74d commit 305c646

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

generators/github/git_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func fileInterceptor(br *bufio.Writer) walker.FileInterceptor {
105105
// Add more calrifying commment and entry inside docs.
106106
func dirInterceptor(br *bufio.Writer) walker.DirInterceptor {
107107
return func(d walker.Directory) error {
108-
err := helm.ConvertToK8sManifest(d.Path, br)
108+
err := helm.ConvertToK8sManifest(d.Path, "", br)
109109
if err != nil {
110110
return err
111111
}

generators/github/url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func ProcessContent(w io.Writer, downloadDirPath, downloadfilePath string) error
7777
}
7878

7979
err = utils.ProcessContent(downloadDirPath, func(path string) error {
80-
err = helm.ConvertToK8sManifest(path, w)
80+
err = helm.ConvertToK8sManifest(path, "", w)
8181
if err != nil {
8282
return err
8383
}

utils/helm/helm.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func extractSemVer(versionConstraint string) string {
2626
}
2727

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

38-
version := "v1.29.5" // Default version
39-
38+
kubeVersion := kubernetesVersion
4039
if chart.Metadata.KubeVersion != "" {
4140
extractedVersion := extractSemVer(chart.Metadata.KubeVersion)
41+
4242
if extractedVersion != "" {
43-
version = extractedVersion
44-
}
43+
kubeVersion = extractedVersion
44+
}
4545
}
4646

47-
act.KubeVersion = &chartutil.KubeVersion{
48-
Version: version,
47+
if kubeVersion != "" {
48+
act.KubeVersion = &chartutil.KubeVersion{
49+
Version: kubeVersion,
50+
}
4951
}
50-
52+
5153
rel, err := act.Run(chart, nil)
5254
if err != nil {
5355
return nil, ErrDryRunHelmChart(err, chart.Name())
@@ -61,7 +63,7 @@ func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
6163
}
6264

6365
// Takes in the directory and converts HelmCharts/multiple manifests into a single K8s manifest
64-
func ConvertToK8sManifest(path string, w io.Writer) error {
66+
func ConvertToK8sManifest(path, kubeVersion string, w io.Writer) error {
6567
info, err := os.Stat(path)
6668
if err != nil {
6769
return utils.ErrReadDir(err, path)
@@ -71,7 +73,7 @@ func ConvertToK8sManifest(path string, w io.Writer) error {
7173
helmChartPath, _ = strings.CutSuffix(path, filepath.Base(path))
7274
}
7375
if IsHelmChart(helmChartPath) {
74-
err := LoadHelmChart(helmChartPath, w, true)
76+
err := LoadHelmChart(helmChartPath, w, true, kubeVersion)
7577
if err != nil {
7678
return err
7779
}
@@ -130,7 +132,7 @@ func IsHelmChart(dirPath string) bool {
130132
return true
131133
}
132134

133-
func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool) error {
135+
func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool, kubeVersion string) error {
134136
var errs []error
135137
chart, err := loader.Load(path)
136138
if err != nil {
@@ -151,7 +153,7 @@ func LoadHelmChart(path string, w io.Writer, extractOnlyCrds bool) error {
151153
_, _ = w.Write([]byte("\n---\n"))
152154
}
153155
} else {
154-
manifests, err := DryRunHelmChart(chart)
156+
manifests, err := DryRunHelmChart(chart, kubeVersion)
155157
if err != nil {
156158
return ErrLoadHelmChart(err, path)
157159
}

utils/kubernetes/apply-helm-chart.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ type ApplyHelmChartConfig struct {
122122
// SkipCRDs while installation
123123
SkipCRDs bool
124124

125+
// Kubernetes version against which the DryRun is performed
126+
KubernetesVersion string
127+
125128
// Upgrade the release if already installed
126129
UpgradeIfInstalled bool
127130

@@ -451,6 +454,7 @@ func generateAction(actionConfig *action.Configuration, cfg ApplyHelmChartConfig
451454
case UNINSTALL:
452455
return func(c *chart.Chart) error {
453456
act := action.NewUninstall(actionConfig)
457+
454458
act.DryRun = cfg.DryRun
455459
if _, err := act.Run(cfg.ReleaseName); err != nil {
456460
return ErrApplyHelmChart(err)

utils/kubernetes/helm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ func ConvertHelmChartToK8sManifest(cfg ApplyHelmChartConfig) (manifest []byte, e
2323
return nil, ErrApplyHelmChart(err)
2424
}
2525

26-
return helm.DryRunHelmChart(helmChart)
26+
return helm.DryRunHelmChart(helmChart, cfg.KubernetesVersion)
2727
}

0 commit comments

Comments
 (0)