Skip to content

Commit

Permalink
feat: support oci charts
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Davim committed May 18, 2021
1 parent 039f714 commit 2d9c47b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
13 changes: 13 additions & 0 deletions internal/app/helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ func updateChartDep(chartPath string) error {
return nil
}

// helmExportChart pulls chart and exports it to the specified destination
func helmExportChart(chart, dest string) error {
cmd := helmCmd([]string{"chart", "pull", chart}, "Pulling chart [ "+chart+" ] to local registry cache")
if _, err := cmd.Exec(); err != nil {
return err
}
cmd = helmCmd([]string{"chart", "export", chart, "-d", dest}, "Exporting chart [ "+chart+" ] to "+dest)
if _, err := cmd.Exec(); err != nil {
return err
}
return nil
}

// addHelmRepos adds repositories to Helm if they don't exist already.
// Helm does not mind if a repo with the same name exists. It treats it as an update.
func addHelmRepos(repos map[string]string) error {
Expand Down
5 changes: 4 additions & 1 deletion internal/app/state_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ func (s *state) expand(relativeToFile string) {
repoName := strings.Split(r.Chart, "/")[0]
_, isRepo := s.HelmRepos[repoName]
isRepo = isRepo || stringInSlice(repoName, s.PreconfiguredHelmRepos)
// if there is no repo for the chart, we assume it's intended to be a local path
// if there is no repo for the chart, we assume it's intended to be a local path or url
if !isRepo {
if strings.HasPrefix(r.Chart, "oci://") && len(strings.Split(r.Chart, ":")) == 2 {
r.Chart = fmt.Sprintf("%s:%s", r.Chart, r.Version)
}
r.Chart, _ = resolveOnePath(r.Chart, dir, downloadDest)
}
}
Expand Down
35 changes: 22 additions & 13 deletions internal/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func stringInSlice(a string, list []string) bool {
// and downloads/fetches the file locally into helmsman temp directory and returns
// its absolute path
func resolveOnePath(file string, dir string, downloadDest string) (string, error) {
if destFile, err := ioutil.TempFile(downloadDest, fmt.Sprintf("*%s", path.Base(file))); err != nil {
destFile, err := ioutil.TempFile(downloadDest, fmt.Sprintf("*%s", path.Base(file)))
if err != nil {
return "", err
} else {
_ = destFile.Close()
return filepath.Abs(downloadFile(file, dir, destFile.Name()))
}
destFile.Close()
return filepath.Abs(downloadFile(file, dir, destFile.Name()))
}

// createTempDir creates a temp directory in a specific location with a pattern
Expand Down Expand Up @@ -208,6 +208,9 @@ func downloadFile(file string, dir string, outfile string) string {
}

switch u.Scheme {
case "oci":
helmExportChart(strings.ReplaceAll(file, "oci://", ""), filepath.Dir(outfile))
return outfile
case "https", "http":
if err := downloadFileFromURL(file, outfile); err != nil {
log.Fatal(err.Error())
Expand Down Expand Up @@ -446,18 +449,24 @@ func isLocalChart(chart string) bool {

// isValidCert checks if a certificate/key path/URI is valid
func isValidCert(value string) bool {
if _, err := os.Stat(value); err != nil {
_, err1 := url.ParseRequestURI(value)
if err1 != nil || (!strings.HasPrefix(value, "s3://") && !strings.HasPrefix(value, "gs://") && !strings.HasPrefix(value, "az://")) {
return false
}
if _, err := os.Stat(value); err == nil {
return true
}
u, err := url.ParseRequestURI(value)
if err != nil {
return false
}
switch u.Scheme {
case "http", "https", "s3", "gs", "az":
return true
default:
return false
}
return true
}

// isValidFile checks if the file exists in the given path or accessible via http and is of allowed file extension (e.g. yaml, json ...)
func isValidFile(filePath string, allowedFileTypes []string) error {
if strings.HasPrefix(filePath, "http") {
if strings.HasPrefix(filePath, "http") || strings.HasPrefix(filePath, "s3://") || strings.HasPrefix(filePath, "gs://") || strings.HasPrefix(filePath, "az://") {
if _, err := url.ParseRequestURI(filePath); err != nil {
return fmt.Errorf("%s must be valid URL path to a raw file", filePath)
}
Expand All @@ -475,11 +484,11 @@ func checkVersion(version, constraint string) bool {
return false
}

jsonConstraint, err := semver.NewConstraint(constraint)
c, err := semver.NewConstraint(constraint)
if err != nil {
return false
}
return jsonConstraint.Check(v)
return c.Check(v)
}

// notify MSTeams sends a JSON formatted message to MSTeams channel over a webhook url
Expand Down

0 comments on commit 2d9c47b

Please sign in to comment.