Merge pull request #117861 from SataQiu/fix-20230508

kubeadm: fix a bug where file copy(backup) could not be executed correctly on Windows platform during upgrade
This commit is contained in:
Kubernetes Prow Robot 2023-05-08 20:06:52 -07:00 committed by GitHub
commit b32941f60d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 27 deletions

View File

@ -27,11 +27,11 @@ import (
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
)
@ -219,7 +219,7 @@ func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {
// If CA Cert existed while dryrun, copy CA Cert to dryrun dir for later use
if data.DryRun() {
err := phases.CopyFile(filepath.Join(data.CertificateDir(), kubeadmconstants.CACertName), filepath.Join(data.CertificateWriteDir(), kubeadmconstants.CACertName))
err := kubeadmutil.CopyFile(filepath.Join(data.CertificateDir(), kubeadmconstants.CACertName), filepath.Join(data.CertificateWriteDir(), kubeadmconstants.CACertName))
if err != nil {
return errors.Wrapf(err, "could not copy %s to dry run directory %s", kubeadmconstants.CACertName, data.CertificateWriteDir())
}
@ -227,7 +227,7 @@ func runCAPhase(ca *certsphase.KubeadmCert) func(c workflow.RunData) error {
if _, err := pkiutil.TryLoadKeyFromDisk(data.CertificateDir(), ca.BaseName); err == nil {
// If CA Key existed while dryrun, copy CA Key to dryrun dir for later use
if data.DryRun() {
err := phases.CopyFile(filepath.Join(data.CertificateDir(), kubeadmconstants.CAKeyName), filepath.Join(data.CertificateWriteDir(), kubeadmconstants.CAKeyName))
err := kubeadmutil.CopyFile(filepath.Join(data.CertificateDir(), kubeadmconstants.CAKeyName), filepath.Join(data.CertificateWriteDir(), kubeadmconstants.CAKeyName))
if err != nil {
return errors.Wrapf(err, "could not copy %s to dry run directory %s", kubeadmconstants.CAKeyName, data.CertificateWriteDir())
}

View File

@ -23,11 +23,11 @@ import (
"github.com/pkg/errors"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
kubeconfigphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubeconfig"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
)
var (
@ -138,7 +138,7 @@ func runKubeConfigFile(kubeConfigFileName string) func(workflow.RunData) error {
fmt.Printf("[kubeconfig] External CA mode: Using user provided %s\n", kubeConfigFileName)
// If using an external CA while dryrun, copy kubeconfig files to dryrun dir for later use
if data.DryRun() {
err := phases.CopyFile(filepath.Join(kubeadmconstants.KubernetesDir, kubeConfigFileName), filepath.Join(data.KubeConfigDir(), kubeConfigFileName))
err := kubeadmutil.CopyFile(filepath.Join(kubeadmconstants.KubernetesDir, kubeConfigFileName), filepath.Join(data.KubeConfigDir(), kubeConfigFileName))
if err != nil {
return errors.Wrapf(err, "could not copy %s to dry run directory %s", kubeConfigFileName, data.KubeConfigDir())
}

View File

@ -17,8 +17,6 @@ limitations under the License.
package phases
import (
"os"
"k8s.io/component-base/version"
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
@ -33,17 +31,3 @@ func SetKubernetesVersion(cfg *kubeadmapiv1.ClusterConfiguration) {
}
cfg.KubernetesVersion = version.Get().String()
}
// CopyFile copy file from src to dest.
func CopyFile(src, dest string) error {
fileInfo, err := os.Stat(src)
if err != nil {
return err
}
contents, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dest, contents, fileInfo.Mode())
return err
}

View File

@ -265,14 +265,10 @@ func WriteKubeletConfigFiles(cfg *kubeadmapi.InitConfiguration, patchesDir strin
dest := filepath.Join(backupDir, kubeadmconstants.KubeletConfigurationFileName)
if !dryRun {
// call `cp` instead of `rename` here since the kubelet config file and back up directory (/etc/kubernetes/tmp/)
// might on the filesystem with different mount points in the test environment, such as kinder.
// This will lead to a failure to move the file from the source to dest since `rename` normally doesn't work
// across different mount points on most Unix system.
fmt.Printf("[upgrade] Backing up kubelet config file to %s\n", dest)
output, err := kubeadmutil.CopyDir(src, dest)
err := kubeadmutil.CopyFile(src, dest)
if err != nil {
return errors.Wrapf(err, "error backing up the kubelet config file, output: %q", output)
return errors.Wrap(err, "error backing up the kubelet config file")
}
} else {
fmt.Printf("[dryrun] Would back up kubelet config file to %s\n", dest)

View File

@ -0,0 +1,35 @@
/*
Copyright 2023 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 util
import (
"os"
)
// CopyFile copies a file from src to dest.
func CopyFile(src, dest string) error {
fileInfo, err := os.Stat(src)
if err != nil {
return err
}
contents, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dest, contents, fileInfo.Mode())
return err
}