Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check if knuu was initialized #67

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 31 additions & 16 deletions pkg/k8s/k8s_pod.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 @@ -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).
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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).
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 @@ -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()
Expand All @@ -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()
}