Skip to content

Commit

Permalink
rename: kele--context-namespaces -> kele--namespaces-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jinnovation committed Apr 12, 2024
1 parent 5af3efe commit 97cdd1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ node `(elisp)Programmed Completion'."
(kele--with-progress (format "Switching to use context `%s'..." context)
(kele-kubectl-do "config" "use-context" context)))

;; TODO(#176): Update `kele--namespace-cache'
(transient-define-suffix kele-context-rename (old-name new-name)
"Rename context named OLD-NAME to NEW-NAME."
:key "r"
Expand All @@ -733,9 +734,10 @@ node `(elisp)Programmed Completion'."
#'kele--contexts-complete
nil t nil nil (kele-current-context-name))
(read-from-minibuffer "Rename to: ")))
;; TODO: This needs to update `kele--global-proxy-manager' as well.
;; TODO(#176): This needs to update `kele--global-proxy-manager' as well.
(kele-kubectl-do "config" "rename-context" old-name new-name))

;; TODO(#176): Update `kele--namespace-cache'
(transient-define-suffix kele-context-delete (context)
:key "d"
:description "Delete a context"
Expand Down Expand Up @@ -781,21 +783,21 @@ existing process *regardless of the value of PORT*."
Values are: (RESOURCE-NAME . (list RESOURCE)).")

(defvar kele--context-namespaces nil
(defvar kele--namespaces-cache nil
"An alist mapping contexts to their constituent namespaces.
If value is nil, the namespaces need to be fetched directly.")

(defun kele--clear-namespaces-for-context (context)
"Clear the stored namespaces for CONTEXT."
(setq kele--context-namespaces
(assoc-delete-all (intern context) kele--context-namespaces)))
(setq kele--namespaces-cache
(assoc-delete-all (intern context) kele--namespaces-cache)))

(defun kele--get-namespaces (context)
"Get namespaces for CONTEXT.
If not cached, will fetch and cache the namespaces."
(if-let ((namespaces (alist-get (intern context) kele--context-namespaces)))
(if-let ((namespaces (alist-get (intern context) kele--namespaces-cache)))
namespaces
(apply #'kele--cache-namespaces context
(kele--fetch-resource-names nil "v1" "namespaces" :context context))))
Expand All @@ -813,7 +815,7 @@ The cache has a TTL as defined by
`kele-resource-default-refresh-interval'.
Returns the passed-in list of namespaces."
(add-to-list 'kele--context-namespaces `(,(intern context) . ,namespace-names))
(add-to-list 'kele--namespaces-cache `(,(intern context) . ,namespace-names))
(run-with-timer
(kele--get-cache-ttl-for-resource 'namespace)
nil
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test-kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@

(describe "kele--clear-namespaces-for-context"
(before-each
(setq kele--context-namespaces '((foo . ("n1" "n2" "n3")))))
(setq kele--namespaces-cache '((foo . ("n1" "n2" "n3")))))
(describe "when context is present in cache"
(it "deletes the entry"
(kele--clear-namespaces-for-context "foo")
(expect kele--context-namespaces :to-equal nil))))
(expect kele--namespaces-cache :to-equal nil))))

(describe "kele--cache-namespaces"
(before-each
(spy-on 'run-with-timer)
(setq kele--context-namespaces nil))
(setq kele--namespaces-cache nil))

(it "adds namespaces correctly"
(kele--cache-namespaces "foobar" "n0" "n1" "n2")
(expect (alist-get 'foobar kele--context-namespaces)
(expect (alist-get 'foobar kele--namespaces-cache)
:to-equal
'("n0" "n1" "n2"))
(expect 'run-with-timer :to-have-been-called))
Expand Down

0 comments on commit 97cdd1c

Please sign in to comment.