Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / kubernetes / typed / core / v1 / event_expansion.go
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package v1
18
19 import (
20         "fmt"
21
22         "k8s.io/api/core/v1"
23         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24         "k8s.io/apimachinery/pkg/fields"
25         "k8s.io/apimachinery/pkg/runtime"
26         "k8s.io/apimachinery/pkg/types"
27         ref "k8s.io/client-go/tools/reference"
28 )
29
30 // The EventExpansion interface allows manually adding extra methods to the EventInterface.
31 type EventExpansion interface {
32         // CreateWithEventNamespace is the same as a Create, except that it sends the request to the event.Namespace.
33         CreateWithEventNamespace(event *v1.Event) (*v1.Event, error)
34         // UpdateWithEventNamespace is the same as a Update, except that it sends the request to the event.Namespace.
35         UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error)
36         PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error)
37         // Search finds events about the specified object
38         Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error)
39         // Returns the appropriate field selector based on the API version being used to communicate with the server.
40         // The returned field selector can be used with List and Watch to filter desired events.
41         GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector
42 }
43
44 // CreateWithEventNamespace makes a new event. Returns the copy of the event the server returns,
45 // or an error. The namespace to create the event within is deduced from the
46 // event; it must either match this event client's namespace, or this event
47 // client must have been created with the "" namespace.
48 func (e *events) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
49         if e.ns != "" && event.Namespace != e.ns {
50                 return nil, fmt.Errorf("can't create an event with namespace '%v' in namespace '%v'", event.Namespace, e.ns)
51         }
52         result := &v1.Event{}
53         err := e.client.Post().
54                 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
55                 Resource("events").
56                 Body(event).
57                 Do().
58                 Into(result)
59         return result, err
60 }
61
62 // UpdateWithEventNamespace modifies an existing event. It returns the copy of the event that the server returns,
63 // or an error. The namespace and key to update the event within is deduced from the event. The
64 // namespace must either match this event client's namespace, or this event client must have been
65 // created with the "" namespace. Update also requires the ResourceVersion to be set in the event
66 // object.
67 func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
68         result := &v1.Event{}
69         err := e.client.Put().
70                 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
71                 Resource("events").
72                 Name(event.Name).
73                 Body(event).
74                 Do().
75                 Into(result)
76         return result, err
77 }
78
79 // PatchWithEventNamespace modifies an existing event. It returns the copy of
80 // the event that the server returns, or an error. The namespace and name of the
81 // target event is deduced from the incompleteEvent. The namespace must either
82 // match this event client's namespace, or this event client must have been
83 // created with the "" namespace.
84 func (e *events) PatchWithEventNamespace(incompleteEvent *v1.Event, data []byte) (*v1.Event, error) {
85         if e.ns != "" && incompleteEvent.Namespace != e.ns {
86                 return nil, fmt.Errorf("can't patch an event with namespace '%v' in namespace '%v'", incompleteEvent.Namespace, e.ns)
87         }
88         result := &v1.Event{}
89         err := e.client.Patch(types.StrategicMergePatchType).
90                 NamespaceIfScoped(incompleteEvent.Namespace, len(incompleteEvent.Namespace) > 0).
91                 Resource("events").
92                 Name(incompleteEvent.Name).
93                 Body(data).
94                 Do().
95                 Into(result)
96         return result, err
97 }
98
99 // Search finds events about the specified object. The namespace of the
100 // object must match this event's client namespace unless the event client
101 // was made with the "" namespace.
102 func (e *events) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
103         ref, err := ref.GetReference(scheme, objOrRef)
104         if err != nil {
105                 return nil, err
106         }
107         if e.ns != "" && ref.Namespace != e.ns {
108                 return nil, fmt.Errorf("won't be able to find any events of namespace '%v' in namespace '%v'", ref.Namespace, e.ns)
109         }
110         stringRefKind := string(ref.Kind)
111         var refKind *string
112         if stringRefKind != "" {
113                 refKind = &stringRefKind
114         }
115         stringRefUID := string(ref.UID)
116         var refUID *string
117         if stringRefUID != "" {
118                 refUID = &stringRefUID
119         }
120         fieldSelector := e.GetFieldSelector(&ref.Name, &ref.Namespace, refKind, refUID)
121         return e.List(metav1.ListOptions{FieldSelector: fieldSelector.String()})
122 }
123
124 // Returns the appropriate field selector based on the API version being used to communicate with the server.
125 // The returned field selector can be used with List and Watch to filter desired events.
126 func (e *events) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
127         apiVersion := e.client.APIVersion().String()
128         field := fields.Set{}
129         if involvedObjectName != nil {
130                 field[GetInvolvedObjectNameFieldLabel(apiVersion)] = *involvedObjectName
131         }
132         if involvedObjectNamespace != nil {
133                 field["involvedObject.namespace"] = *involvedObjectNamespace
134         }
135         if involvedObjectKind != nil {
136                 field["involvedObject.kind"] = *involvedObjectKind
137         }
138         if involvedObjectUID != nil {
139                 field["involvedObject.uid"] = *involvedObjectUID
140         }
141         return field.AsSelector()
142 }
143
144 // Returns the appropriate field label to use for name of the involved object as per the given API version.
145 func GetInvolvedObjectNameFieldLabel(version string) string {
146         return "involvedObject.name"
147 }
148
149 // TODO: This is a temporary arrangement and will be removed once all clients are moved to use the clientset.
150 type EventSinkImpl struct {
151         Interface EventInterface
152 }
153
154 func (e *EventSinkImpl) Create(event *v1.Event) (*v1.Event, error) {
155         return e.Interface.CreateWithEventNamespace(event)
156 }
157
158 func (e *EventSinkImpl) Update(event *v1.Event) (*v1.Event, error) {
159         return e.Interface.UpdateWithEventNamespace(event)
160 }
161
162 func (e *EventSinkImpl) Patch(event *v1.Event, data []byte) (*v1.Event, error) {
163         return e.Interface.PatchWithEventNamespace(event, data)
164 }