2 Copyright 2015 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/fields"
26 "k8s.io/apimachinery/pkg/labels"
27 "k8s.io/apimachinery/pkg/runtime"
28 "k8s.io/apimachinery/pkg/runtime/schema"
29 "k8s.io/apimachinery/pkg/types"
32 func NewRootGetAction(resource schema.GroupVersionResource, name string) GetActionImpl {
33 action := GetActionImpl{}
35 action.Resource = resource
41 func NewGetAction(resource schema.GroupVersionResource, namespace, name string) GetActionImpl {
42 action := GetActionImpl{}
44 action.Resource = resource
45 action.Namespace = namespace
51 func NewGetSubresourceAction(resource schema.GroupVersionResource, namespace, subresource, name string) GetActionImpl {
52 action := GetActionImpl{}
54 action.Resource = resource
55 action.Subresource = subresource
56 action.Namespace = namespace
62 func NewRootGetSubresourceAction(resource schema.GroupVersionResource, subresource, name string) GetActionImpl {
63 action := GetActionImpl{}
65 action.Resource = resource
66 action.Subresource = subresource
72 func NewRootListAction(resource schema.GroupVersionResource, kind schema.GroupVersionKind, opts interface{}) ListActionImpl {
73 action := ListActionImpl{}
75 action.Resource = resource
77 labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
78 action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
83 func NewListAction(resource schema.GroupVersionResource, kind schema.GroupVersionKind, namespace string, opts interface{}) ListActionImpl {
84 action := ListActionImpl{}
86 action.Resource = resource
88 action.Namespace = namespace
89 labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
90 action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
95 func NewRootCreateAction(resource schema.GroupVersionResource, object runtime.Object) CreateActionImpl {
96 action := CreateActionImpl{}
97 action.Verb = "create"
98 action.Resource = resource
99 action.Object = object
104 func NewCreateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) CreateActionImpl {
105 action := CreateActionImpl{}
106 action.Verb = "create"
107 action.Resource = resource
108 action.Namespace = namespace
109 action.Object = object
114 func NewRootCreateSubresourceAction(resource schema.GroupVersionResource, name, subresource string, object runtime.Object) CreateActionImpl {
115 action := CreateActionImpl{}
116 action.Verb = "create"
117 action.Resource = resource
118 action.Subresource = subresource
120 action.Object = object
125 func NewCreateSubresourceAction(resource schema.GroupVersionResource, name, subresource, namespace string, object runtime.Object) CreateActionImpl {
126 action := CreateActionImpl{}
127 action.Verb = "create"
128 action.Resource = resource
129 action.Namespace = namespace
130 action.Subresource = subresource
132 action.Object = object
137 func NewRootUpdateAction(resource schema.GroupVersionResource, object runtime.Object) UpdateActionImpl {
138 action := UpdateActionImpl{}
139 action.Verb = "update"
140 action.Resource = resource
141 action.Object = object
146 func NewUpdateAction(resource schema.GroupVersionResource, namespace string, object runtime.Object) UpdateActionImpl {
147 action := UpdateActionImpl{}
148 action.Verb = "update"
149 action.Resource = resource
150 action.Namespace = namespace
151 action.Object = object
156 func NewRootPatchAction(resource schema.GroupVersionResource, name string, pt types.PatchType, patch []byte) PatchActionImpl {
157 action := PatchActionImpl{}
158 action.Verb = "patch"
159 action.Resource = resource
161 action.PatchType = pt
167 func NewPatchAction(resource schema.GroupVersionResource, namespace string, name string, pt types.PatchType, patch []byte) PatchActionImpl {
168 action := PatchActionImpl{}
169 action.Verb = "patch"
170 action.Resource = resource
171 action.Namespace = namespace
173 action.PatchType = pt
179 func NewRootPatchSubresourceAction(resource schema.GroupVersionResource, name string, pt types.PatchType, patch []byte, subresources ...string) PatchActionImpl {
180 action := PatchActionImpl{}
181 action.Verb = "patch"
182 action.Resource = resource
183 action.Subresource = path.Join(subresources...)
185 action.PatchType = pt
191 func NewPatchSubresourceAction(resource schema.GroupVersionResource, namespace, name string, pt types.PatchType, patch []byte, subresources ...string) PatchActionImpl {
192 action := PatchActionImpl{}
193 action.Verb = "patch"
194 action.Resource = resource
195 action.Subresource = path.Join(subresources...)
196 action.Namespace = namespace
198 action.PatchType = pt
204 func NewRootUpdateSubresourceAction(resource schema.GroupVersionResource, subresource string, object runtime.Object) UpdateActionImpl {
205 action := UpdateActionImpl{}
206 action.Verb = "update"
207 action.Resource = resource
208 action.Subresource = subresource
209 action.Object = object
213 func NewUpdateSubresourceAction(resource schema.GroupVersionResource, subresource string, namespace string, object runtime.Object) UpdateActionImpl {
214 action := UpdateActionImpl{}
215 action.Verb = "update"
216 action.Resource = resource
217 action.Subresource = subresource
218 action.Namespace = namespace
219 action.Object = object
224 func NewRootDeleteAction(resource schema.GroupVersionResource, name string) DeleteActionImpl {
225 action := DeleteActionImpl{}
226 action.Verb = "delete"
227 action.Resource = resource
233 func NewRootDeleteSubresourceAction(resource schema.GroupVersionResource, subresource string, name string) DeleteActionImpl {
234 action := DeleteActionImpl{}
235 action.Verb = "delete"
236 action.Resource = resource
237 action.Subresource = subresource
243 func NewDeleteAction(resource schema.GroupVersionResource, namespace, name string) DeleteActionImpl {
244 action := DeleteActionImpl{}
245 action.Verb = "delete"
246 action.Resource = resource
247 action.Namespace = namespace
253 func NewDeleteSubresourceAction(resource schema.GroupVersionResource, subresource, namespace, name string) DeleteActionImpl {
254 action := DeleteActionImpl{}
255 action.Verb = "delete"
256 action.Resource = resource
257 action.Subresource = subresource
258 action.Namespace = namespace
264 func NewRootDeleteCollectionAction(resource schema.GroupVersionResource, opts interface{}) DeleteCollectionActionImpl {
265 action := DeleteCollectionActionImpl{}
266 action.Verb = "delete-collection"
267 action.Resource = resource
268 labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
269 action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
274 func NewDeleteCollectionAction(resource schema.GroupVersionResource, namespace string, opts interface{}) DeleteCollectionActionImpl {
275 action := DeleteCollectionActionImpl{}
276 action.Verb = "delete-collection"
277 action.Resource = resource
278 action.Namespace = namespace
279 labelSelector, fieldSelector, _ := ExtractFromListOptions(opts)
280 action.ListRestrictions = ListRestrictions{labelSelector, fieldSelector}
285 func NewRootWatchAction(resource schema.GroupVersionResource, opts interface{}) WatchActionImpl {
286 action := WatchActionImpl{}
287 action.Verb = "watch"
288 action.Resource = resource
289 labelSelector, fieldSelector, resourceVersion := ExtractFromListOptions(opts)
290 action.WatchRestrictions = WatchRestrictions{labelSelector, fieldSelector, resourceVersion}
295 func ExtractFromListOptions(opts interface{}) (labelSelector labels.Selector, fieldSelector fields.Selector, resourceVersion string) {
297 switch t := opts.(type) {
298 case metav1.ListOptions:
299 labelSelector, err = labels.Parse(t.LabelSelector)
301 panic(fmt.Errorf("invalid selector %q: %v", t.LabelSelector, err))
303 fieldSelector, err = fields.ParseSelector(t.FieldSelector)
305 panic(fmt.Errorf("invalid selector %q: %v", t.FieldSelector, err))
307 resourceVersion = t.ResourceVersion
309 panic(fmt.Errorf("expect a ListOptions %T", opts))
311 if labelSelector == nil {
312 labelSelector = labels.Everything()
314 if fieldSelector == nil {
315 fieldSelector = fields.Everything()
317 return labelSelector, fieldSelector, resourceVersion
320 func NewWatchAction(resource schema.GroupVersionResource, namespace string, opts interface{}) WatchActionImpl {
321 action := WatchActionImpl{}
322 action.Verb = "watch"
323 action.Resource = resource
324 action.Namespace = namespace
325 labelSelector, fieldSelector, resourceVersion := ExtractFromListOptions(opts)
326 action.WatchRestrictions = WatchRestrictions{labelSelector, fieldSelector, resourceVersion}
331 func NewProxyGetAction(resource schema.GroupVersionResource, namespace, scheme, name, port, path string, params map[string]string) ProxyGetActionImpl {
332 action := ProxyGetActionImpl{}
334 action.Resource = resource
335 action.Namespace = namespace
336 action.Scheme = scheme
340 action.Params = params
344 type ListRestrictions struct {
345 Labels labels.Selector
346 Fields fields.Selector
348 type WatchRestrictions struct {
349 Labels labels.Selector
350 Fields fields.Selector
351 ResourceVersion string
354 type Action interface {
355 GetNamespace() string
357 GetResource() schema.GroupVersionResource
358 GetSubresource() string
359 Matches(verb, resource string) bool
361 // DeepCopy is used to copy an action to avoid any risk of accidental mutation. Most people never need to call this
362 // because the invocation logic deep copies before calls to storage and reactors.
366 type GenericAction interface {
368 GetValue() interface{}
371 type GetAction interface {
376 type ListAction interface {
378 GetListRestrictions() ListRestrictions
381 type CreateAction interface {
383 GetObject() runtime.Object
386 type UpdateAction interface {
388 GetObject() runtime.Object
391 type DeleteAction interface {
396 type DeleteCollectionAction interface {
398 GetListRestrictions() ListRestrictions
401 type PatchAction interface {
404 GetPatchType() types.PatchType
408 type WatchAction interface {
410 GetWatchRestrictions() WatchRestrictions
413 type ProxyGetAction interface {
419 GetParams() map[string]string
422 type ActionImpl struct {
425 Resource schema.GroupVersionResource
429 func (a ActionImpl) GetNamespace() string {
432 func (a ActionImpl) GetVerb() string {
435 func (a ActionImpl) GetResource() schema.GroupVersionResource {
438 func (a ActionImpl) GetSubresource() string {
441 func (a ActionImpl) Matches(verb, resource string) bool {
442 return strings.ToLower(verb) == strings.ToLower(a.Verb) &&
443 strings.ToLower(resource) == strings.ToLower(a.Resource.Resource)
445 func (a ActionImpl) DeepCopy() Action {
450 type GenericActionImpl struct {
455 func (a GenericActionImpl) GetValue() interface{} {
459 func (a GenericActionImpl) DeepCopy() Action {
460 return GenericActionImpl{
461 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
462 // TODO this is wrong, but no worse than before
467 type GetActionImpl struct {
472 func (a GetActionImpl) GetName() string {
476 func (a GetActionImpl) DeepCopy() Action {
477 return GetActionImpl{
478 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
483 type ListActionImpl struct {
485 Kind schema.GroupVersionKind
487 ListRestrictions ListRestrictions
490 func (a ListActionImpl) GetKind() schema.GroupVersionKind {
494 func (a ListActionImpl) GetListRestrictions() ListRestrictions {
495 return a.ListRestrictions
498 func (a ListActionImpl) DeepCopy() Action {
499 return ListActionImpl{
500 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
503 ListRestrictions: ListRestrictions{
504 Labels: a.ListRestrictions.Labels.DeepCopySelector(),
505 Fields: a.ListRestrictions.Fields.DeepCopySelector(),
510 type CreateActionImpl struct {
513 Object runtime.Object
516 func (a CreateActionImpl) GetObject() runtime.Object {
520 func (a CreateActionImpl) DeepCopy() Action {
521 return CreateActionImpl{
522 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
524 Object: a.Object.DeepCopyObject(),
528 type UpdateActionImpl struct {
530 Object runtime.Object
533 func (a UpdateActionImpl) GetObject() runtime.Object {
537 func (a UpdateActionImpl) DeepCopy() Action {
538 return UpdateActionImpl{
539 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
540 Object: a.Object.DeepCopyObject(),
544 type PatchActionImpl struct {
547 PatchType types.PatchType
551 func (a PatchActionImpl) GetName() string {
555 func (a PatchActionImpl) GetPatch() []byte {
559 func (a PatchActionImpl) GetPatchType() types.PatchType {
563 func (a PatchActionImpl) DeepCopy() Action {
564 patch := make([]byte, len(a.Patch))
566 return PatchActionImpl{
567 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
569 PatchType: a.PatchType,
574 type DeleteActionImpl struct {
579 func (a DeleteActionImpl) GetName() string {
583 func (a DeleteActionImpl) DeepCopy() Action {
584 return DeleteActionImpl{
585 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
590 type DeleteCollectionActionImpl struct {
592 ListRestrictions ListRestrictions
595 func (a DeleteCollectionActionImpl) GetListRestrictions() ListRestrictions {
596 return a.ListRestrictions
599 func (a DeleteCollectionActionImpl) DeepCopy() Action {
600 return DeleteCollectionActionImpl{
601 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
602 ListRestrictions: ListRestrictions{
603 Labels: a.ListRestrictions.Labels.DeepCopySelector(),
604 Fields: a.ListRestrictions.Fields.DeepCopySelector(),
609 type WatchActionImpl struct {
611 WatchRestrictions WatchRestrictions
614 func (a WatchActionImpl) GetWatchRestrictions() WatchRestrictions {
615 return a.WatchRestrictions
618 func (a WatchActionImpl) DeepCopy() Action {
619 return WatchActionImpl{
620 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
621 WatchRestrictions: WatchRestrictions{
622 Labels: a.WatchRestrictions.Labels.DeepCopySelector(),
623 Fields: a.WatchRestrictions.Fields.DeepCopySelector(),
624 ResourceVersion: a.WatchRestrictions.ResourceVersion,
629 type ProxyGetActionImpl struct {
635 Params map[string]string
638 func (a ProxyGetActionImpl) GetScheme() string {
642 func (a ProxyGetActionImpl) GetName() string {
646 func (a ProxyGetActionImpl) GetPort() string {
650 func (a ProxyGetActionImpl) GetPath() string {
654 func (a ProxyGetActionImpl) GetParams() map[string]string {
658 func (a ProxyGetActionImpl) DeepCopy() Action {
659 params := map[string]string{}
660 for k, v := range a.Params {
663 return ProxyGetActionImpl{
664 ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),