feat(zfs-localpv): initial commit

provisioning and deprovisioning of
the volumes on the node where zfs pool
has already been setup. Pool name and the volume
parameters has to be given in storage class
which will be used to provision the volume.

Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
Pawan 2019-09-12 12:32:17 +05:30 committed by Kiran Mova
parent 485e2a21f0
commit 9f5cf445df
46 changed files with 6339 additions and 0 deletions

106
pkg/common/env/env.go vendored Normal file
View file

@ -0,0 +1,106 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package env
import (
"os"
"strconv"
"strings"
)
// EnvironmentSetter abstracts setting of environment variable
type EnvironmentSetter func(envKey string, value string) (err error)
// EnvironmentGetter abstracts fetching value from an environment variable
type EnvironmentGetter func(envKey string) (value string)
// EnvironmentLookup abstracts looking up an environment variable
type EnvironmentLookup func(envKey string) (value string, present bool)
// Set sets the provided environment variable
//
// NOTE:
// This is an implementation of EnvironmentSetter
func Set(envKey string, value string) (err error) {
return os.Setenv(string(envKey), value)
}
// Get fetches value from the provided environment variable
//
// NOTE:
// This is an implementation of EnvironmentGetter
func Get(envKey string) (value string) {
return getEnv(string(envKey))
}
// GetOrDefault fetches value from the provided environment variable
// which on empty returns the defaultValue
// NOTE: os.Getenv is used here instead of os.LookupEnv because it is
// not required to know if the environment variable is defined on the system
func GetOrDefault(e string, defaultValue string) (value string) {
envValue := Get(e)
if len(envValue) == 0 {
// ENV not defined or set to ""
return defaultValue
} else {
return envValue
}
}
// Lookup looks up an environment variable
//
// NOTE:
// This is an implementation of EnvironmentLookup
func Lookup(envKey string) (value string, present bool) {
return lookupEnv(string(envKey))
}
// Truthy returns boolean based on the environment variable's value
//
// The lookup value can be truthy (i.e. 1, t, TRUE, true) or falsy (0, false,
// etc) based on strconv.ParseBool logic
func Truthy(envKey string) (truth bool) {
v, found := Lookup(envKey)
if !found {
return
}
truth, _ = strconv.ParseBool(v)
return
}
// LookupOrFalse looks up an environment variable and returns a string "false"
// if environment variable is not present. It returns appropriate values for
// other cases.
func LookupOrFalse(envKey string) string {
val, present := Lookup(envKey)
if !present {
return "false"
}
return strings.TrimSpace(val)
}
// getEnv fetches the provided environment variable's value
func getEnv(envKey string) (value string) {
return strings.TrimSpace(os.Getenv(envKey))
}
// lookupEnv looks up the provided environment variable
func lookupEnv(envKey string) (value string, present bool) {
value, present = os.LookupEnv(envKey)
value = strings.TrimSpace(value)
return
}

View file

@ -0,0 +1,99 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package errors
import (
"fmt"
)
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &err{
prefix: stackTraceMessagePrefix,
msg: message,
stack: callers(),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &err{
prefix: stackTraceMessagePrefix,
msg: fmt.Sprintf(format, args...),
stack: callers(),
}
}
// Wrap annotates err with a new message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
return &wrapper{wrapErrorMessagePrefix, message, err}
}
// Wrapf annotates err with the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &wrapper{wrapErrorMessagePrefix, fmt.Sprintf(format, args...), err}
}
// WithStack annotates err with a stack trace at the
// point WithStack was called. If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
stackTraceMessagePrefix,
err,
callers(),
}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}

188
pkg/common/errors/types.go Normal file
View file

@ -0,0 +1,188 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package errors
import (
"fmt"
"runtime"
"github.com/pkg/errors"
)
const (
wrapErrorMessagePrefix string = " -- "
listErrorMessagePrefix string = " - "
stackTraceMessagePrefix string = " "
)
// stack represents a stack of program counters.
type stack []uintptr
// callers returns stack of caller function
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}
// err implements error interface that has a message and stack
type err struct {
prefix string
msg string
*stack
}
// Error is implementation of error interface
func (e *err) Error() string { return e.msg }
// Format is implementation of Formater interface
func (e *err) Format(s fmt.State, verb rune) {
message := wrapErrorMessagePrefix + e.msg
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprint(s, message)
for i, pc := range *e.stack {
if i > 0 {
return
}
f := errors.Frame(pc)
fmt.Fprintf(s, "\n%s%+v", e.prefix, f)
}
return
}
fallthrough
case 's', 'q':
fmt.Fprint(s, message)
}
}
// wrapper implements error interface that has a message and error
type wrapper struct {
prefix string
msg string
error
}
// Error is implementation of error interface
func (w *wrapper) Error() string { return w.msg }
// Cause is implementation of causer interface
func (w *wrapper) Cause() error { return w.error }
// Format is implementation of Formater interface
func (w *wrapper) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", w.error)
fmt.Fprint(s, w.prefix+w.msg)
return
}
fallthrough
case 's', 'q':
fmt.Fprintf(s, "%s\n", w.error)
fmt.Fprint(s, w.prefix+w.msg)
}
}
// withStack implements error interface that has a stack and error
type withStack struct {
prefix string
error
*stack
}
// Format is implementation of Formater interface
func (ws *withStack) Format(s fmt.State, verb rune) {
message := wrapErrorMessagePrefix + fmt.Sprintf("%s", ws.error)
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprint(s, message)
for i, pc := range *ws.stack {
if i > 0 {
return
}
f := errors.Frame(pc)
fmt.Fprintf(s, "\n%s%+v", ws.prefix, f)
}
return
}
fallthrough
case 's', 'q':
fmt.Fprint(s, message)
}
}
// Cause is implementation of causer interface
func (ws *withStack) Cause() error { return ws.error }
// ErrorList is a wrapper over list of errors
// It implements error interface
type ErrorList struct {
Errors []error
msg string
}
// Error is implementation of error interface
func (el *ErrorList) Error() string {
message := ""
for _, err := range el.Errors {
message += err.Error() + ":"
}
el.msg = message
return message
}
// Format is implementation of Formater interface
func (el *ErrorList) Format(s fmt.State, verb rune) {
message := ""
for _, err := range el.Errors {
message += "\n" + listErrorMessagePrefix + err.Error()
}
fmt.Fprint(s, message)
}
// WithStack annotates ErrorList with a new message and
// stack trace of caller.
func (el *ErrorList) WithStack(message string) error {
if el == nil {
return nil
}
return &withStack{
stackTraceMessagePrefix,
Wrap(el, message),
callers(),
}
}
// WithStackf annotates ErrorList with the format specifier
// and stack trace of caller.
func (el *ErrorList) WithStackf(format string, args ...interface{}) error {
if el == nil {
return nil
}
return &withStack{
stackTraceMessagePrefix,
Wrapf(el, format, args...),
callers(),
}
}

View file

@ -0,0 +1,243 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"strings"
env "github.com/openebs/zfs-localpv/pkg/common/env"
"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
// K8sMasterIPEnvironmentKey is the environment variable key used to
// determine the kubernetes master IP address
K8sMasterIPEnvironmentKey string = "OPENEBS_IO_K8S_MASTER"
// KubeConfigEnvironmentKey is the environment variable key used to
// determine the kubernetes config
KubeConfigEnvironmentKey string = "OPENEBS_IO_KUBE_CONFIG"
)
// getInClusterConfigFunc abstracts the logic to get
// kubernetes incluster config
//
// NOTE:
// typed function makes it simple to mock
type getInClusterConfigFunc func() (*rest.Config, error)
// buildConfigFromFlagsFunc provides the abstraction to get
// kubernetes config from provided flags
//
// NOTE:
// typed function makes it simple to mock
type buildConfigFromFlagsFunc func(string, string) (*rest.Config, error)
// GetConfigFunc provides the abstraction to get
// kubernetes config from provided client instance
//
// NOTE:
// typed function makes it simple to mock
type GetConfigFunc func(*Client) (*rest.Config, error)
// GetConfig returns kubernetes config instance
//
// NOTE:
// This is an implementation of GetConfigFunc
func GetConfig(c *Client) (*rest.Config, error) {
if c == nil {
return nil, errors.New("failed to get kubernetes config: nil client was provided")
}
return c.GetConfigForPathOrDirect()
}
// getKubeMasterIPFunc provides the abstraction to get
// kubernetes master IP address
//
// NOTE:
// typed function makes it simple to mock
type getKubeMasterIPFunc func(string) string
// getKubeConfigPathFunc provides the abstraction to get
// kubernetes config path
//
// NOTE:
// typed function makes it simple to mock
type getKubeConfigPathFunc func(string) string
// getKubernetesDynamicClientFunc provides the abstraction to get
// dynamic kubernetes clientset
//
// NOTE:
// typed function makes it simple to mock
type getKubernetesDynamicClientFunc func(*rest.Config) (dynamic.Interface, error)
// getKubernetesClientsetFunc provides the abstraction to get
// kubernetes clientset
//
// NOTE:
// typed function makes it simple to mock
type getKubernetesClientsetFunc func(*rest.Config) (*kubernetes.Clientset, error)
// Client provides common kuberenetes client operations
type Client struct {
IsInCluster bool // flag to let client point to its own cluster
KubeConfigPath string // kubeconfig path to get kubernetes clientset
// Below functions are useful during mock
// handle to get in cluster config
getInClusterConfig getInClusterConfigFunc
// handle to get desired kubernetes config
buildConfigFromFlags buildConfigFromFlagsFunc
// handle to get kubernetes clienset
getKubernetesClientset getKubernetesClientsetFunc
// handle to get dynamic kubernetes clientset
getKubernetesDynamicClient getKubernetesDynamicClientFunc
// handle to get kubernetes master IP
getKubeMasterIP getKubeMasterIPFunc
// handle to get kubernetes config path
getKubeConfigPath getKubeConfigPathFunc
}
// OptionFunc is a typed function that abstracts any kind of operation
// against the provided client instance
//
// This is the basic building block to create functional operations
// against the client instance
type OptionFunc func(*Client)
// New returns a new instance of client
func New(opts ...OptionFunc) *Client {
c := &Client{}
for _, o := range opts {
o(c)
}
withDefaults(c)
return c
}
func withDefaults(c *Client) {
if c.getInClusterConfig == nil {
c.getInClusterConfig = rest.InClusterConfig
}
if c.buildConfigFromFlags == nil {
c.buildConfigFromFlags = clientcmd.BuildConfigFromFlags
}
if c.getKubernetesClientset == nil {
c.getKubernetesClientset = kubernetes.NewForConfig
}
if c.getKubernetesDynamicClient == nil {
c.getKubernetesDynamicClient = dynamic.NewForConfig
}
if c.getKubeMasterIP == nil {
c.getKubeMasterIP = env.Get
}
if c.getKubeConfigPath == nil {
c.getKubeConfigPath = env.Get
}
}
// InCluster enables IsInCluster flag
func InCluster() OptionFunc {
return func(c *Client) {
c.IsInCluster = true
}
}
// WithKubeConfigPath sets kubeconfig path
// against this client instance
func WithKubeConfigPath(kubeConfigPath string) OptionFunc {
return func(c *Client) {
c.KubeConfigPath = kubeConfigPath
}
}
// Clientset returns a new instance of kubernetes clientset
func (c *Client) Clientset() (*kubernetes.Clientset, error) {
config, err := c.GetConfigForPathOrDirect()
if err != nil {
return nil, errors.Wrapf(err,
"failed to get kubernetes clientset: failed to get kubernetes config: IsInCluster {%t}: KubeConfigPath {%s}",
c.IsInCluster,
c.KubeConfigPath,
)
}
return c.getKubernetesClientset(config)
}
// Config returns the kubernetes config instance based on available criteria
func (c *Client) Config() (config *rest.Config, err error) {
// IsInCluster flag holds the top most priority
if c.IsInCluster {
return c.getInClusterConfig()
}
// ENV holds second priority
if strings.TrimSpace(c.getKubeMasterIP(K8sMasterIPEnvironmentKey)) != "" ||
strings.TrimSpace(c.getKubeConfigPath(KubeConfigEnvironmentKey)) != "" {
return c.getConfigFromENV()
}
// Defaults to InClusterConfig
return c.getInClusterConfig()
}
// ConfigForPath returns the kuberentes config instance based on KubeConfig path
func (c *Client) ConfigForPath(kubeConfigPath string) (config *rest.Config, err error) {
return c.buildConfigFromFlags("", kubeConfigPath)
}
func (c *Client) GetConfigForPathOrDirect() (config *rest.Config, err error) {
if c.KubeConfigPath != "" {
return c.ConfigForPath(c.KubeConfigPath)
}
return c.Config()
}
func (c *Client) getConfigFromENV() (config *rest.Config, err error) {
k8sMaster := c.getKubeMasterIP(K8sMasterIPEnvironmentKey)
kubeConfig := c.getKubeConfigPath(KubeConfigEnvironmentKey)
if strings.TrimSpace(k8sMaster) == "" &&
strings.TrimSpace(kubeConfig) == "" {
return nil, errors.Errorf(
"failed to get kubernetes config: missing ENV: atleast one should be set: {%s} or {%s}",
K8sMasterIPEnvironmentKey,
KubeConfigEnvironmentKey,
)
}
return c.buildConfigFromFlags(k8sMaster, kubeConfig)
}
// Dynamic returns a kubernetes dynamic client capable of invoking operations
// against kubernetes resources
func (c *Client) Dynamic() (dynamic.Interface, error) {
config, err := c.GetConfigForPathOrDirect()
if err != nil {
return nil, errors.Wrap(err, "failed to get dynamic client")
}
return c.getKubernetesDynamicClient(config)
}

View file

@ -0,0 +1,310 @@
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"testing"
"github.com/pkg/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func fakeGetClientsetOk(c *rest.Config) (*kubernetes.Clientset, error) {
return &kubernetes.Clientset{}, nil
}
func fakeGetClientsetErr(c *rest.Config) (*kubernetes.Clientset, error) {
return nil, errors.New("fake error")
}
func fakeInClusterConfigOk() (*rest.Config, error) {
return &rest.Config{}, nil
}
func fakeInClusterConfigErr() (*rest.Config, error) {
return nil, errors.New("fake error")
}
func fakeBuildConfigFromFlagsOk(kubemaster string, kubeconfig string) (*rest.Config, error) {
return &rest.Config{}, nil
}
func fakeBuildConfigFromFlagsErr(kubemaster string, kubeconfig string) (*rest.Config, error) {
return nil, errors.New("fake error")
}
func fakeGetKubeConfigPathOk(e string) string {
return "fake"
}
func fakeGetKubeConfigPathNil(e string) string {
return ""
}
func fakeGetKubeMasterIPOk(e string) string {
return "fake"
}
func fakeGetKubeMasterIPNil(e string) string {
return ""
}
func fakeGetDynamicClientSetOk(c *rest.Config) (dynamic.Interface, error) {
return dynamic.NewForConfig(c)
}
func fakeGetDynamicClientSetNil(c *rest.Config) (dynamic.Interface, error) {
return nil, nil
}
func fakeGetDynamicClientSetErr(c *rest.Config) (dynamic.Interface, error) {
return nil, errors.New("fake error")
}
func TestNewInCluster(t *testing.T) {
c := New(InCluster())
if !c.IsInCluster {
t.Fatalf("test failed: expected IsInCluster as 'true' actual '%t'", c.IsInCluster)
}
}
func TestConfig(t *testing.T) {
tests := map[string]struct {
isInCluster bool
kubeConfigPath string
getInClusterConfig getInClusterConfigFunc
getKubeMasterIP getKubeMasterIPFunc
getKubeConfigPath getKubeConfigPathFunc
getConfigFromENV buildConfigFromFlagsFunc
isErr bool
}{
"t1": {true, "", fakeInClusterConfigOk, nil, nil, nil, false},
"t2": {true, "", fakeInClusterConfigErr, nil, nil, nil, true},
"t3": {false, "", fakeInClusterConfigErr, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, true},
"t4": {false, "", fakeInClusterConfigOk, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, false},
"t5": {false, "fakeKubeConfigPath", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathNil, fakeBuildConfigFromFlagsOk, false},
"t6": {false, "", nil, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, false},
"t7": {false, "", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, false},
"t8": {false, "fakeKubeConfigPath", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsErr, true},
"t9": {false, "fakeKubeConfigpath", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, false},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
IsInCluster: mock.isInCluster,
KubeConfigPath: mock.kubeConfigPath,
getInClusterConfig: mock.getInClusterConfig,
getKubeMasterIP: mock.getKubeMasterIP,
getKubeConfigPath: mock.getKubeConfigPath,
buildConfigFromFlags: mock.getConfigFromENV,
}
_, err := c.Config()
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected no error actual '%s'", name, err)
}
})
}
}
func TestGetConfigFromENV(t *testing.T) {
tests := map[string]struct {
getKubeMasterIP getKubeMasterIPFunc
getKubeConfigPath getKubeConfigPathFunc
getConfigFromENV buildConfigFromFlagsFunc
isErr bool
}{
"t1": {fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, true},
"t2": {fakeGetKubeMasterIPNil, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, false},
"t3": {fakeGetKubeMasterIPOk, fakeGetKubeConfigPathNil, fakeBuildConfigFromFlagsOk, false},
"t4": {fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, false},
"t5": {fakeGetKubeMasterIPNil, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsErr, true},
"t6": {fakeGetKubeMasterIPOk, fakeGetKubeConfigPathNil, fakeBuildConfigFromFlagsErr, true},
"t7": {fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsErr, true},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
getKubeMasterIP: mock.getKubeMasterIP,
getKubeConfigPath: mock.getKubeConfigPath,
buildConfigFromFlags: mock.getConfigFromENV,
}
_, err := c.getConfigFromENV()
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected error actual no error", name)
}
if !mock.isErr && err != nil {
t.Fatalf("test '%s' failed: expected no error actual '%s'", name, err)
}
})
}
}
func TestGetConfigFromPathOrDirect(t *testing.T) {
tests := map[string]struct {
kubeConfigPath string
getConfigFromFlags buildConfigFromFlagsFunc
getInClusterConfig getInClusterConfigFunc
isErr bool
}{
"T1": {"", fakeBuildConfigFromFlagsErr, fakeInClusterConfigOk, false},
"T2": {"fake-path", fakeBuildConfigFromFlagsOk, fakeInClusterConfigErr, false},
"T3": {"fake-path", fakeBuildConfigFromFlagsErr, fakeInClusterConfigOk, true},
"T4": {"", fakeBuildConfigFromFlagsOk, fakeInClusterConfigErr, true},
"T5": {"fake-path", fakeBuildConfigFromFlagsErr, fakeInClusterConfigErr, true},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
KubeConfigPath: mock.kubeConfigPath,
buildConfigFromFlags: mock.getConfigFromFlags,
getInClusterConfig: mock.getInClusterConfig,
getKubeMasterIP: fakeGetKubeMasterIPNil,
getKubeConfigPath: fakeGetKubeConfigPathNil,
}
_, err := c.GetConfigForPathOrDirect()
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected error actual no error", name)
}
if !mock.isErr && err != nil {
t.Fatalf("test '%s' failed: expected no error actual '%s'", name, err)
}
})
}
}
func TestClientset(t *testing.T) {
tests := map[string]struct {
isInCluster bool
kubeConfigPath string
getInClusterConfig getInClusterConfigFunc
getKubeMasterIP getKubeMasterIPFunc
getKubeConfigPath getKubeConfigPathFunc
getConfigFromENV buildConfigFromFlagsFunc
getKubernetesClientset getKubernetesClientsetFunc
isErr bool
}{
"t10": {true, "", fakeInClusterConfigOk, nil, nil, nil, fakeGetClientsetOk, false},
"t11": {true, "", fakeInClusterConfigOk, nil, nil, nil, fakeGetClientsetErr, true},
"t12": {true, "", fakeInClusterConfigErr, nil, nil, nil, fakeGetClientsetOk, true},
"t21": {false, "", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathNil, fakeBuildConfigFromFlagsOk, fakeGetClientsetOk, false},
"t22": {false, "", nil, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, fakeGetClientsetOk, false},
"t23": {false, "", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, fakeGetClientsetOk, false},
"t24": {false, "fake-path", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsErr, fakeGetClientsetOk, true},
"t25": {false, "", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, fakeGetClientsetErr, true},
"t26": {false, "fakePath", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsErr, fakeGetClientsetOk, true},
"t30": {false, "", fakeInClusterConfigOk, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, fakeGetClientsetOk, false},
"t31": {false, "", fakeInClusterConfigOk, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, fakeGetClientsetErr, true},
"t32": {false, "", fakeInClusterConfigErr, fakeGetKubeMasterIPNil, fakeGetKubeConfigPathNil, nil, nil, true},
"t33": {false, "fakePath", nil, fakeGetKubeMasterIPOk, fakeGetKubeConfigPathOk, fakeBuildConfigFromFlagsOk, fakeGetClientsetOk, false},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
IsInCluster: mock.isInCluster,
KubeConfigPath: mock.kubeConfigPath,
getInClusterConfig: mock.getInClusterConfig,
getKubeMasterIP: mock.getKubeMasterIP,
getKubeConfigPath: mock.getKubeConfigPath,
buildConfigFromFlags: mock.getConfigFromENV,
getKubernetesClientset: mock.getKubernetesClientset,
}
_, err := c.Clientset()
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected error actual no error", name)
}
if !mock.isErr && err != nil {
t.Fatalf("test '%s' failed: expected no error actual '%s'", name, err)
}
})
}
}
func TestDynamic(t *testing.T) {
tests := map[string]struct {
getKubeMasterIP getKubeMasterIPFunc
getInClusterConfig getInClusterConfigFunc
getKubernetesDynamicClientSet getKubernetesDynamicClientFunc
kubeConfigPath string
getConfigFromENV buildConfigFromFlagsFunc
getKubeConfigPath getKubeConfigPathFunc
isErr bool
}{
"t1": {fakeGetKubeMasterIPNil, fakeInClusterConfigErr, fakeGetDynamicClientSetOk, "fake-path", fakeBuildConfigFromFlagsOk, fakeGetKubeConfigPathNil, false},
"t2": {fakeGetKubeMasterIPNil, fakeInClusterConfigErr, fakeGetDynamicClientSetErr, "fake-path", fakeBuildConfigFromFlagsOk, fakeGetKubeConfigPathOk, true},
"t3": {fakeGetKubeMasterIPNil, fakeInClusterConfigErr, fakeGetDynamicClientSetOk, "fake-path", fakeBuildConfigFromFlagsErr, fakeGetKubeConfigPathOk, true},
"t4": {fakeGetKubeMasterIPOk, fakeInClusterConfigOk, fakeGetDynamicClientSetOk, "", fakeBuildConfigFromFlagsOk, fakeGetKubeConfigPathOk, false},
"t5": {fakeGetKubeMasterIPOk, fakeInClusterConfigErr, fakeGetDynamicClientSetErr, "", fakeBuildConfigFromFlagsOk, fakeGetKubeConfigPathOk, true},
"t6": {fakeGetKubeMasterIPNil, fakeInClusterConfigOk, fakeGetDynamicClientSetErr, "", fakeBuildConfigFromFlagsErr, fakeGetKubeConfigPathNil, true},
"t7": {fakeGetKubeMasterIPNil, fakeInClusterConfigErr, fakeGetDynamicClientSetOk, "", fakeBuildConfigFromFlagsErr, fakeGetKubeConfigPathNil, true},
"t8": {fakeGetKubeMasterIPNil, fakeInClusterConfigErr, fakeGetDynamicClientSetErr, "", fakeBuildConfigFromFlagsErr, fakeGetKubeConfigPathNil, true},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
getKubeMasterIP: mock.getKubeMasterIP,
KubeConfigPath: mock.kubeConfigPath,
getInClusterConfig: mock.getInClusterConfig,
buildConfigFromFlags: mock.getConfigFromENV,
getKubeConfigPath: mock.getKubeConfigPath,
getKubernetesDynamicClient: mock.getKubernetesDynamicClientSet,
}
_, err := c.Dynamic()
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected error actual no error", name)
}
if !mock.isErr && err != nil {
t.Fatalf("test '%s' failed: expected no error but got '%v'", name, err)
}
})
}
}
func TestConfigForPath(t *testing.T) {
tests := map[string]struct {
kubeConfigPath string
getConfigFromPath buildConfigFromFlagsFunc
isErr bool
}{
"T1": {"", fakeBuildConfigFromFlagsErr, true},
"T2": {"fake-path", fakeBuildConfigFromFlagsOk, false},
}
for name, mock := range tests {
name, mock := name, mock // pin It
t.Run(name, func(t *testing.T) {
c := &Client{
KubeConfigPath: mock.kubeConfigPath,
buildConfigFromFlags: mock.getConfigFromPath,
}
_, err := c.ConfigForPath(mock.kubeConfigPath)
if mock.isErr && err == nil {
t.Fatalf("test '%s' failed: expected error actual no error", name)
}
if !mock.isErr && err != nil {
t.Fatalf("test '%s' failed: expected no error but got '%v'", name, err)
}
})
}
}