...
1
16
17 package upgrade
18
19 import (
20 "fmt"
21 "os"
22 "strings"
23
24 "github.com/coredns/corefile-migration/migration"
25 "github.com/pkg/errors"
26
27 "k8s.io/apimachinery/pkg/util/sets"
28 clientset "k8s.io/client-go/kubernetes"
29 "k8s.io/klog/v2"
30
31 kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
32 "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
33 "k8s.io/kubernetes/cmd/kubeadm/app/preflight"
34 )
35
36
37 type CoreDNSCheck struct {
38 name string
39 client clientset.Interface
40 f func(clientset.Interface) error
41 }
42
43
44 func (c CoreDNSCheck) Name() string {
45 return c.name
46 }
47
48
49 func (c CoreDNSCheck) Check() (warnings, errors []error) {
50 if err := c.f(c.client); err != nil {
51 return nil, []error{err}
52 }
53 return nil, nil
54 }
55
56
57 func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.Set[string]) error {
58 migrationChecks := []preflight.Checker{
59 &CoreDNSCheck{
60 name: "CoreDNSUnsupportedPlugins",
61 client: client,
62 f: checkUnsupportedPlugins,
63 },
64 &CoreDNSCheck{
65 name: "CoreDNSMigration",
66 client: client,
67 f: checkMigration,
68 },
69 }
70
71 return preflight.RunChecks(migrationChecks, os.Stderr, ignorePreflightErrors)
72 }
73
74
75
76 func checkUnsupportedPlugins(client clientset.Interface) error {
77 klog.V(1).Infoln("validating if there are any unsupported CoreDNS plugins in the Corefile")
78 _, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
79 if err != nil {
80 return err
81 }
82
83 currentInstalledCoreDNSversion = strings.TrimLeft(currentInstalledCoreDNSversion, "v")
84 targetCoreDNSVersion := strings.TrimLeft(kubeadmconstants.CoreDNSVersion, "v")
85 if currentInstalledCoreDNSversion == targetCoreDNSVersion {
86 return nil
87 }
88 unsupportedCoreDNS, err := migration.Unsupported(currentInstalledCoreDNSversion, targetCoreDNSVersion, corefile)
89 if err != nil {
90 return err
91 }
92 if len(unsupportedCoreDNS) != 0 {
93 var UnsupportedPlugins []string
94 for _, unsup := range unsupportedCoreDNS {
95 UnsupportedPlugins = append(UnsupportedPlugins, unsup.ToString())
96 }
97 fmt.Println("[preflight] The corefile contains plugins that kubeadm/CoreDNS does not know how to migrate. " +
98 "Each plugin listed should be manually verified for compatibility with the newer version of CoreDNS. " +
99 "Once ready, the upgrade can be initiated by skipping the preflight check. During the upgrade, " +
100 "kubeadm will migrate the configuration while leaving the listed plugin configs untouched, " +
101 "but cannot guarantee that they will work with the newer version of CoreDNS.")
102 return errors.Errorf("CoreDNS cannot migrate the following plugins:\n%s", UnsupportedPlugins)
103 }
104 return nil
105 }
106
107
108 func checkMigration(client clientset.Interface) error {
109 klog.V(1).Infoln("validating if migration can be done for the current CoreDNS release.")
110 _, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
111 if err != nil {
112 return err
113 }
114
115 currentInstalledCoreDNSversion = strings.TrimLeft(currentInstalledCoreDNSversion, "v")
116 _, err = migration.Migrate(currentInstalledCoreDNSversion, strings.TrimLeft(kubeadmconstants.CoreDNSVersion, "v"), corefile, false)
117 return errors.Wrap(err, "CoreDNS will not be upgraded")
118 }
119
View as plain text