mirror of https://github.com/k3s-io/k3s
Consolidate CopyFile functions (#8079)
* Consolidate CopyFile function Signed-off-by: Derek Nola <derek.nola@suse.com> * Copy to File, not destination folder Signed-off-by: Derek Nola <derek.nola@suse.com> --------- Signed-off-by: Derek Nola <derek.nola@suse.com>pull/8092/head
parent
468bddb59c
commit
46cbbab263
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue