Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Always set AutoUpgrade if it is implied (#1459) #1881

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions integration/client/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,50 @@ func TestAppRunImageVariations(t *testing.T) {
t.Fatal(err)
}

for desc, imageName := range map[string]string{
"ref": "foo/bar:baz",
"id": imageID,
"sha256": fmt.Sprintf("sha256:%s", imageID),
"short": imageID[:8],
"autoupgrade": "foo/bar:**",
} {
imageName := imageName
t.Run(desc, func(t *testing.T) {
testCases := []struct {
name string
image string
options *client.AppRunOptions
expectErr bool
}{
{
name: "ref",
image: "foo/bar:baz",
},
{
name: "id",
image: imageID,
},
{
name: "sha256",
image: fmt.Sprintf("sha256:%s", imageID),
},
{
name: "short",
image: imageID[:8],
},
{
name: "autoupgrade",
image: "foo/bar:**",
options: &client.AppRunOptions{
AutoUpgrade: &[]bool{true}[0],
},
},
{
name: "autoupgrade-fail",
image: "foo/bar:**",
expectErr: true,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := c.AppRun(ctx, imageName, nil)
assert.NoError(t, err)
_, err := c.AppRun(ctx, tc.image, tc.options)
if err != nil && !tc.expectErr {
t.Fatal("unexpected error:", err)
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/autoupgrade/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ func AutoUpgradePattern(image string) (string, bool) {
return tag, strings.ContainsAny(tag, "#*")
}

// ImpliedAutoUpgrade returns boolean indicating if auto-upgrade is implied either
// by a pattern in the app.Spec.image or app.Spec.AutoUpgradeInterval being specified.
func ImpliedAutoUpgrade(interval, image string) bool {
if _, isPattern := AutoUpgradePattern(image); isPattern || interval != "" {
return true
}
return false
}

func Mode(appSpec v1.AppInstanceSpec) (string, bool) {
_, isPat := AutoUpgradePattern(appSpec.Image)
on := appSpec.GetAutoUpgrade() || appSpec.GetNotifyUpgrade() || isPat
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/autoupgrade"
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/dev"
Expand Down Expand Up @@ -239,6 +240,11 @@ func (s *Run) Run(cmd *cobra.Command, args []string) (err error) {
return err
}

// Ensure opts.AutoUpgrade is set if is implied by opts.AutoUpgradeInterval or imageSource.Image's pattern.
if opts.AutoUpgrade == nil || !*opts.AutoUpgrade {
opts.AutoUpgrade = &[]bool{autoupgrade.ImpliedAutoUpgrade(opts.AutoUpgradeInterval, imageSource.Image)}[0]
}

// Force install prompt if needed
_, err = c.Info(cmd.Context())
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/registry/apigroups/acorn/apps/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (s *Validator) Validate(ctx context.Context, obj runtime.Object) (result fi
return
}

au := app.Spec.AutoUpgrade
if (au == nil || !*au) && autoupgrade.ImpliedAutoUpgrade(app.Spec.AutoUpgradeInterval, app.Spec.Image) {
result = append(result, field.Invalid(field.NewPath("spec", "autoUpgrade"), app.Spec.AutoUpgrade, "must be true if autoupgrade options are set"))
return
}

var (
imageGrantedPerms []v1.Permissions
checkImage = app.Spec.Image
Expand Down