]> git.eshelyaron.com Git - emacs.git/commitdiff
kubed.el: Support ingress resources
authorEshel Yaron <me@eshelyaron.com>
Sat, 27 Jul 2024 16:15:36 +0000 (18:15 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sat, 27 Jul 2024 16:15:36 +0000 (18:15 +0200)
lisp/net/kubed-transient.el
lisp/net/kubed.el

index 0ee2feb2936239779bda72c228ba0235594911c8..813962cadc0d31cd3e29eb02929cd8b4325d8249 100644 (file)
   "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."
   ["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)]
   (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."
index aeb3caed1f890e1268b0f311908ea1da68122f71..a60d7fd4e4b5b16c2590b1e6dec192148a0c199a 100644 (file)
@@ -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