Skip to content

Commit

Permalink
Merge pull request #156 from Revolyssup/charttoapp
Browse files Browse the repository at this point in the history
Add function for getting app version from chart version
  • Loading branch information
Revolyssup authored Feb 25, 2022
2 parents f719ec2 + 55faa88 commit 9d4dc7c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
33 changes: 33 additions & 0 deletions utils/kubernetes/apply-helm-chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,22 @@ func HelmConvertAppVersionToChartVersion(repo, chart, appVersion string) (string
return HelmAppVersionToChartVersion(repo, chart, normalizeVersion(appVersion))
}

// HelmChartVersionToAppVersion takes in the repo, chart and chart version and
// returns the corresponding app version for the same without normalizing the app version
func HelmChartVersionToAppVersion(repo, chart, chartVersion string) (string, error) {
helmIndex, err := createHelmIndex(repo)
if err != nil {
return "", ErrCreatingHelmIndex(err)
}

entryMetadata, exists := helmIndex.Entries.GetEntryWithChartVersion(chart, chartVersion)
if !exists {
return "", ErrEntryWithChartVersionNotExists(chart, chartVersion)
}

return entryMetadata.AppVersion, nil
}

// HelmAppVersionToChartVersion takes in the repo, chart and app version and
// returns the corresponding chart version for the same without normalizing the app version
func HelmAppVersionToChartVersion(repo, chart, appVersion string) (string, error) {
Expand Down Expand Up @@ -537,6 +553,23 @@ func (helmEntries HelmEntries) GetEntryWithAppVersion(entry, appVersion string)
return HelmEntryMetadata{}, false
}

// GetEntryWithAppVersion takes in the entry name and the appversion and returns the corresponding
// metadata for the parameters if it exists
func (helmEntries HelmEntries) GetEntryWithChartVersion(entry, chartVersion string) (HelmEntryMetadata, bool) {
hem, ok := helmEntries[entry]
if !ok {
return HelmEntryMetadata{}, false
}

for _, v := range hem {
if v.Name == entry && v.Version == chartVersion {
return v, true
}
}

return HelmEntryMetadata{}, false
}

// normalizeVerion takes in a version and adds "v" prefix
// if it isn't already present
func normalizeVersion(version string) string {
Expand Down
13 changes: 10 additions & 3 deletions utils/kubernetes/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ var (
// ErrDecodeYamlCode represents the error which is generated when yaml
// decode process fails
ErrDecodeYamlCode = "11035"

ErrEndpointNotFound = errors.New(ErrEndpointNotFoundCode, errors.Alert, []string{"Unable to discover an endpoint"}, []string{}, []string{}, []string{})
ErrInvalidAPIServer = errors.New(ErrInvalidAPIServerCode, errors.Alert, []string{"Invalid API Server URL"}, []string{}, []string{}, []string{})
// ErrEntryWithChartVersionNotExistsCode represents the error which is generated
// when no entry is found with specified name and app version
ErrEntryWithChartVersionNotExistsCode = "11036"
ErrEndpointNotFound = errors.New(ErrEndpointNotFoundCode, errors.Alert, []string{"Unable to discover an endpoint"}, []string{}, []string{}, []string{})
ErrInvalidAPIServer = errors.New(ErrInvalidAPIServerCode, errors.Alert, []string{"Invalid API Server URL"}, []string{}, []string{}, []string{})
)

func ErrApplyManifest(err error) error {
Expand Down Expand Up @@ -92,6 +94,11 @@ func ErrEntryWithAppVersionNotExists(entry, appVersion string) error {
return errors.New(ErrEntryWithAppVersionNotExistsCode, errors.Alert, []string{"Entry for the app version does not exist"}, []string{fmt.Sprintf("entry %s with app version %s does not exists", entry, appVersion)}, []string{}, []string{})
}

// ErrEntryWithChartVersionNotExists is the error when an entry with the given chart version is not found
func ErrEntryWithChartVersionNotExists(entry, appVersion string) error {
return errors.New(ErrEntryWithChartVersionNotExistsCode, errors.Alert, []string{"Entry for the chart version does not exist"}, []string{fmt.Sprintf("entry %s with chart version %s does not exists", entry, appVersion)}, []string{}, []string{})
}

// ErrHelmRepositoryNotFound is the error when no valid remote helm repository is found
func ErrHelmRepositoryNotFound(repo string, err error) error {
return errors.New(ErrHelmRepositoryNotFoundCode, errors.Alert, []string{"Helm repo not found"}, []string{fmt.Sprintf("either the repo %s does not exists or is corrupt: %v", repo, err)}, []string{}, []string{})
Expand Down

0 comments on commit 9d4dc7c

Please sign in to comment.