Skip to content

Commit

Permalink
Merge pull request #502 from MUzairS15/MUzairS15/helm/dryrun
Browse files Browse the repository at this point in the history
[Helm] Update DryRun to include Kubernetes version.
  • Loading branch information
MUzairS15 committed Jun 6, 2024
2 parents 37f19fa + 1add990 commit 9650969
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions generators/github/git_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package github
import (
"bufio"
"fmt"
"github.com/layer5io/meshkit/generators/models"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/helm"
"github.com/layer5io/meshkit/utils/walker"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/layer5io/meshkit/generators/models"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/helm"
"github.com/layer5io/meshkit/utils/walker"
)

type GitRepo struct {
Expand Down
19 changes: 19 additions & 0 deletions utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/layer5io/meshkit/utils"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
)

func extractSemVer(versionConstraint string) string {
reg := regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`)
match := reg.Find([]byte(versionConstraint))
if match != nil {
return string(match)
}
return ""
}

// DryRun a given helm chart to convert into k8s manifest
func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
actconfig := new(action.Configuration)
Expand All @@ -23,6 +34,14 @@ func DryRunHelmChart(chart *chart.Chart) ([]byte, error) {
act.DryRun = true
act.IncludeCRDs = true
act.ClientOnly = true
if chart.Metadata.KubeVersion != "" {
version := extractSemVer(chart.Metadata.KubeVersion)
if version != "" {
act.KubeVersion = &chartutil.KubeVersion{
Version: version,
}
}
}
rel, err := act.Run(chart, nil)
if err != nil {
return nil, ErrDryRunHelmChart(err, chart.Name())
Expand Down

0 comments on commit 9650969

Please sign in to comment.