This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest.go
103 lines (81 loc) · 3.33 KB
/
rest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package imagesignature
import (
"errors"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion"
)
// REST implements the RESTStorage interface for ImageSignature
type REST struct {
imageClient imageclient.ImagesGetter
}
var _ rest.Creater = &REST{}
var _ rest.GracefulDeleter = &REST{}
// NewREST returns a new REST.
func NewREST(imageClient imageclient.ImagesGetter) *REST {
return &REST{imageClient: imageClient}
}
// New is only implemented to make REST implement RESTStorage
func (r *REST) New() runtime.Object {
return &imageapi.ImageSignature{}
}
func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, _ bool) (runtime.Object, error) {
signature := obj.(*imageapi.ImageSignature)
if err := rest.BeforeCreate(Strategy, ctx, obj); err != nil {
return nil, err
}
// at this point we have a fully formed object. It is time to call the validators that the apiserver
// handling chain wants to enforce.
if createValidation != nil {
if err := createValidation(obj.DeepCopyObject()); err != nil {
return nil, err
}
}
imageName, _, err := imageapi.SplitImageSignatureName(signature.Name)
if err != nil {
return nil, kapierrors.NewBadRequest(err.Error())
}
image, err := r.imageClient.Images().Get(imageName, metav1.GetOptions{})
if err != nil {
return nil, err
}
// ensure that given signature already doesn't exist - either by its name or type:content
if byName, byContent := imageapi.IndexOfImageSignatureByName(image.Signatures, signature.Name), imageapi.IndexOfImageSignature(image.Signatures, signature.Type, signature.Content); byName >= 0 || byContent >= 0 {
return nil, kapierrors.NewAlreadyExists(imageapi.Resource("imageSignatures"), signature.Name)
}
image.Signatures = append(image.Signatures, *signature)
image, err = r.imageClient.Images().Update(image)
if err != nil {
return nil, err
}
byName := imageapi.IndexOfImageSignatureByName(image.Signatures, signature.Name)
if byName < 0 {
return nil, kapierrors.NewInternalError(errors.New("failed to store given signature"))
}
return &image.Signatures[byName], nil
}
func (r *REST) Delete(ctx apirequest.Context, name string, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
imageName, _, err := imageapi.SplitImageSignatureName(name)
if err != nil {
return nil, false, kapierrors.NewBadRequest("ImageSignatures must be accessed with <imageName>@<signatureName>")
}
image, err := r.imageClient.Images().Get(imageName, metav1.GetOptions{})
if err != nil {
return nil, false, err
}
index := imageapi.IndexOfImageSignatureByName(image.Signatures, name)
if index < 0 {
return nil, false, kapierrors.NewNotFound(imageapi.Resource("imageSignatures"), name)
}
size := len(image.Signatures)
copy(image.Signatures[index:size-1], image.Signatures[index+1:size])
image.Signatures = image.Signatures[0 : size-1]
if _, err := r.imageClient.Images().Update(image); err != nil {
return nil, false, err
}
return &metav1.Status{Status: metav1.StatusSuccess}, true, nil
}