-
Notifications
You must be signed in to change notification settings - Fork 459
/
virtual_garden_access.go
162 lines (123 loc) · 6.87 KB
/
virtual_garden_access.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package rotation
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/test/utils/access"
)
type clients struct {
accessSecret, clientCert, serviceAccountDynamic kubernetes.Interface
}
// VirtualGardenAccessVerifier uses the various access methods to access the virtual garden.
type VirtualGardenAccessVerifier struct {
RuntimeClient client.Client
Namespace string
clientsBefore, clientsPrepared, clientsAfter clients
}
// Before is called before the rotation is started.
func (v *VirtualGardenAccessVerifier) Before(ctx context.Context) {
var err error
v.clientsBefore.accessSecret, err = kubernetes.NewClientFromSecret(ctx, v.RuntimeClient, v.Namespace, "gardener", kubernetes.WithDisabledCachedClient())
Expect(err).NotTo(HaveOccurred())
By("Request new client certificate and using it to access virtual garden")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromCSR(ctx, v.clientsBefore.accessSecret, "e2e-rotate-csr-before")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsBefore.clientCert = virtualGardenClient
}).Should(Succeed())
By("Request new dynamic token for a ServiceAccount and using it to access target cluster")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromDynamicServiceAccountToken(ctx, v.clientsBefore.accessSecret, "e2e-rotate-sa-dynamic-before")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsBefore.serviceAccountDynamic = virtualGardenClient
}).Should(Succeed())
}
// ExpectPreparingStatus is called while waiting for the Preparing status.
func (v *VirtualGardenAccessVerifier) ExpectPreparingStatus(_ Gomega) {}
// AfterPrepared is called when the Shoot is in Prepared status.
func (v *VirtualGardenAccessVerifier) AfterPrepared(ctx context.Context) {
By("Use client certificate from before rotation to access target cluster")
Eventually(func(g Gomega) {
g.Expect(v.clientsBefore.clientCert.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
}).Should(Succeed())
By("Use dynamic ServiceAccount token from before rotation to access target cluster")
Eventually(func(g Gomega) {
g.Expect(v.clientsBefore.serviceAccountDynamic.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
}).Should(Succeed())
var err error
v.clientsPrepared.accessSecret, err = kubernetes.NewClientFromSecret(ctx, v.RuntimeClient, v.Namespace, "gardener", kubernetes.WithDisabledCachedClient())
Expect(err).NotTo(HaveOccurred())
By("Request new client certificate and using it to access target cluster")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromCSR(ctx, v.clientsPrepared.accessSecret, "e2e-rotate-csr-prepared")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsPrepared.clientCert = virtualGardenClient
}).Should(Succeed())
By("Request new dynamic token for a ServiceAccount and using it to access target cluster")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromDynamicServiceAccountToken(ctx, v.clientsPrepared.accessSecret, "e2e-rotate-sa-dynamic-prepared")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsPrepared.serviceAccountDynamic = virtualGardenClient
}).Should(Succeed())
}
// ExpectCompletingStatus is called while waiting for the Completing status.
func (v *VirtualGardenAccessVerifier) ExpectCompletingStatus(_ Gomega) {}
// AfterCompleted is called when the Shoot is in Completed status.
func (v *VirtualGardenAccessVerifier) AfterCompleted(ctx context.Context) {
By("Use client certificate from before rotation to access target cluster")
Consistently(func(g Gomega) {
g.Expect(v.clientsBefore.clientCert.Client().List(ctx, &corev1.NamespaceList{})).NotTo(Succeed())
}).Should(Succeed())
By("Use dynamic ServiceAccount token from before rotation to access target cluster")
Consistently(func(g Gomega) {
g.Expect(v.clientsBefore.serviceAccountDynamic.Client().List(ctx, &corev1.NamespaceList{})).NotTo(Succeed())
}).Should(Succeed())
By("Use client certificate from after preparation to access target cluster")
Eventually(func(g Gomega) {
g.Expect(v.clientsPrepared.clientCert.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
}).Should(Succeed())
By("Use dynamic ServiceAccount token from after preparation to access target cluster")
Eventually(func(g Gomega) {
g.Expect(v.clientsPrepared.serviceAccountDynamic.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
}).Should(Succeed())
var err error
v.clientsAfter.accessSecret, err = kubernetes.NewClientFromSecret(ctx, v.RuntimeClient, v.Namespace, "gardener", kubernetes.WithDisabledCachedClient())
Expect(err).NotTo(HaveOccurred())
By("Request new client certificate and using it to access target cluster")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromCSR(ctx, v.clientsAfter.accessSecret, "e2e-rotate-csr-after")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsAfter.clientCert = virtualGardenClient
}).Should(Succeed())
By("Request new dynamic token for a ServiceAccount and using it to access target cluster")
Eventually(func(g Gomega) {
virtualGardenClient, err := access.CreateTargetClientFromDynamicServiceAccountToken(ctx, v.clientsAfter.accessSecret, "e2e-rotate-sa-dynamic-after")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(virtualGardenClient.Client().List(ctx, &corev1.NamespaceList{})).To(Succeed())
v.clientsAfter.serviceAccountDynamic = virtualGardenClient
}).Should(Succeed())
}
// Cleanup is passed to ginkgo.DeferCleanup.
func (v *VirtualGardenAccessVerifier) Cleanup(ctx context.Context) {
virtualGardenClient, err := kubernetes.NewClientFromSecret(ctx, v.RuntimeClient, v.Namespace, "gardener", kubernetes.WithDisabledCachedClient())
Expect(err).NotTo(HaveOccurred())
By("Clean up objects in virtual garden from client certificate access")
Eventually(func(g Gomega) {
g.Expect(access.CleanupObjectsFromCSRAccess(ctx, virtualGardenClient)).To(Succeed())
}).Should(Succeed())
By("Clean up objects in virtual garden from dynamic ServiceAccount token access")
Eventually(func(g Gomega) {
g.Expect(access.CleanupObjectsFromDynamicServiceAccountTokenAccess(ctx, virtualGardenClient)).To(Succeed())
}).Should(Succeed())
}