-
Notifications
You must be signed in to change notification settings - Fork 473
/
controlplane.go
321 lines (285 loc) · 14.1 KB
/
controlplane.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 botanist
import (
"context"
"path/filepath"
"time"
"github.com/gardener/gardener/charts"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/client/kubernetes/clientmap/keys"
extensionscontrolplane "github.com/gardener/gardener/pkg/operation/botanist/component/extensions/controlplane"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils"
gutil "github.com/gardener/gardener/pkg/utils/gardener"
"github.com/gardener/gardener/pkg/utils/images"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
hvpav1alpha1 "github.com/gardener/hvpa-controller/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// DeployVerticalPodAutoscaler deploys the VPA into the shoot namespace in the seed.
func (b *Botanist) DeployVerticalPodAutoscaler(ctx context.Context) error {
if !b.Shoot.WantsVerticalPodAutoscaler {
return common.DeleteVpa(ctx, b.K8sSeedClient.Client(), b.Shoot.SeedNamespace, true)
}
for _, name := range []string{
v1beta1constants.DeploymentNameVPAAdmissionController,
v1beta1constants.DeploymentNameVPARecommender,
v1beta1constants.DeploymentNameVPAUpdater,
} {
if err := gutil.NewShootAccessSecret(name, b.Shoot.SeedNamespace).Reconcile(ctx, b.K8sSeedClient.Client()); err != nil {
return err
}
}
var (
podLabels = map[string]interface{}{
v1beta1constants.LabelNetworkPolicyToDNS: "allowed",
v1beta1constants.LabelNetworkPolicyToShootAPIServer: "allowed",
}
admissionController = map[string]interface{}{
"replicas": b.Shoot.GetReplicas(1),
"podAnnotations": map[string]interface{}{
"checksum/secret-vpa-tls-certs": b.LoadCheckSum(common.VPASecretName),
"checksum/secret-vpa-admission-controller": b.LoadCheckSum("vpa-admission-controller"),
},
"podLabels": utils.MergeMaps(podLabels, map[string]interface{}{
v1beta1constants.LabelNetworkPolicyFromShootAPIServer: "allowed",
}),
"createServiceAccount": false,
}
exporter = map[string]interface{}{
"enabled": false,
"replicas": 0,
}
recommender = map[string]interface{}{
"replicas": b.Shoot.GetReplicas(1),
"podAnnotations": map[string]interface{}{
"checksum/secret-vpa-recommender": b.LoadCheckSum("vpa-recommender"),
},
"podLabels": podLabels,
"createServiceAccount": false,
"recommendationMarginFraction": gardencorev1beta1.DefaultRecommendationMarginFraction,
"interval": gardencorev1beta1.DefaultRecommenderInterval,
}
updater = map[string]interface{}{
"replicas": b.Shoot.GetReplicas(1),
"podAnnotations": map[string]interface{}{
"checksum/secret-vpa-updater": b.LoadCheckSum("vpa-updater"),
},
"podLabels": podLabels,
"createServiceAccount": false,
"evictAfterOOMThreshold": gardencorev1beta1.DefaultEvictAfterOOMThreshold,
"evictionRateBurst": gardencorev1beta1.DefaultEvictionRateBurst,
"evictionRateLimit": gardencorev1beta1.DefaultEvictionRateLimit,
"evictionTolerance": gardencorev1beta1.DefaultEvictionTolerance,
"interval": gardencorev1beta1.DefaultUpdaterInterval,
}
defaultValues = map[string]interface{}{
"admissionController": admissionController,
"exporter": exporter,
"recommender": recommender,
"updater": updater,
"deploymentLabels": map[string]interface{}{
v1beta1constants.GardenRole: v1beta1constants.GardenRoleControlPlane,
},
"clusterType": "shoot",
}
)
if verticalPodAutoscaler := b.Shoot.GetInfo().Spec.Kubernetes.VerticalPodAutoscaler; verticalPodAutoscaler != nil {
if val := verticalPodAutoscaler.EvictAfterOOMThreshold; val != nil {
updater["evictAfterOOMThreshold"] = *val
}
if val := verticalPodAutoscaler.EvictionRateBurst; val != nil {
updater["evictionRateBurst"] = *val
}
if val := verticalPodAutoscaler.EvictionRateLimit; val != nil {
updater["evictionRateLimit"] = *val
}
if val := verticalPodAutoscaler.EvictionTolerance; val != nil {
updater["evictionTolerance"] = *val
}
if val := verticalPodAutoscaler.UpdaterInterval; val != nil {
updater["interval"] = *val
}
if val := verticalPodAutoscaler.RecommendationMarginFraction; val != nil {
recommender["recommendationMarginFraction"] = *val
}
if val := verticalPodAutoscaler.RecommenderInterval; val != nil {
recommender["interval"] = *val
}
}
values, err := b.InjectSeedShootImages(defaultValues, images.ImageNameVpaAdmissionController, images.ImageNameVpaExporter, images.ImageNameVpaRecommender, images.ImageNameVpaUpdater)
if err != nil {
return err
}
values["global"] = map[string]interface{}{"images": values["images"]}
if err := b.K8sSeedClient.ChartApplier().Apply(ctx, filepath.Join(charts.Path, "seed-bootstrap", "charts", "vpa", "charts", "runtime"), b.Shoot.SeedNamespace, "vpa", kubernetes.Values(values)); err != nil {
return err
}
// TODO(rfranzke): Remove in a future release.
return kutil.DeleteObjects(ctx, b.K8sSeedClient.Client(),
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "vpa-admission-controller", Namespace: b.Shoot.SeedNamespace}},
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "vpa-recommender", Namespace: b.Shoot.SeedNamespace}},
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "vpa-updater", Namespace: b.Shoot.SeedNamespace}},
)
}
func (b *Botanist) determineControllerReplicas(ctx context.Context, deploymentName string, defaultReplicas int32) (int32, error) {
isCreateOrRestoreOperation := b.Shoot.GetInfo().Status.LastOperation != nil &&
(b.Shoot.GetInfo().Status.LastOperation.Type == gardencorev1beta1.LastOperationTypeCreate ||
b.Shoot.GetInfo().Status.LastOperation.Type == gardencorev1beta1.LastOperationTypeRestore)
if (isCreateOrRestoreOperation && b.Shoot.HibernationEnabled) ||
(!isCreateOrRestoreOperation && b.Shoot.HibernationEnabled == b.Shoot.GetInfo().Status.IsHibernated) {
// Shoot is being created or restored with .spec.hibernation.enabled=true or
// shoot is being reconciled with .spec.hibernation.enabled=.status.isHibernated,
// so keep the replicas which are already available.
return kutil.CurrentReplicaCountForDeployment(ctx, b.K8sSeedClient.Client(), b.Shoot.SeedNamespace, deploymentName)
}
// If Kube-Apiserver is set to 0 replicas then we also want to return 0 here
// since the controller is most likely not able to run w/o communicating to the Apiserver.
if pointer.Int32Deref(b.Shoot.Components.ControlPlane.KubeAPIServer.GetAutoscalingReplicas(), 0) == 0 {
return 0, nil
}
// Shoot is being reconciled with .spec.hibernation.enabled!=.status.isHibernated, so deploy the controller.
// In case the shoot is being hibernated then it will be scaled down to zero later after all machines are gone.
return defaultReplicas, nil
}
// HibernateControlPlane hibernates the entire control plane if the shoot shall be hibernated.
func (b *Botanist) HibernateControlPlane(ctx context.Context) error {
if b.K8sShootClient != nil {
ctxWithTimeOut, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
// If a shoot is hibernated we only want to scale down the entire control plane if no nodes exist anymore. The node-lifecycle-controller
// inside KCM is responsible for deleting Node objects of terminated/non-existing VMs, so let's wait for that before scaling down.
if err := b.WaitUntilNodesDeleted(ctxWithTimeOut); err != nil {
return err
}
// Also wait for all Pods to reflect the correct state before scaling down the control plane.
// KCM should remove all Pods in the cluster that are bound to Nodes that no longer exist and
// therefore there should be no Pods with state `Running` anymore.
if err := b.WaitUntilNoPodRunning(ctxWithTimeOut); err != nil {
return err
}
// Also wait for all Endpoints to not contain any IPs from the Shoot's PodCIDR.
// This is to make sure that the Endpoints objects also reflect the correct state of the hibernated cluster.
// Otherwise this could cause timeouts in user-defined webhooks for CREATE Pods or Nodes on wakeup.
if err := b.WaitUntilEndpointsDoNotContainPodIPs(ctxWithTimeOut); err != nil {
return err
}
// TODO: check if we can remove this mitigation once there is a garbage collection for VolumeAttachments (ref https://github.com/kubernetes/kubernetes/issues/77324)
// Currently on hibernation Machines are forecefully deleted and machine-controller-manager does not wait volumes to be detached.
// In this case kube-controller-manager cannot delete the corresponding VolumeAttachment objects and they are orphaned.
// Such orphaned VolumeAttachments then prevent/block PV deletion. For more details see https://github.com/gardener/gardener-extension-provider-gcp/issues/172.
// As the Nodes are already deleted, we can delete all VolumeAttachments.
// Note: if custom csi-drivers are installed in the cluster (controllers running on the shoot itself), the VolumeAttachments will
// probably not be finalized, because the controller pods are drained like all the other pods, so we still need to cleanup
// VolumeAttachments of those csi-drivers.
if err := CleanVolumeAttachments(ctxWithTimeOut, b.K8sShootClient.Client()); err != nil {
return err
}
}
// invalidate shoot client here before scaling down API server
if err := b.ClientMap.InvalidateClient(keys.ForShoot(b.Shoot.GetInfo())); err != nil {
return err
}
b.K8sShootClient = nil
deployments := []string{
v1beta1constants.DeploymentNameGardenerResourceManager,
v1beta1constants.DeploymentNameKubeControllerManager,
v1beta1constants.DeploymentNameKubeAPIServer,
}
for _, deployment := range deployments {
if err := kubernetes.ScaleDeployment(ctx, b.K8sSeedClient.Client(), kutil.Key(b.Shoot.SeedNamespace, deployment), 0); client.IgnoreNotFound(err) != nil {
return err
}
}
if err := b.K8sSeedClient.Client().Delete(ctx, &hvpav1alpha1.Hvpa{ObjectMeta: metav1.ObjectMeta{Name: v1beta1constants.DeploymentNameKubeAPIServer, Namespace: b.Shoot.SeedNamespace}}, kubernetes.DefaultDeleteOptions...); err != nil {
if !apierrors.IsNotFound(err) && !meta.IsNoMatchError(err) {
return err
}
}
if !b.Shoot.DisableDNS && !b.APIServerSNIEnabled() {
if err := b.Shoot.Components.ControlPlane.KubeAPIServerService.Destroy(ctx); err != nil {
return err
}
if err := b.Shoot.Components.ControlPlane.KubeAPIServerService.WaitCleanup(ctx); err != nil {
return err
}
}
if err := b.Shoot.Components.ControlPlane.KubeAPIServerSNI.Destroy(ctx); err != nil {
return err
}
return client.IgnoreNotFound(b.ScaleETCDToZero(ctx))
}
// DefaultControlPlane creates the default deployer for the ControlPlane custom resource with the given purpose.
func (b *Botanist) DefaultControlPlane(purpose extensionsv1alpha1.Purpose) extensionscontrolplane.Interface {
values := &extensionscontrolplane.Values{
Name: b.Shoot.GetInfo().Name,
Namespace: b.Shoot.SeedNamespace,
Purpose: purpose,
}
switch purpose {
case extensionsv1alpha1.Normal:
values.Type = b.Shoot.GetInfo().Spec.Provider.Type
values.ProviderConfig = b.Shoot.GetInfo().Spec.Provider.ControlPlaneConfig
values.Region = b.Shoot.GetInfo().Spec.Region
case extensionsv1alpha1.Exposure:
values.Type = b.Seed.GetInfo().Spec.Provider.Type
values.Region = b.Seed.GetInfo().Spec.Provider.Region
}
return extensionscontrolplane.New(
b.Logger,
b.K8sSeedClient.Client(),
values,
extensionscontrolplane.DefaultInterval,
extensionscontrolplane.DefaultSevereThreshold,
extensionscontrolplane.DefaultTimeout,
)
}
// DeployControlPlane deploys or restores the ControlPlane custom resource (purpose normal).
func (b *Botanist) DeployControlPlane(ctx context.Context) error {
b.Shoot.Components.Extensions.ControlPlane.SetInfrastructureProviderStatus(b.Shoot.Components.Extensions.Infrastructure.ProviderStatus())
return b.deployOrRestoreControlPlane(ctx, b.Shoot.Components.Extensions.ControlPlane)
}
// DeployControlPlaneExposure deploys or restores the ControlPlane custom resource (purpose exposure).
func (b *Botanist) DeployControlPlaneExposure(ctx context.Context) error {
return b.deployOrRestoreControlPlane(ctx, b.Shoot.Components.Extensions.ControlPlaneExposure)
}
func (b *Botanist) deployOrRestoreControlPlane(ctx context.Context, controlPlane extensionscontrolplane.Interface) error {
if b.isRestorePhase() {
return controlPlane.Restore(ctx, b.GetShootState())
}
return controlPlane.Deploy(ctx)
}
// RestoreControlPlane restores the ControlPlane custom resource (purpose normal)
func (b *Botanist) RestoreControlPlane(ctx context.Context) error {
b.Shoot.Components.Extensions.ControlPlane.SetInfrastructureProviderStatus(b.Shoot.Components.Extensions.Infrastructure.ProviderStatus())
return b.Shoot.Components.Extensions.ControlPlane.Restore(ctx, b.GetShootState())
}
// RestartControlPlanePods restarts (deletes) pods of the shoot control plane.
func (b *Botanist) RestartControlPlanePods(ctx context.Context) error {
return b.K8sSeedClient.Client().DeleteAllOf(
ctx,
&corev1.Pod{},
client.InNamespace(b.Shoot.SeedNamespace),
client.MatchingLabels{v1beta1constants.LabelPodMaintenanceRestart: "true"},
)
}