@@ -21,12 +21,15 @@ import (
21
21
"fmt"
22
22
23
23
"k8s.io/apimachinery/pkg/api/meta"
24
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/runtime/schema"
27
28
"k8s.io/apimachinery/pkg/runtime/serializer"
28
29
"k8s.io/client-go/kubernetes/scheme"
30
+ "k8s.io/client-go/metadata"
29
31
"k8s.io/client-go/rest"
32
+
30
33
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
31
34
)
32
35
@@ -76,6 +79,11 @@ func New(config *rest.Config, options Options) (Client, error) {
76
79
resourceByType : make (map [schema.GroupVersionKind ]* resourceMeta ),
77
80
}
78
81
82
+ rawMetaClient , err := metadata .NewForConfig (config )
83
+ if err != nil {
84
+ return nil , fmt .Errorf ("unable to construct metadata-only client for use as part of client: %w" , err )
85
+ }
86
+
79
87
c := & client {
80
88
typedClient : typedClient {
81
89
cache : clientcache ,
@@ -85,6 +93,10 @@ func New(config *rest.Config, options Options) (Client, error) {
85
93
cache : clientcache ,
86
94
paramCodec : noConversionParamCodec {},
87
95
},
96
+ metadataClient : metadataClient {
97
+ client : rawMetaClient ,
98
+ restMapper : options .Mapper ,
99
+ },
88
100
scheme : options .Scheme ,
89
101
mapper : options .Mapper ,
90
102
}
@@ -99,6 +111,7 @@ var _ Client = &client{}
99
111
type client struct {
100
112
typedClient typedClient
101
113
unstructuredClient unstructuredClient
114
+ metadataClient metadataClient
102
115
scheme * runtime.Scheme
103
116
mapper meta.RESTMapper
104
117
}
@@ -125,67 +138,88 @@ func (c *client) RESTMapper() meta.RESTMapper {
125
138
126
139
// Create implements client.Client
127
140
func (c * client ) Create (ctx context.Context , obj runtime.Object , opts ... CreateOption ) error {
128
- _ , ok := obj .(* unstructured. Unstructured )
129
- if ok {
141
+ switch obj .(type ) {
142
+ case * unstructured. Unstructured :
130
143
return c .unstructuredClient .Create (ctx , obj , opts ... )
144
+ case * metav1.PartialObjectMetadata :
145
+ return fmt .Errorf ("cannot create using only metadata" )
146
+ default :
147
+ return c .typedClient .Create (ctx , obj , opts ... )
131
148
}
132
- return c .typedClient .Create (ctx , obj , opts ... )
133
149
}
134
150
135
151
// Update implements client.Client
136
152
func (c * client ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
137
153
defer c .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
138
- _ , ok := obj .(* unstructured. Unstructured )
139
- if ok {
154
+ switch obj .(type ) {
155
+ case * unstructured. Unstructured :
140
156
return c .unstructuredClient .Update (ctx , obj , opts ... )
157
+ case * metav1.PartialObjectMetadata :
158
+ return fmt .Errorf ("cannot update using only metadata -- did you mean to patch?" )
159
+ default :
160
+ return c .typedClient .Update (ctx , obj , opts ... )
141
161
}
142
- return c .typedClient .Update (ctx , obj , opts ... )
143
162
}
144
163
145
164
// Delete implements client.Client
146
165
func (c * client ) Delete (ctx context.Context , obj runtime.Object , opts ... DeleteOption ) error {
147
- _ , ok := obj .(* unstructured. Unstructured )
148
- if ok {
166
+ switch obj .(type ) {
167
+ case * unstructured. Unstructured :
149
168
return c .unstructuredClient .Delete (ctx , obj , opts ... )
169
+ case * metav1.PartialObjectMetadata :
170
+ return c .metadataClient .Delete (ctx , obj , opts ... )
171
+ default :
172
+ return c .typedClient .Delete (ctx , obj , opts ... )
150
173
}
151
- return c .typedClient .Delete (ctx , obj , opts ... )
152
174
}
153
175
154
176
// DeleteAllOf implements client.Client
155
177
func (c * client ) DeleteAllOf (ctx context.Context , obj runtime.Object , opts ... DeleteAllOfOption ) error {
156
- _ , ok := obj .(* unstructured. Unstructured )
157
- if ok {
178
+ switch obj .(type ) {
179
+ case * unstructured. Unstructured :
158
180
return c .unstructuredClient .DeleteAllOf (ctx , obj , opts ... )
181
+ case * metav1.PartialObjectMetadata :
182
+ return c .metadataClient .DeleteAllOf (ctx , obj , opts ... )
183
+ default :
184
+ return c .typedClient .DeleteAllOf (ctx , obj , opts ... )
159
185
}
160
- return c .typedClient .DeleteAllOf (ctx , obj , opts ... )
161
186
}
162
187
163
188
// Patch implements client.Client
164
189
func (c * client ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
165
190
defer c .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
166
- _ , ok := obj .(* unstructured. Unstructured )
167
- if ok {
191
+ switch obj .(type ) {
192
+ case * unstructured. Unstructured :
168
193
return c .unstructuredClient .Patch (ctx , obj , patch , opts ... )
194
+ case * metav1.PartialObjectMetadata :
195
+ return c .metadataClient .Patch (ctx , obj , patch , opts ... )
196
+ default :
197
+ return c .typedClient .Patch (ctx , obj , patch , opts ... )
169
198
}
170
- return c .typedClient .Patch (ctx , obj , patch , opts ... )
171
199
}
172
200
173
201
// Get implements client.Client
174
202
func (c * client ) Get (ctx context.Context , key ObjectKey , obj runtime.Object ) error {
175
- _ , ok := obj .(* unstructured. Unstructured )
176
- if ok {
203
+ switch obj .(type ) {
204
+ case * unstructured. Unstructured :
177
205
return c .unstructuredClient .Get (ctx , key , obj )
206
+ case * metav1.PartialObjectMetadata :
207
+ return c .metadataClient .Get (ctx , key , obj )
208
+ default :
209
+ return c .typedClient .Get (ctx , key , obj )
178
210
}
179
- return c .typedClient .Get (ctx , key , obj )
180
211
}
181
212
182
213
// List implements client.Client
183
214
func (c * client ) List (ctx context.Context , obj runtime.Object , opts ... ListOption ) error {
184
- _ , ok := obj .(* unstructured. UnstructuredList )
185
- if ok {
215
+ switch obj .(type ) {
216
+ case * unstructured. Unstructured :
186
217
return c .unstructuredClient .List (ctx , obj , opts ... )
218
+ case * metav1.PartialObjectMetadata :
219
+ return c .metadataClient .List (ctx , obj , opts ... )
220
+ default :
221
+ return c .typedClient .List (ctx , obj , opts ... )
187
222
}
188
- return c .typedClient .List (ctx , obj , opts ... )
189
223
}
190
224
191
225
// Status implements client.StatusClient
@@ -204,19 +238,25 @@ var _ StatusWriter = &statusWriter{}
204
238
// Update implements client.StatusWriter
205
239
func (sw * statusWriter ) Update (ctx context.Context , obj runtime.Object , opts ... UpdateOption ) error {
206
240
defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
207
- _ , ok := obj .(* unstructured. Unstructured )
208
- if ok {
241
+ switch obj .(type ) {
242
+ case * unstructured. Unstructured :
209
243
return sw .client .unstructuredClient .UpdateStatus (ctx , obj , opts ... )
244
+ case * metav1.PartialObjectMetadata :
245
+ return fmt .Errorf ("cannot update status using only metadata -- did you mean to patch?" )
246
+ default :
247
+ return sw .client .typedClient .UpdateStatus (ctx , obj , opts ... )
210
248
}
211
- return sw .client .typedClient .UpdateStatus (ctx , obj , opts ... )
212
249
}
213
250
214
251
// Patch implements client.Client
215
252
func (sw * statusWriter ) Patch (ctx context.Context , obj runtime.Object , patch Patch , opts ... PatchOption ) error {
216
253
defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
217
- _ , ok := obj .(* unstructured. Unstructured )
218
- if ok {
254
+ switch obj .(type ) {
255
+ case * unstructured. Unstructured :
219
256
return sw .client .unstructuredClient .PatchStatus (ctx , obj , patch , opts ... )
257
+ case * metav1.PartialObjectMetadata :
258
+ return sw .client .metadataClient .PatchStatus (ctx , obj , patch , opts ... )
259
+ default :
260
+ return sw .client .typedClient .PatchStatus (ctx , obj , patch , opts ... )
220
261
}
221
- return sw .client .typedClient .PatchStatus (ctx , obj , patch , opts ... )
222
262
}
0 commit comments