-
Notifications
You must be signed in to change notification settings - Fork 404
/
fake.go
60 lines (52 loc) · 1.76 KB
/
fake.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
package deployment
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
dynamicfake "k8s.io/client-go/dynamic/fake"
)
func NewFakeClient(deployments *appsv1.DeploymentList) (Client, error) {
scheme, err := SetupSchemeOrDie()
if err != nil {
return Client{}, err
}
deploymentsUnstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deployments)
if err != nil {
return Client{}, err
}
unstructuredItems, err := toUnstructuredItems(deployments)
if err != nil {
return Client{}, err
}
deployUnstructured := &unstructured.UnstructuredList{
Object: deploymentsUnstructuredMap,
Items: unstructuredItems,
}
deployUnstructured.SetGroupVersionKind(GroupVersionKindList())
dynamicClient := dynamicfake.NewSimpleDynamicClient(scheme, deployUnstructured)
return Client{DynamicClient: dynamicClient}, nil
}
func SetupSchemeOrDie() (*runtime.Scheme, error) {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
return nil, err
}
if err := appsv1.AddToScheme(scheme); err != nil {
return nil, err
}
return scheme, nil
}
func toUnstructuredItems(deploymentList *appsv1.DeploymentList) ([]unstructured.Unstructured, error) {
unstructuredItems := make([]unstructured.Unstructured, 0)
for _, deployment := range deploymentList.Items {
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&deployment)
if err != nil {
return nil, err
}
deploymentUnstructured := &unstructured.Unstructured{Object: unstructuredMap}
deploymentUnstructured.SetGroupVersionKind(GroupVersionKind())
unstructuredItems = append(unstructuredItems, *deploymentUnstructured)
}
return unstructuredItems, nil
}