blob: 211cf0603c09cd8e608486a8ec4292eca370fae5 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20 "context"
21 "fmt"
22
23 v1 "k8s.io/api/core/v1"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/fields"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/types"
28 ref "k8s.io/client-go/tools/reference"
29)
30
31// The EventExpansion interface allows manually adding extra methods to the EventInterface.
32type EventExpansion interface {
33 // CreateWithEventNamespace is the same as a Create, except that it sends the request to the event.Namespace.
34 CreateWithEventNamespace(event *v1.Event) (*v1.Event, error)
35 // UpdateWithEventNamespace is the same as a Update, except that it sends the request to the event.Namespace.
36 UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error)
37 PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error)
38 // Search finds events about the specified object
39 Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error)
40 // Returns the appropriate field selector based on the API version being used to communicate with the server.
41 // The returned field selector can be used with List and Watch to filter desired events.
42 GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector
43}
44
45// CreateWithEventNamespace makes a new event. Returns the copy of the event the server returns,
46// or an error. The namespace to create the event within is deduced from the
47// event; it must either match this event client's namespace, or this event
48// client must have been created with the "" namespace.
49func (e *events) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
50 if e.ns != "" && event.Namespace != e.ns {
51 return nil, fmt.Errorf("can't create an event with namespace '%v' in namespace '%v'", event.Namespace, e.ns)
52 }
53 result := &v1.Event{}
54 err := e.client.Post().
55 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
56 Resource("events").
57 Body(event).
58 Do(context.TODO()).
59 Into(result)
60 return result, err
61}
62
63// UpdateWithEventNamespace modifies an existing event. It returns the copy of the event that the server returns,
64// or an error. The namespace and key to update the event within is deduced from the event. The
65// namespace must either match this event client's namespace, or this event client must have been
66// created with the "" namespace. Update also requires the ResourceVersion to be set in the event
67// object.
68func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
69 result := &v1.Event{}
70 err := e.client.Put().
71 NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
72 Resource("events").
73 Name(event.Name).
74 Body(event).
75 Do(context.TODO()).
76 Into(result)
77 return result, err
78}
79
80// PatchWithEventNamespace modifies an existing event. It returns the copy of
81// the event that the server returns, or an error. The namespace and name of the
82// target event is deduced from the incompleteEvent. The namespace must either
83// match this event client's namespace, or this event client must have been
84// created with the "" namespace.
85func (e *events) PatchWithEventNamespace(incompleteEvent *v1.Event, data []byte) (*v1.Event, error) {
86 if e.ns != "" && incompleteEvent.Namespace != e.ns {
87 return nil, fmt.Errorf("can't patch an event with namespace '%v' in namespace '%v'", incompleteEvent.Namespace, e.ns)
88 }
89 result := &v1.Event{}
90 err := e.client.Patch(types.StrategicMergePatchType).
91 NamespaceIfScoped(incompleteEvent.Namespace, len(incompleteEvent.Namespace) > 0).
92 Resource("events").
93 Name(incompleteEvent.Name).
94 Body(data).
95 Do(context.TODO()).
96 Into(result)
97 return result, err
98}
99
100// Search finds events about the specified object. The namespace of the
101// object must match this event's client namespace unless the event client
102// was made with the "" namespace.
103func (e *events) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
104 ref, err := ref.GetReference(scheme, objOrRef)
105 if err != nil {
106 return nil, err
107 }
108 if len(e.ns) > 0 && ref.Namespace != e.ns {
109 return nil, fmt.Errorf("won't be able to find any events of namespace '%v' in namespace '%v'", ref.Namespace, e.ns)
110 }
111 stringRefKind := string(ref.Kind)
112 var refKind *string
113 if len(stringRefKind) > 0 {
114 refKind = &stringRefKind
115 }
116 stringRefUID := string(ref.UID)
117 var refUID *string
118 if len(stringRefUID) > 0 {
119 refUID = &stringRefUID
120 }
121 fieldSelector := e.GetFieldSelector(&ref.Name, &ref.Namespace, refKind, refUID)
122 return e.List(context.TODO(), metav1.ListOptions{FieldSelector: fieldSelector.String()})
123}
124
125// Returns the appropriate field selector based on the API version being used to communicate with the server.
126// The returned field selector can be used with List and Watch to filter desired events.
127func (e *events) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
128 field := fields.Set{}
129 if involvedObjectName != nil {
130 field["involvedObject.name"] = *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// DEPRECATED: please use "involvedObject.name" inline.
146func GetInvolvedObjectNameFieldLabel(version string) string {
147 return "involvedObject.name"
148}
149
150// TODO: This is a temporary arrangement and will be removed once all clients are moved to use the clientset.
151type EventSinkImpl struct {
152 Interface EventInterface
153}
154
155func (e *EventSinkImpl) Create(event *v1.Event) (*v1.Event, error) {
156 return e.Interface.CreateWithEventNamespace(event)
157}
158
159func (e *EventSinkImpl) Update(event *v1.Event) (*v1.Event, error) {
160 return e.Interface.UpdateWithEventNamespace(event)
161}
162
163func (e *EventSinkImpl) Patch(event *v1.Event, data []byte) (*v1.Event, error) {
164 return e.Interface.PatchWithEventNamespace(event, data)
165}