-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aleksandr Zimin <[email protected]> Signed-off-by: Alexandr Ohrimenko <[email protected]> Co-authored-by: Alexandr Ohrimenko <[email protected]>
- Loading branch information
1 parent
54470f4
commit 4aa82a4
Showing
7 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
images/csi-nfs/patches/0001-add-functionality-of-the-tar-utility.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
From f514b56bbe619c7d85db12483182620fb6349819 Mon Sep 17 00:00:00 2001 | ||
From: Alexandr Ohrimenko <[email protected]> | ||
Date: Mon, 20 May 2024 12:25:31 +0300 | ||
Subject: [PATCH] add functionality of the tar utility | ||
|
||
Signed-off-by: Alexandr Ohrimenko <[email protected]> | ||
--- | ||
pkg/nfs/controllerserver.go | 10 ++- | ||
pkg/nfs/func_tar.go | 157 ++++++++++++++++++++++++++++++++++++ | ||
2 files changed, 163 insertions(+), 4 deletions(-) | ||
create mode 100644 pkg/nfs/func_tar.go | ||
|
||
diff --git a/pkg/nfs/controllerserver.go b/pkg/nfs/controllerserver.go | ||
index 7fdad5dd..726df875 100644 | ||
--- a/pkg/nfs/controllerserver.go | ||
+++ b/pkg/nfs/controllerserver.go | ||
@@ -360,9 +360,10 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS | ||
srcPath := getInternalVolumePath(cs.Driver.workingMountDir, srcVol) | ||
dstPath := filepath.Join(snapInternalVolPath, snapshot.archiveName()) | ||
klog.V(2).Infof("archiving %v -> %v", srcPath, dstPath) | ||
- out, err := exec.Command("tar", "-C", srcPath, "-czvf", dstPath, ".").CombinedOutput() | ||
+ //out, err := exec.Command("tar", "-C", srcPath, "-czvf", dstPath, ".").CombinedOutput() | ||
+ err = tarPack(dstPath, srcPath, true) | ||
if err != nil { | ||
- return nil, status.Errorf(codes.Internal, "failed to create archive for snapshot: %v: %v", err, string(out)) | ||
+ return nil, status.Errorf(codes.Internal, "failed to create archive for snapshot: %v", err) | ||
} | ||
klog.V(2).Infof("archived %s -> %s", srcPath, dstPath) | ||
|
||
@@ -515,9 +516,10 @@ func (cs *ControllerServer) copyFromSnapshot(ctx context.Context, req *csi.Creat | ||
snapPath := filepath.Join(getInternalVolumePath(cs.Driver.workingMountDir, snapVol), snap.archiveName()) | ||
dstPath := getInternalVolumePath(cs.Driver.workingMountDir, dstVol) | ||
klog.V(2).Infof("copy volume from snapshot %v -> %v", snapPath, dstPath) | ||
- out, err := exec.Command("tar", "-xzvf", snapPath, "-C", dstPath).CombinedOutput() | ||
+ //out, err := exec.Command("tar", "-xzvf", snapPath, "-C", dstPath).CombinedOutput() | ||
+ err = tarUnPack(snapPath, dstPath, true) | ||
if err != nil { | ||
- return status.Errorf(codes.Internal, "failed to copy volume for snapshot: %v: %v", err, string(out)) | ||
+ return status.Errorf(codes.Internal, "failed to copy volume for snapshot: %v", err) | ||
} | ||
klog.V(2).Infof("volume copied from snapshot %v -> %v", snapPath, dstPath) | ||
return nil | ||
diff --git a/pkg/nfs/func_tar.go b/pkg/nfs/func_tar.go | ||
new file mode 100644 | ||
index 00000000..0131b564 | ||
--- /dev/null | ||
+++ b/pkg/nfs/func_tar.go | ||
@@ -0,0 +1,157 @@ | ||
+package nfs | ||
+ | ||
+import ( | ||
+ "archive/tar" | ||
+ "compress/gzip" | ||
+ "errors" | ||
+ "fmt" | ||
+ "io" | ||
+ "os" | ||
+ "path/filepath" | ||
+) | ||
+ | ||
+func tarPack(dstPathAndFileName, srcPath string, enableCompression bool) error { | ||
+ tarFile, err := os.Create(dstPathAndFileName) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ defer func() { | ||
+ err = tarFile.Close() | ||
+ }() | ||
+ | ||
+ absDstPathAndFileName, err := filepath.Abs(dstPathAndFileName) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ tarWriter := tar.NewWriter(tarFile) | ||
+ if enableCompression { | ||
+ gzipWriter := gzip.NewWriter(tarFile) | ||
+ defer gzipWriter.Close() | ||
+ tarWriter = tar.NewWriter(gzipWriter) | ||
+ } | ||
+ defer tarWriter.Close() | ||
+ | ||
+ srcPath = filepath.Clean(srcPath) | ||
+ absSrcPath, err := filepath.Abs(srcPath) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ if absSrcPath == absDstPathAndFileName { | ||
+ return errors.New(fmt.Sprintf("tar file %s cannot be the source\n", dstPathAndFileName)) | ||
+ } | ||
+ if absSrcPath == filepath.Dir(absDstPathAndFileName) { | ||
+ return errors.New(fmt.Sprintf("tar file %s cannot be in source directory %s\n", dstPathAndFileName, absSrcPath)) | ||
+ } | ||
+ | ||
+ walker := func(file string, fileInfo os.FileInfo, err error) error { | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ tarHeader, err := tar.FileInfoHeader(fileInfo, fileInfo.Name()) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ relFilePath := file | ||
+ if filepath.IsAbs(srcPath) { | ||
+ relFilePath, err = filepath.Rel(srcPath, file) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ } | ||
+ tarHeader.Name = relFilePath | ||
+ if err = tarWriter.WriteHeader(tarHeader); err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ if fileInfo.Mode().IsDir() { | ||
+ return nil | ||
+ } | ||
+ | ||
+ srcFile, err := os.Open(file) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ defer srcFile.Close() | ||
+ _, err = io.Copy(tarWriter, srcFile) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ return nil | ||
+ } | ||
+ | ||
+ if err = filepath.Walk(srcPath, walker); err != nil { | ||
+ return err | ||
+ } | ||
+ return nil | ||
+} | ||
+ | ||
+func tarUnPack(sourcePathAndFileName, desPath string, enableCompression bool) error { | ||
+ tarFile, err := os.Open(sourcePathAndFileName) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ defer func() { | ||
+ err = tarFile.Close() | ||
+ }() | ||
+ | ||
+ absPath, err := filepath.Abs(desPath) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ tarReader := tar.NewReader(tarFile) | ||
+ if enableCompression { | ||
+ gzipReader, err := gzip.NewReader(tarFile) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ defer gzipReader.Close() | ||
+ tarReader = tar.NewReader(gzipReader) | ||
+ } | ||
+ | ||
+ for { | ||
+ tarHeader, err := tarReader.Next() | ||
+ if err == io.EOF { | ||
+ break | ||
+ } | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ | ||
+ fileInfo := tarHeader.FileInfo() | ||
+ fileName := tarHeader.Name | ||
+ if filepath.IsAbs(fileName) { | ||
+ fileName, err = filepath.Rel("/", fileName) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ } | ||
+ absFileName := filepath.Join(absPath, fileName) | ||
+ | ||
+ if fileInfo.Mode().IsDir() { | ||
+ if err := os.MkdirAll(absFileName, 0755); err != nil { | ||
+ return err | ||
+ } | ||
+ continue | ||
+ } | ||
+ | ||
+ file, err := os.OpenFile(absFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileInfo.Mode().Perm()) | ||
+ if err != nil { | ||
+ return err | ||
+ } | ||
+ n, cpErr := io.Copy(file, tarReader) | ||
+ if closeErr := file.Close(); closeErr != nil { | ||
+ return err | ||
+ } | ||
+ if cpErr != nil { | ||
+ return cpErr | ||
+ } | ||
+ if n != fileInfo.Size() { | ||
+ return fmt.Errorf("unexpected bytes written: wrote %d, want %d", n, fileInfo.Size()) | ||
+ } | ||
+ } | ||
+ return nil | ||
+} | ||
-- | ||
2.39.3 (Apple Git-146) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
{{- $_ := set . "BASE_GOLANG_22_ALPINE" "registry.deckhouse.io/base_images/golang:1.22.1-alpine@sha256:0de6cf7cceab6ecbf0718bdfb675b08b78113c3709c5e4b99456cdb2ae8c2495" }} | ||
{{- $_ := set . "BASE_SCRATCH" "registry.deckhouse.io/base_images/scratch@sha256:b054705fcc9f2205777d80a558d920c0b4209efdc3163c22b5bfcb5dda1db5fc" }} | ||
|
||
|
||
|
||
--- | ||
image: webhooks | ||
from: "registry.deckhouse.io/base_images/golang:1.22.1-alpine@sha256:0de6cf7cceab6ecbf0718bdfb675b08b78113c3709c5e4b99456cdb2ae8c2495" | ||
image: {{ $.ImageName }}-golang-artifact | ||
from: {{ $.BASE_GOLANG_22_ALPINE }} | ||
|
||
git: | ||
- add: /images/webhooks/src | ||
to: /src | ||
stageDependencies: | ||
setup: | ||
- "**/*" | ||
|
||
mount: | ||
- fromPath: ~/go-pkg-cache | ||
to: /go/pkg | ||
shell: | ||
setup: | ||
- cd /src | ||
- GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" -o webhooks | ||
- mv webhooks /webhooks | ||
--- | ||
image: {{ $.ImageName }} | ||
from: {{ $.BASE_SCRATCH }} | ||
|
||
import: | ||
- image: {{ $.ImageName }}-golang-artifact | ||
add: /webhooks | ||
to: /webhooks | ||
before: setup | ||
docker: | ||
ENTRYPOINT: ["/webhooks"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters