Skip to content

Commit

Permalink
Port-forward: Completion for exposed ports on services (#245)
Browse files Browse the repository at this point in the history
Contributes to #240.
  • Loading branch information
jinnovation authored Sep 27, 2024
1 parent e9d5e8f commit 09c465a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
28 changes: 26 additions & 2 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -2034,9 +2034,16 @@ NAMESPACE and CONTEXT are used to identify the resource type to query for."
(format "%s to port-forward to: " (kele--singular gvk))
(-cut kele--resources-complete <> <> <> :cands cands)))

;; TODO: Completion on the resource's exposed ports
(resource (kele--get-resource gvk name :context context :namespace ns))

;; TODO: Error if the port is already in use
(port (number-to-string (read-number "Port: "))))
;; TODO: Extend port completion to all appropriate resource types
(port (if (string-equal (oref resource kind) "services")
(completing-read "Port: "
(-map (lambda (port-spec) (number-to-string (alist-get 'port port-spec)))
(kele--service-ports resource))
nil t)
(number-to-string (read-number "Port: ")))))
(list context ns gvk name port)))
(let* ((proc-name (format "kele: port-forward (%s/%s, %s, %s, %s)" (oref gvk kind) name context namespace port))
(proc (make-process
Expand Down Expand Up @@ -2082,6 +2089,23 @@ PORTS is used according to `completion-extra-properties'."
'face 'completions-annotations))))
ports))

(cl-defun kele--service-ports (obj &key (protocol nil))
"Get the exposed ports for service OBJ.
If PROTOCOL is provided, filter for only ports of that protocol.
OBJ is assumed to be a `kele--resource-container' containing a
Service resource."
(let-alist (oref obj resource)
(let ((ports .spec.ports))
(if protocol
(-filter (lambda (port-spec)
(equal
(alist-get 'protocol port-spec)
protocol))
(append ports '()))
(append ports '())))))

(defun kele--port-forwards-active-p ()
"Return non-nil if there are any port-forwards active."
(< 0 (length (mapcar 'car kele--active-port-forwards))))
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test-kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,25 @@ metadata:
(kele-list-kill)
(expect 'vtable-revert-command :to-have-been-called))))

(describe "`kele--service-ports'"
(it "retrieves the port specs"
(expect
(kele--service-ports
(kele--resource-container-create
:resource '((spec . ((ports . [((name . "foo")
(protocol . "TCP")
(port . 8081))]))))))
:to-equal
'(((name . "foo") (protocol . "TCP") (port . 8081)))))
(it "filters by protocol"
(expect
(kele--service-ports
(kele--resource-container-create
:resource '((spec . ((ports . [((name . "foo")
(protocol . "TCP")
(port . 8081))])))))
:protocol "UDP")
:to-equal
nil)))

;;; test-kele.el ends here

0 comments on commit 09c465a

Please sign in to comment.