-
Notifications
You must be signed in to change notification settings - Fork 113
/
gardener_client.go
150 lines (120 loc) · 4.44 KB
/
gardener_client.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
140
141
142
143
144
145
146
147
148
149
150
package shoots
import (
"context"
"testing"
"github.com/gardener/gardener/pkg/apis/authentication/v1alpha1"
gardener_types "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardener_apis "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
func NewFakeShootsInterface(t *testing.T, config *rest.Config) gardener_apis.ShootInterface {
dynamicClient, err := dynamic.NewForConfig(config)
require.NoError(t, err)
resourceInterface := dynamicClient.Resource(gardener_types.SchemeGroupVersion.WithResource("shoots"))
return &fakeShootsInterface{
client: resourceInterface,
}
}
type fakeShootsInterface struct {
client dynamic.ResourceInterface
}
func (f fakeShootsInterface) Create(ctx context.Context, shoot *gardener_types.Shoot, options metav1.CreateOptions) (*gardener_types.Shoot, error) {
addTypeMeta(shoot)
shoot.SetFinalizers([]string{"finalizer"})
unstructuredShoot, err := toUnstructured(shoot)
if err != nil {
return nil, err
}
create, err := f.client.Create(ctx, unstructuredShoot, options)
if err != nil {
return nil, err
}
return fromUnstructured(create)
}
func (f *fakeShootsInterface) Update(ctx context.Context, shoot *gardener_types.Shoot, options metav1.UpdateOptions) (*gardener_types.Shoot, error) {
obj, err := toUnstructured(shoot)
if err != nil {
return nil, err
}
updated, err := f.client.Update(context.Background(), obj, options)
if err != nil {
return nil, err
}
return fromUnstructured(updated)
}
func (f *fakeShootsInterface) UpdateStatus(_ context.Context, _ *gardener_types.Shoot, _ metav1.UpdateOptions) (*gardener_types.Shoot, error) {
return nil, nil
}
func (f *fakeShootsInterface) Delete(ctx context.Context, name string, options metav1.DeleteOptions) error {
return f.client.Delete(ctx, name, options)
}
func (f *fakeShootsInterface) DeleteCollection(_ context.Context, _ metav1.DeleteOptions, _ metav1.ListOptions) error {
return nil
}
func (f *fakeShootsInterface) Get(ctx context.Context, name string, options metav1.GetOptions) (*gardener_types.Shoot, error) {
obj, err := f.client.Get(ctx, name, options)
if err != nil {
return nil, err
}
return fromUnstructured(obj)
}
func (f *fakeShootsInterface) List(ctx context.Context, options metav1.ListOptions) (*gardener_types.ShootList, error) {
list, err := f.client.List(ctx, options)
if err != nil {
return nil, err
}
return listFromUnstructured(list)
}
func (f *fakeShootsInterface) Watch(_ context.Context, _ metav1.ListOptions) (watch.Interface, error) {
return nil, nil
}
func (f *fakeShootsInterface) Patch(_ context.Context, _ string, _ types.PatchType, _ []byte, _ metav1.PatchOptions, _ ...string) (result *gardener_types.Shoot, err error) {
return nil, nil
}
func (f *fakeShootsInterface) CreateAdminKubeconfigRequest(_ context.Context, _ string, _ *v1alpha1.AdminKubeconfigRequest, _ metav1.CreateOptions) (*v1alpha1.AdminKubeconfigRequest, error) {
return nil, nil
}
func (f *fakeShootsInterface) UpdateBinding(ctx context.Context, shootName string, shoot *gardener_types.Shoot, opts metav1.UpdateOptions) (*gardener_types.Shoot, error) {
return nil, nil
}
func addTypeMeta(shoot *gardener_types.Shoot) {
shoot.TypeMeta = metav1.TypeMeta{
Kind: "Shoot",
APIVersion: "core.gardener.cloud/v1beta1",
}
}
func toUnstructured(obj interface{}) (*unstructured.Unstructured, error) {
object, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: object}, nil
}
func fromUnstructured(object *unstructured.Unstructured) (*gardener_types.Shoot, error) {
var newShoot gardener_types.Shoot
err := runtime.DefaultUnstructuredConverter.FromUnstructured(object.Object, &newShoot)
if err != nil {
return nil, err
}
return &newShoot, err
}
func listFromUnstructured(list *unstructured.UnstructuredList) (*gardener_types.ShootList, error) {
shootList := &gardener_types.ShootList{
Items: []gardener_types.Shoot{},
}
for _, obj := range list.Items {
shoot, err := fromUnstructured(&obj)
if err != nil {
return &gardener_types.ShootList{}, err
}
shootList.Items = append(shootList.Items, *shoot)
}
return shootList, nil
}