-
Notifications
You must be signed in to change notification settings - Fork 479
/
etcd.go
205 lines (176 loc) · 7.63 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
// Copyright (c) 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"
"fmt"
"hash/crc32"
"time"
"github.com/gardener/gardener/extensions/pkg/controller/backupentry/genericactuator"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/features"
gardenletfeatures "github.com/gardener/gardener/pkg/gardenlet/features"
"github.com/gardener/gardener/pkg/operation/botanist/component"
"github.com/gardener/gardener/pkg/operation/botanist/controlplane/etcd"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/flow"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// 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.Etcd, error) {
defragmentationSchedule, err := determineDefragmentationSchedule(b.Shoot.Info, b.ShootedSeed, class)
if err != nil {
return nil, err
}
e := NewEtcd(
b.K8sSeedClient.Client(),
b.Shoot.SeedNamespace,
role,
class,
b.Shoot.HibernationEnabled,
b.Seed.GetValidVolumeSize("10Gi"),
&defragmentationSchedule,
)
hvpaEnabled := gardenletfeatures.FeatureGate.Enabled(features.HVPA)
if b.ShootedSeed != nil {
hvpaEnabled = gardenletfeatures.FeatureGate.Enabled(features.HVPAForShootedSeed)
}
e.SetHVPAConfig(&etcd.HVPAConfig{
Enabled: hvpaEnabled,
MaintenanceTimeWindow: *b.Shoot.Info.Spec.Maintenance.TimeWindow,
})
return e, nil
}
// DeployEtcd deploys the etcd main and events.
func (b *Botanist) DeployEtcd(ctx context.Context) error {
secrets := etcd.Secrets{
CA: component.Secret{Name: etcd.SecretNameCA, Checksum: b.CheckSums[etcd.SecretNameCA]},
Server: component.Secret{Name: etcd.SecretNameServer, Checksum: b.CheckSums[etcd.SecretNameServer]},
Client: component.Secret{Name: etcd.SecretNameClient, Checksum: b.CheckSums[etcd.SecretNameClient]},
}
b.Shoot.Components.ControlPlane.EtcdMain.SetSecrets(secrets)
b.Shoot.Components.ControlPlane.EtcdEvents.SetSecrets(secrets)
if b.Seed.Info.Spec.Backup != nil {
secret := &corev1.Secret{}
if err := b.K8sSeedClient.Client().Get(ctx, kutil.Key(b.Shoot.SeedNamespace, genericactuator.BackupSecretName), secret); err != nil {
return err
}
snapshotSchedule, err := determineBackupSchedule(b.Shoot.Info)
if err != nil {
return err
}
b.Shoot.Components.ControlPlane.EtcdMain.SetBackupConfig(&etcd.BackupConfig{
Provider: b.Seed.Info.Spec.Backup.Provider,
SecretRefName: genericactuator.BackupSecretName,
Prefix: common.GenerateBackupEntryName(b.Shoot.Info.Status.TechnicalID, b.Shoot.Info.Status.UID),
Container: string(secret.Data[genericactuator.DataKeyBackupBucketName]),
FullSnapshotSchedule: snapshotSchedule,
})
}
return flow.Parallel(
b.Shoot.Components.ControlPlane.EtcdMain.Deploy,
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 etcd.WaitUntilEtcdsReady(
ctx,
b.K8sSeedClient.DirectClient(),
b.Logger,
b.Shoot.SeedNamespace,
2,
5*time.Second,
3*time.Minute,
5*time.Minute,
)
}
// SnapshotEtcd executes into the etcd-main pod and triggers a full snapshot.
func (b *Botanist) SnapshotEtcd(ctx context.Context) error {
return b.Shoot.Components.ControlPlane.EtcdMain.Snapshot(ctx, kubernetes.NewPodExecutor(b.K8sSeedClient.RESTConfig()))
}
// ScaleETCDToZero scales ETCD main and events replicas to zero.
func (b *Botanist) ScaleETCDToZero(ctx context.Context) error {
return b.scaleETCD(ctx, 0)
}
// ScaleETCDToOne scales ETCD main and events replicas to one.
func (b *Botanist) ScaleETCDToOne(ctx context.Context) error {
return b.scaleETCD(ctx, 1)
}
func (b *Botanist) scaleETCD(ctx context.Context, replicas int) error {
for _, etcd := range []string{v1beta1constants.ETCDEvents, v1beta1constants.ETCDMain} {
if err := kubernetes.ScaleEtcd(ctx, b.K8sSeedClient.DirectClient(), kutil.Key(b.Shoot.SeedNamespace, etcd), replicas); client.IgnoreNotFound(err) != nil {
return err
}
}
return nil
}
func determineBackupSchedule(shoot *gardencorev1beta1.Shoot) (string, error) {
schedule := "%d %d * * *"
return determineSchedule(shoot, schedule, func(maintenanceTimeWindow *utils.MaintenanceTimeWindow, shootUID types.UID) string {
// Randomize the snapshot timing daily but within last hour.
// The 15 minutes buffer is set to snapshot upload time before actual maintenance window start.
snapshotWindowBegin := maintenanceTimeWindow.Begin().Add(-1, -15, 0)
randomMinutes := int(crc32.ChecksumIEEE([]byte(shootUID)) % 60)
snapshotTime := snapshotWindowBegin.Add(0, randomMinutes, 0)
return fmt.Sprintf(schedule, snapshotTime.Minute(), snapshotTime.Hour())
})
}
func determineDefragmentationSchedule(shoot *gardencorev1beta1.Shoot, shootedSeed *gardencorev1beta1helper.ShootedSeed, class etcd.Class) (string, error) {
schedule := "%d %d */3 * *"
if shootedSeed != nil && class == etcd.ClassImportant {
// defrag important etcds of shooted seeds daily in the maintenance window
schedule = "%d %d * * *"
}
return determineSchedule(shoot, schedule, func(maintenanceTimeWindow *utils.MaintenanceTimeWindow, shootUID types.UID) string {
// Randomize the defragmentation timing but within the maintenance window.
maintenanceWindowBegin := maintenanceTimeWindow.Begin()
windowInMinutes := uint32(maintenanceTimeWindow.Duration().Minutes())
randomMinutes := int(crc32.ChecksumIEEE([]byte(shootUID)) % windowInMinutes)
maintenanceTime := maintenanceWindowBegin.Add(0, randomMinutes, 0)
return fmt.Sprintf(schedule, maintenanceTime.Minute(), maintenanceTime.Hour())
})
}
func determineSchedule(shoot *gardencorev1beta1.Shoot, schedule string, f func(*utils.MaintenanceTimeWindow, types.UID) string) (string, error) {
var (
begin, end string
shootUID types.UID
)
if shoot.Spec.Maintenance != nil && shoot.Spec.Maintenance.TimeWindow != nil {
begin = shoot.Spec.Maintenance.TimeWindow.Begin
end = shoot.Spec.Maintenance.TimeWindow.End
shootUID = shoot.Status.UID
}
if len(begin) != 0 && len(end) != 0 {
maintenanceTimeWindow, err := utils.ParseMaintenanceTimeWindow(begin, end)
if err != nil {
return "", err
}
if !maintenanceTimeWindow.Equal(utils.AlwaysTimeWindow) {
return f(maintenanceTimeWindow, shootUID), nil
}
}
creationMinute := shoot.CreationTimestamp.Minute()
creationHour := shoot.CreationTimestamp.Hour()
return fmt.Sprintf(schedule, creationMinute, creationHour), nil
}