Skip to content

Commit

Permalink
Add function to look up arbitrary resource names in discovery cache (#…
Browse files Browse the repository at this point in the history
…214)

To implement #212, we need the ability to convert from the kind name,
e.g. `Pod`, to the plural form, e.g. `pods`. This would be analogous to
the existing `kele--get-singular-for-plural`, e.g.
`kele--get-plural-for-kind`.

Since we'd rather not implement one function for each 2-permutation of
singular/plural/kind, we generalize the existing
`get-singular-for-plural` to allow for looking up a "discovery resource"
by any given key within it. This will allow us to look up with, say,
`singularName`, and use the retval to get the `kind`.
  • Loading branch information
jinnovation authored Jun 6, 2024
1 parent 8082e53 commit feb048d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
39 changes: 23 additions & 16 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -352,21 +352,18 @@ If CONTEXT is nil, use the current context."
(-map (-partial #'alist-get 'groupVersion))
(-sort (lambda (a _) (equal a "v1")))))

(defun kele--resource-list-has-resource (name resource-list)
"Return non-nil when RESOURCE-LIST has resource named NAME.
(defun kele--resource-list-find-resource (name field resource-list)
"Return non-nil when RESOURCE-LIST has resource with value NAME at FIELD.
RESOURCE-LIST expected to be an alist mirroring the
APIResourceList schema."
(->> (alist-get 'resources resource-list)
(-any (lambda (resource)
(equal (alist-get 'name resource) name)))))
(equal (alist-get field resource) name)))))

(cl-defmethod kele--get-singular-for-plural ((cache kele--discovery-cache)
type
&key context)
"Look up the singular name for a given resource TYPE in CACHE.
TYPE is expected to be the plural name of the resource.
(cl-defmethod kele--get-discovery-resource ((cache kele--discovery-cache)
type &key context (lookup-key 'name))
"Look up the discovery cache entry in CACHE using TYPE and LOOKUP-KEY.
If CONTEXT is nil, use the current context."
(let* ((ctx (or context (kele-current-context-name)))
Expand All @@ -376,7 +373,7 @@ If CONTEXT is nil, use the current context."
;; - Accept group-version optional kwarg
;; - Error if len(filtered-resource-lists) > 1 AND group-version not specified
;; - Filter for group-version
(filtered-resource-lists (-filter (-partial #'kele--resource-list-has-resource type) resource-lists)))
(filtered-resource-lists (-filter (-partial #'kele--resource-list-find-resource type lookup-key) resource-lists)))

(when (> (length filtered-resource-lists) 1)
(warn "More than one group-version found for resource name `%s'; assuming `%s'"
Expand All @@ -386,13 +383,23 @@ If CONTEXT is nil, use the current context."
(let-alist (car filtered-resource-lists)
(let* ((resource (-first (lambda (resource)
(string-equal
(alist-get 'name resource)
(alist-get lookup-key resource)
type))
.resources))
(lower (alist-get 'singularName resource)))
(if (or (not lower) (string-equal lower ""))
(downcase (alist-get 'kind resource))
lower)))))
.resources)))
resource))))

(cl-defmethod kele--get-singular-for-plural ((cache kele--discovery-cache)
type
&key context)
"Look up the singular name for a given resource TYPE in CACHE.
TYPE is expected to be the plural name of the resource.
If CONTEXT is nil, use the current context."
(let-alist (kele--get-discovery-resource cache type :context context)
(if (or (not .singularName) (string-equal .singularName ""))
(downcase .kind)
.singularName)))

(cl-defmethod kele--resource-namespaced-p ((cache kele--discovery-cache)
group-version
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test-kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@
(it "sets the timer"
(expect (oref cache timer) :to-equal :timer)))

(describe "kele--get-discovery-resource"
(before-each
(setq kele-cache-dir (f-expand "./tests/testdata/cache"))
(async-wait (kele--cache-update kele--global-discovery-cache)))

(it "fetches by kind"
(expect (kele--get-discovery-resource kele--global-discovery-cache
"AmbiguousThingFoo" :lookup-key 'kind)
:to-be-truthy)))

(describe "kele--get-singular-for-plural"
(before-each
(setq kele-cache-dir (f-expand "./tests/testdata/cache"))
Expand Down

0 comments on commit feb048d

Please sign in to comment.