From 8744ff12861475a82ceafad1de0948b6701bc33e Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Sat, 27 Jul 2024 18:15:36 +0200 Subject: [PATCH] kubed.el: Support ingress resources --- lisp/net/kubed-transient.el | 35 ++++++++++++++++++- lisp/net/kubed.el | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/lisp/net/kubed-transient.el b/lisp/net/kubed-transient.el index 0ee2feb2936..813962cadc0 100644 --- a/lisp/net/kubed-transient.el +++ b/lisp/net/kubed-transient.el @@ -34,6 +34,15 @@ "Prompt with PROMPT for Kubernetes namespace." (kubed-read-namespace prompt (kubed-current-namespace))) +(defun kubed-transient-read-ingressclass (prompt _initial-input _history) + "Prompt with PROMPT for Kubernetes ingress class." + (kubed-read-ingressclass prompt)) + +(defun kubed-transient-read-service-and-port (prompt _initial-input _history) + "Prompt with PROMPT for Kubernetes service and port number." + (let ((service (kubed-read-service prompt))) + (concat service ":" (number-to-string (read-number "Port number: "))))) + (defun kubed-transient-read-resource-definition-file-name (_prompt _initial-input _history) "Read and return Kubernetes resource definition file name." @@ -153,7 +162,8 @@ ["Kinds" ("d" "deployment" kubed-transient-create-deployment) ("n" "namespace" kubed-create-namespace) - ("c" "cronjob" kubed-transient-create-cronjob)] + ("c" "cronjob" kubed-transient-create-cronjob) + ("i" "ingress" kubed-transient-create-ingress)] ["Actions" ("+" "Create" kubed-create) ("!" "Command line" kubed-kubectl-command)] @@ -180,6 +190,29 @@ (transient-setup 'kubed-transient-create-cronjob nil nil :scope '("create" "cronjob"))) +;;;###autoload +(transient-define-prefix kubed-transient-create-ingress () + "Create Kubernetes ingress." + ["Options" + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace) + ("-c" "Class" "--class=" + :prompt "Class" :reader kubed-transient-read-ingressclass) + ("-d" "Default backend service" "--default-backend=" + :prompt "Default backend service" + :reader kubed-transient-read-service-and-port) + ("-a" "Annotation" "--annotation=" + :prompt "Ingress annotations: " + :multi-value repeat) + ("-r" "Rule" "--rule=" + :prompt "Ingress rule: ")] + ["Actions" + ("+" "Create" kubed-create-ingress) + ("!" "Command line" kubed-kubectl-command)] + (interactive) + (transient-setup 'kubed-transient-create-ingress nil nil + :scope '("create" "ingress"))) + ;;;###autoload (transient-define-prefix kubed-transient-create-deployment () "Create Kubernetes deployment." diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index aeb3caed1f8..a60d7fd4e4b 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -1155,6 +1155,61 @@ overrides the default command IMAGE runs." :namespaced nil :plural ingressclasses) +;;;###autoload (autoload 'kubed-display-ingress "kubed" nil t) +;;;###autoload (autoload 'kubed-edit-ingress "kubed" nil t) +;;;###autoload (autoload 'kubed-delete-ingresses "kubed" nil t) +;;;###autoload (autoload 'kubed-list-ingresss "kubed" nil t) +;;;###autoload (autoload 'kubed-create-ingress "kubed" nil t) +;;;###autoload (autoload 'kubed-ingress-prefix-map "kubed" nil t 'keymap) +(kubed-define-resource ingress + ((class ".spec.ingressClassName" 8) + (creationtimestamp ".metadata.creationTimestamp" 20)) + :plural ingresses + :create + ((name rules &optional namespace class default-backend annotations) + "Create Kubernetes ingress with name NAME and rules RULES. + +Optional argument NAMESPACE is the namespace to use for the ingress, +defaulting to the current namespace. CLASS is the ingress class, +ANNOTATIONS are a list of annotations for the created ingress, and +DEFAULT-BACKEND is the service to use as a backend for unhandled URLs." + (interactive + (let ((name (read-string "Create ingress with name: ")) + (rules nil) + (namespace nil) + (class nil) + (annotations nil) + (default-backend nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-create-ingress)) + (cond + ((string-match "--rule=\\(.+\\)" arg) + (push (match-string 1 arg) rules)) + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--class=\\(.+\\)" arg) + (setq class (match-string 1 arg))) + ((string-match "--default-backend=\\(.+\\)" arg) + (setq default-backend (match-string 1 arg))) + ((string-match "--annotation=\\(.+\\)" arg) + (push (match-string 1 arg) annotations)))) + (unless rules (setq rules (kubed-read-ingress-rules))) + (list name rules namespace class default-backend annotations))) + (unless (zerop + (apply #'call-process + kubed-kubectl-program nil nil nil + "create" "ingress" name + (append + (mapcan (lambda (rule) (list "--rule" rule)) rules) + (when namespace (list "--namespace" namespace)) + (when class (list "--class" class)) + (when default-backend + (list "--default-backend" default-backend)) + (mapcan (lambda (ann) (list "--annotation" ann)) + annotations)))) + (user-error "Failed to create Kubernetes ingress `%s'" name)) + (message "Created Kubernetes ingress `%s'." name) + (kubed-update-ingresses))) + ;; TODO: Events may be numerous. Need to only get a few. ;; ;;;###autoload (autoload 'kubed-list-events "kubed" nil t) ;; ;;;###autoload (autoload 'kubed-event-prefix-map "kubed" nil t 'keymap) @@ -1523,6 +1578,20 @@ Optional argument DEFAULT is the minibuffer default argument." (format-prompt prompt default) nil nil nil nil 'kubed-container-images-history default)) +(defvar kubed-ingress-rule-history nil + "Minibuffer history for `kubed-read-ingress-rules'.") + +(defun kubed-read-ingress-rules () + "Prompt with PROMPT for Kubernetes ingress rules." + (let ((rules (list (read-string "Ingress rule: " + nil 'kubed-ingress-rule-history)))) + (while (not + (string-empty-p + (car (push (read-string (format-prompt "Additional rules" "done") + nil 'kubed-ingress-rule-history) + rules))))) + (nreverse (cdr rules)))) + (defvar transient-current-command) (defun kubed-transient-args (&optional prefix) @@ -1784,6 +1853,7 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments." "S" 'kubed-secret-prefix-map "j" 'kubed-job-prefix-map "d" 'kubed-deployment-prefix-map + "i" 'kubed-ingress-prefix-map "c" 'kubed-cronjob-prefix-map "C" #'kubed-use-context "U" #'kubed-update-all -- 2.39.2