From 8e4b7202694e5da673fbc1483eb0bab2e80dba7d Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Tue, 14 May 2024 12:27:04 +0200 Subject: [PATCH] Remove deadcode Remove functions and variables that are reported by `deadcode`. --- pkg/havener/common.go | 9 ---- pkg/havener/convert.go | 40 --------------- pkg/havener/convert_test.go | 43 ---------------- pkg/havener/havener.go | 20 -------- pkg/havener/list.go | 55 --------------------- pkg/havener/purge.go | 98 ------------------------------------- 6 files changed, 265 deletions(-) delete mode 100644 pkg/havener/convert.go delete mode 100644 pkg/havener/convert_test.go diff --git a/pkg/havener/common.go b/pkg/havener/common.go index 7ec6efd5..d834f27a 100644 --- a/pkg/havener/common.go +++ b/pkg/havener/common.go @@ -74,15 +74,6 @@ func outOfClusterAuthentication(kubeConfig string) (*kubernetes.Clientset, *rest return clientset, config, err } -func isSystemNamespace(namespace string) bool { - switch namespace { - case "default", "kube-system", "ibm-system": - return true - } - - return false -} - func clusterName(kubeConfig string) (string, error) { data, err := os.ReadFile(kubeConfig) if err != nil { diff --git a/pkg/havener/convert.go b/pkg/havener/convert.go deleted file mode 100644 index 845639c2..00000000 --- a/pkg/havener/convert.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright © 2021 The Homeport Team -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package havener - -import ( - "fmt" -) - -var mods = []string{"Byte", "KiB", "MiB", "GiB", "TiB"} - -// HumanReadableSize returns the human readable version of a byte value. Values -// over 1024 TiB are currently not supported. -func HumanReadableSize(bytes int64) string { - value := float64(bytes) - i := 0 - for value > 1023.99999 { - value /= 1024.0 - i++ - } - - return fmt.Sprintf("%.1f %s", value, mods[i]) -} diff --git a/pkg/havener/convert_test.go b/pkg/havener/convert_test.go deleted file mode 100644 index 137e6ab7..00000000 --- a/pkg/havener/convert_test.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright © 2022 The Homeport Team -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -package havener_test - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - . "github.com/homeport/havener/pkg/havener" -) - -var _ = Describe("Convert", func() { - Context("value conversions", func() { - It("should return the human readable version of given number of bytes", func() { - Expect(HumanReadableSize(15784004812)).To(BeEquivalentTo("14.7 GiB")) - Expect(HumanReadableSize(1073741824)).To(BeEquivalentTo("1.0 GiB")) - Expect(HumanReadableSize(0)).To(BeEquivalentTo("0.0 Byte")) - }) - - It("should not fail on negative numbers", func() { - // esoteric edge case (negative numbers are not processed) - Expect(HumanReadableSize(-1)).To(BeEquivalentTo("-1.0 Byte")) - }) - }) -}) diff --git a/pkg/havener/havener.go b/pkg/havener/havener.go index 59954470..ce164aac 100644 --- a/pkg/havener/havener.go +++ b/pkg/havener/havener.go @@ -116,15 +116,6 @@ type Havener interface { // Option provides a way to set specific settings for creating the Havener setup type Option func(*Hvnr) -// KubeConfig is an option to set a specific Kubernetes configuration path -// -// Deprecated: Use WithKubeConfigPath instead -func KubeConfig(kubeConfig string) Option { - return func(h *Hvnr) { - h.kubeConfigPath = kubeConfig - } -} - func WithKubeConfigPath(kubeConfig string) Option { return func(h *Hvnr) { h.kubeConfigPath = kubeConfig } } @@ -134,17 +125,6 @@ func WithContext(ctx context.Context) Option { return func(h *Hvnr) { h.ctx = ctx } } -// NewHavenerFromFields returns a new Havener handle using the provided -// input arguments (use for unit testing only) -func NewHavenerFromFields(client kubernetes.Interface, restconfig *rest.Config, clusterName string, kubeConfigPath string) *Hvnr { - return &Hvnr{ - client: client, - restconfig: restconfig, - clusterName: clusterName, - kubeConfigPath: kubeConfigPath, - } -} - // NewHavener returns a new Havener handle to perform cluster actions func NewHavener(opts ...Option) (hvnr *Hvnr, err error) { hvnr = &Hvnr{} diff --git a/pkg/havener/list.go b/pkg/havener/list.go index 30a6faab..45625f79 100644 --- a/pkg/havener/list.go +++ b/pkg/havener/list.go @@ -37,36 +37,6 @@ import ( "github.com/gonvenience/text" ) -// ListStatefulSetsInNamespace returns all names of stateful sets in the given namespace -func ListStatefulSetsInNamespace(client kubernetes.Interface, namespace string) ([]string, error) { - list, err := client.AppsV1beta1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{}) - if err != nil { - return nil, err - } - - result := make([]string, len(list.Items)) - for idx, item := range list.Items { - result[idx] = item.Name - } - - return result, nil -} - -// ListDeploymentsInNamespace returns all names of deployments in the given namespace -func ListDeploymentsInNamespace(client kubernetes.Interface, namespace string) ([]string, error) { - list, err := client.AppsV1beta1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{}) - if err != nil { - return nil, err - } - - result := make([]string, len(list.Items)) - for idx, item := range list.Items { - result[idx] = item.Name - } - - return result, nil -} - // ListNamespaces lists all namespaces func (h *Hvnr) ListNamespaces() ([]string, error) { logf(Verbose, "Listing all namespaces") @@ -85,31 +55,6 @@ func (h *Hvnr) ListNamespaces() ([]string, error) { return result, nil } -// ListSecretsInNamespace lists all secrets in a given namespace -func ListSecretsInNamespace(client kubernetes.Interface, namespace string) ([]string, error) { - secretList, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{}) - if err != nil { - return nil, err - } - - result := make([]string, len(secretList.Items)) - for i, secret := range secretList.Items { - result[i] = secret.Name - } - - return result, nil -} - -// SecretsInNamespace lists all secrets in a given namespace -func SecretsInNamespace(client kubernetes.Interface, namespace string) ([]corev1.Secret, error) { - secretList, err := client.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{}) - if err != nil { - return nil, err - } - - return secretList.Items, nil -} - // ListPods lists all pods in the given namespaces, if no namespace is given, // then all namespaces currently available in the cluster will be used func (h *Hvnr) ListPods(namespaces ...string) ([]*corev1.Pod, error) { diff --git a/pkg/havener/purge.go b/pkg/havener/purge.go index d527050d..b0272ef0 100644 --- a/pkg/havener/purge.go +++ b/pkg/havener/purge.go @@ -22,110 +22,12 @@ package havener import ( "context" - "fmt" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" ) -var defaultPropagationPolicy = metav1.DeletePropagationForeground - -// PurgeDeploymentsInNamespace removes all deployments in the given namespace. -func PurgeDeploymentsInNamespace(kubeClient kubernetes.Interface, namespace string) error { - // Skip known system namespaces - if isSystemNamespace(namespace) { - return nil - } - - if deployments, err := ListDeploymentsInNamespace(kubeClient, namespace); err == nil { - for _, name := range deployments { - err := kubeClient.AppsV1beta1().Deployments(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &defaultPropagationPolicy, - }) - - if err != nil { - return err - } - } - } - - return nil -} - -// PurgeStatefulSetsInNamespace removes all stateful sets in the given namespace. -func PurgeStatefulSetsInNamespace(kubeClient kubernetes.Interface, namespace string) error { - // Skip known system namespaces - if isSystemNamespace(namespace) { - return nil - } - - if statefulsets, err := ListStatefulSetsInNamespace(kubeClient, namespace); err == nil { - for _, name := range statefulsets { - err := kubeClient.AppsV1beta1().StatefulSets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &defaultPropagationPolicy, - }) - - if err != nil { - return err - } - } - } - - return nil -} - -// PurgeNamespace removes the namespace from the cluster. -func PurgeNamespace(kubeClient kubernetes.Interface, namespace string) error { - // Skip known system namespaces - if isSystemNamespace(namespace) { - return nil - } - - ns, err := kubeClient.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{}) - if err != nil { - // Bail out if namespace is already deleted - switch err.(type) { - case *errors.StatusError: - if err.Error() == fmt.Sprintf(`namespaces "%s" not found`, namespace) { - return nil - } - } - - return err - } - - // Bail out if namespace is already in Phase Terminating - switch ns.Status.Phase { - case corev1.NamespaceTerminating: - return nil - } - - watcher, err := kubeClient.CoreV1().Namespaces().Watch(context.TODO(), metav1.SingleObject(ns.ObjectMeta)) - if err != nil { - return err - } - - if err := kubeClient.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{PropagationPolicy: &defaultPropagationPolicy}); err != nil { - return err - } - - for event := range watcher.ResultChan() { - switch event.Type { - case watch.Deleted: - watcher.Stop() - - case watch.Error: - return fmt.Errorf("failed to watch namespace %s during deletion: %w", namespace, err) - } - } - - return nil -} - // PurgePod removes the pod in the given namespace. func PurgePod(kubeClient kubernetes.Interface, namespace string, podName string, gracePeriodSeconds int64, propagationPolicy metav1.DeletionPropagation) error { logf(Verbose, "Deleting pod %s in namespace %s", podName, namespace)