Skip to content

Commit 10e6625

Browse files
committed
use pgzip
1 parent b05002d commit 10e6625

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ require (
250250
github.com/k0sproject/k0s v1.30.10-0.20250117153350-dcf3c22bb568 // indirect
251251
github.com/kevinburke/ssh_config v1.2.0 // indirect
252252
github.com/klauspost/compress v1.17.11 // indirect
253-
github.com/klauspost/pgzip v1.2.6 // indirect
253+
github.com/klauspost/pgzip v1.2.6
254254
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
255255
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
256256
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec // indirect

pkg/handlers/embedded_cluster_binary.go renamed to pkg/handlers/embedded_cluster_artifacts.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package handlers
22

33
import (
4+
"archive/tar"
45
"fmt"
56
"io"
67
"net/http"
78
"os"
89
"path/filepath"
10+
"time"
911

12+
"github.com/klauspost/pgzip"
1013
"github.com/pkg/errors"
1114
"github.com/replicatedhq/kots/pkg/embeddedcluster"
1215
"github.com/replicatedhq/kots/pkg/logger"
@@ -72,10 +75,10 @@ func (h *Handler) GetEmbeddedClusterBinary(w http.ResponseWriter, r *http.Reques
7275
return
7376
}
7477

75-
// Set response headers for binary file
76-
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", binaryName))
77-
w.Header().Set("Content-Type", "application/octet-stream")
78-
w.Header().Set("Content-Length", fmt.Sprintf("%d", binaryStat.Size()))
78+
// Set response headers for the .tgz file
79+
filename := fmt.Sprintf("%s.tgz", binaryName)
80+
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
81+
w.Header().Set("Content-Type", "application/gzip")
7982

8083
// Open binary file
8184
binaryFile, err := os.Open(binaryPath)
@@ -86,9 +89,31 @@ func (h *Handler) GetEmbeddedClusterBinary(w http.ResponseWriter, r *http.Reques
8689
}
8790
defer binaryFile.Close()
8891

89-
// Stream the binary directly to the response
90-
if _, err := io.Copy(w, binaryFile); err != nil {
91-
logger.Error(errors.Wrap(err, "failed to write binary to response"))
92+
// Create pgzip writer
93+
gzipWriter := pgzip.NewWriter(w)
94+
defer gzipWriter.Close()
95+
96+
// Create tar writer
97+
tarWriter := tar.NewWriter(gzipWriter)
98+
defer tarWriter.Close()
99+
100+
// Add binary file to tar archive
101+
header := &tar.Header{
102+
Name: binaryName,
103+
Mode: 0755, // Executable permission
104+
Size: binaryStat.Size(),
105+
ModTime: time.Now(),
106+
Format: tar.FormatGNU,
107+
}
108+
109+
if err := tarWriter.WriteHeader(header); err != nil {
110+
logger.Error(errors.Wrap(err, "failed to write tar header"))
111+
return
112+
}
113+
114+
// Copy binary content to tar archive
115+
if _, err := io.Copy(tarWriter, binaryFile); err != nil {
116+
logger.Error(errors.Wrap(err, "failed to write binary to tar archive"))
92117
return
93118
}
94119
}

0 commit comments

Comments
 (0)