forked from openshift/origin
/
rest.go
122 lines (102 loc) · 3.91 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
117
118
119
120
121
122
package deploy
import (
"fmt"
"code.google.com/p/go-uuid/uuid"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "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/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/golang/glog"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/deploy/api/validation"
)
// REST is an implementation of RESTStorage for the api server.
type REST struct {
registry Registry
}
// NewREST creates a new REST backed by the given registry.
func NewREST(registry Registry) apiserver.RESTStorage {
return &REST{
registry: registry,
}
}
// New creates a new Deployment for use with Create and Update.
func (s *REST) New() runtime.Object {
return &deployapi.Deployment{}
}
func (*REST) NewList() runtime.Object {
return &deployapi.Deployment{}
}
// List obtains a list of Deployments that match selector.
func (s *REST) List(ctx kapi.Context, label, field labels.Selector) (runtime.Object, error) {
deployments, err := s.registry.ListDeployments(ctx, label, field)
if err != nil {
return nil, err
}
return deployments, nil
}
// Get obtains the Deployment specified by its id.
func (s *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
deployment, err := s.registry.GetDeployment(ctx, id)
if err != nil {
return nil, err
}
return deployment, err
}
// Delete asynchronously deletes the Deployment specified by its id.
func (s *REST) Delete(ctx kapi.Context, id string) (<-chan apiserver.RESTResult, error) {
return apiserver.MakeAsync(func() (runtime.Object, error) {
return &kapi.Status{Status: kapi.StatusSuccess}, s.registry.DeleteDeployment(ctx, id)
}), nil
}
// Create registers a given new Deployment instance to s.registry.
func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
deployment, ok := obj.(*deployapi.Deployment)
if !ok {
return nil, fmt.Errorf("not a deployment: %#v", obj)
}
if !kapi.ValidNamespace(ctx, &deployment.ObjectMeta) {
return nil, kerrors.NewConflict("deployment", deployment.Namespace, fmt.Errorf("Deployment.Namespace does not match the provided context"))
}
kapi.FillObjectMetaSystemFields(ctx, &deployment.ObjectMeta)
if len(deployment.Name) == 0 {
deployment.Name = uuid.NewUUID().String()
}
deployment.Status = deployapi.DeploymentStatusNew
glog.Infof("Creating deployment with namespace::Name: %v::%v", deployment.Namespace, deployment.Name)
if errs := validation.ValidateDeployment(deployment); len(errs) > 0 {
return nil, kerrors.NewInvalid("deployment", deployment.Name, errs)
}
return apiserver.MakeAsync(func() (runtime.Object, error) {
err := s.registry.CreateDeployment(ctx, deployment)
if err != nil {
return nil, err
}
return deployment, nil
}), nil
}
// Update replaces a given Deployment instance with an existing instance in s.registry.
func (s *REST) Update(ctx kapi.Context, obj runtime.Object) (<-chan apiserver.RESTResult, error) {
deployment, ok := obj.(*deployapi.Deployment)
if !ok {
return nil, fmt.Errorf("not a deployment: %#v", obj)
}
if len(deployment.Name) == 0 {
return nil, fmt.Errorf("name is unspecified: %#v", deployment)
}
if !kapi.ValidNamespace(ctx, &deployment.ObjectMeta) {
return nil, kerrors.NewConflict("deployment", deployment.Namespace, fmt.Errorf("Deployment.Namespace does not match the provided context"))
}
return apiserver.MakeAsync(func() (runtime.Object, error) {
err := s.registry.UpdateDeployment(ctx, deployment)
if err != nil {
return nil, err
}
return deployment, nil
}), nil
}
func (s *REST) Watch(ctx kapi.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
return s.registry.WatchDeployments(ctx, label, field, resourceVersion)
}