forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconciler_test.go
174 lines (149 loc) · 6.71 KB
/
reconciler_test.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
169
170
171
172
173
174
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package quota_test
import (
"context"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
securityv1alpha1 "github.com/gardener/gardener/pkg/apis/security/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
shootquota "github.com/gardener/gardener/pkg/controllermanager/controller/shoot/quota"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
var _ = Describe("Reconciler", func() {
var (
ctx = context.TODO()
fakeClient client.Client
reconciler reconcile.Reconciler
lifetime = ptr.To[int32](1)
namespace = "test-namespace"
quotaName = "test-quota"
quota *gardencorev1beta1.Quota
secretBinding *gardencorev1beta1.SecretBinding
credentialsBinding *securityv1alpha1.CredentialsBinding
shoot *gardencorev1beta1.Shoot
)
BeforeEach(func() {
fakeClient = fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).Build()
reconciler = &shootquota.Reconciler{
Client: fakeClient,
Clock: clock.RealClock{},
Config: config.ShootQuotaControllerConfiguration{
ConcurrentSyncs: ptr.To(1),
SyncPeriod: &metav1.Duration{},
},
}
quota = &gardencorev1beta1.Quota{
ObjectMeta: metav1.ObjectMeta{
Name: quotaName,
Namespace: namespace,
},
Spec: gardencorev1beta1.QuotaSpec{
ClusterLifetimeDays: ptr.To[int32](1),
},
}
secretBinding = &gardencorev1beta1.SecretBinding{
ObjectMeta: metav1.ObjectMeta{Name: "test-secretbinding", Namespace: namespace},
Quotas: []corev1.ObjectReference{
{
Name: quotaName,
Namespace: namespace,
},
},
}
credentialsBinding = &securityv1alpha1.CredentialsBinding{
ObjectMeta: metav1.ObjectMeta{Name: "test-credentialsbinding", Namespace: namespace},
Quotas: []corev1.ObjectReference{
{
Name: quotaName,
Namespace: namespace,
},
},
}
shoot = &gardencorev1beta1.Shoot{
ObjectMeta: metav1.ObjectMeta{Name: "test-shoot", Namespace: namespace, CreationTimestamp: metav1.Now()},
Spec: gardencorev1beta1.ShootSpec{
SecretBindingName: ptr.To("test-secretbinding"),
},
}
})
It("should delete the shoot using secret binding because it is expired", func() {
expiredTime := shoot.CreationTimestamp.Add(-(time.Duration(*lifetime*24) * time.Hour) * 2)
shoot.Annotations = map[string]string{
"shoot.gardener.cloud/expiration-timestamp": expiredTime.Format(time.RFC3339),
}
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Create(ctx, secretBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, shoot)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: shoot.Name, Namespace: shoot.Namespace}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(shoot), shoot)).To(BeNotFoundError())
})
It("should not delete the shoot using secret binding because it is not expired", func() {
notExpiredTime := shoot.CreationTimestamp.Add((time.Duration(*lifetime*24) * time.Hour) * 2)
shoot.Annotations = map[string]string{
"shoot.gardener.cloud/expiration-timestamp": notExpiredTime.Format(time.RFC3339),
}
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Create(ctx, secretBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, shoot)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: shoot.Name, Namespace: shoot.Namespace}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(shoot), shoot)).To(Succeed())
})
It("should delete the shoot using credentials binding because it is expired", func() {
expiredTime := shoot.CreationTimestamp.Add(-(time.Duration(*lifetime*24) * time.Hour) * 2)
shoot.Annotations = map[string]string{
"shoot.gardener.cloud/expiration-timestamp": expiredTime.Format(time.RFC3339),
}
shoot.Spec.SecretBindingName = nil
shoot.Spec.CredentialsBindingName = &credentialsBinding.Name
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Create(ctx, credentialsBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, shoot)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: shoot.Name, Namespace: shoot.Namespace}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(shoot), shoot)).To(BeNotFoundError())
})
It("should not delete the shoot using credentials binding because it is not expired", func() {
notExpiredTime := shoot.CreationTimestamp.Add((time.Duration(*lifetime*24) * time.Hour) * 2)
shoot.Annotations = map[string]string{
"shoot.gardener.cloud/expiration-timestamp": notExpiredTime.Format(time.RFC3339),
}
shoot.Spec.SecretBindingName = nil
shoot.Spec.CredentialsBindingName = &credentialsBinding.Name
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Create(ctx, credentialsBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, shoot)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: shoot.Name, Namespace: shoot.Namespace}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(shoot), shoot)).To(Succeed())
})
It("should set the expiration timestamp annotation on the shoot", func() {
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Create(ctx, secretBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, shoot)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: shoot.Name, Namespace: shoot.Namespace}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(shoot), shoot)).To(Succeed())
_, ok := shoot.Annotations["shoot.gardener.cloud/expiration-timestamp"]
Expect(ok).To(BeTrue())
})
})