/
util.go
291 lines (241 loc) · 10.2 KB
/
util.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
/*
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 controllers
import (
"context"
"net"
"regexp"
"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/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/coredns/corefile-migration/migration"
"github.com/pkg/errors"
)
// getCoreDNSService fetches the CoreDNS Service
func getCoreDNSService(ctx context.Context, c client.Client) (*corev1.Service, error) {
kubernetesService := &corev1.Service{}
id := client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "kubernetes"}
// Get the CoreDNS Service
err := c.Get(ctx, id, kubernetesService)
return kubernetesService, err
}
// getCoreDNSConfigMap fetches the CoreDNS ConfigMap
func getCoreDNSConfigMap(ctx context.Context, c client.Client, configMapName string) (*corev1.ConfigMap, error) {
coreDNSConfigMap := &corev1.ConfigMap{}
id := client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: configMapName}
// Get the CoreDNS ConfigMap
err := c.Get(ctx, id, coreDNSConfigMap)
return coreDNSConfigMap, err
}
// getCoreDNSDeployment fetches the CoreDNS Deployment
func getCoreDNSDeployment(ctx context.Context, c client.Client) (*appsv1.Deployment, error) {
coreDNSDeploy := &appsv1.Deployment{}
deployId := client.ObjectKey{Namespace: metav1.NamespaceSystem, Name: coreDNSName}
// Get the CoreDNS Deployment
err := c.Get(ctx, deployId, coreDNSDeploy)
return coreDNSDeploy, err
}
// findDNSClusterIP tries to find the Cluster IP to be used by the DNS service
// It is usually the 10th address to the Kubernetes Service Cluster IP
// If the Kubernetes Service Cluster IP is not found, we default it to be "10.96.0.10"
func findDNSClusterIP(ctx context.Context, c client.Client) (string, error) {
kubernetesService, err := getCoreDNSService(ctx, c)
if err != nil && !apierrors.IsNotFound(err) {
return "", err
}
if apierrors.IsNotFound(err) {
// If it cannot determine the Cluster IP, we default it to "10.96.0.10"
return coreDNSIP, nil
}
ip := net.ParseIP(kubernetesService.Spec.ClusterIP)
if ip == nil {
return "", errors.Errorf("cannot parse kubernetes ClusterIP %q", kubernetesService.Spec.ClusterIP)
}
// The kubernetes Service ClusterIP is the 1st IP in the Service Subnet.
// Increment the right-most byte by 9 to get to the 10th address, canonically used for CoreDNS.
// This works for both IPV4, IPV6, and 16-byte IPV4 addresses.
ip[len(ip)-1] += 9
result := ip.String()
klog.Infof("determined ClusterIP for CoreDNS should be %q", result)
return result, nil
}
// getDNSDomain returns Kubernetes DNS cluster domain
// If it cannot determine the domain, we default it to "cluster.local"
// TODO (rajansandeep): find a better way to implement this?
func getDNSDomain() string {
svc := "kubernetes.default.svc"
cname, err := net.LookupCNAME(svc)
if err != nil {
// If it cannot determine the domain, we default it to "cluster.local"
klog.Infof("determined DNS Domain for CoreDNS should be %q", coreDNSDomain)
return coreDNSDomain
}
domain := strings.TrimPrefix(cname, svc)
domain = strings.TrimSuffix(coreDNSDomain, ".")
klog.Infof("determined DNS Domain for CoreDNS should be %q", domain)
return domain
}
// getCorefile defines the Corefile to be generated for use in the CoreDNS ConfigMap
// depending on whether this is a fresh install or a change of CoreDNS version. This function
// tries to extract the existing Corefile from the cluster if available.
// If there is none available, we set CoreDNS to configure with the default Corefile, which is defined in each version.
func getCorefile(ctx context.Context, c client.Client) (string, error) {
var err error
// Get the CoreDNS Deployment
coreDNSDeploy, err := getCoreDNSDeployment(ctx, c)
if err != nil && !apierrors.IsNotFound(err) {
return "", err
}
if apierrors.IsNotFound(err) {
// If the CoreDNS Deployment isn't found, it is assumed that it is a new install
// of CoreDNS and we proceed to use the default Corefile
return "", nil
}
// Due to Kustomize, the CoreDNS ConfigMap name is hashed.
// The name of the ConfigMap is extracted from the deployment
var configMapName string
coreDNSDeploySpec := coreDNSDeploy.Spec.Template.Spec
for _, volume := range coreDNSDeploySpec.Volumes {
if volume.Name == "config-volume" && volume.ConfigMap != nil {
configMapName = volume.ConfigMap.Name
}
}
// Get the CoreDNS ConfigMap
coreDNSConfigMap, err := getCoreDNSConfigMap(ctx, c, configMapName)
if err != nil && !apierrors.IsNotFound(err) {
return "", err
}
if apierrors.IsNotFound(err) {
// If the CoreDNS ConfigMap isn't found, use the default Corefile
return "", nil
}
// Get the Corefile
corefile, ok := coreDNSConfigMap.Data["Corefile"]
if !ok {
return "", errors.New("unable to find the CoreDNS Corefile data")
}
return corefile, nil
}
// corefileMigration if necessary/possible, migrates the Corefile to reflect the latest changes, when the CoreDNS version is being upgraded.
func corefileMigration(ctx context.Context, c client.Client, coreDNSVersion, corefile string) (string, error) {
var err error
// Get the CoreDNS Deployment
coreDNSDeploy, err := getCoreDNSDeployment(ctx, c)
if err != nil && !apierrors.IsNotFound(err) {
return "", err
}
// The deployment is used to extract the CoreDNS Image version
var coreDNSImage string
coreDNSDeploySpec := coreDNSDeploy.Spec.Template.Spec
for _, container := range coreDNSDeploySpec.Containers {
if container.Name == coreDNSName {
coreDNSImage = container.Image
}
}
coreDNSImageParts := strings.Split(coreDNSImage, ":")
currentCoreDNSVersion := strings.TrimLeft(coreDNSImageParts[len(coreDNSImageParts)-1], "v")
if currentCoreDNSVersion != coreDNSVersion {
// Check if Corefile Migration is necessary and get the migrated Corefile
// If the Corefile from the previous version is untouched, we can proceed to replace it with the
// Corefile of the current version
klog.Infof("from: %q to: %q", currentCoreDNSVersion, coreDNSVersion)
isDefault := migration.Default("", corefile)
switch isDefault {
case true:
corefile = ""
klog.Infof("the default Corefile will be applied")
case false:
corefile, err = performMigrationOfCorefile(ctx, c, corefile, currentCoreDNSVersion, coreDNSVersion)
if err != nil {
return "", errors.Errorf("unable to migrate the CoreDNS Corefile data: %v", err)
}
klog.Infof("determined Corefile for CoreDNS should be %q", corefile)
}
}
return corefile, nil
}
// performMigrationOfCorefile checks for a possibility or requirement of Migrating the Corefile
// during a change of the CoreDNS Version. Currently, it is NOT recommended to support CoreDNS downgrades,
// but it has full capability of supporting CoreDNS upgrades.
// It first checks, whether the current Corefile is modified or not. If it the default Corefile, it skips migrating.
// In case the Corefile is modified, it goes through the entire Corefile to check for any configuration that can break
// the functionality of CoreDNS or is deprecated and migrates accordingly.
func performMigrationOfCorefile(ctx context.Context, c client.Client, corefile, fromVersion, toVersion string) (string, error) {
if fromVersion == "" || fromVersion == toVersion {
return corefile, nil
}
// Checks if the CoreDNS version is officially supported
isVersionSupported, err := isCoreDNSVersionSupported(ctx, c)
if !isVersionSupported {
klog.Warningf("the CoreDNS Configuration will not be migrated due to unsupported version of CoreDNS. " +
"The existing CoreDNS Corefile configuration and deployment has been retained.")
return corefile, err
}
migratedCorefile, err := migration.Migrate(fromVersion, toVersion, corefile, false)
if err != nil {
klog.Warningf("the CoreDNS Configuration was not migrated: %v. The existing CoreDNS Corefile configuration has been retained.", err)
return corefile, err
}
// show the migration changes
klog.Infof("the CoreDNS configuration has been migrated and applied: %v.", migratedCorefile)
return migratedCorefile, nil
}
var (
// imageDigestMatcher is used to match the SHA256 digest from the ImageID of the CoreDNS pods
imageDigestMatcher = regexp.MustCompile(`^.*(?i:sha256:([[:alnum:]]{64}))$`)
)
func isCoreDNSVersionSupported(ctx context.Context, c client.Client) (bool, error) {
var err error
isValidVersion := true
coreDNSPodList := &corev1.PodList{}
err = c.List(ctx, coreDNSPodList, client.InNamespace(metav1.NamespaceSystem), client.MatchingLabels(map[string]string{"k8s-app": "kube-dns"}))
if err != nil && !apierrors.IsNotFound(err) {
return false, errors.Errorf("error getting CoreDNS Pod list: %v", err)
}
for _, pod := range coreDNSPodList.Items {
imageID := imageDigestMatcher.FindStringSubmatch(pod.Status.ContainerStatuses[0].ImageID)
klog.Info(imageID)
if len(imageID) != 2 {
return false, errors.Errorf("unable to match SHA256 digest ID in %q", pod.Status.ContainerStatuses[0].ImageID)
}
// The actual digest should be at imageID[1]
if !migration.Released(imageID[1]) {
isValidVersion = false
}
}
return isValidVersion, nil
}
// prepCorefileFormat indents the output of the Corefile and replaces tabs with spaces
// to neatly format the configmap, making it readable and ensure there is no error converting YAML to JSON.
func prepCorefileFormat(s string, indentation int) string {
var str []string
if s == "" {
return ""
}
for _, line := range strings.Split(s, "\n") {
indented := strings.Repeat(" ", indentation) + line
str = append(str, indented)
}
corefile := strings.Join(str, "\n")
corefile = strings.TrimSpace(corefile)
return strings.Replace(corefile, "\t", " ", -1)
}
const (
coreDNSDomain = "cluster.local"
coreDNSIP = "10.96.0.10"
coreDNSName = "coredns"
)