Skip to content

Commit

Permalink
fix: singularize resource names where appropriate (#197)
Browse files Browse the repository at this point in the history
Closes #196.

Currently, `kele-resource` uses the plural form of the resource name,
e.g. `pods`, everywhere. This was done primarily out of convenience but
obviously is grammatically inaccurate in some places, e.g. "get a single
pods."

Here we introduce a mechanism for looking up the canonical singular form
of any given resource name from the discovery cache and apply it to the
`kele-resource` suffix labels.
  • Loading branch information
jinnovation authored Jun 2, 2024
1 parent 23e947c commit 0e800a5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/references/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ versioning][semver].
- Fixed a bug where kubeconfig cluster entries with uppercase letters in the
server address erroneously cause resource kind completion to silently fail and
show no kinds present in cluster
- Fixed a bug in `kele-resource` where the improper singular/plural form of the
resource name is used, e.g. "Get a single pods" instead of "Get a single pod"

### Changed

Expand Down
56 changes: 54 additions & 2 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,48 @@ 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.
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)))))

(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* ((ctx (or context (kele-current-context-name)))
(resource-lists (kele--get-resource-lists-for-context cache ctx))

;; TODO:
;; - 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)))

(when (> (length filtered-resource-lists) 1)
(warn "More than one group-version found for resource name `%s'; assuming `%s'"
type
(alist-get 'groupVersion (car filtered-resource-lists))))

(let-alist (car filtered-resource-lists)
(let* ((resource (-first (lambda (resource)
(string-equal
(alist-get 'name resource)
type))
.resources))
(lower (alist-get 'singularName resource)))
(if (or (not lower) (string-equal lower ""))
(downcase (alist-get 'kind resource))
lower)))))

(cl-defmethod kele--resource-namespaced-p ((cache kele--discovery-cache)
group-version
type
Expand Down Expand Up @@ -1698,7 +1740,13 @@ instead of \"pod.\""
:verb 'delete
:resource .kind
:context .context)
(format "Delete a single %s" (propertize .kind 'face 'warning))
(format "Delete a single %s" (propertize
(kele--get-singular-for-plural
kele--global-discovery-cache
.kind
:context .context)
'face
'warning))
(format "Don't have permission to delete %s" .kind))))
(interactive
(-let* ((kind (kele--get-kind-arg))
Expand Down Expand Up @@ -1748,7 +1796,11 @@ instead of \"pod.\""
:verb 'get
:resource .kind
:context .context)
(format "Get a single %s" (propertize .kind 'face 'warning))
(format "Get a single %s"
(propertize
(kele--get-singular-for-plural kele--global-discovery-cache .kind :context .context)
'face
'warning))
(format "Don't have permission to get %s" .kind))))
(interactive
(-let* ((kind (kele--get-kind-arg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"kind": "AmbiguousThingFoo",
"name": "ambiguousthings",
"namespaced": true,
"singularName": "",
"singularName": "ambiguousthing",
"verbs": [
"get"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"kind": "AmbiguousThingBar",
"name": "ambiguousthings",
"namespaced": true,
"singularName": "",
"singularName": "ambiguousthing",
"verbs": [
"get"
]
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test-kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,25 @@
(it "sets the timer"
(expect (oref cache timer) :to-equal :timer)))

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

(it "fetches singularName"
(expect
(kele--get-singular-for-plural kele--global-discovery-cache
"ambiguousthings")
:to-equal
"ambiguousthing"))

(describe "when singularName not present"
(it "returns lowercased .metadata.name"
(expect (kele--get-singular-for-plural kele--global-discovery-cache
"componentstatuses")
:to-equal
"componentstatus"))))

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

0 comments on commit 0e800a5

Please sign in to comment.