forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_test.go
More file actions
230 lines (196 loc) · 7.43 KB
/
add_test.go
File metadata and controls
230 lines (196 loc) · 7.43 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package runtime_test
import (
"context"
"sync"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/handler"
logzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
operatorv1alpha1 "github.com/gardener/gardener/pkg/apis/operator/v1alpha1"
"github.com/gardener/gardener/pkg/logger"
operatorclient "github.com/gardener/gardener/pkg/operator/client"
. "github.com/gardener/gardener/pkg/operator/controller/extension/required/runtime"
)
var _ = Describe("Add", func() {
Describe("Reconciler", func() {
var (
ctx context.Context
log logr.Logger
reconciler *Reconciler
)
BeforeEach(func() {
ctx = context.Background()
log = logger.MustNewZapLogger(logger.DebugLevel, logger.FormatJSON, logzap.WriteTo(GinkgoWriter))
reconciler = &Reconciler{
Lock: &sync.RWMutex{},
}
})
Describe("#MapGardenToExtensions", func() {
var (
fakeClient client.Client
garden *operatorv1alpha1.Garden
mapperFunc handler.MapFunc
)
BeforeEach(func() {
fakeClient = fake.NewClientBuilder().WithScheme(operatorclient.RuntimeScheme).Build()
reconciler.Client = fakeClient
garden = &operatorv1alpha1.Garden{
Spec: operatorv1alpha1.GardenSpec{
DNS: &operatorv1alpha1.DNSManagement{
Providers: []operatorv1alpha1.DNSProvider{
{Type: "local-dns"},
},
},
Extensions: []operatorv1alpha1.GardenExtension{
{Type: "local-extension-1"},
{Type: "local-extension-2"},
},
VirtualCluster: operatorv1alpha1.VirtualCluster{
ETCD: &operatorv1alpha1.ETCD{
Main: &operatorv1alpha1.ETCDMain{
Backup: &operatorv1alpha1.Backup{
Provider: "local-infrastructure",
},
},
},
},
},
}
mapperFunc = reconciler.MapGardenToExtensions(log)
})
Context("without extensions", func() {
It("should not return any requests", func() {
Expect(mapperFunc(ctx, garden)).To(BeEmpty())
})
})
Context("with extensions", func() {
var (
infraExtension, dnsExtension *operatorv1alpha1.Extension
)
BeforeEach(func() {
infraExtension = &operatorv1alpha1.Extension{
ObjectMeta: metav1.ObjectMeta{
Name: "local-infra",
},
Spec: operatorv1alpha1.ExtensionSpec{
Resources: []gardencorev1beta1.ControllerResource{
{Kind: "BackupBucket", Type: "local-infrastructure"},
},
},
}
dnsExtension = &operatorv1alpha1.Extension{
ObjectMeta: metav1.ObjectMeta{
Name: "local-dns",
},
Spec: operatorv1alpha1.ExtensionSpec{
Resources: []gardencorev1beta1.ControllerResource{
{Kind: "DNSRecord", Type: "local-dns"},
},
},
}
Expect(fakeClient.Create(ctx, infraExtension)).To(Succeed())
Expect(fakeClient.Create(ctx, dnsExtension)).To(Succeed())
})
It("should return the expected extensions", func() {
Expect(mapperFunc(ctx, garden)).To(ConsistOf(
Equal(reconcile.Request{NamespacedName: types.NamespacedName{Name: infraExtension.Name}}),
Equal(reconcile.Request{NamespacedName: types.NamespacedName{Name: dnsExtension.Name}}),
))
})
})
})
Describe("#MapObjectKindToExtensions", func() {
var (
fakeClient client.Client
kindToRequiredTypes map[string]sets.Set[string]
mapperFunc handler.MapFunc
)
BeforeEach(func() {
kindToRequiredTypes = map[string]sets.Set[string]{}
fakeClient = fake.NewClientBuilder().WithScheme(operatorclient.RuntimeScheme).Build()
reconciler.KindToRequiredTypes = kindToRequiredTypes
reconciler.Client = fakeClient
mapperFunc = reconciler.MapObjectKindToExtensions(log, "BackupBucket", func() client.ObjectList { return &extensionsv1alpha1.BackupBucketList{} })
})
Context("without extensions", func() {
It("should not return any requests", func() {
Expect(mapperFunc(ctx, nil)).To(BeEmpty())
})
})
Context("with extensions", func() {
var (
testExtension1, testExtension2 *operatorv1alpha1.Extension
requiredExtensionKind string
requiredExtensionType string
)
BeforeEach(func() {
requiredExtensionKind = "BackupBucket"
requiredExtensionType = "local"
testExtension1 = &operatorv1alpha1.Extension{
ObjectMeta: metav1.ObjectMeta{
Name: "test-extension-1",
},
Spec: operatorv1alpha1.ExtensionSpec{
Resources: []gardencorev1beta1.ControllerResource{
{Kind: requiredExtensionKind, Type: requiredExtensionType},
},
},
}
testExtension2 = &operatorv1alpha1.Extension{
ObjectMeta: metav1.ObjectMeta{
Name: "test-extension-2",
},
Spec: operatorv1alpha1.ExtensionSpec{
Resources: []gardencorev1beta1.ControllerResource{
{Kind: "DNSRecord", Type: requiredExtensionType},
},
},
}
Expect(fakeClient.Create(ctx, testExtension1)).To(Succeed())
Expect(fakeClient.Create(ctx, testExtension2)).To(Succeed())
})
It("should add the kind with an empty set to the map and return the extension", func() {
Expect(mapperFunc(ctx, nil)).To(ConsistOf(Equal(reconcile.Request{NamespacedName: types.NamespacedName{Name: testExtension1.Name, Namespace: testExtension1.Namespace}})))
Expect(kindToRequiredTypes).To(HaveKeyWithValue(requiredExtensionKind, sets.New[string]()))
})
It("should correctly calculate the kind-to-types map and return the expected extension in the requests", func() {
By("Invoke mapper the first time and expect requests")
backupBucket := &extensionsv1alpha1.BackupBucket{
ObjectMeta: metav1.ObjectMeta{
Name: "test-backup-bucket",
},
Spec: extensionsv1alpha1.BackupBucketSpec{
DefaultSpec: extensionsv1alpha1.DefaultSpec{
Type: requiredExtensionType,
Class: ptr.To(extensionsv1alpha1.ExtensionClassGarden),
},
},
}
Expect(fakeClient.Create(ctx, backupBucket)).To(Succeed())
Expect(mapperFunc(ctx, nil)).To(ConsistOf(Equal(reconcile.Request{NamespacedName: types.NamespacedName{Name: testExtension1.Name, Namespace: testExtension1.Namespace}})))
Expect(kindToRequiredTypes).To(HaveKeyWithValue(requiredExtensionKind, sets.New(requiredExtensionType)))
By("Invoke mapper again w/o changes and expect no requests")
Expect(kindToRequiredTypes).To(HaveKeyWithValue(requiredExtensionKind, sets.New(requiredExtensionType)))
Expect(mapperFunc(ctx, nil)).To(BeEmpty())
By("Delete BackupBucket and expect the extension in the requests")
Expect(fakeClient.Delete(ctx, backupBucket)).To(Succeed())
Expect(mapperFunc(ctx, nil)).To(ConsistOf(Equal(reconcile.Request{NamespacedName: types.NamespacedName{Name: testExtension1.Name, Namespace: testExtension1.Namespace}})))
Expect(kindToRequiredTypes).To(HaveKeyWithValue(requiredExtensionKind, sets.New[string]()))
})
})
})
})
})