-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(portainer): sync version with license [EE-5765] (#18)
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package portainer | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Masterminds/semver" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// validateImageWithLicense validates the image name and tag based on the license type | ||
func validateImageWithLicense(license, image string) string { | ||
if !strings.HasPrefix(license, "3-") { | ||
log.Debug(). | ||
Str("license", license). | ||
Msg("License is a valid type 2 Portainer EE license, leaving it as is") | ||
return image | ||
} | ||
|
||
parts := strings.Split(image, ":") | ||
if len(parts) != 2 { | ||
log.Debug(). | ||
Str("imageName", image). | ||
Msg("Image name is not a standard image (image:tag), leaving it as is") | ||
return image | ||
} | ||
|
||
imageName := parts[0] | ||
tag := parts[1] | ||
|
||
if !strings.HasSuffix(imageName, "portainer-ee") { | ||
log.Debug(). | ||
Str("imageName", image). | ||
Msg("Image name is not portainer-ee, leaving it as is") | ||
return image | ||
} | ||
|
||
requiredVersion, err := semver.NewVersion(tag) | ||
if err != nil { | ||
log.Debug(). | ||
Err(err). | ||
Str("tag", tag). | ||
Msg("Tag is not a valid semver, leaving it as is") | ||
return image | ||
} | ||
|
||
minVersion := "2.18.4" | ||
if requiredVersion.GreaterThan(semver.MustParse(minVersion)) { | ||
log.Debug(). | ||
Str("tag", tag). | ||
Str("minVersion", minVersion). | ||
Msg("Tag is higher than minimum version, leaving it as is") | ||
return image | ||
} | ||
|
||
log.Info(). | ||
Str("tag", tag). | ||
Str("minVersion", minVersion). | ||
Msg("Tag is lower than minimum version for this license type, updating version to 2.18.4") | ||
|
||
return fmt.Sprintf("%s:%s", imageName, minVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package portainer | ||
|
||
import "testing" | ||
|
||
func TestValidateImageWithLicense(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
license string | ||
image string | ||
want string | ||
}{ | ||
{ | ||
name: "valid type 2 license", | ||
license: "2-abc123", | ||
image: "portainer-ee:2.18.4", | ||
want: "portainer-ee:2.18.4", | ||
}, | ||
{ | ||
name: "invalid license", | ||
license: "1-abc123", | ||
image: "portainer-ee:2.18.4", | ||
want: "portainer-ee:2.18.4", | ||
}, | ||
{ | ||
name: "invalid image name", | ||
license: "3-abc123", | ||
image: "invalid-image", | ||
want: "invalid-image", | ||
}, | ||
{ | ||
name: "invalid semver tag", | ||
license: "3-abc123", | ||
image: "portainer-ee:invalid-tag", | ||
want: "portainer-ee:invalid-tag", | ||
}, | ||
{ | ||
name: "lower than minimum version", | ||
license: "3-abc123", | ||
image: "portainer-ee:2.18.3", | ||
want: "portainer-ee:2.18.4", | ||
}, | ||
{ | ||
name: "higher than minimum version", | ||
license: "3-abc123", | ||
image: "portainer-ee:2.18.5", | ||
want: "portainer-ee:2.18.5", | ||
}, | ||
{ | ||
name: "with repo name", | ||
license: "3-abc123", | ||
image: "portainer/portainer-ee:2.18.2", | ||
want: "portainer/portainer-ee:2.18.4", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := validateImageWithLicense(tt.license, tt.image); got != tt.want { | ||
t.Errorf("validateImageWithLicense() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |