mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
move the corefile backup into a single configmap with a corefile-backup data key
This commit is contained in:
parent
6821d21260
commit
6bb9eeeb1c
@ -120,7 +120,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
|
|||||||
|
|
||||||
// Ensure the user is root
|
// Ensure the user is root
|
||||||
klog.V(1).Info("running preflight checks")
|
klog.V(1).Info("running preflight checks")
|
||||||
if err := runPreflightChecks(client, ignorePreflightErrorsSet); err != nil {
|
if err := runPreflightChecks(client, ignorePreflightErrorsSet, &cfg.ClusterConfiguration); err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,13 +178,13 @@ func printConfiguration(clustercfg *kubeadmapi.ClusterConfiguration, w io.Writer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// runPreflightChecks runs the root preflight check
|
// runPreflightChecks runs the root preflight check
|
||||||
func runPreflightChecks(client clientset.Interface, ignorePreflightErrors sets.String) error {
|
func runPreflightChecks(client clientset.Interface, ignorePreflightErrors sets.String, cfg *kubeadmapi.ClusterConfiguration) error {
|
||||||
fmt.Println("[preflight] Running pre-flight checks.")
|
fmt.Println("[preflight] Running pre-flight checks.")
|
||||||
err := preflight.RunRootCheckOnly(ignorePreflightErrors)
|
err := preflight.RunRootCheckOnly(ignorePreflightErrors)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = upgrade.RunCoreDNSMigrationCheck(client, ignorePreflightErrors)
|
err = upgrade.RunCoreDNSMigrationCheck(client, ignorePreflightErrors, cfg.DNS.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,7 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
"//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library",
|
||||||
"//vendor/github.com/caddyserver/caddy/caddyfile:go_default_library",
|
"//vendor/github.com/caddyserver/caddy/caddyfile:go_default_library",
|
||||||
|
@ -32,6 +32,7 @@ import (
|
|||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
kuberuntime "k8s.io/apimachinery/pkg/runtime"
|
kuberuntime "k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
|
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
@ -232,10 +233,10 @@ func createCoreDNSAddon(deploymentBytes, serviceBytes, configBytes []byte, clien
|
|||||||
// Create the ConfigMap for CoreDNS or update/migrate it in case it already exists
|
// Create the ConfigMap for CoreDNS or update/migrate it in case it already exists
|
||||||
_, corefile, currentInstalledCoreDNSVersion, err := GetCoreDNSInfo(client)
|
_, corefile, currentInstalledCoreDNSVersion, err := GetCoreDNSInfo(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to fetch CoreDNS current installed version and ConfigMap.")
|
return errors.Wrap(err, "unable to fetch CoreDNS current installed version and ConfigMap.")
|
||||||
}
|
}
|
||||||
if IsCoreDNSConfigMapMigrationRequired(corefile) {
|
if IsCoreDNSConfigMapMigrationRequired(corefile) {
|
||||||
if err := migrateCoreDNSConfigMap(client, coreDNSConfigMap, corefile, currentInstalledCoreDNSVersion); err != nil {
|
if err := migrateCoreDNSCorefile(client, coreDNSConfigMap, corefile, currentInstalledCoreDNSVersion); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -317,8 +318,27 @@ func IsCoreDNSConfigMapMigrationRequired(corefile string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func migrateCoreDNSConfigMap(client clientset.Interface, cm *v1.ConfigMap, corefile, currentInstalledCoreDNSVersion string) error {
|
func migrateCoreDNSCorefile(client clientset.Interface, cm *v1.ConfigMap, corefile, currentInstalledCoreDNSVersion string) error {
|
||||||
// Since the current Configuration present is the not the default version, try and migrate the Config.
|
// Take a copy of the Corefile data as `Corefile-backup` and update the ConfigMap
|
||||||
|
// Also point the CoreDNS deployment to the `Corefile-backup` data.
|
||||||
|
|
||||||
|
if _, err := client.CoreV1().ConfigMaps(cm.ObjectMeta.Namespace).Update(&v1.ConfigMap{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: kubeadmconstants.CoreDNSConfigMap,
|
||||||
|
Namespace: metav1.NamespaceSystem,
|
||||||
|
},
|
||||||
|
Data: map[string]string{
|
||||||
|
"Corefile": corefile,
|
||||||
|
"Corefile-backup": corefile,
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
return errors.Wrap(err, "unable to update the CoreDNS ConfigMap with backup Corefile")
|
||||||
|
}
|
||||||
|
if err := patchCoreDNSDeployment(client, "Corefile-backup"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since the current configuration present is not the default version, try and migrate it.
|
||||||
updatedCorefile, err := migration.Migrate(currentInstalledCoreDNSVersion, kubeadmconstants.CoreDNSVersion, corefile, false)
|
updatedCorefile, err := migration.Migrate(currentInstalledCoreDNSVersion, kubeadmconstants.CoreDNSVersion, corefile, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "unable to migrate CoreDNS ConfigMap")
|
return errors.Wrap(err, "unable to migrate CoreDNS ConfigMap")
|
||||||
@ -330,20 +350,20 @@ func migrateCoreDNSConfigMap(client clientset.Interface, cm *v1.ConfigMap, coref
|
|||||||
Namespace: metav1.NamespaceSystem,
|
Namespace: metav1.NamespaceSystem,
|
||||||
},
|
},
|
||||||
Data: map[string]string{
|
Data: map[string]string{
|
||||||
"Corefile": updatedCorefile,
|
"Corefile": updatedCorefile,
|
||||||
"Corefile-previous": corefile,
|
"Corefile-backup": corefile,
|
||||||
},
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return errors.Wrap(err, "unable to update the CoreDNS ConfigMap")
|
return errors.Wrap(err, "unable to update the CoreDNS ConfigMap")
|
||||||
}
|
}
|
||||||
fmt.Println("Migrating CoreDNS Corefile")
|
fmt.Println("[addons]: Migrating CoreDNS Corefile")
|
||||||
changes, err := migration.Deprecated(currentInstalledCoreDNSVersion, kubeadmconstants.CoreDNSVersion, corefile)
|
changes, err := migration.Deprecated(currentInstalledCoreDNSVersion, kubeadmconstants.CoreDNSVersion, corefile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "unable to get list of changes to the configuration.")
|
return errors.Wrap(err, "unable to get list of changes to the configuration.")
|
||||||
}
|
}
|
||||||
// show the migration changes
|
// show the migration changes
|
||||||
klog.V(2).Infof("the CoreDNS configuration has been migrated and applied: %v.", updatedCorefile)
|
klog.V(2).Infof("the CoreDNS configuration has been migrated and applied: %v.", updatedCorefile)
|
||||||
klog.V(2).Infoln("the old migration has been saved in the CoreDNS ConfigMap under the name [Corefile-previous]")
|
klog.V(2).Infoln("the old migration has been saved in the CoreDNS ConfigMap under the name [Corefile-backup]")
|
||||||
klog.V(2).Infoln("The changes in the new CoreDNS Configuration are as follows:")
|
klog.V(2).Infoln("The changes in the new CoreDNS Configuration are as follows:")
|
||||||
for _, change := range changes {
|
for _, change := range changes {
|
||||||
klog.V(2).Infof("%v", change.ToString())
|
klog.V(2).Infof("%v", change.ToString())
|
||||||
@ -373,6 +393,19 @@ func GetCoreDNSInfo(client clientset.Interface) (*v1.ConfigMap, string, string,
|
|||||||
return coreDNSConfigMap, corefile, currentCoreDNSversion, nil
|
return coreDNSConfigMap, corefile, currentCoreDNSversion, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func patchCoreDNSDeployment(client clientset.Interface, coreDNSCorefileName string) error {
|
||||||
|
dnsDeployment, err := client.AppsV1().Deployments(metav1.NamespaceSystem).Get(kubeadmconstants.CoreDNSDeploymentName, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
patch := fmt.Sprintf(`{"spec":{"template":{"spec":{"volumes":[{"name": "config-volume", "configMap":{"name": "coredns", "items":[{"key": "%s", "path": "%s"}]}}]}}}}`, coreDNSCorefileName, coreDNSCorefileName)
|
||||||
|
|
||||||
|
if _, err := client.AppsV1().Deployments(dnsDeployment.ObjectMeta.Namespace).Patch(dnsDeployment.Name, types.StrategicMergePatchType, []byte(patch)); err != nil {
|
||||||
|
return errors.Wrap(err, "unable to patch the CoreDNS deployment")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// translateStubDomainOfKubeDNSToForwardCoreDNS translates StubDomain Data in kube-dns ConfigMap
|
// translateStubDomainOfKubeDNSToForwardCoreDNS translates StubDomain Data in kube-dns ConfigMap
|
||||||
// in the form of Proxy for the CoreDNS Corefile.
|
// in the form of Proxy for the CoreDNS Corefile.
|
||||||
func translateStubDomainOfKubeDNSToForwardCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {
|
func translateStubDomainOfKubeDNSToForwardCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {
|
||||||
|
@ -681,7 +681,7 @@ func TestCreateCoreDNSConfigMap(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to fetch CoreDNS current installed version and ConfigMap.")
|
t.Fatalf("unable to fetch CoreDNS current installed version and ConfigMap.")
|
||||||
}
|
}
|
||||||
err = migrateCoreDNSConfigMap(client, cm, corefile, currentInstalledCoreDNSVersion)
|
err = migrateCoreDNSCorefile(client, cm, corefile, currentInstalledCoreDNSVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error creating the CoreDNS ConfigMap: %v", err)
|
t.Fatalf("error creating the CoreDNS ConfigMap: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ go_library(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"compute.go",
|
"compute.go",
|
||||||
"health.go",
|
"health.go",
|
||||||
"migrate.go",
|
|
||||||
"policy.go",
|
"policy.go",
|
||||||
"postupgrade.go",
|
"postupgrade.go",
|
||||||
|
"preflight.go",
|
||||||
"prepull.go",
|
"prepull.go",
|
||||||
"staticpods.go",
|
"staticpods.go",
|
||||||
"versiongetter.go",
|
"versiongetter.go",
|
||||||
@ -41,7 +41,6 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
|
||||||
|
@ -17,16 +17,13 @@ limitations under the License.
|
|||||||
package upgrade
|
package upgrade
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
|
||||||
errorsutil "k8s.io/apimachinery/pkg/util/errors"
|
errorsutil "k8s.io/apimachinery/pkg/util/errors"
|
||||||
"k8s.io/apimachinery/pkg/util/version"
|
"k8s.io/apimachinery/pkg/util/version"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
@ -97,17 +94,6 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
|
|||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
coreDNSConfigMap, corefile, _, err := dns.GetCoreDNSInfo(client)
|
|
||||||
if err != nil {
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
|
||||||
isMigrationRequired := dns.IsCoreDNSConfigMapMigrationRequired(corefile)
|
|
||||||
if isMigrationRequired {
|
|
||||||
if err := prepareCoreDNSForCorefileMigration(client, coreDNSConfigMap, corefile); err != nil {
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upgrade kube-dns/CoreDNS and kube-proxy
|
// Upgrade kube-dns/CoreDNS and kube-proxy
|
||||||
if err := dns.EnsureDNSAddon(&cfg.ClusterConfiguration, client); err != nil {
|
if err := dns.EnsureDNSAddon(&cfg.ClusterConfiguration, client); err != nil {
|
||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
@ -123,47 +109,6 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
|
|||||||
return errorsutil.NewAggregate(errs)
|
return errorsutil.NewAggregate(errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareCoreDNSForCorefileMigration(client clientset.Interface, coreDNSConfigMap *v1.ConfigMap, corefile string) error {
|
|
||||||
existingCoreDNSConfigMapName := kubeadmconstants.CoreDNSConfigMap + "-previous"
|
|
||||||
if _, err := client.CoreV1().ConfigMaps(coreDNSConfigMap.ObjectMeta.Namespace).Get(existingCoreDNSConfigMapName, metav1.GetOptions{}); err != nil {
|
|
||||||
if !apierrors.IsNotFound(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := client.CoreV1().ConfigMaps(coreDNSConfigMap.ObjectMeta.Namespace).Delete(existingCoreDNSConfigMapName, nil); err != nil {
|
|
||||||
return errors.Wrap(err, "failed to delete previous CoreDNS ConfigMap")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := client.CoreV1().ConfigMaps(coreDNSConfigMap.ObjectMeta.Namespace).Create(&v1.ConfigMap{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: existingCoreDNSConfigMapName,
|
|
||||||
Namespace: metav1.NamespaceSystem,
|
|
||||||
},
|
|
||||||
Data: map[string]string{
|
|
||||||
"Corefile": corefile,
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
return errors.Wrap(err, "unable to create the migrated CoreDNS ConfigMap")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := patchCoreDNSDeployment(client, existingCoreDNSConfigMapName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func patchCoreDNSDeployment(client clientset.Interface, coreDNSConfigMapName string) error {
|
|
||||||
dnsDeployment, err := client.AppsV1().Deployments(metav1.NamespaceSystem).Get(kubeadmconstants.CoreDNSDeploymentName, metav1.GetOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
patch := fmt.Sprintf(`{"spec":{"template": {"spec":{"volumes":[{"name": "config-volume","configMap":{"name": "%s"}}]}}}}`, coreDNSConfigMapName)
|
|
||||||
if _, err := client.AppsV1().Deployments(dnsDeployment.ObjectMeta.Namespace).Patch(dnsDeployment.Name, types.StrategicMergePatchType, []byte(patch)); err != nil {
|
|
||||||
return errors.Wrap(err, "unable to patch the CoreDNS deployment")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeOldDNSDeploymentIfAnotherDNSIsUsed(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface, dryRun bool) error {
|
func removeOldDNSDeploymentIfAnotherDNSIsUsed(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface, dryRun bool) error {
|
||||||
return apiclient.TryRunCommand(func() error {
|
return apiclient.TryRunCommand(func() error {
|
||||||
installedDeploymentName := kubeadmconstants.KubeDNSDeploymentName
|
installedDeploymentName := kubeadmconstants.KubeDNSDeploymentName
|
||||||
|
@ -52,12 +52,8 @@ func (c CoreDNSCheck) Check() (warnings, errors []error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration.
|
// RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration.
|
||||||
func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String) error {
|
func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String, dnsType kubeadmapi.DNSAddOnType) error {
|
||||||
currentDNSType, _, err := dns.DeployedDNSAddon(client)
|
if dnsType != kubeadmapi.CoreDNS {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if currentDNSType != kubeadmapi.CoreDNS {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
migrationChecks := []preflight.Checker{
|
migrationChecks := []preflight.Checker{
|
||||||
@ -77,7 +73,7 @@ func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkUnsupportedPlugins checks if there are any plugins included in the current configuration
|
// checkUnsupportedPlugins checks if there are any plugins included in the current configuration
|
||||||
// that is unsupported for migration.
|
// that are unsupported for migration.
|
||||||
func checkUnsupportedPlugins(client clientset.Interface) error {
|
func checkUnsupportedPlugins(client clientset.Interface) error {
|
||||||
klog.V(1).Infoln("validating if there are any unsupported CoreDNS plugins in the Corefile")
|
klog.V(1).Infoln("validating if there are any unsupported CoreDNS plugins in the Corefile")
|
||||||
_, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
|
_, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
|
||||||
@ -114,6 +110,5 @@ func checkMigration(client clientset.Interface) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user