Get retrieves a Kubernetes resource by namespace and name. It first tries the cached client, and optionally falls back to a direct API server call if the resource is not found and WithGetFallback option is provided. Returns the object (either from cache or fallback) and any error.
(ctx context.Context, c client.Client, key client.ObjectKey, obj T, opts ...GetOption[T])
| 243 | // call if the resource is not found and WithGetFallback option is provided. |
| 244 | // Returns the object (either from cache or fallback) and any error. |
| 245 | func Get[T client.Object](ctx context.Context, c client.Client, key client.ObjectKey, obj T, opts ...GetOption[T]) (T, error) { |
| 246 | options := &getOptions[T]{} |
| 247 | for _, opt := range opts { |
| 248 | opt(options) |
| 249 | } |
| 250 | |
| 251 | typeLogLine := logLineForObject(obj) |
| 252 | |
| 253 | if err := c.Get(ctx, key, obj); err != nil { |
| 254 | if !apierrors.IsNotFound(err) || options.fallback == nil { |
| 255 | return obj, fmt.Errorf("failed to get %s %s/%s from cached client: %w", typeLogLine, key.Namespace, key.Name, err) |
| 256 | } |
| 257 | // Resource not in cache - try fallback. |
| 258 | r, err := options.fallback(ctx, key.Namespace, key.Name) |
| 259 | if err != nil { |
| 260 | return obj, fmt.Errorf("failed to get %s %s/%s from API Server: %w", typeLogLine, key.Namespace, key.Name, err) |
| 261 | } |
| 262 | return r, nil |
| 263 | } |
| 264 | return obj, nil |
| 265 | } |
no test coverage detected