-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
fake_proxy.go
168 lines (144 loc) · 4.93 KB
/
fake_proxy.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2019 The Kubernetes 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 test
import (
apiextensionslv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
fakebootstrap "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/bootstrap"
fakecontrolplane "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/controlplane"
fakeexternal "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/external"
fakeinfrastructure "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/infrastructure"
addonsv1alpha3 "sigs.k8s.io/cluster-api/exp/addons/api/v1alpha3"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
type FakeProxy struct {
cs client.Client
namespace string
objs []runtime.Object
}
var (
FakeScheme = runtime.NewScheme()
)
func init() {
_ = clientgoscheme.AddToScheme(FakeScheme)
_ = clusterctlv1.AddToScheme(FakeScheme)
_ = clusterv1.AddToScheme(FakeScheme)
_ = expv1.AddToScheme(FakeScheme)
_ = addonsv1alpha3.AddToScheme(FakeScheme)
_ = apiextensionslv1.AddToScheme(FakeScheme)
_ = fakebootstrap.AddToScheme(FakeScheme)
_ = fakecontrolplane.AddToScheme(FakeScheme)
_ = fakeexternal.AddToScheme(FakeScheme)
_ = fakeinfrastructure.AddToScheme(FakeScheme)
}
func (f *FakeProxy) CurrentNamespace() (string, error) {
return f.namespace, nil
}
func (f *FakeProxy) ValidateKubernetesVersion() error {
return nil
}
func (f *FakeProxy) GetConfig() (*rest.Config, error) {
return nil, nil
}
func (f *FakeProxy) NewClient() (client.Client, error) {
if f.cs != nil {
return f.cs, nil
}
f.cs = fake.NewFakeClientWithScheme(FakeScheme, f.objs...)
return f.cs, nil
}
// ListResources returns all the resources known by the FakeProxy
func (f *FakeProxy) ListResources(labels map[string]string, namespaces ...string) ([]unstructured.Unstructured, error) {
var ret []unstructured.Unstructured //nolint
for _, o := range f.objs {
u := unstructured.Unstructured{}
err := FakeScheme.Convert(o, &u, nil)
if err != nil {
return nil, err
}
// filter by namespace, if any
if len(namespaces) > 0 && u.GetNamespace() != "" {
inNamespaces := false
for _, namespace := range namespaces {
if u.GetNamespace() == namespace {
inNamespaces = true
break
}
}
if !inNamespaces {
continue
}
}
// filter by label, if any
haslabel := false
for l, v := range labels {
for ul, uv := range u.GetLabels() {
if l == ul && v == uv {
haslabel = true
}
}
}
if !haslabel {
continue
}
ret = append(ret, u)
}
return ret, nil
}
func NewFakeProxy() *FakeProxy {
return &FakeProxy{
namespace: "default",
}
}
func (f *FakeProxy) WithObjs(objs ...runtime.Object) *FakeProxy {
f.objs = append(f.objs, objs...)
return f
}
func (f *FakeProxy) WithNamespace(n string) *FakeProxy {
f.namespace = n
return f
}
// WithProviderInventory can be used as a fast track for setting up test scenarios requiring an already initialized management cluster.
// NB. this method adds an items to the Provider inventory, but it doesn't install the corresponding provider; if the
// test case requires the actual provider to be installed, use the the fake client to install both the provider
// components and the corresponding inventory item.
func (f *FakeProxy) WithProviderInventory(name string, providerType clusterctlv1.ProviderType, version, targetNamespace, watchingNamespace string) *FakeProxy {
f.objs = append(f.objs, &clusterctlv1.Provider{
TypeMeta: metav1.TypeMeta{
APIVersion: clusterctlv1.GroupVersion.String(),
Kind: "Provider",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: targetNamespace,
Name: clusterctlv1.ManifestLabel(name, providerType),
Labels: map[string]string{
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: clusterctlv1.ManifestLabel(name, providerType),
clusterctlv1.ClusterctlCoreLabelName: "inventory",
},
},
ProviderName: name,
Type: string(providerType),
Version: version,
WatchedNamespace: watchingNamespace,
})
return f
}