Skip to content

Commit

Permalink
feat: add check if knuu was initialized (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
  • Loading branch information
smuu authored Jun 16, 2023
1 parent 51c72d1 commit f2f5889
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
5 changes: 5 additions & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pkg/k8s/k8s_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -118,6 +124,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).
Expand Down Expand Up @@ -178,6 +187,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)
}
Expand Down Expand Up @@ -401,6 +413,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).
Expand Down
23 changes: 16 additions & 7 deletions pkg/k8s/k8s_pvc.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
30 changes: 21 additions & 9 deletions pkg/k8s/k8s_service.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
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.
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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()
Expand All @@ -53,3 +56,8 @@ func InitializeWithIdentifier(uniqueIdentifier string) error {

return nil
}

// IsInitialized returns true if knuu is initialized, and false otherwise
func IsInitialized() bool {
return k8s.IsInitialized()
}

0 comments on commit f2f5889

Please sign in to comment.