-
Notifications
You must be signed in to change notification settings - Fork 451
/
apiserver.go
229 lines (195 loc) · 7.66 KB
/
apiserver.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package shared
import (
"context"
"fmt"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/component/apiserver"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
"github.com/gardener/gardener/pkg/utils/gardener/secretsrotation"
kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes"
)
func computeAPIServerAuditConfig(
ctx context.Context,
cl client.Client,
objectMeta metav1.ObjectMeta,
config *gardencorev1beta1.AuditConfig,
webhookConfig *apiserver.AuditWebhook,
) (
*apiserver.AuditConfig,
error,
) {
if config == nil || config.AuditPolicy == nil || config.AuditPolicy.ConfigMapRef == nil {
return nil, nil
}
var (
out = &apiserver.AuditConfig{
Webhook: webhookConfig,
}
key = kubernetesutils.Key(objectMeta.Namespace, config.AuditPolicy.ConfigMapRef.Name)
)
configMap := &corev1.ConfigMap{}
if err := cl.Get(ctx, key, configMap); err != nil {
// Ignore missing audit configuration on cluster deletion to prevent failing redeployments of the
// API server in case the end-user deleted the configmap before/simultaneously to the deletion.
if !apierrors.IsNotFound(err) || objectMeta.DeletionTimestamp == nil {
return nil, fmt.Errorf("retrieving audit policy from the ConfigMap %s failed: %w", key, err)
}
} else {
policy, ok := configMap.Data["policy"]
if !ok {
return nil, fmt.Errorf("missing '.data.policy' in audit policy ConfigMap %s", key)
}
out.Policy = &policy
}
return out, nil
}
func computeEnabledAPIServerAdmissionPlugins(defaultPlugins, configuredPlugins []gardencorev1beta1.AdmissionPlugin) []gardencorev1beta1.AdmissionPlugin {
for _, plugin := range configuredPlugins {
pluginOverwritesDefault := false
for i, defaultPlugin := range defaultPlugins {
if defaultPlugin.Name == plugin.Name {
pluginOverwritesDefault = true
defaultPlugins[i] = plugin
break
}
}
if !pluginOverwritesDefault {
defaultPlugins = append(defaultPlugins, plugin)
}
}
var admissionPlugins []gardencorev1beta1.AdmissionPlugin
for _, defaultPlugin := range defaultPlugins {
if !ptr.Deref(defaultPlugin.Disabled, false) {
admissionPlugins = append(admissionPlugins, defaultPlugin)
}
}
return admissionPlugins
}
func computeDisabledAPIServerAdmissionPlugins(configuredPlugins []gardencorev1beta1.AdmissionPlugin) []gardencorev1beta1.AdmissionPlugin {
var disabledAdmissionPlugins []gardencorev1beta1.AdmissionPlugin
for _, plugin := range configuredPlugins {
if ptr.Deref(plugin.Disabled, false) {
disabledAdmissionPlugins = append(disabledAdmissionPlugins, plugin)
}
}
return disabledAdmissionPlugins
}
func convertToAdmissionPluginConfigs(ctx context.Context, gardenClient client.Client, namespace string, plugins []gardencorev1beta1.AdmissionPlugin) ([]apiserver.AdmissionPluginConfig, error) {
var (
err error
out []apiserver.AdmissionPluginConfig
)
for _, plugin := range plugins {
config := apiserver.AdmissionPluginConfig{AdmissionPlugin: plugin}
if plugin.KubeconfigSecretName != nil {
key := client.ObjectKey{Namespace: namespace, Name: *plugin.KubeconfigSecretName}
config.Kubeconfig, err = gardenerutils.FetchKubeconfigFromSecret(ctx, gardenClient, key)
if err != nil {
return nil, fmt.Errorf("failed reading kubeconfig for admission plugin from referenced secret %s: %w", key, err)
}
}
out = append(out, config)
}
return out, nil
}
func computeAPIServerETCDEncryptionConfig(
ctx context.Context,
runtimeClient client.Client,
runtimeNamespace string,
deploymentName string,
etcdEncryptionKeyRotationPhase gardencorev1beta1.CredentialsRotationPhase,
resourcesToEncrypt []string,
encryptedResources []string,
) (
apiserver.ETCDEncryptionConfig,
error,
) {
config := apiserver.ETCDEncryptionConfig{
RotationPhase: etcdEncryptionKeyRotationPhase,
EncryptWithCurrentKey: true,
ResourcesToEncrypt: resourcesToEncrypt,
EncryptedResources: encryptedResources,
}
if etcdEncryptionKeyRotationPhase == gardencorev1beta1.RotationPreparing {
deployment := &metav1.PartialObjectMetadata{}
deployment.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("Deployment"))
if err := runtimeClient.Get(ctx, kubernetesutils.Key(runtimeNamespace, deploymentName), deployment); err != nil {
if !apierrors.IsNotFound(err) {
return apiserver.ETCDEncryptionConfig{}, err
}
}
// If the new encryption key was not yet populated to all replicas then we should still use the old key for
// encryption of data. Only if all replicas know the new key we can switch and start encrypting with the new/
// current key, see https://kubernetes.io/docs/tasks/administer-cluster/encrypt-data/#rotating-a-decryption-key.
if !metav1.HasAnnotation(deployment.ObjectMeta, secretsrotation.AnnotationKeyNewEncryptionKeyPopulated) {
config.EncryptWithCurrentKey = false
}
}
return config, nil
}
func handleETCDEncryptionKeyRotation(
ctx context.Context,
runtimeClient client.Client,
runtimeNamespace string,
deploymentName string,
apiServer apiserver.Interface,
etcdEncryptionConfig apiserver.ETCDEncryptionConfig,
etcdEncryptionKeyRotationPhase gardencorev1beta1.CredentialsRotationPhase,
) error {
switch etcdEncryptionKeyRotationPhase {
case gardencorev1beta1.RotationPreparing:
if !etcdEncryptionConfig.EncryptWithCurrentKey {
if err := apiServer.Wait(ctx); err != nil {
return err
}
// If we have hit this point then we have deployed API server successfully with the configuration option to
// still use the old key for the encryption of ETCD data. Now we can mark this step as "completed" (via an
// annotation) and redeploy it with the option to use the current/new key for encryption, see
// https://kubernetes.io/docs/tasks/administer-cluster/encrypt-data/#rotating-a-decryption-key for details.
if err := secretsrotation.PatchAPIServerDeploymentMeta(ctx, runtimeClient, runtimeNamespace, deploymentName, func(meta *metav1.PartialObjectMetadata) {
metav1.SetMetaDataAnnotation(&meta.ObjectMeta, secretsrotation.AnnotationKeyNewEncryptionKeyPopulated, "true")
}); err != nil {
return err
}
etcdEncryptionConfig.EncryptWithCurrentKey = true
apiServer.SetETCDEncryptionConfig(etcdEncryptionConfig)
if err := apiServer.Deploy(ctx); err != nil {
return err
}
}
case gardencorev1beta1.RotationCompleting:
if err := secretsrotation.PatchAPIServerDeploymentMeta(ctx, runtimeClient, runtimeNamespace, deploymentName, func(meta *metav1.PartialObjectMetadata) {
delete(meta.Annotations, secretsrotation.AnnotationKeyNewEncryptionKeyPopulated)
}); err != nil {
return err
}
}
return nil
}
// GetResourcesForEncryptionFromConfig returns the list of resources requiring encryption from the EncryptionConfig.
func GetResourcesForEncryptionFromConfig(encryptionConfig *gardencorev1beta1.EncryptionConfig) []string {
if encryptionConfig == nil {
return nil
}
return sets.List(sets.New(encryptionConfig.Resources...))
}
// NormalizeResources returns the list of resources after trimming the suffix '.' if present.
// This is needed for core resources which can be specified as '<resource>.' as well.
func NormalizeResources(resources []string) []string {
var out []string
for _, resource := range resources {
out = append(out, strings.TrimSuffix(resource, "."))
}
return out
}