feat(climc): support download helm chart

This commit is contained in:
Zexi Li
2023-07-20 18:18:29 +08:00
parent e4b2ee1edd
commit 75cfc39fe8
2 changed files with 61 additions and 0 deletions
+34
View File
@@ -15,12 +15,14 @@
package k8s
import (
"io"
"os"
"path/filepath"
"github.com/cheggaaa/pb/v3"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
@@ -64,4 +66,36 @@ func initRepo() {
printObject(ret)
return nil
})
type DownloadOptions struct {
REPO string `help:"The name or id of helm repository`
CHART string `help:"The name of chart"`
Output string `help:"Saved file path"`
Version string `help:"The version of chart"`
}
R(new(DownloadOptions), "k8s-repo-download-chart", "Download helm chart to a file", func(s *mcclient.ClientSession, args *DownloadOptions) error {
chartFileName, src, size, err := k8s.Repos.DownloadChart(s, args.REPO, args.CHART, args.Version)
if err != nil {
return errors.Wrap(err, "download chart")
}
output := args.Output
if output == "" && chartFileName != "" {
output = chartFileName
}
if output == "" {
return errors.Errorf("--output filepath must provide")
}
f, err := os.Create(output)
if err != nil {
return errors.Wrapf(err, "create saved file: %q", args.Output)
}
defer f.Close()
var sink io.Writer = f
bar := pb.Full.Start64(size)
barReader := bar.NewProxyReader(src)
if _, err := io.Copy(sink, barReader); err != nil {
return errors.Wrap(err, "save chart")
}
return nil
})
}
+27
View File
@@ -18,8 +18,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/util/httputils"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -62,3 +65,27 @@ func (m *RepoManager) UploadChart(s *mcclient.ClientSession, id string, params j
}
return json, nil
}
func (m *RepoManager) DownloadChart(s *mcclient.ClientSession, id string, chartName string, chartVersion string) (string, io.Reader, int64, error) {
params := map[string]string{
"chart_name": chartName,
"version": chartVersion,
}
query := jsonutils.Marshal(params)
path := fmt.Sprintf("/%s/%s/download-chart", m.URLPath(), url.PathEscape(id))
queryString := query.QueryString()
if len(queryString) > 0 {
path = fmt.Sprintf("%s?%s", path, queryString)
}
resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, "GET", path, nil, nil)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
sizeBytes, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
if err != nil {
log.Errorf("Download chart unknown size")
sizeBytes = -1
}
return resp.Header.Get("Chart-Filename"), resp.Body, sizeBytes, nil
}
_, _, err = s.ParseJSONResponse("", resp, err)
return "", nil, -1, err
}