2 Copyright 2016 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.
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"
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
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)
53 err := e.client.Post().
54 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
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
67 func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
69 err := e.client.Put().
70 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
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)
89 err := e.client.Patch(types.StrategicMergePatchType).
90 NamespaceIfScoped(incompleteEvent.Namespace, len(incompleteEvent.Namespace) > 0).
92 Name(incompleteEvent.Name).
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)
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)
110 stringRefKind := string(ref.Kind)
112 if stringRefKind != "" {
113 refKind = &stringRefKind
115 stringRefUID := string(ref.UID)
117 if stringRefUID != "" {
118 refUID = &stringRefUID
120 fieldSelector := e.GetFieldSelector(&ref.Name, &ref.Namespace, refKind, refUID)
121 return e.List(metav1.ListOptions{FieldSelector: fieldSelector.String()})
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
132 if involvedObjectNamespace != nil {
133 field["involvedObject.namespace"] = *involvedObjectNamespace
135 if involvedObjectKind != nil {
136 field["involvedObject.kind"] = *involvedObjectKind
138 if involvedObjectUID != nil {
139 field["involvedObject.uid"] = *involvedObjectUID
141 return field.AsSelector()
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"
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
154 func (e *EventSinkImpl) Create(event *v1.Event) (*v1.Event, error) {
155 return e.Interface.CreateWithEventNamespace(event)
158 func (e *EventSinkImpl) Update(event *v1.Event) (*v1.Event, error) {
159 return e.Interface.UpdateWithEventNamespace(event)
162 func (e *EventSinkImpl) Patch(event *v1.Event, data []byte) (*v1.Event, error) {
163 return e.Interface.PatchWithEventNamespace(event, data)