/
workload_cluster_coredns.go
330 lines (291 loc) · 11.3 KB
/
workload_cluster_coredns.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
322
323
324
325
326
327
328
329
330
/*
Copyright 2020 The Kubernetes Authors.
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 internal
import (
"context"
"fmt"
"github.com/coredns/corefile-migration/migration"
"github.com/pkg/errors"
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"
kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
"sigs.k8s.io/cluster-api/util"
containerutil "sigs.k8s.io/cluster-api/util/container"
"sigs.k8s.io/cluster-api/util/patch"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
corefileKey = "Corefile"
corefileBackupKey = "Corefile-backup"
coreDNSKey = "coredns"
coreDNSVolumeKey = "config-volume"
)
type coreDNSMigrator interface {
Migrate(currentVersion string, toVersion string, corefile string, deprecations bool) (string, error)
}
type CoreDNSMigrator struct{}
func (c *CoreDNSMigrator) Migrate(fromCoreDNSVersion, toCoreDNSVersion, corefile string, deprecations bool) (string, error) {
return migration.Migrate(fromCoreDNSVersion, toCoreDNSVersion, corefile, deprecations)
}
type coreDNSInfo struct {
Corefile string
Deployment *appsv1.Deployment
FromImageTag string
ToImageTag string
CurrentMajorMinorPatch string
TargetMajorMinorPatch string
FromImage string
ToImage string
}
// UpdateCoreDNS updates the kubeadm configmap, coredns corefile and coredns
// deployment.
func (w *Workload) UpdateCoreDNS(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane) error {
// Return early if we've been asked to skip CoreDNS upgrades entirely.
if _, ok := kcp.Annotations[controlplanev1.SkipCoreDNSAnnotation]; ok {
return nil
}
// Return early if the configuration is nil.
if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration == nil {
return nil
}
clusterConfig := kcp.Spec.KubeadmConfigSpec.ClusterConfiguration
// Return early if the type is anything other than empty (default), or CoreDNS.
if clusterConfig.DNS.Type != "" && clusterConfig.DNS.Type != kubeadmv1.CoreDNS {
return nil
}
// Get the CoreDNS info needed for the upgrade.
info, err := w.getCoreDNSInfo(ctx, clusterConfig)
if err != nil {
// Return early if we get a not found error, this can happen if any of the CoreDNS components
// cannot be found, e.g. configmap, deployment.
if apierrors.IsNotFound(errors.Cause(err)) {
return nil
}
return err
}
// Return early if the from/to image is the same.
if info.FromImage == info.ToImage {
return nil
}
// Validate the image tag.
if err := validateCoreDNSImageTag(info.FromImageTag, info.ToImageTag); err != nil {
return errors.Wrapf(err, "failed to validate CoreDNS")
}
// Perform the upgrade.
if err := w.updateCoreDNSImageInfoInKubeadmConfigMap(ctx, &clusterConfig.DNS); err != nil {
return err
}
if err := w.updateCoreDNSCorefile(ctx, info); err != nil {
return err
}
if err := w.updateCoreDNSDeployment(ctx, info); err != nil {
return errors.Wrap(err, "unable to update coredns deployment")
}
return nil
}
// getCoreDNSInfo returns all necessary coredns based information.
func (w *Workload) getCoreDNSInfo(ctx context.Context, clusterConfig *kubeadmv1.ClusterConfiguration) (*coreDNSInfo, error) {
// Get the coredns configmap and corefile.
key := ctrlclient.ObjectKey{Name: coreDNSKey, Namespace: metav1.NamespaceSystem}
cm, err := w.getConfigMap(ctx, key)
if err != nil {
return nil, errors.Wrapf(err, "error getting %v config map from target cluster", key)
}
corefile, ok := cm.Data[corefileKey]
if !ok {
return nil, errors.New("unable to find the CoreDNS Corefile data")
}
// Get the current CoreDNS deployment.
deployment := &appsv1.Deployment{}
if err := w.Client.Get(ctx, key, deployment); err != nil {
return nil, errors.Wrapf(err, "unable to get %v deployment from target cluster", key)
}
var container *corev1.Container
for _, c := range deployment.Spec.Template.Spec.Containers {
if c.Name == coreDNSKey {
container = c.DeepCopy()
break
}
}
if container == nil {
return nil, errors.Errorf("failed to update coredns deployment: deployment spec has no %q container", coreDNSKey)
}
// Parse container image.
parsedImage, err := containerutil.ImageFromString(container.Image)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse %q deployment image", container.Image)
}
// Handle imageRepository.
toImageRepository := fmt.Sprintf("%s/%s", parsedImage.Repository, parsedImage.Name)
if clusterConfig.ImageRepository != "" {
toImageRepository = fmt.Sprintf("%s/%s", clusterConfig.ImageRepository, coreDNSKey)
}
if clusterConfig.DNS.ImageRepository != "" {
toImageRepository = fmt.Sprintf("%s/%s", clusterConfig.DNS.ImageRepository, coreDNSKey)
}
// Handle imageTag.
if parsedImage.Tag == "" {
return nil, errors.Errorf("failed to update coredns deployment: does not have a valid image tag: %q", container.Image)
}
currentMajorMinorPatch, err := extractImageVersion(parsedImage.Tag)
if err != nil {
return nil, err
}
toImageTag := parsedImage.Tag
if clusterConfig.DNS.ImageTag != "" {
toImageTag = clusterConfig.DNS.ImageTag
}
targetMajorMinorPatch, err := extractImageVersion(toImageTag)
if err != nil {
return nil, err
}
return &coreDNSInfo{
Corefile: corefile,
Deployment: deployment,
CurrentMajorMinorPatch: currentMajorMinorPatch,
TargetMajorMinorPatch: targetMajorMinorPatch,
FromImageTag: parsedImage.Tag,
ToImageTag: toImageTag,
FromImage: container.Image,
ToImage: fmt.Sprintf("%s:%s", toImageRepository, toImageTag),
}, nil
}
// UpdateCoreDNSDeployment will patch the deployment image to the
// imageRepo:imageTag in the KCP dns. It will also ensure the volume of the
// deployment uses the Corefile key of the coredns configmap.
func (w *Workload) updateCoreDNSDeployment(ctx context.Context, info *coreDNSInfo) error {
helper, err := patch.NewHelper(info.Deployment, w.Client)
if err != nil {
return err
}
// Form the final image before issuing the patch.
patchCoreDNSDeploymentImage(info.Deployment, info.ToImage)
// Flip the deployment volume back to Corefile (from the backup key).
patchCoreDNSDeploymentVolume(info.Deployment, corefileBackupKey, corefileKey)
return helper.Patch(ctx, info.Deployment)
}
// UpdateCoreDNSImageInfoInKubeadmConfigMap updates the kubernetes version in the kubeadm config map.
func (w *Workload) updateCoreDNSImageInfoInKubeadmConfigMap(ctx context.Context, dns *kubeadmv1.DNS) error {
configMapKey := ctrlclient.ObjectKey{Name: kubeadmConfigKey, Namespace: metav1.NamespaceSystem}
kubeadmConfigMap, err := w.getConfigMap(ctx, configMapKey)
if err != nil {
return err
}
config := &kubeadmConfig{ConfigMap: kubeadmConfigMap}
if err := config.UpdateCoreDNSImageInfo(dns.ImageRepository, dns.ImageTag); err != nil {
return err
}
if err := w.Client.Update(ctx, config.ConfigMap); err != nil {
return errors.Wrap(err, "error updating kubeadm ConfigMap")
}
return nil
}
// updateCoreDNSCorefile migrates the coredns corefile if there is an increase
// in version number. It also creates a corefile backup and patches the
// deployment to point to the backup corefile before migrating.
func (w *Workload) updateCoreDNSCorefile(ctx context.Context, info *coreDNSInfo) error {
// Run the CoreDNS migration tool first because if it cannot migrate the
// corefile, then there's no point in continuing further.
updatedCorefile, err := w.CoreDNSMigrator.Migrate(info.CurrentMajorMinorPatch, info.TargetMajorMinorPatch, info.Corefile, false)
if err != nil {
return errors.Wrap(err, "unable to migrate CoreDNS corefile")
}
// First we backup the Corefile by backing it up.
if err := w.Client.Update(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: coreDNSKey,
Namespace: metav1.NamespaceSystem,
},
Data: map[string]string{
corefileKey: info.Corefile,
corefileBackupKey: info.Corefile,
},
}); err != nil {
return errors.Wrap(err, "unable to update CoreDNS config map with backup Corefile")
}
// Patching the coredns deployment to point to the Corefile-backup
// contents before performing the migration.
helper, err := patch.NewHelper(info.Deployment, w.Client)
if err != nil {
return err
}
patchCoreDNSDeploymentVolume(info.Deployment, corefileKey, corefileBackupKey)
if err := helper.Patch(ctx, info.Deployment); err != nil {
return err
}
if err := w.Client.Update(ctx, &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: coreDNSKey,
Namespace: metav1.NamespaceSystem,
},
Data: map[string]string{
corefileKey: updatedCorefile,
corefileBackupKey: info.Corefile,
},
}); err != nil {
return errors.Wrap(err, "unable to update CoreDNS config map")
}
return nil
}
func patchCoreDNSDeploymentVolume(deployment *appsv1.Deployment, fromKey, toKey string) {
for _, volume := range deployment.Spec.Template.Spec.Volumes {
if volume.Name == coreDNSVolumeKey && volume.ConfigMap != nil && volume.ConfigMap.Name == coreDNSKey {
for i, item := range volume.ConfigMap.Items {
if item.Key == fromKey || item.Key == toKey {
volume.ConfigMap.Items[i].Key = toKey
}
}
}
}
}
func patchCoreDNSDeploymentImage(deployment *appsv1.Deployment, image string) {
containers := deployment.Spec.Template.Spec.Containers
for idx, c := range containers {
if c.Name == coreDNSKey {
containers[idx].Image = image
}
}
}
func extractImageVersion(tag string) (string, error) {
ver, err := util.ParseMajorMinorPatch(tag)
if err != nil {
return "", err
}
return fmt.Sprintf("%d.%d.%d", ver.Major, ver.Minor, ver.Patch), nil
}
// validateCoreDNSImageTag returns error if the versions don't meet requirements.
// Some of the checks come from
// https://github.com/coredns/corefile-migration/blob/v1.0.6/migration/migrate.go#L414
func validateCoreDNSImageTag(fromTag, toTag string) error {
from, err := util.ParseMajorMinorPatch(fromTag)
if err != nil {
return errors.Wrapf(err, "failed to parse CoreDNS current version %q", fromTag)
}
to, err := util.ParseMajorMinorPatch(toTag)
if err != nil {
return errors.Wrapf(err, "failed to parse CoreDNS target version %q", toTag)
}
// make sure that the version we're upgrading to is greater than the current one,
// or if they're the same version, the raw tags should be different (e.g. allow from `v1.17.4-somevendor.0` to `v1.17.4-somevendor.1`).
if x := from.Compare(to); x > 0 || (x == 0 && fromTag == toTag) {
return fmt.Errorf("toVersion %q must be greater than fromVersion %q", toTag, fromTag)
}
// check if the from version is even in the list of coredns versions
if _, ok := migration.Versions[fmt.Sprintf("%d.%d.%d", from.Major, from.Minor, from.Patch)]; !ok {
return fmt.Errorf("fromVersion %q is not a compatible coredns version", from.String())
}
return nil
}