Skip to content

Commit

Permalink
Remove deadcode
Browse files Browse the repository at this point in the history
Remove functions and variables that are reported by `deadcode`.
  • Loading branch information
HeavyWombat committed May 14, 2024
1 parent e3f2cde commit 8e4b720
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 265 deletions.
9 changes: 0 additions & 9 deletions pkg/havener/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 0 additions & 40 deletions pkg/havener/convert.go

This file was deleted.

43 changes: 0 additions & 43 deletions pkg/havener/convert_test.go

This file was deleted.

20 changes: 0 additions & 20 deletions pkg/havener/havener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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{}
Expand Down
55 changes: 0 additions & 55 deletions pkg/havener/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
98 changes: 0 additions & 98 deletions pkg/havener/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8e4b720

Please sign in to comment.