...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade/preflight.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  // CoreDNSCheck validates installed kubelet version
    37  type CoreDNSCheck struct {
    38  	name   string
    39  	client clientset.Interface
    40  	f      func(clientset.Interface) error
    41  }
    42  
    43  // Name is part of the preflight.Checker interface
    44  func (c CoreDNSCheck) Name() string {
    45  	return c.name
    46  }
    47  
    48  // Check is part of the preflight.Checker interface
    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  // RunCoreDNSMigrationCheck initializes checks related to CoreDNS migration.
    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  // checkUnsupportedPlugins checks if there are any plugins included in the current configuration
    75  // that are unsupported for migration.
    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  // checkMigration validates if migration of the current CoreDNS ConfigMap is possible.
   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