helm.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "net/url"
  20. "strconv"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/log"
  23. "yunion.io/x/pkg/util/httputils"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  26. "yunion.io/x/onecloud/pkg/mcclient/modules"
  27. )
  28. var (
  29. Repos *RepoManager
  30. )
  31. func init() {
  32. Repos = NewRepoManager()
  33. modules.Register(Repos)
  34. }
  35. type RepoManager struct {
  36. *ResourceManager
  37. }
  38. func NewRepoManager() *RepoManager {
  39. return &RepoManager{
  40. ResourceManager: NewResourceManager("repo", "repos",
  41. NewResourceCols("Url", "Is_Public", "Source", "Type", "Backend"),
  42. NewColumns()),
  43. }
  44. }
  45. func (m *RepoManager) UploadChart(s *mcclient.ClientSession, id string, params jsonutils.JSONObject, body io.Reader, size int64) (jsonutils.JSONObject, error) {
  46. path := fmt.Sprintf("/%s/%s/upload-chart?%s", m.URLPath(), id, params.QueryString())
  47. headers := http.Header{}
  48. headers.Add("Content-Type", "application/octet-stream")
  49. if size > 0 {
  50. headers.Add("Content-Length", fmt.Sprintf("%d", size))
  51. }
  52. resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, httputils.POST, path, headers, body)
  53. _, json, err := s.ParseJSONResponse("", resp, err)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return json, nil
  58. }
  59. func (m *RepoManager) DownloadChart(s *mcclient.ClientSession, id string, chartName string, chartVersion string) (string, io.Reader, int64, error) {
  60. params := map[string]string{
  61. "chart_name": chartName,
  62. "version": chartVersion,
  63. }
  64. query := jsonutils.Marshal(params)
  65. path := fmt.Sprintf("/%s/%s/download-chart", m.URLPath(), url.PathEscape(id))
  66. queryString := query.QueryString()
  67. if len(queryString) > 0 {
  68. path = fmt.Sprintf("%s?%s", path, queryString)
  69. }
  70. resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, "GET", path, nil, nil)
  71. if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
  72. sizeBytes, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
  73. if err != nil {
  74. log.Errorf("Download chart unknown size")
  75. sizeBytes = -1
  76. }
  77. return resp.Header.Get("Chart-Filename"), resp.Body, sizeBytes, nil
  78. }
  79. _, _, err = s.ParseJSONResponse("", resp, err)
  80. return "", nil, -1, err
  81. }