-
Notifications
You must be signed in to change notification settings - Fork 451
/
etcd.go
286 lines (247 loc) · 10.2 KB
/
etcd.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
// Copyright 2020 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"
hvpav1alpha1 "github.com/gardener/hvpa-controller/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
seedmanagementv1alpha1 "github.com/gardener/gardener/pkg/apis/seedmanagement/v1alpha1"
"github.com/gardener/gardener/pkg/component/etcd"
"github.com/gardener/gardener/pkg/component/shared"
"github.com/gardener/gardener/pkg/features"
"github.com/gardener/gardener/pkg/gardenlet/apis/config"
"github.com/gardener/gardener/pkg/operation/shoot"
"github.com/gardener/gardener/pkg/utils/flow"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/timewindow"
)
// NewEtcd is a function exposed for testing.
var NewEtcd = etcd.New
// DefaultEtcd returns a deployer for the etcd.
func (b *Botanist) DefaultEtcd(role string, class etcd.Class) (etcd.Interface, error) {
defragmentationSchedule, err := determineDefragmentationSchedule(b.Shoot.GetInfo(), b.ManagedSeed, class)
if err != nil {
return nil, err
}
var replicas *int32
if !b.Shoot.HibernationEnabled {
replicas = pointer.Int32(getEtcdReplicas(b.Shoot.GetInfo()))
}
e := NewEtcd(
b.Logger,
b.SeedClientSet.Client(),
b.Shoot.SeedNamespace,
b.SecretsManager,
etcd.Values{
Role: role,
Class: class,
Replicas: replicas,
StorageCapacity: b.Seed.GetValidVolumeSize("10Gi"),
DefragmentationSchedule: &defragmentationSchedule,
CARotationPhase: v1beta1helper.GetShootCARotationPhase(b.Shoot.GetInfo().Status.Credentials),
RuntimeKubernetesVersion: b.Seed.KubernetesVersion,
PriorityClassName: v1beta1constants.PriorityClassNameShootControlPlane500,
HighAvailabilityEnabled: v1beta1helper.IsHAControlPlaneConfigured(b.Shoot.GetInfo()),
TopologyAwareRoutingEnabled: b.Shoot.TopologyAwareRoutingEnabled,
},
)
hvpaEnabled := features.DefaultFeatureGate.Enabled(features.HVPA)
if b.ManagedSeed != nil {
hvpaEnabled = features.DefaultFeatureGate.Enabled(features.HVPAForShootedSeed)
}
e.SetHVPAConfig(&etcd.HVPAConfig{
Enabled: hvpaEnabled,
MaintenanceTimeWindow: *b.Shoot.GetInfo().Spec.Maintenance.TimeWindow,
ScaleDownUpdateMode: getScaleDownUpdateMode(class, b.Shoot),
})
return e, nil
}
func getScaleDownUpdateMode(c etcd.Class, s *shoot.Shoot) *string {
if c == etcd.ClassImportant && (s.Purpose == gardencorev1beta1.ShootPurposeProduction || s.Purpose == gardencorev1beta1.ShootPurposeInfrastructure) {
return pointer.String(hvpav1alpha1.UpdateModeOff)
}
if metav1.HasAnnotation(s.GetInfo().ObjectMeta, v1beta1constants.ShootAlphaControlPlaneScaleDownDisabled) {
return pointer.String(hvpav1alpha1.UpdateModeOff)
}
return pointer.String(hvpav1alpha1.UpdateModeMaintenanceWindow)
}
// DeployEtcd deploys the etcd main and events.
func (b *Botanist) DeployEtcd(ctx context.Context) error {
if b.Seed.GetInfo().Spec.Backup != nil {
secret := &corev1.Secret{}
if err := b.SeedClientSet.Client().Get(ctx, kubernetesutils.Key(b.Shoot.SeedNamespace, v1beta1constants.BackupSecretName), secret); err != nil {
return err
}
snapshotSchedule, err := determineBackupSchedule(b.Shoot.GetInfo())
if err != nil {
return err
}
var backupLeaderElection *config.ETCDBackupLeaderElection
if b.Config != nil && b.Config.ETCDConfig != nil {
backupLeaderElection = b.Config.ETCDConfig.BackupLeaderElection
}
b.Shoot.Components.ControlPlane.EtcdMain.SetBackupConfig(&etcd.BackupConfig{
Provider: b.Seed.GetInfo().Spec.Backup.Provider,
SecretRefName: v1beta1constants.BackupSecretName,
Prefix: b.Shoot.BackupEntryName,
Container: string(secret.Data[v1beta1constants.DataKeyBackupBucketName]),
FullSnapshotSchedule: snapshotSchedule,
LeaderElection: backupLeaderElection,
})
}
// Roll out the new peer CA first so that every member in the cluster trusts the old and the new CA.
// This is required because peer certificates which are used for client and server authentication at the same time,
// are re-created with the new CA in the `Deploy` step.
if v1beta1helper.GetShootCARotationPhase(b.Shoot.GetInfo().Status.Credentials) == gardencorev1beta1.RotationPreparing {
if err := flow.Parallel(
b.Shoot.Components.ControlPlane.EtcdMain.RolloutPeerCA,
b.Shoot.Components.ControlPlane.EtcdEvents.RolloutPeerCA,
)(ctx); err != nil {
return err
}
if err := b.WaitUntilEtcdsReady(ctx); err != nil {
return err
}
}
return flow.Parallel(
b.deployOrRestoreMainEtcd,
b.Shoot.Components.ControlPlane.EtcdEvents.Deploy,
)(ctx)
}
// WaitUntilEtcdsReady waits until both etcd-main and etcd-events are ready.
func (b *Botanist) WaitUntilEtcdsReady(ctx context.Context) error {
return flow.Parallel(
b.Shoot.Components.ControlPlane.EtcdMain.Wait,
b.Shoot.Components.ControlPlane.EtcdEvents.Wait,
)(ctx)
}
// DestroyEtcd destroys the etcd main and events.
func (b *Botanist) DestroyEtcd(ctx context.Context) error {
return flow.Parallel(
b.Shoot.Components.ControlPlane.EtcdMain.Destroy,
b.Shoot.Components.ControlPlane.EtcdEvents.Destroy,
)(ctx)
}
// WaitUntilEtcdsDeleted waits until both etcd-main and etcd-events are deleted.
func (b *Botanist) WaitUntilEtcdsDeleted(ctx context.Context) error {
return flow.Parallel(
b.Shoot.Components.ControlPlane.EtcdMain.WaitCleanup,
b.Shoot.Components.ControlPlane.EtcdEvents.WaitCleanup,
)(ctx)
}
// SnapshotEtcd executes into the etcd-main pod and triggers a full snapshot.
func (b *Botanist) SnapshotEtcd(ctx context.Context) error {
return shared.SnapshotEtcd(ctx, b.SecretsManager, b.Shoot.Components.ControlPlane.EtcdMain)
}
// ScaleETCDToZero scales ETCD main and events replicas to zero.
func (b *Botanist) ScaleETCDToZero(ctx context.Context) error {
return b.scaleETCD(ctx, 0)
}
// ScaleUpETCD scales ETCD main and events replicas to the configured replica count.
func (b *Botanist) ScaleUpETCD(ctx context.Context) error {
return b.scaleETCD(ctx, getEtcdReplicas(b.Shoot.GetInfo()))
}
func (b *Botanist) scaleETCD(ctx context.Context, replicas int32) error {
if err := b.Shoot.Components.ControlPlane.EtcdMain.Scale(ctx, replicas); err != nil {
return err
}
return b.Shoot.Components.ControlPlane.EtcdEvents.Scale(ctx, replicas)
}
func (b *Botanist) deployOrRestoreMainEtcd(ctx context.Context) error {
isRestoreRequired, err := b.isRestorationOfMultiNodeMainEtcdRequired(ctx)
if err != nil {
return err
}
if isRestoreRequired {
return b.restoreMultiNodeMainEtcd(ctx)
}
return b.Shoot.Components.ControlPlane.EtcdMain.Deploy(ctx)
}
func (b *Botanist) isRestorationOfMultiNodeMainEtcdRequired(ctx context.Context) (bool, error) {
if !b.IsRestorePhase() || !v1beta1helper.IsHAControlPlaneConfigured(b.Shoot.GetInfo()) {
return false, nil
}
etcd, err := b.Shoot.Components.ControlPlane.EtcdMain.Get(ctx)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
// The etcd has already been scaled up to the desired number of replicas
// and therefore has been restored, then it has been scaled down to 0
// replicas as part of the reconciliation flow for hibernated shoots.
if b.Shoot.HibernationEnabled && etcd.Spec.Replicas == 0 {
return false, nil
}
// The etcd has already been scaled up to the desired number of replicas
// and therefore has been restored.
if etcd.Spec.Replicas == getEtcdReplicas(b.Shoot.GetInfo()) {
return false, nil
}
return true, nil
}
func (b *Botanist) restoreMultiNodeMainEtcd(ctx context.Context) error {
originalReplicas := b.Shoot.Components.ControlPlane.EtcdMain.GetReplicas()
defer func() {
// Revert the original replica count for the etcd. This is done in case a step
// is added to the reconciliation flow that depends on the etcd's replica count.
b.Shoot.Components.ControlPlane.EtcdMain.SetReplicas(originalReplicas)
}()
b.Shoot.Components.ControlPlane.EtcdMain.SetReplicas(pointer.Int32(1))
if err := b.Shoot.Components.ControlPlane.EtcdMain.Deploy(ctx); err != nil {
return err
}
if err := b.Shoot.Components.ControlPlane.EtcdMain.Wait(ctx); err != nil {
return err
}
return b.Shoot.Components.ControlPlane.EtcdMain.Scale(ctx, getEtcdReplicas(b.Shoot.GetInfo()))
}
func determineBackupSchedule(shoot *gardencorev1beta1.Shoot) (string, error) {
return timewindow.DetermineSchedule(
"%d %d * * *",
shoot.Spec.Maintenance.TimeWindow.Begin,
shoot.Spec.Maintenance.TimeWindow.End,
shoot.Status.UID,
shoot.CreationTimestamp,
timewindow.RandomizeWithinFirstHourOfTimeWindow,
)
}
func determineDefragmentationSchedule(shoot *gardencorev1beta1.Shoot, managedSeed *seedmanagementv1alpha1.ManagedSeed, class etcd.Class) (string, error) {
scheduleFormat := "%d %d */3 * *"
if managedSeed != nil && class == etcd.ClassImportant {
// defrag important etcds of ManagedSeeds daily in the maintenance window
scheduleFormat = "%d %d * * *"
}
return timewindow.DetermineSchedule(
scheduleFormat,
shoot.Spec.Maintenance.TimeWindow.Begin,
shoot.Spec.Maintenance.TimeWindow.End,
shoot.Status.UID,
shoot.CreationTimestamp,
timewindow.RandomizeWithinTimeWindow,
)
}
func getEtcdReplicas(shoot *gardencorev1beta1.Shoot) int32 {
if v1beta1helper.IsHAControlPlaneConfigured(shoot) {
return 3
}
return 1
}