-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update-civogo-v0.3.92
- Loading branch information
Showing
9 changed files
with
1,219 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package hook | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// Hook defines the lifecycle methods for a hook, such as PreStop, PostStart, etc. | ||
// Implementations of this interface can define actions to be performed at different lifecycle stages. | ||
type Hook interface { | ||
PreStop(ctx context.Context) error | ||
} | ||
|
||
type hook struct { | ||
client kubernetes.Interface | ||
nodeName string | ||
clientCfgPath string | ||
} | ||
|
||
// NewHook creates a new Hook with the provided options. It returns an error if setup fails. | ||
func NewHook(opts ...Option) (Hook, error) { | ||
h := &hook{} | ||
for _, opt := range append(defaultOpts, opts...) { | ||
opt(h) | ||
} | ||
if h.nodeName == "" { | ||
return nil, errors.New("node name not found") | ||
} | ||
if err := h.setupKubernetesClient(); err != nil { | ||
return nil, fmt.Errorf("failed to setup kubernetes API client: %w", err) | ||
} | ||
return h, nil | ||
} | ||
|
||
// setupKubernetesClient creates Kubernetes client based on the kubeconfig path. | ||
// If kubeconfig path is not empty, the client will be created using that path. | ||
// Otherwise, if the kubeconfig path is empty, the client will be created using the in-clustetr config. | ||
func (h *hook) setupKubernetesClient() (err error) { | ||
if h.clientCfgPath != "" && h.client == nil { | ||
cfg, err := clientcmd.BuildConfigFromFlags("", h.clientCfgPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to build kubeconfig from path %q: %w", h.clientCfgPath, err) | ||
} | ||
h.client, err = kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return fmt.Errorf("failed to create kubernetes API client: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
if h.client == nil { | ||
cfg, err := rest.InClusterConfig() | ||
if err != nil { | ||
return fmt.Errorf("failed to load in-cluster kubeconfig: %w", err) | ||
} | ||
h.client, err = kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return fmt.Errorf("failed to create kubernetes API client: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package hook | ||
|
||
import ( | ||
"os" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// Option represents a configuration function that modifies hook object. | ||
type Option func(*hook) | ||
|
||
var defaultOpts = []Option{ | ||
WithNodeName(os.Getenv("KUBE_NODE_NAME")), | ||
} | ||
|
||
// WithKubernetesClient returns Option to set Kubernetes API client. | ||
func WithKubernetesClient(client kubernetes.Interface) Option { | ||
return func(h *hook) { | ||
if client != nil { | ||
h.client = client | ||
} | ||
} | ||
} | ||
|
||
// WithKubernetesClient returns Option to set Kubernetes config path. | ||
func WithKubernetesClientConfigPath(path string) Option { | ||
return func(h *hook) { | ||
if path != "" { | ||
h.clientCfgPath = path | ||
} | ||
} | ||
} | ||
|
||
// WithNodeName returns Option to set node name. | ||
func WithNodeName(name string) Option { | ||
return func(h *hook) { | ||
if name != "" { | ||
h.nodeName = name | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package hook | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func TestWithKubernetesClient(t *testing.T) { | ||
type test struct { | ||
name string | ||
client kubernetes.Interface | ||
beforeFunc func(*hook) | ||
wantClient kubernetes.Interface | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "Succeeds to apply option", | ||
client: &kubernetes.Clientset{}, | ||
wantClient: &kubernetes.Clientset{}, | ||
}, | ||
{ | ||
name: "Does nothing when client is nil", | ||
beforeFunc: func(h *hook) { | ||
h.client = &kubernetes.Clientset{} | ||
}, | ||
wantClient: &kubernetes.Clientset{}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
h := &hook{} | ||
|
||
if test.beforeFunc != nil { | ||
test.beforeFunc(h) | ||
} | ||
|
||
WithKubernetesClient(test.client)(h) | ||
|
||
assert.Equal(tt, test.wantClient, h.client) | ||
}) | ||
} | ||
} | ||
|
||
func TestWithKubernetesClientConfigPath(t *testing.T) { | ||
type test struct { | ||
name string | ||
path string | ||
beforeFunc func(*hook) | ||
wantPath string | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "Succeeds to apply option", | ||
path: "kubeconfig.yaml", | ||
wantPath: "kubeconfig.yaml", | ||
}, | ||
{ | ||
name: "Do nothing when path is empty", | ||
beforeFunc: func(h *hook) { | ||
h.clientCfgPath = "kubeconfig.yaml" | ||
}, | ||
wantPath: "kubeconfig.yaml", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
h := &hook{} | ||
|
||
if test.beforeFunc != nil { | ||
test.beforeFunc(h) | ||
} | ||
|
||
WithKubernetesClientConfigPath(test.path)(h) | ||
|
||
assert.Equal(tt, test.wantPath, h.clientCfgPath) | ||
}) | ||
} | ||
} | ||
|
||
func TestWithNodeName(t *testing.T) { | ||
type test struct { | ||
name string | ||
nodeName string | ||
beforeFunc func(*hook) | ||
wantNodeName string | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
name: "Succeeds to apply option", | ||
nodeName: "node-01", | ||
wantNodeName: "node-01", | ||
}, | ||
{ | ||
name: "Do nothing when Node name is empty", | ||
beforeFunc: func(h *hook) { | ||
h.nodeName = "node-01" | ||
}, | ||
wantNodeName: "node-01", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(tt *testing.T) { | ||
h := &hook{} | ||
|
||
if test.beforeFunc != nil { | ||
test.beforeFunc(h) | ||
} | ||
|
||
WithNodeName(test.nodeName)(h) | ||
|
||
assert.Equal(tt, test.wantNodeName, h.nodeName) | ||
}) | ||
} | ||
} |
Oops, something went wrong.