From 46cbbab263ecbe92778ad8dca3c77c63c26711e0 Mon Sep 17 00:00:00 2001 From: Derek Nola Date: Tue, 1 Aug 2023 08:55:34 -0700 Subject: [PATCH] Consolidate CopyFile functions (#8079) * Consolidate CopyFile function Signed-off-by: Derek Nola * Copy to File, not destination folder Signed-off-by: Derek Nola --------- Signed-off-by: Derek Nola --- pkg/agent/flannel/setup.go | 2 +- pkg/agent/util/file.go | 6 ++++-- pkg/cli/cert/cert.go | 39 ++++++++++++++------------------------ 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/pkg/agent/flannel/setup.go b/pkg/agent/flannel/setup.go index 95669f8369..2a5db3f4a2 100644 --- a/pkg/agent/flannel/setup.go +++ b/pkg/agent/flannel/setup.go @@ -157,7 +157,7 @@ func createCNIConf(dir string, nodeConfig *config.Node) error { if nodeConfig.AgentConfig.FlannelCniConfFile != "" { logrus.Debugf("Using %s as the flannel CNI conf", nodeConfig.AgentConfig.FlannelCniConfFile) - return util.CopyFile(nodeConfig.AgentConfig.FlannelCniConfFile, p) + return util.CopyFile(nodeConfig.AgentConfig.FlannelCniConfFile, p, false) } return util.WriteFile(p, cniConf) } diff --git a/pkg/agent/util/file.go b/pkg/agent/util/file.go index ae4633e7a0..2420acc8f4 100644 --- a/pkg/agent/util/file.go +++ b/pkg/agent/util/file.go @@ -16,10 +16,12 @@ func WriteFile(name string, content string) error { return nil } -func CopyFile(sourceFile string, destinationFile string) error { +func CopyFile(sourceFile string, destinationFile string, ignoreNotExist bool) error { os.MkdirAll(filepath.Dir(destinationFile), 0755) input, err := os.ReadFile(sourceFile) - if err != nil { + if errors.Is(err, os.ErrNotExist) && ignoreNotExist { + return nil + } else if err != nil { return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile) } err = os.WriteFile(destinationFile, input, 0644) diff --git a/pkg/cli/cert/cert.go b/pkg/cli/cert/cert.go index ebe36fc441..ed9999fe7c 100644 --- a/pkg/cli/cert/cert.go +++ b/pkg/cli/cert/cert.go @@ -9,6 +9,7 @@ import ( "time" "github.com/erikdubbelboer/gspt" + "github.com/k3s-io/k3s/pkg/agent/util" "github.com/k3s-io/k3s/pkg/bootstrap" "github.com/k3s-io/k3s/pkg/cli/cmds" "github.com/k3s-io/k3s/pkg/clientaccess" @@ -210,20 +211,6 @@ func rotate(app *cli.Context, cfg *cmds.Server) error { return nil } -func copyFile(src, destDir string) error { - _, err := os.Stat(src) - if err == nil { - input, err := os.ReadFile(src) - if err != nil { - return err - } - return os.WriteFile(filepath.Join(destDir, filepath.Base(src)), input, 0644) - } else if errors.Is(err, os.ErrNotExist) { - return nil - } - return err -} - func backupCertificates(serverDataDir, agentDataDir string) (string, error) { serverTLSDir := filepath.Join(serverDataDir, "tls") tlsBackupDir := filepath.Join(serverDataDir, "tls-"+strconv.Itoa(int(time.Now().Unix()))) @@ -234,18 +221,20 @@ func backupCertificates(serverDataDir, agentDataDir string) (string, error) { if err := copy.Copy(serverTLSDir, tlsBackupDir); err != nil { return "", err } - agentCerts := []string{ - filepath.Join(agentDataDir, "client-"+version.Program+"-controller.crt"), - filepath.Join(agentDataDir, "client-"+version.Program+"-controller.key"), - filepath.Join(agentDataDir, "client-kubelet.crt"), - filepath.Join(agentDataDir, "client-kubelet.key"), - filepath.Join(agentDataDir, "serving-kubelet.crt"), - filepath.Join(agentDataDir, "serving-kubelet.key"), - filepath.Join(agentDataDir, "client-kube-proxy.crt"), - filepath.Join(agentDataDir, "client-kube-proxy.key"), + certs := []string{ + "client-" + version.Program + "-controller.crt", + "client-" + version.Program + "-controller.key", + "client-kubelet.crt", + "client-kubelet.key", + "serving-kubelet.crt", + "serving-kubelet.key", + "client-kube-proxy.crt", + "client-kube-proxy.key", } - for _, cert := range agentCerts { - if err := copyFile(cert, tlsBackupDir); err != nil { + for _, cert := range certs { + agentCert := filepath.Join(agentDataDir, cert) + tlsBackupCert := filepath.Join(tlsBackupDir, cert) + if err := util.CopyFile(agentCert, tlsBackupCert, true); err != nil { return "", err } }