Skip to content

Commit

Permalink
TestHelmLatestVersion: Remove helm chart version suffix (#966)
Browse files Browse the repository at this point in the history
* Remove helm chart version suffix

* Move validation
  • Loading branch information
victorpras authored Oct 26, 2023
1 parent 8e773bb commit 7169968
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
4 changes: 3 additions & 1 deletion e2e/nomostest/artifactregistry/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func updateYAMLFile(name string, updateFn func(map[string]interface{}) error) er
// SetVersion updates the local version of the helm chart to the specified
// version with a random suffix
func (r *HelmChart) SetVersion(version string) error {
r.Image.SetVersion(version)
if err := r.Image.SetVersion(version); err != nil {
return err
}
version = r.Image.Version
chartFilePath := filepath.Join(r.Image.BuildPath, "Chart.yaml")
err := updateYAMLFile(chartFilePath, func(chartMap map[string]interface{}) error {
Expand Down
37 changes: 20 additions & 17 deletions e2e/nomostest/artifactregistry/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/ettle/strcase"
"github.com/google/uuid"
"kpt.dev/configsync/e2e"
"kpt.dev/configsync/e2e/nomostest"
"kpt.dev/configsync/e2e/nomostest/testlogger"
Expand All @@ -48,8 +47,10 @@ func SetupImage(nt *nomostest.NT, name, version string) (*Image, error) {
if name == "" {
return nil, fmt.Errorf("image name must not be empty")
}
if version == "" {
return nil, fmt.Errorf("image version must not be empty")

// Version will error out if it's empty or longer than 20 characters
if err := validateImageVersion(version); err != nil {
return nil, err
}
chart := &Image{
Shell: nt.Shell,
Expand All @@ -62,9 +63,8 @@ func SetupImage(nt *nomostest.NT, name, version string) (*Image, error) {
// Use chart name to avoid overlap.
BuildPath: filepath.Join(nt.TmpDir, name),
// Use test name to avoid overlap. Truncate to 40 characters.
Name: generateImageName(nt, name),
// Use random suffix to avoid overlap. Truncate to 20 characters.
Version: generateImageVersion(version),
Name: generateImageName(nt, name),
Version: version,
}
nt.T.Cleanup(func() {
if err := chart.Delete(); err != nil {
Expand Down Expand Up @@ -216,10 +216,12 @@ func (r *Image) SetName(nt *nomostest.NT, name string) {

// SetVersion updates the local version of the image to the specified tag with a
// random suffix
func (r *Image) SetVersion(version string) {
version = generateImageVersion(version)
r.Logger.Infof("Updating image version to %q", version)
func (r *Image) SetVersion(version string) error {
if err := validateImageVersion(version); err != nil {
return err
}
r.Version = version
return nil
}

// Delete the package from the remote registry, including all versions and tags.
Expand Down Expand Up @@ -248,14 +250,15 @@ func generateImageName(nt *nomostest.NT, chartName string) string {
return chartName
}

// generateImageVersion returns the version with a random 8 character suffix.
// Result will be no more than 20 characters and can function as a k8s metadata.name.
// validateImageVersion will validate if chart version string is not empty and
// is 20 characters maximum and can function as a k8s metadata.name.
// Chart name and version must be less than 63 characters combined.
func generateImageVersion(chartVersion string) string {
if len(chartVersion) > 12 {
chartVersion = chartVersion[:12]
chartVersion = strings.Trim(strings.Trim(chartVersion, "-"), ".")
func validateImageVersion(chartVersion string) error {
if chartVersion == "" {
return fmt.Errorf("image version must not be empty")
}
if len(chartVersion) > 20 {
return fmt.Errorf("chart version string %q should not exceed 20 characters", chartVersion)
}
return fmt.Sprintf("%s-%s", chartVersion,
strings.ReplaceAll(uuid.NewString(), "-", "")[:7])
return nil
}

0 comments on commit 7169968

Please sign in to comment.