forked from openshift/origin
/
rest.go
116 lines (98 loc) · 4.21 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
104
105
106
107
108
109
110
111
112
113
114
115
116
package imagerepositorymapping
import (
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/api/validation"
"github.com/openshift/origin/pkg/image/registry/image"
"github.com/openshift/origin/pkg/image/registry/imagerepository"
)
// REST implements the RESTStorage interface in terms of an Registry and Registry.
// It Only supports the Create method and is used to simply adding a new Image and tag to an ImageRepository.
type REST struct {
imageRegistry image.Registry
imageRepositoryRegistry imagerepository.Registry
}
// NewREST returns a new REST.
func NewREST(imageRegistry image.Registry, imageRepositoryRegistry imagerepository.Registry) apiserver.RESTStorage {
return &REST{imageRegistry, imageRepositoryRegistry}
}
// New returns a new ImageRepositoryMapping for use with Create.
func (s *REST) New() runtime.Object {
return &api.ImageRepositoryMapping{}
}
func (*REST) NewList() runtime.Object {
return &api.ImageRepositoryMapping{}
}
// List is not supported.
func (s *REST) List(ctx kapi.Context, selector, fields labels.Selector) (runtime.Object, error) {
return nil, errors.NewNotFound("imageRepositoryMapping", "")
}
// Get is not supported.
func (s *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
return nil, errors.NewNotFound("imageRepositoryMapping", id)
}
// Create registers a new image (if it doesn't exist) and updates the specified ImageRepository's tags.
func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
mapping := obj.(*api.ImageRepositoryMapping)
if !kapi.ValidNamespace(ctx, &mapping.ObjectMeta) {
return nil, errors.NewConflict("imageRepositoryMapping", mapping.Namespace, fmt.Errorf("ImageRepositoryMapping.Namespace does not match the provided context"))
}
kapi.FillObjectMetaSystemFields(ctx, &mapping.ObjectMeta)
kapi.FillObjectMetaSystemFields(ctx, &mapping.Image.ObjectMeta)
// TODO: allow cross namespace mappings if the user has access
mapping.Image.Namespace = ""
if errs := validation.ValidateImageRepositoryMapping(mapping); len(errs) > 0 {
return nil, errors.NewInvalid("imageRepositoryMapping", mapping.Name, errs)
}
repo, err := s.findRepositoryForMapping(ctx, mapping)
if err != nil {
return nil, err
}
image := mapping.Image
//TODO apply metadata overrides
if repo.Tags == nil {
repo.Tags = make(map[string]string)
}
repo.Tags[mapping.Tag] = image.Name
return apiserver.MakeAsync(func() (runtime.Object, error) {
if err := s.imageRegistry.CreateImage(ctx, &image); err != nil {
return nil, err
}
if err := s.imageRepositoryRegistry.UpdateImageRepository(ctx, repo); err != nil {
return nil, err
}
return &kapi.Status{Status: kapi.StatusSuccess}, nil
}), nil
}
// findRepositoryForMapping retrieves an ImageRepository whose DockerImageRepository matches dockerRepo.
func (s *REST) findRepositoryForMapping(ctx kapi.Context, mapping *api.ImageRepositoryMapping) (*api.ImageRepository, error) {
if len(mapping.DockerImageRepository) != 0 {
//TODO make this more efficient
list, err := s.imageRepositoryRegistry.ListImageRepositories(ctx, labels.Everything())
if err != nil {
return nil, err
}
for i := range list.Items {
if mapping.DockerImageRepository == list.Items[i].DockerImageRepository {
return &list.Items[i], nil
}
}
return nil, errors.NewInvalid("imageRepositoryMapping", "", errors.ValidationErrorList{
errors.NewFieldNotFound("dockerImageRepository", mapping.DockerImageRepository),
})
}
return s.imageRepositoryRegistry.GetImageRepository(ctx, mapping.Name)
}
// Update is not supported.
func (s *REST) Update(ctx kapi.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
return nil, fmt.Errorf("ImageRepositoryMappings may not be changed.")
}
// Delete is not supported.
func (s *REST) Delete(ctx kapi.Context, id string) (<-chan apiserver.RESTResult, error) {
return nil, errors.NewNotFound("imageRepositoryMapping", id)
}