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