This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
/
pull.go
69 lines (59 loc) · 2.12 KB
/
pull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package apps
import (
"context"
"strings"
"github.com/acorn-io/mink/pkg/stores"
"github.com/acorn-io/mink/pkg/types"
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"
kclient "github.com/acorn-io/runtime/pkg/k8sclient"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func NewPullAppImage(c client.WithWatch) rest.Storage {
return stores.NewBuilder(c.Scheme(), &apiv1.AppPullImage{}).
WithCreate(&PullAppImageStrategy{
client: c,
}).
WithValidateName(PullAppImageNameValidator{}).
Build()
}
type PullAppImageNameValidator struct{}
type PullAppImageStrategy struct {
client client.WithWatch
}
func (s *PullAppImageStrategy) Create(ctx context.Context, obj types.Object) (types.Object, error) {
p := obj.(*apiv1.AppPullImage)
ri, _ := request.RequestInfoFrom(ctx)
// Use app instance here because in Manager this request is forwarded to the workload cluster.
// The app validation logic should not run there.
app := &v1.AppInstance{}
err := s.client.Get(ctx, kclient.ObjectKey{Namespace: ri.Namespace, Name: ri.Name}, app)
if err != nil {
return nil, err
}
if _, pattern := autoupgrade.AutoUpgradePattern(app.Spec.Image); pattern {
if app.Status.AppImage.Name != "" {
app.Status.AvailableAppImage = app.Status.AppImage.Name
}
} else {
app.Status.AvailableAppImage = app.Spec.Image
}
err = s.client.Status().Update(ctx, app)
return p, err
}
func (s *PullAppImageStrategy) New() types.Object {
return &apiv1.AppPullImage{}
}
func (v PullAppImageNameValidator) ValidateName(ctx context.Context, obj runtime.Object) (result field.ErrorList) {
appPullImage := obj.(*apiv1.AppPullImage)
if len(strings.Split(appPullImage.Name, ".")) == 2 {
result = append(result, field.Invalid(field.NewPath("metadata", "name"), appPullImage.Name, "To update a nested Acorn or a service, update the parent Acorn instead."))
return result
}
return nil
}