Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove kubectl version check #117

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 9 additions & 59 deletions kubel.el
Original file line number Diff line number Diff line change
Expand Up @@ -289,34 +289,6 @@ CMD is the command string to run."
(defvar kubel-selector-history '()
"List of previously used selectors.")

;; fallback list of resources if the version of kubectl doesn't support api-resources command
(defvar kubel-kubernetes-resources-list
'("Pods"
"Services"
"Namespaces"
"Nodes"
"Configmaps"
"Secrets"
"Bindings"
"PersistentVolumeClaims"
"PersistentVolumes"
"ReplicationControllers"
"ResourceQuotas"
"ServiceAccounts"
"Deployments"
"DaemonSets"
"ReplicaSets"
"StatefulSets"
"Jobs"
"hpa"
"Images"
"Ingresses"
"ClusterRoles"
"RoleBindings"
"Roles"))

(defvar kubel--kubernetes-version-cached nil)

(defvar kubel--kubernetes-resources-list-cached nil)

(defvar kubel--can-get-namespace-cached nil)
Expand All @@ -327,37 +299,20 @@ CMD is the command string to run."

(defvar kubel--selected-items '())

(defun kubel--kubernetes-resources-list ()
"Get list of resources from cache or from fetching the api resource."
(if (null kubel--kubernetes-resources-list-cached)
(setq kubel--kubernetes-resources-list-cached
(kubel--fetch-api-resource-list))
kubel--kubernetes-resources-list-cached))

(defun kubel--invalidate-context-caches ()
"Invalidate the context caches."
(setq kubel--kubernetes-resources-list-cached nil)
(setq kubel--kubernetes-version-cached nil)
(setq kubel--can-get-namespace-cached nil)
(setq kubel--namespace-list-cached nil)
(setq kubel--label-values-cached nil))

(defun kubel-kubernetes-version ()
"Return a list with (major-version minor-version)."
(let* ((version-json (json-read-from-string
(if (null kubel--kubernetes-version-cached)
(setq kubel--kubernetes-version-cached
(kubel--exec-to-string (concat kubel-kubectl " version --output=json")))
kubel--kubernetes-version-cached)))
(version-string (assoc-default 'serverVersion version-json)))
(list
(string-to-number (cdr (assoc-string "major" version-string)))
(string-to-number (cdr (assoc-string "minor" version-string))))))

(defun kubel-kubernetes-compatible-p (version)
"Return TRUE if kubernetes version is greater than or equal to VERSION.
VERSION should be a list of (major-version minor-version)."
(let*
((kubernetes-version (kubel-kubernetes-version))
(kubernetes-major-version (nth 0 kubernetes-version))
(kubernetes-minor-version (nth 1 kubernetes-version)))
(and
(<= (nth 0 version) kubernetes-major-version)
(or (<= (nth 1 version) kubernetes-minor-version) (< (nth 0 version) kubernetes-major-version)))))

(defun kubel--populate-list ()
"Return a list with a tabulated list format and \"tabulated-list-entries\"."
(let* ((body (kubel--exec-to-string (concat (kubel--get-command-prefix) " get " kubel-resource)))
Expand Down Expand Up @@ -893,12 +848,7 @@ the context caches, including the cached resource list."
(interactive "P")
(when refresh (kubel--invalidate-context-caches))
(let* ((current-buffer-name (kubel--buffer-name))
(resource-list (if (kubel-kubernetes-compatible-p '(1 13))
(if (null kubel--kubernetes-resources-list-cached)
(setq kubel--kubernetes-resources-list-cached
(kubel--fetch-api-resource-list))
kubel--kubernetes-resources-list-cached)
kubel-kubernetes-resources-list))
(resource-list (kubel--kubernetes-resources-list))
(kubel--buffer (get-buffer current-buffer-name))
(last-default-directory (when kubel--buffer (with-current-buffer kubel--buffer default-directory))))
(setq kubel-resource
Expand Down Expand Up @@ -1117,7 +1067,7 @@ RESET is to be called if the search is nil after the first attempt."
"Quickly edit any resource."
(interactive)
(kubel--describe-resource
(completing-read "Select resource: " kubel-kubernetes-resources-list)))
(completing-read "Select resource: " kubel--kubernetes-resources-list)))

(defun kubel-show-process-buffer ()
"Show the kubel-process-buffer."
Expand Down
39 changes: 0 additions & 39 deletions test/kubel-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
(require 'ert)
(require 'cl-macs)

(ert-deftest kubel--test-kubernetes-compatible-p ()
(cl-letf (((symbol-function 'kubel-kubernetes-version)
(lambda () '(1 10 9))))
(should (kubel-kubernetes-compatible-p '(1 9 0)))
(should (kubel-kubernetes-compatible-p '(0 11 0)))
(should (not (kubel-kubernetes-compatible-p '(1 11 0))))
(should (not (kubel-kubernetes-compatible-p '(2 0 25))))))

(ert-deftest kubel--test-extract-value ()
(let ((line "0123456789"))
(should (equal "0" (kubel--extract-value line 0 1)))
Expand All @@ -24,37 +16,6 @@
(should (equal "-" (kubel--extract-value " " 0 5)))
(should (equal "-" (kubel--extract-value " " 0 "end")))))

(ert-deftest kubel--test-kubernetes-version ()
(setq kubel--kubernetes-version-cached nil)
(cl-letf (((symbol-function 'kubel--exec-to-string)
(lambda (cmd) "{
\"clientVersion\": {
\"major\": \"1\",
\"minor\": \"28\",
\"gitVersion\": \"v1.28.1\",
\"gitCommit\": \"8dc49c4b984b897d423aab4971090e1879eb4f23\",
\"gitTreeState\": \"archive\",
\"buildDate\": \"1980-01-01T00:00:00Z\",
\"goVersion\": \"go1.20.7\",
\"compiler\": \"gc\",
\"platform\": \"darwin/arm64\"
},
\"kustomizeVersion\": \"v5.0.4-0.20230601165947-6ce0bf390ce3\",
\"serverVersion\": {
\"major\": \"1\",
\"minor\": \"27\",
\"gitVersion\": \"v1.27.4+k3s1\",
\"gitCommit\": \"36645e7311e9bdbbf2adb79ecd8bd68556bc86f6\",
\"gitTreeState\": \"clean\",
\"buildDate\": \"1970-01-01T01:01:01Z\",
\"goVersion\": \"go1.20.7\",
\"compiler\": \"gc\",
\"platform\": \"linux/amd64\"
}
}
")))
(should (equal '(1 27) (kubel-kubernetes-version)))))

;; (ert "kubel--test-.*")

(provide 'kubel-test)
Expand Down