2020-07-09 19:30:59 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package library
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2020-07-24 15:40:08 +00:00
|
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-05 14:54:51 +00:00
|
|
|
placeholdernameclientset "github.com/suzerain-io/placeholder-name/kubernetes/1.19/client-go/clientset/versioned"
|
2020-07-09 19:30:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewClientConfig(t *testing.T) *rest.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-24 15:40:08 +00:00
|
|
|
return newClientConfigWithOverrides(t, &clientcmd.ConfigOverrides{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientConfigWithCertAndKey(t *testing.T, cert, key string) *rest.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return newClientConfigWithOverrides(t, &clientcmd.ConfigOverrides{
|
|
|
|
AuthInfo: clientcmdapi.AuthInfo{
|
2020-07-27 12:52:36 +00:00
|
|
|
ClientCertificateData: []byte(cert),
|
|
|
|
ClientKeyData: []byte(key),
|
2020-07-24 15:40:08 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientConfigWithOverrides(t *testing.T, overrides *clientcmd.ConfigOverrides) *rest.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-09 19:30:59 +00:00
|
|
|
loader := clientcmd.NewDefaultClientConfigLoadingRules()
|
2020-07-24 15:40:08 +00:00
|
|
|
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loader, overrides)
|
2020-07-09 19:30:59 +00:00
|
|
|
config, err := clientConfig.ClientConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientset(t *testing.T) kubernetes.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-24 15:40:08 +00:00
|
|
|
return NewClientsetWithConfig(t, NewClientConfig(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientsetWithConfig(t *testing.T, config *rest.Config) kubernetes.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-27 12:52:36 +00:00
|
|
|
result, err := kubernetes.NewForConfig(config)
|
|
|
|
require.NoError(t, err, "unexpected failure from kubernetes.NewForConfig()")
|
|
|
|
return result
|
2020-07-09 19:30:59 +00:00
|
|
|
}
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
func NewPlaceholderNameClientset(t *testing.T) placeholdernameclientset.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return placeholdernameclientset.NewForConfigOrDie(NewClientConfig(t))
|
|
|
|
}
|