Skip to content

Commit 49e218c

Browse files
committed
client-go event: add WithContext expansion methods
Only the v1 API should be in use. The v1beta1 API therefore doesn't get updated and doesn't need the context.TODO anymore.
1 parent 00dd287 commit 49e218c

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

staging/src/k8s.io/client-go/kubernetes/typed/core/v1/event_expansion.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,29 @@ import (
3131
// The EventExpansion interface allows manually adding extra methods to the EventInterface.
3232
type EventExpansion interface {
3333
// CreateWithEventNamespace is the same as a Create, except that it sends the request to the event.Namespace.
34+
//
35+
// Deprecated: use [CreateWithEventNamespaceWithContext] instead.
3436
CreateWithEventNamespace(event *v1.Event) (*v1.Event, error)
3537
// UpdateWithEventNamespace is the same as a Update, except that it sends the request to the event.Namespace.
38+
//
39+
// Deprecated: use [UpdateWithEventNamespaceWithContext] instead.
3640
UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error)
3741
// PatchWithEventNamespace is the same as a Patch, except that it sends the request to the event.Namespace.
42+
//
43+
// Deprecated: use [PatchWithEventNamespaceWithContext] instead.
3844
PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error)
3945
// Search finds events about the specified object
46+
//
47+
// Deprecated: use [SearchWithContext] instead.
4048
Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error)
49+
// CreateWithEventNamespaceWithContext is the same as a Create, except that it sends the request to the event.Namespace.
50+
CreateWithEventNamespaceWithContext(ctx context.Context, event *v1.Event) (*v1.Event, error)
51+
// UpdateWithEventNamespaceWithContext is the same as a Update, except that it sends the request to the event.Namespace.
52+
UpdateWithEventNamespaceWithContext(ctx context.Context, event *v1.Event) (*v1.Event, error)
53+
// PatchWithEventNamespaceWithContext is the same as a Patch, except that it sends the request to the event.Namespace.
54+
PatchWithEventNamespaceWithContext(ctx context.Context, event *v1.Event, data []byte) (*v1.Event, error)
55+
// SearchWithContext finds events about the specified object
56+
SearchWithContext(ctx context.Context, scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error)
4157
// Returns the appropriate field selector based on the API version being used to communicate with the server.
4258
// The returned field selector can be used with List and Watch to filter desired events.
4359
GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector
@@ -47,7 +63,17 @@ type EventExpansion interface {
4763
// or an error. The namespace to create the event within is deduced from the
4864
// event; it must either match this event client's namespace, or this event
4965
// client must have been created with the "" namespace.
66+
//
67+
// Deprecated: use [CreateWithEventNamespaceWithContext] instead.
5068
func (e *events) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
69+
return e.CreateWithEventNamespaceWithContext(context.Background(), event)
70+
}
71+
72+
// CreateWithEventNamespaceWithContext makes a new event. Returns the copy of the event the server returns,
73+
// or an error. The namespace to create the event within is deduced from the
74+
// event; it must either match this event client's namespace, or this event
75+
// client must have been created with the "" namespace.
76+
func (e *events) CreateWithEventNamespaceWithContext(ctx context.Context, event *v1.Event) (*v1.Event, error) {
5177
if e.GetNamespace() != "" && event.Namespace != e.GetNamespace() {
5278
return nil, fmt.Errorf("can't create an event with namespace '%v' in namespace '%v'", event.Namespace, e.GetNamespace())
5379
}
@@ -56,7 +82,7 @@ func (e *events) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
5682
NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
5783
Resource("events").
5884
Body(event).
59-
Do(context.TODO()).
85+
Do(ctx).
6086
Into(result)
6187
return result, err
6288
}
@@ -66,7 +92,18 @@ func (e *events) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
6692
// namespace must either match this event client's namespace, or this event client must have been
6793
// created with the "" namespace. Update also requires the ResourceVersion to be set in the event
6894
// object.
95+
//
96+
// Deprecated: use [UpdateWithEventNamespaceWithContext] instead.
6997
func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
98+
return e.UpdateWithEventNamespaceWithContext(context.Background(), event)
99+
}
100+
101+
// UpdateWithEventNamespaceWithContext modifies an existing event. It returns the copy of the event that the server returns,
102+
// or an error. The namespace and key to update the event within is deduced from the event. The
103+
// namespace must either match this event client's namespace, or this event client must have been
104+
// created with the "" namespace. Update also requires the ResourceVersion to be set in the event
105+
// object.
106+
func (e *events) UpdateWithEventNamespaceWithContext(ctx context.Context, event *v1.Event) (*v1.Event, error) {
70107
if e.GetNamespace() != "" && event.Namespace != e.GetNamespace() {
71108
return nil, fmt.Errorf("can't update an event with namespace '%v' in namespace '%v'", event.Namespace, e.GetNamespace())
72109
}
@@ -76,7 +113,7 @@ func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
76113
Resource("events").
77114
Name(event.Name).
78115
Body(event).
79-
Do(context.TODO()).
116+
Do(ctx).
80117
Into(result)
81118
return result, err
82119
}
@@ -86,7 +123,18 @@ func (e *events) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
86123
// target event is deduced from the incompleteEvent. The namespace must either
87124
// match this event client's namespace, or this event client must have been
88125
// created with the "" namespace.
126+
//
127+
// Deprecated: use [PatchWithEventNamespaceWithContext] instead.
89128
func (e *events) PatchWithEventNamespace(incompleteEvent *v1.Event, data []byte) (*v1.Event, error) {
129+
return e.PatchWithEventNamespaceWithContext(context.Background(), incompleteEvent, data)
130+
}
131+
132+
// PatchWithEventNamespaceWithContext modifies an existing event. It returns the copy of
133+
// the event that the server returns, or an error. The namespace and name of the
134+
// target event is deduced from the incompleteEvent. The namespace must either
135+
// match this event client's namespace, or this event client must have been
136+
// created with the "" namespace.
137+
func (e *events) PatchWithEventNamespaceWithContext(ctx context.Context, incompleteEvent *v1.Event, data []byte) (*v1.Event, error) {
90138
if e.GetNamespace() != "" && incompleteEvent.Namespace != e.GetNamespace() {
91139
return nil, fmt.Errorf("can't patch an event with namespace '%v' in namespace '%v'", incompleteEvent.Namespace, e.GetNamespace())
92140
}
@@ -96,15 +144,24 @@ func (e *events) PatchWithEventNamespace(incompleteEvent *v1.Event, data []byte)
96144
Resource("events").
97145
Name(incompleteEvent.Name).
98146
Body(data).
99-
Do(context.TODO()).
147+
Do(ctx).
100148
Into(result)
101149
return result, err
102150
}
103151

104152
// Search finds events about the specified object. The namespace of the
105153
// object must match this event's client namespace unless the event client
106154
// was made with the "" namespace.
155+
//
156+
// Deprecated: use [SearchWithContext] instead.
107157
func (e *events) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
158+
return e.SearchWithContext(context.Background(), scheme, objOrRef)
159+
}
160+
161+
// SearchWithContext finds events about the specified object. The namespace of the
162+
// object must match this event's client namespace unless the event client
163+
// was made with the "" namespace.
164+
func (e *events) SearchWithContext(ctx context.Context, scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
108165
ref, err := ref.GetReference(scheme, objOrRef)
109166
if err != nil {
110167
return nil, err
@@ -123,7 +180,7 @@ func (e *events) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.Ev
123180
refUID = &stringRefUID
124181
}
125182
fieldSelector := e.GetFieldSelector(&ref.Name, &ref.Namespace, refKind, refUID)
126-
return e.List(context.TODO(), metav1.ListOptions{FieldSelector: fieldSelector.String()})
183+
return e.List(ctx, metav1.ListOptions{FieldSelector: fieldSelector.String()})
127184
}
128185

129186
// Returns the appropriate field selector based on the API version being used to communicate with the server.

staging/src/k8s.io/client-go/kubernetes/typed/core/v1/fake/fake_event_expansion.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package fake
1818

1919
import (
20+
"context"
21+
2022
v1 "k8s.io/api/core/v1"
2123
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
"k8s.io/apimachinery/pkg/fields"
@@ -25,7 +27,12 @@ import (
2527
core "k8s.io/client-go/testing"
2628
)
2729

30+
// Deprecated: use [CreateWithEventNamespaceWithContext] instead.
2831
func (c *fakeEvents) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
32+
return c.CreateWithEventNamespaceWithContext(context.Background(), event)
33+
}
34+
35+
func (c *fakeEvents) CreateWithEventNamespaceWithContext(_ context.Context, event *v1.Event) (*v1.Event, error) {
2936
var action core.CreateActionImpl
3037
if c.Namespace() != "" {
3138
action = core.NewCreateAction(c.Resource(), c.Namespace(), event)
@@ -41,7 +48,14 @@ func (c *fakeEvents) CreateWithEventNamespace(event *v1.Event) (*v1.Event, error
4148
}
4249

4350
// Update replaces an existing event. Returns the copy of the event the server returns, or an error.
51+
//
52+
// Deprecated: use [UpdateWithEventNamespaceWithContext] instead.
4453
func (c *fakeEvents) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error) {
54+
return c.UpdateWithEventNamespaceWithContext(context.Background(), event)
55+
}
56+
57+
// Update replaces an existing event. Returns the copy of the event the server returns, or an error.
58+
func (c *fakeEvents) UpdateWithEventNamespaceWithContext(_ context.Context, event *v1.Event) (*v1.Event, error) {
4559
var action core.UpdateActionImpl
4660
if c.Namespace() != "" {
4761
action = core.NewUpdateAction(c.Resource(), c.Namespace(), event)
@@ -59,6 +73,12 @@ func (c *fakeEvents) UpdateWithEventNamespace(event *v1.Event) (*v1.Event, error
5973
// PatchWithEventNamespace patches an existing event. Returns the copy of the event the server returns, or an error.
6074
// TODO: Should take a PatchType as an argument probably.
6175
func (c *fakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.Event, error) {
76+
return c.PatchWithEventNamespaceWithContext(context.Background(), event, data)
77+
}
78+
79+
// PatchWithEventNamespaceWithContext patches an existing event. Returns the copy of the event the server returns, or an error.
80+
// TODO: Should take a PatchType as an argument probably.
81+
func (c *fakeEvents) PatchWithEventNamespaceWithContext(_ context.Context, event *v1.Event, data []byte) (*v1.Event, error) {
6282
// TODO: Should be configurable to support additional patch strategies.
6383
pt := types.StrategicMergePatchType
6484
var action core.PatchActionImpl
@@ -76,7 +96,14 @@ func (c *fakeEvents) PatchWithEventNamespace(event *v1.Event, data []byte) (*v1.
7696
}
7797

7898
// Search returns a list of events matching the specified object.
99+
//
100+
// Deprecated: use [SearchWithContext] instead.
79101
func (c *fakeEvents) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
102+
return c.SearchWithContext(context.Background(), scheme, objOrRef)
103+
}
104+
105+
// SearchWithContext returns a list of events matching the specified object.
106+
func (c *fakeEvents) SearchWithContext(_ context.Context, scheme *runtime.Scheme, objOrRef runtime.Object) (*v1.EventList, error) {
80107
var action core.ListActionImpl
81108
if c.Namespace() != "" {
82109
action = core.NewListAction(c.Resource(), c.Kind(), c.Namespace(), metav1.ListOptions{})

staging/src/k8s.io/client-go/kubernetes/typed/events/v1beta1/event_expansion.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (e *events) CreateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event,
5252
NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
5353
Resource("events").
5454
Body(event).
55-
Do(context.TODO()).
55+
Do(context.Background() /* Nothing to do here, v1beta1 will be removed eventually. */).
5656
Into(result)
5757
return result, err
5858
}
@@ -73,7 +73,7 @@ func (e *events) UpdateWithEventNamespace(event *v1beta1.Event) (*v1beta1.Event,
7373
Resource("events").
7474
Name(event.Name).
7575
Body(event).
76-
Do(context.TODO()).
76+
Do(context.Background() /* Nothing to do here, v1beta1 will be removed eventually. */).
7777
Into(result)
7878
return result, err
7979
}
@@ -93,7 +93,7 @@ func (e *events) PatchWithEventNamespace(event *v1beta1.Event, data []byte) (*v1
9393
Resource("events").
9494
Name(event.Name).
9595
Body(data).
96-
Do(context.TODO()).
96+
Do(context.Background() /* Nothing to do here, v1beta1 will be removed eventually. */).
9797
Into(result)
9898
return result, err
9999
}

0 commit comments

Comments
 (0)