| 1 |
/* |
| 2 |
Copyright 2024 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 gentype |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"time" |
| 23 |
|
| 24 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 |
"k8s.io/apimachinery/pkg/runtime" |
| 26 |
types "k8s.io/apimachinery/pkg/types" |
| 27 |
watch "k8s.io/apimachinery/pkg/watch" |
| 28 |
rest "k8s.io/client-go/rest" |
| 29 |
"k8s.io/client-go/util/apply" |
| 30 |
"k8s.io/client-go/util/consistencydetector" |
| 31 |
"k8s.io/client-go/util/watchlist" |
| 32 |
"k8s.io/klog/v2" |
| 33 |
) |
| 34 |
|
| 35 |
// objectWithMeta matches objects implementing both runtime.Object and metav1.Object. |
| 36 |
type objectWithMeta interface { |
| 37 |
runtime.Object |
| 38 |
metav1.Object |
| 39 |
} |
| 40 |
|
| 41 |
// namedObject matches comparable objects implementing GetName(); it is intended for use with apply declarative configurations. |
| 42 |
type namedObject interface { |
| 43 |
comparable |
| 44 |
GetName() *string |
| 45 |
} |
| 46 |
|
| 47 |
// Client represents a client, optionally namespaced, with no support for lists or apply declarative configurations. |
| 48 |
type Client[T objectWithMeta] struct { |
| 49 |
resource string |
| 50 |
client rest.Interface |
| 51 |
namespace string // "" for non-namespaced clients |
| 52 |
newObject func() T |
| 53 |
parameterCodec runtime.ParameterCodec |
| 54 |
|
| 55 |
prefersProtobuf bool |
| 56 |
} |
| 57 |
|
| 58 |
// ClientWithList represents a client with support for lists. |
| 59 |
type ClientWithList[T objectWithMeta, L runtime.Object] struct { |
| 60 |
*Client[T] |
| 61 |
alsoLister[T, L] |
| 62 |
} |
| 63 |
|
| 64 |
// ClientWithApply represents a client with support for apply declarative configurations. |
| 65 |
type ClientWithApply[T objectWithMeta, C namedObject] struct { |
| 66 |
*Client[T] |
| 67 |
alsoApplier[T, C] |
| 68 |
} |
| 69 |
|
| 70 |
// ClientWithListAndApply represents a client with support for lists and apply declarative configurations. |
| 71 |
type ClientWithListAndApply[T objectWithMeta, L runtime.Object, C namedObject] struct { |
| 72 |
*Client[T] |
| 73 |
alsoLister[T, L] |
| 74 |
alsoApplier[T, C] |
| 75 |
} |
| 76 |
|
| 77 |
// Helper types for composition |
| 78 |
type alsoLister[T objectWithMeta, L runtime.Object] struct { |
| 79 |
client *Client[T] |
| 80 |
newList func() L |
| 81 |
} |
| 82 |
|
| 83 |
type alsoApplier[T objectWithMeta, C namedObject] struct { |
| 84 |
client *Client[T] |
| 85 |
} |
| 86 |
|
| 87 |
type Option[T objectWithMeta] func(*Client[T]) |
| 88 |
|
| 89 |
func PrefersProtobuf[T objectWithMeta]() Option[T] { |
| 90 |
return func(c *Client[T]) { c.prefersProtobuf = true } |
| 91 |
} |
| 92 |
|
| 93 |
// NewClient constructs a client, namespaced or not, with no support for lists or apply. |
| 94 |
// Non-namespaced clients are constructed by passing an empty namespace (""). |
| 95 |
func NewClient[T objectWithMeta]( |
| 96 |
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T, |
| 97 |
options ...Option[T], |
| 98 |
) *Client[T] { |
| 99 |
c := &Client[T]{ |
| 100 |
resource: resource, |
| 101 |
client: client, |
| 102 |
parameterCodec: parameterCodec, |
| 103 |
namespace: namespace, |
| 104 |
newObject: emptyObjectCreator, |
| 105 |
} |
| 106 |
for _, option := range options { |
| 107 |
option(c) |
| 108 |
} |
| 109 |
return c |
| 110 |
} |
| 111 |
|
| 112 |
// NewClientWithList constructs a namespaced client with support for lists. |
| 113 |
func NewClientWithList[T objectWithMeta, L runtime.Object]( |
| 114 |
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T, |
| 115 |
emptyListCreator func() L, options ...Option[T], |
| 116 |
) *ClientWithList[T, L] { |
| 117 |
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...) |
| 118 |
return &ClientWithList[T, L]{ |
| 119 |
typeClient, |
| 120 |
alsoLister[T, L]{typeClient, emptyListCreator}, |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// NewClientWithApply constructs a namespaced client with support for apply declarative configurations. |
| 125 |
func NewClientWithApply[T objectWithMeta, C namedObject]( |
| 126 |
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T, |
| 127 |
options ...Option[T], |
| 128 |
) *ClientWithApply[T, C] { |
| 129 |
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...) |
| 130 |
return &ClientWithApply[T, C]{ |
| 131 |
typeClient, |
| 132 |
alsoApplier[T, C]{typeClient}, |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// NewClientWithListAndApply constructs a client with support for lists and applying declarative configurations. |
| 137 |
func NewClientWithListAndApply[T objectWithMeta, L runtime.Object, C namedObject]( |
| 138 |
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T, |
| 139 |
emptyListCreator func() L, options ...Option[T], |
| 140 |
) *ClientWithListAndApply[T, L, C] { |
| 141 |
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...) |
| 142 |
return &ClientWithListAndApply[T, L, C]{ |
| 143 |
typeClient, |
| 144 |
alsoLister[T, L]{typeClient, emptyListCreator}, |
| 145 |
alsoApplier[T, C]{typeClient}, |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// GetClient returns the REST interface. |
| 150 |
func (c *Client[T]) GetClient() rest.Interface { |
| 151 |
return c.client |
| 152 |
} |
| 153 |
|
| 154 |
// GetNamespace returns the client's namespace, if any. |
| 155 |
func (c *Client[T]) GetNamespace() string { |
| 156 |
return c.namespace |
| 157 |
} |
| 158 |
|
| 159 |
// Get takes name of the resource, and returns the corresponding object, and an error if there is any. |
| 160 |
func (c *Client[T]) Get(ctx context.Context, name string, options metav1.GetOptions) (T, error) { |
| 161 |
result := c.newObject() |
| 162 |
err := c.client.Get(). |
| 163 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 164 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 165 |
Resource(c.resource). |
| 166 |
Name(name). |
| 167 |
VersionedParams(&options, c.parameterCodec). |
| 168 |
Do(ctx). |
| 169 |
Into(result) |
| 170 |
return result, err |
| 171 |
} |
| 172 |
|
| 173 |
// List takes label and field selectors, and returns the list of resources that match those selectors. |
| 174 |
func (l *alsoLister[T, L]) List(ctx context.Context, opts metav1.ListOptions) (L, error) { |
| 175 |
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := watchlist.PrepareWatchListOptionsFromListOptions(opts); watchListOptionsErr != nil { |
| 176 |
klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr) |
| 177 |
} else if hasWatchListOptionsPrepared { |
| 178 |
result, err := l.watchList(ctx, watchListOptions) |
| 179 |
if err == nil { |
| 180 |
consistencydetector.CheckWatchListFromCacheDataConsistencyIfRequested(ctx, "watchlist request for "+l.client.resource, l.list, opts, result) |
| 181 |
return result, nil |
| 182 |
} |
| 183 |
klog.Warningf("The watchlist request for %s ended with an error, falling back to the standard LIST semantics, err = %v", l.client.resource, err) |
| 184 |
} |
| 185 |
result, err := l.list(ctx, opts) |
| 186 |
if err == nil { |
| 187 |
consistencydetector.CheckListFromCacheDataConsistencyIfRequested(ctx, "list request for "+l.client.resource, l.list, opts, result) |
| 188 |
} |
| 189 |
return result, err |
| 190 |
} |
| 191 |
|
| 192 |
func (l *alsoLister[T, L]) list(ctx context.Context, opts metav1.ListOptions) (L, error) { |
| 193 |
list := l.newList() |
| 194 |
var timeout time.Duration |
| 195 |
if opts.TimeoutSeconds != nil { |
| 196 |
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second |
| 197 |
} |
| 198 |
err := l.client.client.Get(). |
| 199 |
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf). |
| 200 |
NamespaceIfScoped(l.client.namespace, l.client.namespace != ""). |
| 201 |
Resource(l.client.resource). |
| 202 |
VersionedParams(&opts, l.client.parameterCodec). |
| 203 |
Timeout(timeout). |
| 204 |
Do(ctx). |
| 205 |
Into(list) |
| 206 |
return list, err |
| 207 |
} |
| 208 |
|
| 209 |
// watchList establishes a watch stream with the server and returns the list of resources. |
| 210 |
func (l *alsoLister[T, L]) watchList(ctx context.Context, opts metav1.ListOptions) (result L, err error) { |
| 211 |
var timeout time.Duration |
| 212 |
if opts.TimeoutSeconds != nil { |
| 213 |
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second |
| 214 |
} |
| 215 |
result = l.newList() |
| 216 |
err = l.client.client.Get(). |
| 217 |
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf). |
| 218 |
NamespaceIfScoped(l.client.namespace, l.client.namespace != ""). |
| 219 |
Resource(l.client.resource). |
| 220 |
VersionedParams(&opts, l.client.parameterCodec). |
| 221 |
Timeout(timeout). |
| 222 |
WatchList(ctx). |
| 223 |
Into(result) |
| 224 |
return |
| 225 |
} |
| 226 |
|
| 227 |
// Watch returns a watch.Interface that watches the requested resources. |
| 228 |
func (c *Client[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { |
| 229 |
var timeout time.Duration |
| 230 |
if opts.TimeoutSeconds != nil { |
| 231 |
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second |
| 232 |
} |
| 233 |
opts.Watch = true |
| 234 |
return c.client.Get(). |
| 235 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 236 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 237 |
Resource(c.resource). |
| 238 |
VersionedParams(&opts, c.parameterCodec). |
| 239 |
Timeout(timeout). |
| 240 |
Watch(ctx) |
| 241 |
} |
| 242 |
|
| 243 |
// Create takes the representation of a resource and creates it. Returns the server's representation of the resource, and an error, if there is any. |
| 244 |
func (c *Client[T]) Create(ctx context.Context, obj T, opts metav1.CreateOptions) (T, error) { |
| 245 |
result := c.newObject() |
| 246 |
err := c.client.Post(). |
| 247 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 248 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 249 |
Resource(c.resource). |
| 250 |
VersionedParams(&opts, c.parameterCodec). |
| 251 |
Body(obj). |
| 252 |
Do(ctx). |
| 253 |
Into(result) |
| 254 |
return result, err |
| 255 |
} |
| 256 |
|
| 257 |
// Update takes the representation of a resource and updates it. Returns the server's representation of the resource, and an error, if there is any. |
| 258 |
func (c *Client[T]) Update(ctx context.Context, obj T, opts metav1.UpdateOptions) (T, error) { |
| 259 |
result := c.newObject() |
| 260 |
err := c.client.Put(). |
| 261 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 262 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 263 |
Resource(c.resource). |
| 264 |
Name(obj.GetName()). |
| 265 |
VersionedParams(&opts, c.parameterCodec). |
| 266 |
Body(obj). |
| 267 |
Do(ctx). |
| 268 |
Into(result) |
| 269 |
return result, err |
| 270 |
} |
| 271 |
|
| 272 |
// UpdateStatus updates the status subresource of a resource. Returns the server's representation of the resource, and an error, if there is any. |
| 273 |
func (c *Client[T]) UpdateStatus(ctx context.Context, obj T, opts metav1.UpdateOptions) (T, error) { |
| 274 |
result := c.newObject() |
| 275 |
err := c.client.Put(). |
| 276 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 277 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 278 |
Resource(c.resource). |
| 279 |
Name(obj.GetName()). |
| 280 |
SubResource("status"). |
| 281 |
VersionedParams(&opts, c.parameterCodec). |
| 282 |
Body(obj). |
| 283 |
Do(ctx). |
| 284 |
Into(result) |
| 285 |
return result, err |
| 286 |
} |
| 287 |
|
| 288 |
// Delete takes name of the resource and deletes it. Returns an error if one occurs. |
| 289 |
func (c *Client[T]) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { |
| 290 |
return c.client.Delete(). |
| 291 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 292 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 293 |
Resource(c.resource). |
| 294 |
Name(name). |
| 295 |
Body(&opts). |
| 296 |
Do(ctx). |
| 297 |
Error() |
| 298 |
} |
| 299 |
|
| 300 |
// DeleteCollection deletes a collection of objects. |
| 301 |
func (l *alsoLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { |
| 302 |
var timeout time.Duration |
| 303 |
if listOpts.TimeoutSeconds != nil { |
| 304 |
timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second |
| 305 |
} |
| 306 |
return l.client.client.Delete(). |
| 307 |
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf). |
| 308 |
NamespaceIfScoped(l.client.namespace, l.client.namespace != ""). |
| 309 |
Resource(l.client.resource). |
| 310 |
VersionedParams(&listOpts, l.client.parameterCodec). |
| 311 |
Timeout(timeout). |
| 312 |
Body(&opts). |
| 313 |
Do(ctx). |
| 314 |
Error() |
| 315 |
} |
| 316 |
|
| 317 |
// Patch applies the patch and returns the patched resource. |
| 318 |
func (c *Client[T]) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (T, error) { |
| 319 |
result := c.newObject() |
| 320 |
err := c.client.Patch(pt). |
| 321 |
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf). |
| 322 |
NamespaceIfScoped(c.namespace, c.namespace != ""). |
| 323 |
Resource(c.resource). |
| 324 |
Name(name). |
| 325 |
SubResource(subresources...). |
| 326 |
VersionedParams(&opts, c.parameterCodec). |
| 327 |
Body(data). |
| 328 |
Do(ctx). |
| 329 |
Into(result) |
| 330 |
return result, err |
| 331 |
} |
| 332 |
|
| 333 |
// Apply takes the given apply declarative configuration, applies it and returns the applied resource. |
| 334 |
func (a *alsoApplier[T, C]) Apply(ctx context.Context, obj C, opts metav1.ApplyOptions) (T, error) { |
| 335 |
result := a.client.newObject() |
| 336 |
if obj == *new(C) { |
| 337 |
return *new(T), fmt.Errorf("object provided to Apply must not be nil") |
| 338 |
} |
| 339 |
patchOpts := opts.ToPatchOptions() |
| 340 |
if obj.GetName() == nil { |
| 341 |
return *new(T), fmt.Errorf("obj.Name must be provided to Apply") |
| 342 |
} |
| 343 |
|
| 344 |
request, err := apply.NewRequest(a.client.client, obj) |
| 345 |
if err != nil { |
| 346 |
return *new(T), err |
| 347 |
} |
| 348 |
|
| 349 |
err = request. |
| 350 |
UseProtobufAsDefaultIfPreferred(a.client.prefersProtobuf). |
| 351 |
NamespaceIfScoped(a.client.namespace, a.client.namespace != ""). |
| 352 |
Resource(a.client.resource). |
| 353 |
Name(*obj.GetName()). |
| 354 |
VersionedParams(&patchOpts, a.client.parameterCodec). |
| 355 |
Do(ctx). |
| 356 |
Into(result) |
| 357 |
return result, err |
| 358 |
} |
| 359 |
|
| 360 |
// Apply takes the given apply declarative configuration, applies it to the status subresource and returns the applied resource. |
| 361 |
func (a *alsoApplier[T, C]) ApplyStatus(ctx context.Context, obj C, opts metav1.ApplyOptions) (T, error) { |
| 362 |
if obj == *new(C) { |
| 363 |
return *new(T), fmt.Errorf("object provided to Apply must not be nil") |
| 364 |
} |
| 365 |
patchOpts := opts.ToPatchOptions() |
| 366 |
|
| 367 |
if obj.GetName() == nil { |
| 368 |
return *new(T), fmt.Errorf("obj.Name must be provided to Apply") |
| 369 |
} |
| 370 |
|
| 371 |
request, err := apply.NewRequest(a.client.client, obj) |
| 372 |
if err != nil { |
| 373 |
return *new(T), err |
| 374 |
} |
| 375 |
|
| 376 |
result := a.client.newObject() |
| 377 |
err = request. |
| 378 |
UseProtobufAsDefaultIfPreferred(a.client.prefersProtobuf). |
| 379 |
NamespaceIfScoped(a.client.namespace, a.client.namespace != ""). |
| 380 |
Resource(a.client.resource). |
| 381 |
Name(*obj.GetName()). |
| 382 |
SubResource("status"). |
| 383 |
VersionedParams(&patchOpts, a.client.parameterCodec). |
| 384 |
Do(ctx). |
| 385 |
Into(result) |
| 386 |
return result, err |
| 387 |
} |