Skip to content

Commit

Permalink
Merge branch 'develop' into fix/EE-6939/upgrade-go
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-portainer authored Apr 12, 2024
2 parents a0b204f + 0721f24 commit 621830f
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
)

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/containerd/containerd v1.6.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZ
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ type (
}
)

const fiveMinutes = int64(300)

var errUpdateFailure = errors.New("update failure")

func Update(ctx context.Context, cli *kubernetes.Clientset, imageName string, deployment *appV1.Deployment, licenseKey string) error {
log.Info().
Str("deploymentName", deployment.Name).
Str("image", imageName).
Str("license", licenseKey).
Msg("Starting update process")

originalImage := deployment.Spec.Template.Spec.Containers[0].Image
Expand Down Expand Up @@ -149,7 +150,7 @@ func waitForDeployment(ctx context.Context, deployCli v1.DeploymentInterface, de
// we will wait 5 seconds before starting to watch
time.Sleep(5 * time.Second)

timeoutSeconds := int64(30)
timeoutSeconds := fiveMinutes
watcher, err := deployCli.Watch(ctx, metaV1.ListOptions{
FieldSelector: fmt.Sprintf("metadata.name=%s", deploymentName),
TimeoutSeconds: &timeoutSeconds,
Expand Down
2 changes: 2 additions & 0 deletions portainer/portainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Command struct {
func (r *Command) Run() error {
ctx := context.Background()

r.Image = validateImageWithLicense(r.License, r.Image)

switch r.EnvType {
case EnvTypeDockerStandalone:
return r.runStandalone(ctx)
Expand Down
62 changes: 62 additions & 0 deletions portainer/validate_image_license.go
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)
}
63 changes: 63 additions & 0 deletions portainer/validate_image_license_test.go
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)
}
})
}
}

0 comments on commit 621830f

Please sign in to comment.