From 18bb5d4aa06accdc5d172aad5e4a825895f82ecb Mon Sep 17 00:00:00 2001 From: Smuu <18609909+Smuu@users.noreply.github.com> Date: Thu, 15 Jun 2023 12:28:32 +0200 Subject: [PATCH] feat: add check if knuu was initialized Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com> --- pkg/k8s/k8s.go | 5 +++++ pkg/k8s/k8s_pod.go | 47 ++++++++++++++++++++++++++++-------------- pkg/k8s/k8s_pvc.go | 23 ++++++++++++++------- pkg/k8s/k8s_service.go | 30 +++++++++++++++++++-------- pkg/knuu/knuu.go | 8 +++++++ 5 files changed, 81 insertions(+), 32 deletions(-) diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index d30e005..dfbed8d 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -48,6 +48,11 @@ func Initialize() error { return nil } +// IsInitialized checks if the Kubernetes clientset has been initialized. +func IsInitialized() bool { + return clientset != nil +} + // Namespace returns the current namespace in use. func Namespace() string { return namespace diff --git a/pkg/k8s/k8s_pod.go b/pkg/k8s/k8s_pod.go index 6be83f9..6a006c4 100644 --- a/pkg/k8s/k8s_pod.go +++ b/pkg/k8s/k8s_pod.go @@ -1,22 +1,22 @@ package k8s import ( - "bytes" - "context" - "fmt" - "io" - "k8s.io/client-go/tools/portforward" - "k8s.io/client-go/transport/spdy" - "net/http" - "k8s.io/apimachinery/pkg/api/resource" - "strings" - "time" - - "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/tools/remotecommand" + "bytes" + "context" + "fmt" + "io" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/client-go/tools/portforward" + "k8s.io/client-go/transport/spdy" + "net/http" + "strings" + "time" + + "github.com/sirupsen/logrus" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/remotecommand" ) // getPod retrieves a pod from the given namespace and logs any errors. @@ -25,6 +25,9 @@ func getPod(namespace, name string) (*v1.Pod, error) { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return nil, fmt.Errorf("knuu is not initialized") + } pod, err := Clientset().CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return nil, fmt.Errorf("failed to get pod %s: %w", name, err) @@ -45,6 +48,9 @@ func DeployPod(podConfig PodConfig, init bool) (*v1.Pod, error) { defer cancel() // Try to create the pod + if !IsInitialized() { + return nil, fmt.Errorf("knuu is not initialized") + } createdPod, err := Clientset().CoreV1().Pods(podConfig.Namespace).Create(ctx, pod, metav1.CreateOptions{}) if err != nil { return nil, fmt.Errorf("failed to create pod: %v", err) @@ -126,6 +132,9 @@ func RunCommandInPod(namespace, podName, containerName string, cmd []string) (st } // Construct the request for executing the command in the specified container + if !IsInitialized() { + return "", fmt.Errorf("knuu is not initialized") + } req := Clientset().CoreV1().RESTClient().Post(). Resource("pods"). Name(podName). @@ -185,6 +194,9 @@ func DeletePod(namespace, name string) error { defer cancel() // Delete the pod using the Kubernetes client API + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } if err := Clientset().CoreV1().Pods(namespace).Delete(ctx, name, metav1.DeleteOptions{}); err != nil { return fmt.Errorf("failed to delete pod %s: %v", name, err) } @@ -408,6 +420,9 @@ func PortForwardPod(namespace string, podName string, localPort int, remotePort } // Setup the port forwarding + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } url := Clientset().CoreV1().RESTClient().Post(). Resource("pods"). Namespace(namespace). diff --git a/pkg/k8s/k8s_pvc.go b/pkg/k8s/k8s_pvc.go index 7b6df7d..48aad60 100644 --- a/pkg/k8s/k8s_pvc.go +++ b/pkg/k8s/k8s_pvc.go @@ -1,14 +1,14 @@ package k8s import ( - "context" - "fmt" - "time" + "context" + "fmt" + "time" - "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/sirupsen/logrus" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // createPersistentVolumeClaim deploys a PersistentVolumeClaim if it does not exist. @@ -32,6 +32,9 @@ func createPersistentVolumeClaim(namespace, name string, labels map[string]strin ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } if _, err := Clientset().CoreV1().PersistentVolumeClaims(namespace).Create(ctx, pvc, metav1.CreateOptions{}); err != nil { return err } @@ -52,6 +55,9 @@ func deletePersistentVolumeClaim(namespace, name string) error { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } if err := Clientset().CoreV1().PersistentVolumeClaims(namespace).Delete(ctx, name, metav1.DeleteOptions{}); err != nil { return fmt.Errorf("error deleting PersistentVolumeClaim %s: %w", name, err) } @@ -66,6 +72,9 @@ func getPersistentVolumeClaim(namespace, name string) (*v1.PersistentVolumeClaim ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return nil, fmt.Errorf("knuu is not initialized") + } pv, err := Clientset().CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return nil, err diff --git a/pkg/k8s/k8s_service.go b/pkg/k8s/k8s_service.go index 7e519ba..fd09b67 100644 --- a/pkg/k8s/k8s_service.go +++ b/pkg/k8s/k8s_service.go @@ -1,15 +1,15 @@ package k8s import ( - "context" - "errors" - "fmt" - "github.com/sirupsen/logrus" - "time" - - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/intstr" + "context" + "errors" + "fmt" + "github.com/sirupsen/logrus" + "time" + + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" ) // GetService retrieves a service. @@ -17,6 +17,9 @@ func GetService(namespace, name string) (*v1.Service, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() + if !IsInitialized() { + return nil, fmt.Errorf("knuu is not initialized") + } svc, err := Clientset().CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { return nil, fmt.Errorf("error getting service %s: %w", name, err) @@ -35,6 +38,9 @@ func DeployService(namespace, name string, labels, selectorMap map[string]string ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return nil, fmt.Errorf("knuu is not initialized") + } serv, err := Clientset().CoreV1().Services(namespace).Create(ctx, svc, metav1.CreateOptions{}) if err != nil { return nil, fmt.Errorf("error creating service %s: %w", name, err) @@ -54,6 +60,9 @@ func PatchService(namespace, name string, labels, selectorMap map[string]string, ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } _, err = Clientset().CoreV1().Services(namespace).Update(ctx, svc, metav1.UpdateOptions{}) if err != nil { return fmt.Errorf("error patching service %s: %w", name, err) @@ -73,6 +82,9 @@ func DeleteService(namespace, name string) error { return fmt.Errorf("error getting service %s: %w", name, err) } + if !IsInitialized() { + return fmt.Errorf("knuu is not initialized") + } err = Clientset().CoreV1().Services(namespace).Delete(ctx, name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("error deleting service %s: %w", name, err) diff --git a/pkg/knuu/knuu.go b/pkg/knuu/knuu.go index ee3e974..5bd3957 100644 --- a/pkg/knuu/knuu.go +++ b/pkg/knuu/knuu.go @@ -29,6 +29,9 @@ func Identifier() string { // InitializeWithIdentifier initializes knuu with a unique identifier func InitializeWithIdentifier(uniqueIdentifier string) error { + if uniqueIdentifier == "" { + return fmt.Errorf("cannot initialize knuu with empty identifier") + } identifier = uniqueIdentifier t := time.Now() @@ -54,3 +57,8 @@ func InitializeWithIdentifier(uniqueIdentifier string) error { return nil } + +// IsInitialized returns true if knuu is initialized, and false otherwise +func IsInitialized() bool { + return k8s.IsInitialized() +}