/
clients.go
139 lines (117 loc) · 4.21 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package client
import (
"os"
"github.com/golang/glog"
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
prometheusClientV1 "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
discovery "k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes/scheme"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
networkv1client "k8s.io/client-go/kubernetes/typed/networking/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
ptpv1api "github.com/openshift/ptp-operator/api/v1"
ptpv1fake "github.com/openshift/ptp-operator/pkg/client/clientset/versioned/fake"
ptpv1 "github.com/openshift/ptp-operator/pkg/client/clientset/versioned/typed/ptp/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
k8sFakeClient "k8s.io/client-go/kubernetes/fake"
)
// Client defines the client set that will be used for testing
var Client = &ClientSet{}
// ClientSet provides the struct to talk with relevant API
type ClientSet struct {
client.Client
kubernetes.Interface
networkv1client.NetworkingV1Client
appsv1client.AppsV1Interface
discovery.DiscoveryInterface
ptpv1.PtpV1Interface
Config *rest.Config
OcpClient clientconfigv1.ConfigV1Interface
corev1client.CoreV1Interface
KubeConfigPath string
prometheusClientV1.MonitoringV1Client
}
func Setup() {
Client = New("")
}
// New returns a *ClientBuilder with the given kubeconfig.
func New(kubeconfig string) *ClientSet {
var config *rest.Config
var err error
if kubeconfig == "" {
kubeconfig = os.Getenv("KUBECONFIG")
}
if kubeconfig != "" {
glog.V(4).Infof("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
glog.V(4).Infof("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
}
if err != nil {
glog.Infof("Failed to create a valid client, environement variable.")
// Cannot create client, nothing else to do
os.Exit(1)
}
clientSet := &ClientSet{}
// Save the kubeconfig for later use
clientSet.KubeConfigPath = kubeconfig
clientSet.CoreV1Interface = corev1client.NewForConfigOrDie(config)
clientSet.Interface = kubernetes.NewForConfigOrDie(config)
clientSet.AppsV1Interface = appsv1client.NewForConfigOrDie(config)
clientSet.DiscoveryInterface = discovery.NewDiscoveryClientForConfigOrDie(config)
clientSet.NetworkingV1Client = *networkv1client.NewForConfigOrDie(config)
clientSet.PtpV1Interface = ptpv1.NewForConfigOrDie(config)
clientSet.OcpClient = clientconfigv1.NewForConfigOrDie(config)
clientSet.MonitoringV1Client = *prometheusClientV1.NewForConfigOrDie(config)
clientSet.Config = config
myScheme := runtime.NewScheme()
if err = scheme.AddToScheme(myScheme); err != nil {
panic(err)
}
if err := apiext.AddToScheme(myScheme); err != nil {
panic(err)
}
clientSet.Client, err = client.New(config, client.Options{
Scheme: myScheme,
})
if err != nil {
return nil
}
return clientSet
}
// GetTestClientSet Overwrites the existing clientholders with a mocked version for unit testing.
func GetTestClientSet(k8sMockObjects []runtime.Object) *ClientSet {
// Build slices of different objects depending on what client
// is supposed to expect them.
var ptpClientObjects []runtime.Object
var k8sClientObjects []runtime.Object
for _, v := range k8sMockObjects {
// Based on what type of object is, populate certain object slices
// with what is supported by a certain client.
// Add more items below if/when needed.
switch v.(type) {
// K8s Client Objects
case *ptpv1api.PtpConfig:
ptpClientObjects = append(ptpClientObjects, v)
case *corev1.Node:
k8sClientObjects = append(k8sClientObjects, v)
}
}
// Add the objects to their corresponding API Clients
Client.Interface = k8sFakeClient.NewSimpleClientset(k8sClientObjects...)
Client.PtpV1Interface = ptpv1fake.NewSimpleClientset(ptpClientObjects...).PtpV1()
return Client
}
func ClearTestClientsHolder() {
if Client != nil {
Client.Interface = nil
Client.PtpV1Interface = nil
}
}