(clientProvider ClientProvider)
| 55 | } |
| 56 | |
| 57 | func newLookupFunction(clientProvider ClientProvider) lookupFunc { |
| 58 | return func(apiversion string, kind string, namespace string, name string) (map[string]any, error) { |
| 59 | var client dynamic.ResourceInterface |
| 60 | c, namespaced, err := clientProvider.GetClientFor(apiversion, kind) |
| 61 | if err != nil { |
| 62 | return map[string]any{}, err |
| 63 | } |
| 64 | if namespaced && namespace != "" { |
| 65 | client = c.Namespace(namespace) |
| 66 | } else { |
| 67 | client = c |
| 68 | } |
| 69 | if name != "" { |
| 70 | // this will return a single object |
| 71 | obj, err := client.Get(context.Background(), name, metav1.GetOptions{}) |
| 72 | if err != nil { |
| 73 | if apierrors.IsNotFound(err) { |
| 74 | // Just return an empty interface when the object was not found. |
| 75 | // That way, users can use `if not (lookup ...)` in their templates. |
| 76 | return map[string]any{}, nil |
| 77 | } |
| 78 | return map[string]any{}, err |
| 79 | } |
| 80 | return obj.UnstructuredContent(), nil |
| 81 | } |
| 82 | // this will return a list |
| 83 | obj, err := client.List(context.Background(), metav1.ListOptions{}) |
| 84 | if err != nil { |
| 85 | if apierrors.IsNotFound(err) { |
| 86 | // Just return an empty interface when the object was not found. |
| 87 | // That way, users can use `if not (lookup ...)` in their templates. |
| 88 | return map[string]any{}, nil |
| 89 | } |
| 90 | return map[string]any{}, err |
| 91 | } |
| 92 | return obj.UnstructuredContent(), nil |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // getDynamicClientOnKind returns a dynamic client on an Unstructured type. This client can be further namespaced. |
| 97 | func getDynamicClientOnKind(apiversion string, kind string, config *rest.Config) (dynamic.NamespaceableResourceInterface, bool, error) { |
no test coverage detected
searching dependent graphs…