move the corefile backup into a single configmap with a corefile-backup data key

This commit is contained in:
Sandeep Rajan 2019-08-13 18:24:57 -04:00
parent 6821d21260
commit 6bb9eeeb1c
7 changed files with 50 additions and 77 deletions

View File

@ -120,7 +120,7 @@ func enforceRequirements(flags *applyPlanFlags, dryRun bool, newK8sVersion strin
// Ensure the user is root
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
}
@ -178,13 +178,13 @@ func printConfiguration(clustercfg *kubeadmapi.ClusterConfiguration, w io.Writer
}
// 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.")
err := preflight.RunRootCheckOnly(ignorePreflightErrors)
if err != nil {
return err
}
err = upgrade.RunCoreDNSMigrationCheck(client, ignorePreflightErrors)
err = upgrade.RunCoreDNSMigrationCheck(client, ignorePreflightErrors, cfg.DNS.Type)
if err != nil {
return err
}

View File

@ -44,6 +44,7 @@ go_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/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/scheme:go_default_library",
"//vendor/github.com/caddyserver/caddy/caddyfile:go_default_library",

View File

@ -32,6 +32,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientset "k8s.io/client-go/kubernetes"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
"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
_, corefile, currentInstalledCoreDNSVersion, err := GetCoreDNSInfo(client)
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 err := migrateCoreDNSConfigMap(client, coreDNSConfigMap, corefile, currentInstalledCoreDNSVersion); err != nil {
if err := migrateCoreDNSCorefile(client, coreDNSConfigMap, corefile, currentInstalledCoreDNSVersion); err != nil {
return err
}
} else {
@ -317,8 +318,27 @@ func IsCoreDNSConfigMapMigrationRequired(corefile string) bool {
return true
}
func migrateCoreDNSConfigMap(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.
func migrateCoreDNSCorefile(client clientset.Interface, cm *v1.ConfigMap, corefile, currentInstalledCoreDNSVersion string) error {
// 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)
if err != nil {
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,
},
Data: map[string]string{
"Corefile": updatedCorefile,
"Corefile-previous": corefile,
"Corefile": updatedCorefile,
"Corefile-backup": corefile,
},
}); err != nil {
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)
if err != nil {
return errors.Wrap(err, "unable to get list of changes to the configuration.")
}
// show the migration changes
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:")
for _, change := range changes {
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
}
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
// in the form of Proxy for the CoreDNS Corefile.
func translateStubDomainOfKubeDNSToForwardCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {

View File

@ -681,7 +681,7 @@ func TestCreateCoreDNSConfigMap(t *testing.T) {
if err != nil {
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 {
t.Fatalf("error creating the CoreDNS ConfigMap: %v", err)
}

View File

@ -5,9 +5,9 @@ go_library(
srcs = [
"compute.go",
"health.go",
"migrate.go",
"policy.go",
"postupgrade.go",
"preflight.go",
"prepull.go",
"staticpods.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/apis/meta/v1: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/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",

View File

@ -17,16 +17,13 @@ limitations under the License.
package upgrade
import (
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
errorsutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/version"
clientset "k8s.io/client-go/kubernetes"
@ -97,17 +94,6 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
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
if err := dns.EnsureDNSAddon(&cfg.ClusterConfiguration, client); err != nil {
errs = append(errs, err)
@ -123,47 +109,6 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
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 {
return apiclient.TryRunCommand(func() error {
installedDeploymentName := kubeadmconstants.KubeDNSDeploymentName

View File

@ -52,12 +52,8 @@ func (c CoreDNSCheck) Check() (warnings, errors []error) {
}
// RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration.
func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String) error {
currentDNSType, _, err := dns.DeployedDNSAddon(client)
if err != nil {
return err
}
if currentDNSType != kubeadmapi.CoreDNS {
func RunCoreDNSMigrationCheck(client clientset.Interface, ignorePreflightErrors sets.String, dnsType kubeadmapi.DNSAddOnType) error {
if dnsType != kubeadmapi.CoreDNS {
return nil
}
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
// that is unsupported for migration.
// that are unsupported for migration.
func checkUnsupportedPlugins(client clientset.Interface) error {
klog.V(1).Infoln("validating if there are any unsupported CoreDNS plugins in the Corefile")
_, corefile, currentInstalledCoreDNSversion, err := dns.GetCoreDNSInfo(client)
@ -114,6 +110,5 @@ func checkMigration(client clientset.Interface) error {
if err != nil {
return err
}
return nil
}