Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2019 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 v1beta1 |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | |
| 23 | "k8s.io/api/events/v1beta1" |
| 24 | "k8s.io/apimachinery/pkg/types" |
| 25 | ) |
| 26 | |
| 27 | // The EventExpansion interface allows manually adding extra methods to the EventInterface. |
| 28 | // TODO: Add querying functions to the event expansion |
| 29 | type EventExpansion interface { |
| 30 | // CreateWithEventNamespace is the same as a Create |
| 31 | // except that it sends the request to the event.Namespace. |
| 32 | CreateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event, error) |
| 33 | // UpdateWithEventNamespace is the same as a Update |
| 34 | // except that it sends the request to the event.Namespace. |
| 35 | UpdateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event, error) |
| 36 | // PatchWithEventNamespace is the same as an Update |
| 37 | // except that it sends the request to the event.Namespace. |
| 38 | PatchWithEventNamespace(event *v1beta1.Event, data []byte) (*v1beta1.Event, error) |
| 39 | } |
| 40 | |
| 41 | // CreateWithEventNamespace makes a new event. |
| 42 | // Returns the copy of the event the server returns, or an error. |
| 43 | // The namespace to create the event within is deduced from the event. |
| 44 | // it must either match this event client's namespace, or this event client must |
| 45 | // have been created with the "" namespace. |
| 46 | func (e *events) CreateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event, error) { |
| 47 | if e.ns != "" && event.Namespace != e.ns { |
| 48 | return nil, fmt.Errorf("can't create an event with namespace '%v' in namespace '%v'", event.Namespace, e.ns) |
| 49 | } |
| 50 | result := &v1beta1.Event{} |
| 51 | err := e.client.Post(). |
| 52 | NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0). |
| 53 | Resource("events"). |
| 54 | Body(event). |
| 55 | Do(context.TODO()). |
| 56 | Into(result) |
| 57 | return result, err |
| 58 | } |
| 59 | |
| 60 | // UpdateWithEventNamespace modifies an existing event. |
| 61 | // It returns the copy of the event that the server returns, or an error. |
| 62 | // The namespace and key to update the event within is deduced from the event. |
| 63 | // The namespace must either match this event client's namespace, or this event client must have been |
| 64 | // created with the "" namespace. |
| 65 | // Update also requires the ResourceVersion to be set in the event object. |
| 66 | func (e *events) UpdateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event, error) { |
| 67 | if e.ns != "" && event.Namespace != e.ns { |
| 68 | return nil, fmt.Errorf("can't update an event with namespace '%v' in namespace '%v'", event.Namespace, e.ns) |
| 69 | } |
| 70 | result := &v1beta1.Event{} |
| 71 | err := e.client.Put(). |
| 72 | NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0). |
| 73 | Resource("events"). |
| 74 | Name(event.Name). |
| 75 | Body(event). |
| 76 | Do(context.TODO()). |
| 77 | Into(result) |
| 78 | return result, err |
| 79 | } |
| 80 | |
| 81 | // PatchWithEventNamespace modifies an existing event. |
| 82 | // It returns the copy of the event that the server returns, or an error. |
| 83 | // The namespace and name of the target event is deduced from the event. |
| 84 | // The namespace must either match this event client's namespace, or this event client must |
| 85 | // have been created with the "" namespace. |
| 86 | func (e *events) PatchWithEventNamespace(event *v1beta1.Event, data []byte) (*v1beta1.Event, error) { |
| 87 | if e.ns != "" && event.Namespace != e.ns { |
| 88 | return nil, fmt.Errorf("can't patch an event with namespace '%v' in namespace '%v'", event.Namespace, e.ns) |
| 89 | } |
| 90 | result := &v1beta1.Event{} |
| 91 | err := e.client.Patch(types.StrategicMergePatchType). |
| 92 | NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0). |
| 93 | Resource("events"). |
| 94 | Name(event.Name). |
| 95 | Body(data). |
| 96 | Do(context.TODO()). |
| 97 | Into(result) |
| 98 | return result, err |
| 99 | } |