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

Move ReplaceFile function to util package from driver package #335

Merged
merged 1 commit into from
Jan 14, 2025
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
4 changes: 2 additions & 2 deletions cmd/install-mp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"path/filepath"

"github.com/awslabs/aws-s3-csi-driver/pkg/driver"
"github.com/awslabs/aws-s3-csi-driver/pkg/util"
)

const (
Expand Down Expand Up @@ -49,7 +49,7 @@ func installFiles(binDir string, installDir string) error {
destFile := filepath.Join(installDir, name)

// First copy to a temporary location then rename to handle replacing running binaries
err = driver.ReplaceFile(destFile, filepath.Join(binDir, name), 0755)
err = util.ReplaceFile(destFile, filepath.Join(binDir, name), 0755)
if err != nil {
return fmt.Errorf("Failed to copy file %s: %w", name, err)
}
Expand Down
36 changes: 2 additions & 34 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package driver
import (
"context"
"fmt"
"io"
"io/fs"
"net"
"os"
"time"

"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/mounter"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/version"
"github.com/awslabs/aws-s3-csi-driver/pkg/util"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -154,7 +153,7 @@ func (d *Driver) Stop() {
func tokenFileTender(ctx context.Context, sourcePath string, destPath string) {
for {
timer := time.After(10 * time.Second)
err := ReplaceFile(destPath, sourcePath, 0600)
err := util.ReplaceFile(destPath, sourcePath, 0600)
if err != nil {
klog.Infof("Failed to sync AWS web token file: %v", err)
}
Expand All @@ -167,37 +166,6 @@ func tokenFileTender(ctx context.Context, sourcePath string, destPath string) {
}
}

// replaceFile safely replaces a file with a new file by copying to a temporary location first
// then renaming.
func ReplaceFile(destPath string, sourcePath string, perm fs.FileMode) error {
tmpDest := destPath + ".tmp"

sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.OpenFile(tmpDest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
defer destFile.Close()

buf := make([]byte, 64*1024)
_, err = io.CopyBuffer(destFile, sourceFile, buf)
if err != nil {
return err
}

err = os.Rename(tmpDest, destPath)
if err != nil {
return fmt.Errorf("Failed to rename file %s: %w", destPath, err)
}

return nil
}

func kubernetesVersion(clientset *kubernetes.Clientset) (string, error) {
version, err := clientset.ServerVersion()
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package util

import (
"fmt"
"io"
"io/fs"
"os"
)

// ReplaceFile safely replaces a file with a new file by copying to a temporary location first
// then renaming.
func ReplaceFile(destPath string, sourcePath string, perm fs.FileMode) error {
tmpDest := destPath + ".tmp"

sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.OpenFile(tmpDest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
defer destFile.Close()

buf := make([]byte, 64*1024)
_, err = io.CopyBuffer(destFile, sourceFile, buf)
if err != nil {
return err
}

err = os.Rename(tmpDest, destPath)
if err != nil {
return fmt.Errorf("Failed to rename file %s: %w", destPath, err)
}

return nil
}
Loading