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

Consolidate CopyFile functions #8079

Merged
merged 2 commits into from
Aug 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/agent/flannel/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/agent/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 14 additions & 25 deletions pkg/cli/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())))
Expand All @@ -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
}
}
Expand Down