Skip to content

Commit

Permalink
Enhanced Kubernetes dynamic execution and client operations
Browse files Browse the repository at this point in the history
**Added:**
- Added ExecutorCreator interface to abstract SPDYExecutor creation.
- Introduced DefaultExecutorCreator with NewSPDYExecutor method.
- Implemented detailed documentation for new ExecutorCreator in README.md.
- Added comprehensive tests for new execution structure in execution_test.go.
- Added `KubernetesClientInterface` to define the interface for `KubernetesClient`.
- Introduced `RealKubernetesClient` as an implementation of `KubernetesClientInterface`.
- Added extensive inline documentation to the methods of `RealKubernetesClient`.
- Updated unit tests in `client_test.go` to use mock implementations of the new interface.

**Changed:**
- Updated ExecKubernetesResources to utilize ExecutorCreator.
- Modified README.md to reflect new parameters and structure in execution method.
- Enhanced execution.go with new interface and improved command execution logic.
- Refactored `NewKubernetesClient` function to accept `KubernetesClientInterface`.
- Updated function calls in existing code to use the new interface methods.
- Enhanced error checking and added validation for `ExecKubernetesResources` function.
- Fixed potential nil pointer dereferences in `deploymentLogger.go` and
  `serviceLogger.go` by adding checks before accessing objects.
- Update go mods
- Replaced return of *kubernetes.Clientset with interface

**Removed:**
- Removed outdated blackfriday v1.6.0 entries from go.sum to clean up dependencies.
- Failing test that doesn't crash
  • Loading branch information
l50 authored May 2, 2024
1 parent 463dc79 commit 5a2b1ab
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 149 deletions.
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ toolchain go1.22.2
require (
github.com/bitfield/script v0.22.0
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/chromedp/cdproto v0.0.0-20240426225625-909263490071
github.com/chromedp/cdproto v0.0.0-20240501202034-ef67d660e9fd
github.com/chromedp/chromedp v0.9.5
github.com/fatih/color v1.16.0
github.com/glendc/go-external-ip v0.1.0
github.com/go-git/go-git/v5 v5.12.0
github.com/golang/mock v1.6.0
github.com/magefile/mage v1.15.0
github.com/mattn/go-isatty v0.0.20
github.com/otiai10/copy v1.14.0
Expand All @@ -22,6 +23,7 @@ require (
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
k8s.io/kubectl v0.30.0
)

require (
Expand All @@ -33,7 +35,7 @@ require (
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/Microsoft/hcsshim v0.12.3 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/chai2010/gettext-go v1.0.3 // indirect
github.com/containerd/containerd v1.7.16 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/distribution/reference v0.6.0 // indirect
Expand All @@ -44,7 +46,6 @@ require (
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/pprof v0.0.0-20231023181126-ff6d637d2a7b // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
Expand Down Expand Up @@ -79,7 +80,6 @@ require (
k8s.io/apiserver v0.30.0 // indirect
k8s.io/cli-runtime v0.30.0 // indirect
k8s.io/component-base v0.30.0 // indirect
k8s.io/kubectl v0.30.0 // indirect
oras.land/oras-go v1.2.5 // indirect
sigs.k8s.io/kustomize/api v0.17.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.0 // indirect
Expand All @@ -98,9 +98,9 @@ require (
github.com/cloudflare/circl v1.3.8 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/cli v26.1.0+incompatible // indirect
github.com/docker/cli v26.1.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v26.1.0+incompatible // indirect
github.com/docker/docker v26.1.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
Expand Down Expand Up @@ -176,17 +176,17 @@ require (
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
helm.sh/helm/v3 v3.14.4
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240423202451-8948a665c108 // indirect
k8s.io/utils v0.0.0-20240423183400-0849a56e8f22 // indirect
k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
mvdan.cc/sh/v3 v3.8.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
58 changes: 33 additions & 25 deletions go.sum

Large diffs are not rendered by default.

66 changes: 64 additions & 2 deletions k8s/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ designed to simplify common k8s tasks.

## Functions

### NewKubernetesClient(string, fileReaderFunc)
### NewKubernetesClient(string, FileReaderFunc, KubernetesClientInterface)

```go
NewKubernetesClient(string, fileReaderFunc) *KubernetesClient, error
NewKubernetesClient(string FileReaderFunc KubernetesClientInterface) *KubernetesClient error
```

NewKubernetesClient creates a new KubernetesClient using the provided
Expand All @@ -41,6 +41,68 @@ error: An error if any issue occurs while creating the Kubernetes client.

---

### RealKubernetesClient.NewDynamicForConfig(*rest.Config)

```go
NewDynamicForConfig(*rest.Config) dynamic.Interface, error
```

NewDynamicForConfig creates a new dynamic client using the provided REST
configuration.

**Parameters:**

config: The REST configuration to use to create the dynamic client.

**Returns:**

dynamic.Interface: A new dynamic client instance created using the provided
REST configuration.
error: An error if any issue occurs while creating the dynamic client.

---

### RealKubernetesClient.NewForConfig(*rest.Config)

```go
NewForConfig(*rest.Config) kubernetes.Interface, error
```

NewForConfig creates a new clientset using the provided REST configuration.

**Parameters:**

config: The REST configuration to use to create the clientset.

**Returns:**

*kubernetes.Clientset: A new clientset instance created using the provided
REST configuration.
error: An error if any issue occurs while creating the clientset.

---

### RealKubernetesClient.RESTConfigFromKubeConfig([]byte)

```go
RESTConfigFromKubeConfig([]byte) *rest.Config, error
```

RESTConfigFromKubeConfig creates a REST configuration from the provided
kubeconfig data.

**Parameters:**

configData: The kubeconfig data to use to create the REST configuration.

**Returns:**

*rest.Config: A new REST configuration instance created using the provided
kubeconfig data.
error: An error if any issue occurs while creating the REST configuration.

---

## Installation

To use the goutils/v2/k8s package, you first need to install it.
Expand Down
78 changes: 73 additions & 5 deletions k8s/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

type fileReaderFunc func(string) ([]byte, error)
// FileReaderFunc defines a function signature for reading a file from a given
// path.
type FileReaderFunc func(string) ([]byte, error)

// KubernetesClient wraps a clientset to interact with Kubernetes APIs.
//
Expand All @@ -27,6 +29,72 @@ type KubernetesClient struct {
Config *rest.Config
}

// KubernetesClientInterface defines the interface for the KubernetesClient.
//
// **Methods:**
//
// NewForConfig: Creates a new clientset using the provided REST configuration.
// NewDynamicForConfig: Creates a new dynamic client using the provided REST
// configuration.
// RESTConfigFromKubeConfig: Creates a REST configuration from the provided
// kubeconfig data.
type KubernetesClientInterface interface {
NewForConfig(config *rest.Config) (kubernetes.Interface, error)
NewDynamicForConfig(config *rest.Config) (dynamic.Interface, error)
RESTConfigFromKubeConfig(configData []byte) (*rest.Config, error)
}

// RealKubernetesClient implements the KubernetesClientInterface using the
// client-go library.
type RealKubernetesClient struct{}

// NewForConfig creates a new clientset using the provided REST configuration.
//
// **Parameters:**
//
// config: The REST configuration to use to create the clientset.
//
// **Returns:**
//
// *kubernetes.Clientset: A new clientset instance created using the provided
// REST configuration.
// error: An error if any issue occurs while creating the clientset.
func (r *RealKubernetesClient) NewForConfig(config *rest.Config) (kubernetes.Interface, error) {
return kubernetes.NewForConfig(config)
}

// NewDynamicForConfig creates a new dynamic client using the provided REST
// configuration.
//
// **Parameters:**
//
// config: The REST configuration to use to create the dynamic client.
//
// **Returns:**
//
// dynamic.Interface: A new dynamic client instance created using the provided
// REST configuration.
// error: An error if any issue occurs while creating the dynamic client.
func (r *RealKubernetesClient) NewDynamicForConfig(config *rest.Config) (dynamic.Interface, error) {
return dynamic.NewForConfig(config)
}

// RESTConfigFromKubeConfig creates a REST configuration from the provided
// kubeconfig data.
//
// **Parameters:**
//
// configData: The kubeconfig data to use to create the REST configuration.
//
// **Returns:**
//
// *rest.Config: A new REST configuration instance created using the provided
// kubeconfig data.
// error: An error if any issue occurs while creating the REST configuration.
func (r *RealKubernetesClient) RESTConfigFromKubeConfig(configData []byte) (*rest.Config, error) {
return clientcmd.RESTConfigFromKubeConfig(configData)
}

// NewKubernetesClient creates a new KubernetesClient using the provided
// kubeconfig path and file reader function.
//
Expand All @@ -41,23 +109,23 @@ type KubernetesClient struct {
// *KubernetesClient: A new KubernetesClient instance configured with the
// specified kubeconfig.
// error: An error if any issue occurs while creating the Kubernetes client.
func NewKubernetesClient(kubeconfig string, reader fileReaderFunc) (*KubernetesClient, error) {
func NewKubernetesClient(kubeconfig string, reader FileReaderFunc, client KubernetesClientInterface) (*KubernetesClient, error) {
configData, err := reader(kubeconfig)
if err != nil {
return nil, fmt.Errorf("error reading kubeconfig: %v", err)
}

config, err := clientcmd.RESTConfigFromKubeConfig(configData)
config, err := client.RESTConfigFromKubeConfig(configData)
if err != nil {
return nil, fmt.Errorf("error building kubeconfig: %v", err)
}

clientset, err := kubernetes.NewForConfig(config)
clientset, err := client.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("error creating Kubernetes client: %v", err)
}

dynamicClient, err := dynamic.NewForConfig(config)
dynamicClient, err := client.NewDynamicForConfig(config)
if err != nil {
return nil, fmt.Errorf("error creating dynamic Kubernetes client: %v", err)
}
Expand Down
61 changes: 59 additions & 2 deletions k8s/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,62 @@ import (
"fmt"
"testing"

k8s "github.com/l50/goutils/v2/k8s/client"
client "github.com/l50/goutils/v2/k8s/client"
"github.com/stretchr/testify/mock"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

type MockKubernetesClient struct {
mock.Mock
}

func NewKubernetesClient(kubeconfig string, reader client.FileReaderFunc, kClient client.KubernetesClientInterface) (*client.KubernetesClient, error) {
configData, err := reader(kubeconfig)
if err != nil {
return nil, fmt.Errorf("error reading kubeconfig: %v", err)
}

config, err := kClient.RESTConfigFromKubeConfig(configData)
if err != nil {
return nil, fmt.Errorf("error building kubeconfig: %v", err)
}

kInterface, err := kClient.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("error creating Kubernetes client: %v", err)
}

// Safely assert the type to *kubernetes.Clientset
kClientset, ok := kInterface.(*kubernetes.Clientset)
if !ok {
return nil, fmt.Errorf("failed to assert Kubernetes interface to Clientset")
}

dynamicClient, err := kClient.NewDynamicForConfig(config)
if err != nil {
return nil, fmt.Errorf("error creating dynamic Kubernetes client: %v", err)
}

return &client.KubernetesClient{Clientset: kClientset, DynamicClient: dynamicClient, Config: config}, nil
}

func (m *MockKubernetesClient) NewForConfig(config *rest.Config) (kubernetes.Interface, error) {
args := m.Called(config)
return args.Get(0).(kubernetes.Interface), args.Error(1)
}

func (m *MockKubernetesClient) NewDynamicForConfig(config *rest.Config) (dynamic.Interface, error) {
args := m.Called(config)
return args.Get(0).(dynamic.Interface), args.Error(1)
}

func (m *MockKubernetesClient) RESTConfigFromKubeConfig(configData []byte) (*rest.Config, error) {
args := m.Called(configData)
return args.Get(0).(*rest.Config), args.Error(1)
}

func TestNewKubernetesClient(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -53,7 +106,11 @@ users:
return nil, fmt.Errorf("error reading kubeconfig")
}

client, err := k8s.NewKubernetesClient(tc.kubeconfig, reader)
mockClient := new(MockKubernetesClient)
mockClient.On("RESTConfigFromKubeConfig", tc.data).Return(&rest.Config{}, nil)
mockClient.On("NewForConfig", mock.Anything).Return(&kubernetes.Clientset{}, nil)
mockClient.On("NewDynamicForConfig", mock.Anything).Return(dynamic.NewForConfigOrDie(&rest.Config{}), nil)
client, err := client.NewKubernetesClient(tc.kubeconfig, reader, mockClient)
if (err != nil) != tc.expectError {
t.Errorf("Test '%s' failed - expected error: %v, got: %v", tc.name, tc.expectError, err)
}
Expand Down
Loading

0 comments on commit 5a2b1ab

Please sign in to comment.