Skip to content

Commit 1428571

Browse files
committed
Add endpoint that returns the EC charts from the data dir
1 parent 8528206 commit 1428571

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

pkg/handlers/embedded_cluster_artifacts.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,99 @@ func (h *Handler) GetEmbeddedClusterBinary(w http.ResponseWriter, r *http.Reques
112112
}
113113
}
114114

115+
// GetEmbeddedClusterCharts returns the charts in the <data-dir>/charts directory as a .tgz file
116+
func (h *Handler) GetEmbeddedClusterCharts(w http.ResponseWriter, r *http.Request) {
117+
if !util.IsEmbeddedCluster() {
118+
logger.Error(errors.New("not an embedded cluster"))
119+
w.WriteHeader(http.StatusBadRequest)
120+
return
121+
}
122+
123+
// Get data directory path from env var
124+
dataDir := util.EmbeddedClusterDataDir()
125+
if dataDir == "" {
126+
logger.Error(errors.New("environment variable EMBEDDED_CLUSTER_DATA_DIR not set"))
127+
w.WriteHeader(http.StatusInternalServerError)
128+
return
129+
}
130+
131+
// Path to charts directory
132+
chartsDir := filepath.Join(dataDir, "charts")
133+
134+
// Check if charts directory exists
135+
if _, err := os.Stat(chartsDir); os.IsNotExist(err) {
136+
logger.Error(errors.Wrap(err, "charts directory not found"))
137+
w.WriteHeader(http.StatusNotFound)
138+
return
139+
} else if err != nil {
140+
logger.Error(errors.Wrap(err, "failed to stat charts directory"))
141+
w.WriteHeader(http.StatusInternalServerError)
142+
return
143+
}
144+
145+
// Set response headers for the .tgz file
146+
w.Header().Set("Content-Disposition", "attachment; filename=ec-charts.tgz")
147+
w.Header().Set("Content-Type", "application/gzip")
148+
149+
// Create pgzip writer
150+
gzipWriter := pgzip.NewWriter(w)
151+
defer gzipWriter.Close()
152+
153+
// Create tar writer
154+
tarWriter := tar.NewWriter(gzipWriter)
155+
defer tarWriter.Close()
156+
157+
// Walk through the charts directory and add each file to the tar archive
158+
err := filepath.Walk(chartsDir, func(path string, info os.FileInfo, err error) error {
159+
if err != nil {
160+
return errors.Wrap(err, "error walking charts directory")
161+
}
162+
163+
// Skip directories
164+
if info.IsDir() {
165+
return nil
166+
}
167+
168+
// Get relative path for the tar header
169+
relPath, err := filepath.Rel(chartsDir, path)
170+
if err != nil {
171+
return errors.Wrap(err, "failed to get relative path")
172+
}
173+
174+
// Create header
175+
header := &tar.Header{
176+
Name: relPath,
177+
Mode: int64(info.Mode()),
178+
Size: info.Size(),
179+
ModTime: info.ModTime(),
180+
}
181+
182+
// Write header
183+
if err := tarWriter.WriteHeader(header); err != nil {
184+
return errors.Wrap(err, "failed to write tar header")
185+
}
186+
187+
// Open file
188+
file, err := os.Open(path)
189+
if err != nil {
190+
return errors.Wrap(err, "failed to open file")
191+
}
192+
defer file.Close()
193+
194+
// Copy file contents to tar archive
195+
if _, err := io.Copy(tarWriter, file); err != nil {
196+
return errors.Wrap(err, "failed to write file to tar archive")
197+
}
198+
199+
return nil
200+
})
201+
202+
if err != nil {
203+
logger.Error(errors.Wrap(err, "failed to create tar archive of charts directory"))
204+
return
205+
}
206+
}
207+
115208
// GetEmbeddedClusterK0sImages returns the k0s images file
116209
func (h *Handler) GetEmbeddedClusterK0sImages(w http.ResponseWriter, r *http.Request) {
117210
if !util.IsEmbeddedCluster() {

pkg/handlers/handlers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ func RegisterUnauthenticatedRoutes(handler *Handler, kotsStore store.Store, debu
391391
// This endpoint returns the embedded cluster binary as a .tgz file
392392
loggingRouter.Path("/api/v1/embedded-cluster/binary").Methods("GET").HandlerFunc(handler.GetEmbeddedClusterBinary)
393393

394+
// This endpoint returns the embedded cluster charts directory as a .tgz file
395+
loggingRouter.Path("/api/v1/embedded-cluster/charts").Methods("GET").HandlerFunc(handler.GetEmbeddedClusterCharts)
396+
394397
// This endpoint returns the embedded cluster k0s images file
395398
loggingRouter.Path("/api/v1/embedded-cluster/k0s-images").Methods("GET").HandlerFunc(handler.GetEmbeddedClusterK0sImages)
396399
}

0 commit comments

Comments
 (0)