Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeadm: fix a bug where file copy(backup) could not be executed correctly on Windows platform during upgrade #117861

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/kubeadm/app/cmd/phases/init/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -219,15 +219,15 @@ 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())
}
}
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())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/cmd/phases/init/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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())
}
Expand Down
16 changes: 0 additions & 16 deletions cmd/kubeadm/app/cmd/phases/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
8 changes: 2 additions & 6 deletions cmd/kubeadm/app/phases/upgrade/postupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions cmd/kubeadm/app/util/copy.go
Original file line number Diff line number Diff line change
@@ -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
}