Skip to content

Commit

Permalink
Remove kubectl version check (#117)
Browse files Browse the repository at this point in the history
I noticed a check that verifies if the server version is greater than 1.13. As per the Kubernetes
release documentation (https://kubernetes.io/releases/patch-releases/), version 1.13 has reached its
end-of-life (EOL) status since 2019. Considering this information, I strongly believe that this
check is unnecessary and can be safely removed.

It's only used here:
- https://github.com/abrochard/kubel/blob/a84f09cca8cdc71d55aa78304db4156df46d04e8/kubel.el#L896

Removing this check will not impact the functionality of the codebase, as it is only required for
outdated Kubernetes versions that are more than four years old and no longer supported. It is highly
unlikely that anyone would be using such an outdated version that cannot be found online for
download.

By removing this unnecessary code, we can eliminate technical debt and improve the overall
readability and maintainability of the codebase.

Thank you for considering this proposal.
  • Loading branch information
d1egoaz authored Nov 14, 2023
1 parent a84f09c commit 4e3d362
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 98 deletions.
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

0 comments on commit 4e3d362

Please sign in to comment.