This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
etcd.go
201 lines (167 loc) 路 6.17 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
// Copyright (c) 2021 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 virtualgarden
import (
"context"
"github.com/gardener/virtual-garden/pkg/api/helper"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// ETCDRoleMain is a constant for the 'main' role of etcd.
ETCDRoleMain = "main"
// ETCDRoleEvents is a constant for the 'events' role of etcd.
ETCDRoleEvents = "events"
)
// DeployETCD generates a dedicated CA, two server certificates (for main and events etcd), and one client
// certificate. It deploys them as secrets together with the bootstrap config map. Finally, it creates a service for
// etcd that can be used by clients and deploys it as a stateful set with a persistent volume for its data.
// It waits until the stateful sets are ready.
func (o *operation) DeployETCD(ctx context.Context) error {
if err := o.deployETCDResources(ctx); err != nil {
return err
}
for _, role := range []string{ETCDRoleMain, ETCDRoleEvents} {
if err := waitForStatefulSetReady(ctx, o.client, emptyETCDStatefulSet(o.namespace, role), o.log); err != nil {
return err
}
}
return nil
}
func (o *operation) deployETCDResources(ctx context.Context) error {
o.log.Infof("Deploying the storage class for persistent volumes of etcd")
if err := o.deployETCDStorageClass(ctx); err != nil {
return err
}
o.log.Infof("Generating or loading CA, client certificates")
caCertificate, caCertChecksum, err := o.deployETCDCACertificate(ctx)
if err != nil {
return err
}
_, clientCertChecksum, err := o.deployETCDClientCertificate(ctx, caCertificate)
if err != nil {
return err
}
for _, role := range []string{ETCDRoleMain, ETCDRoleEvents} {
o.log.Infof("Deploying etcd service for role %q", role)
if err := o.deployETCDService(ctx, role); err != nil {
return err
}
o.log.Infof("Deploying etcd bootstrap configmap for role %q", role)
configMapChecksum, err := o.deployETCDConfigMap(ctx, role)
if err != nil {
return err
}
o.log.Infof("Generating or loading server certificate for role %q", role)
_, serverCertChecksum, err := o.deployETCDServerCertificate(ctx, caCertificate, role)
if err != nil {
return err
}
var (
checksums = map[string]string{
"checksum/configmap-etcd-bootstrap-config": configMapChecksum,
"checksum/secret-etcd-ca": caCertChecksum,
"checksum/secret-etcd-server": serverCertChecksum,
"checksum/secret-etcd-client": clientCertChecksum,
}
// data volume settings
storageCapacity = "10Gi"
storageClassName *string
// backup settings
storageProviderName string
secretData map[string][]byte
environment []corev1.EnvVar
)
if role == ETCDRoleMain {
storageCapacity = "25Gi"
storageClassName = pointer.StringPtr(ETCDStorageClassName(o.imports.VirtualGarden.ETCD))
if helper.ETCDBackupEnabled(o.imports.VirtualGarden.ETCD) {
o.log.Infof("Deploying etcd backup secret for role %q", role)
storageProviderName, secretData, environment = o.backupProvider.ComputeETCDBackupConfiguration(ETCDVolumeMountPathBackupSecret, ETCDSecretNameBackup)
backupSecretChecksum, err := o.deployETCDBackupSecret(ctx, secretData)
if err != nil {
return err
}
checksums["checksum/secret-etcd-backup"] = backupSecretChecksum
}
}
o.log.Infof("Deploying etcd statefulset for role %q", role)
if err := o.deployETCDStatefulSet(ctx, role, checksums, storageCapacity, storageClassName, storageProviderName, environment); err != nil {
return err
}
}
return nil
}
// DeleteETCD deletes etcd and all related resources.
func (o *operation) DeleteETCD(ctx context.Context) error {
for _, role := range []string{ETCDRoleMain, ETCDRoleEvents} {
o.log.Infof("Deleting etcd statefulset for role %q", role)
if err := o.deleteETCDStatefulSet(ctx, role); err != nil {
return err
}
if role == ETCDRoleMain && helper.ETCDBackupEnabled(o.imports.VirtualGarden.ETCD) {
o.log.Infof("Deleting etcd backup secret for role %q", role)
if err := o.deleteETCDBackupSecret(ctx); err != nil {
return err
}
}
o.log.Infof("Deleting etcd bootstrap configmap for role %q", role)
if err := o.deleteETCDConfigMap(ctx, role); err != nil {
return err
}
o.log.Infof("Deleting etcd service for role %q", role)
if err := o.deleteETCDService(ctx, role); err != nil {
return err
}
}
o.log.Infof("Deleting all certificates related to etcd")
if err := o.deleteETCDCertificateSecrets(ctx); err != nil {
return err
}
mustKeepStorageClass, err := OtherVirtualGardensInHostingCluster(ctx, o.client, o.namespace)
if err != nil {
return err
}
if !mustKeepStorageClass {
o.log.Infof("Deleting the storage class for persistent volumes of etcd")
if err := o.deleteETCDStorageClass(ctx); err != nil {
return err
}
}
return nil
}
// OtherVirtualGardensInHostingCluster returns true if there are other statefulsets of virtual garden in the hosting
// cluster.
func OtherVirtualGardensInHostingCluster(ctx context.Context, c client.Client, namespace string) (bool, error) {
statefulSetList := &appsv1.StatefulSetList{}
if err := c.List(ctx, statefulSetList); err != nil {
return false, err
}
for _, sts := range statefulSetList.Items {
if sts.Namespace == namespace {
continue
}
return true, nil
}
return false, nil
}
func etcdLabels(role string) map[string]string {
return map[string]string{
LabelKeyApp: Prefix,
LabelKeyComponent: "etcd",
LabelKeyRole: role,
}
}