forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
129 lines (106 loc) · 4.33 KB
/
etcd.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
123
124
125
126
127
128
129
package etcd
import (
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/apis/extensions"
extvalidation "k8s.io/kubernetes/pkg/apis/extensions/validation"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/registry/generic"
"k8s.io/kubernetes/pkg/registry/generic/registry"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage"
"github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/deploy/registry/deployconfig"
"github.com/openshift/origin/pkg/util/restoptions"
)
// REST contains the REST storage for DeploymentConfig objects.
type REST struct {
*registry.Store
}
// NewREST returns a deploymentConfigREST containing the REST storage for DeploymentConfig objects,
// a statusREST containing the REST storage for changing the status of a DeploymentConfig,
// and a scaleREST containing the REST storage for the Scale subresources of DeploymentConfigs.
func NewREST(optsGetter restoptions.Getter) (*REST, *StatusREST, *ScaleREST, error) {
store := ®istry.Store{
NewFunc: func() runtime.Object { return &api.DeploymentConfig{} },
NewListFunc: func() runtime.Object { return &api.DeploymentConfigList{} },
QualifiedResource: api.Resource("deploymentconfigs"),
ObjectNameFunc: func(obj runtime.Object) (string, error) {
return obj.(*api.DeploymentConfig).Name, nil
},
PredicateFunc: func(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
return deployconfig.Matcher(label, field)
},
CreateStrategy: deployconfig.Strategy,
UpdateStrategy: deployconfig.Strategy,
DeleteStrategy: deployconfig.Strategy,
ReturnDeletedObject: false,
}
if err := restoptions.ApplyOptions(optsGetter, store, true, storage.NoTriggerPublisher); err != nil {
return nil, nil, nil, err
}
deploymentConfigREST := &REST{store}
statusStore := *store
statusStore.UpdateStrategy = deployconfig.StatusStrategy
statusREST := &StatusREST{store: &statusStore}
scaleREST := &ScaleREST{registry: deployconfig.NewRegistry(deploymentConfigREST)}
return deploymentConfigREST, statusREST, scaleREST, nil
}
// ScaleREST contains the REST storage for the Scale subresource of DeploymentConfigs.
type ScaleREST struct {
registry deployconfig.Registry
}
// ScaleREST implements Patcher
var _ = rest.Patcher(&ScaleREST{})
// New creates a new Scale object
func (r *ScaleREST) New() runtime.Object {
return &extensions.Scale{}
}
// Get retrieves (computes) the Scale subresource for the given DeploymentConfig name.
func (r *ScaleREST) Get(ctx kapi.Context, name string) (runtime.Object, error) {
deploymentConfig, err := r.registry.GetDeploymentConfig(ctx, name)
if err != nil {
return nil, err
}
return api.ScaleFromConfig(deploymentConfig), nil
}
// Update scales the DeploymentConfig for the given Scale subresource, returning the updated Scale.
func (r *ScaleREST) Update(ctx kapi.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
deploymentConfig, err := r.registry.GetDeploymentConfig(ctx, name)
if err != nil {
return nil, false, errors.NewNotFound(extensions.Resource("scale"), name)
}
old := api.ScaleFromConfig(deploymentConfig)
obj, err := objInfo.UpdatedObject(ctx, old)
if err != nil {
return nil, false, err
}
scale, ok := obj.(*extensions.Scale)
if !ok {
return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
}
if errs := extvalidation.ValidateScale(scale); len(errs) > 0 {
return nil, false, errors.NewInvalid(extensions.Kind("Scale"), scale.Name, errs)
}
deploymentConfig.Spec.Replicas = scale.Spec.Replicas
if err := r.registry.UpdateDeploymentConfig(ctx, deploymentConfig); err != nil {
return nil, false, err
}
return scale, false, nil
}
// StatusREST implements the REST endpoint for changing the status of a DeploymentConfig.
type StatusREST struct {
store *registry.Store
}
// StatusREST implements the Updater interface.
var _ = rest.Updater(&StatusREST{})
func (r *StatusREST) New() runtime.Object {
return &api.DeploymentConfig{}
}
// Update alters the status subset of an deploymentConfig.
func (r *StatusREST) Update(ctx kapi.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
return r.store.Update(ctx, name, objInfo)
}