container_registry.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ContainerRegistries *ContainerRegistryManager
  30. )
  31. func init() {
  32. ContainerRegistries = NewContainerRegistryManager()
  33. modules.Register(ContainerRegistries)
  34. }
  35. type ContainerRegistryManager struct {
  36. *ResourceManager
  37. }
  38. func NewContainerRegistryManager() *ContainerRegistryManager {
  39. man := &ContainerRegistryManager{
  40. ResourceManager: NewResourceManager("container_registry", "container_registries",
  41. NewResourceCols("Url", "Type"),
  42. NewColumns()),
  43. }
  44. man.SetSpecificMethods("images")
  45. return man
  46. }
  47. func (m *ContainerRegistryManager) UploadImage(s *mcclient.ClientSession, id string, params jsonutils.JSONObject, body io.Reader, size int64) (jsonutils.JSONObject, error) {
  48. path := fmt.Sprintf("/%s/%s/upload-image", m.URLPath(), id)
  49. headers := http.Header{}
  50. headers.Add("Content-Type", "application/octet-stream")
  51. if size > 0 {
  52. headers.Add("Content-Length", fmt.Sprintf("%d", size))
  53. }
  54. resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, httputils.POST, path, headers, body)
  55. _, json, err := s.ParseJSONResponse("", resp, err)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return json, nil
  60. }
  61. type DownloadImageByManagerInput struct {
  62. Insecure bool `json:"insecure"`
  63. Image string `json:"image"`
  64. Username string `json:"username"`
  65. Password string `json:"password"`
  66. }
  67. func (m *ContainerRegistryManager) DownloadImageByManager(s *mcclient.ClientSession, input *DownloadImageByManagerInput) (string, io.Reader, int64, error) {
  68. path := fmt.Sprintf("/%s/download-image", m.URLPath())
  69. return m.downloadImage(s, path, input)
  70. }
  71. func (m *ContainerRegistryManager) DownloadImage(s *mcclient.ClientSession, id string, imageName string, imageTag string) (string, io.Reader, int64, error) {
  72. params := map[string]string{
  73. "image_name": imageName,
  74. "tag": imageTag,
  75. }
  76. path := fmt.Sprintf("/%s/%s/download-image", m.URLPath(), url.PathEscape(id))
  77. return m.downloadImage(s, path, params)
  78. }
  79. func (m *ContainerRegistryManager) downloadImage(s *mcclient.ClientSession, path string, params interface{}) (string, io.Reader, int64, error) {
  80. query := jsonutils.Marshal(params)
  81. queryString := query.QueryString()
  82. if len(queryString) > 0 {
  83. path = fmt.Sprintf("%s?%s", path, queryString)
  84. }
  85. resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, "GET", path, nil, nil)
  86. if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
  87. sizeBytes, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
  88. if err != nil {
  89. log.Errorf("Download image unknown size")
  90. sizeBytes = -1
  91. }
  92. return resp.Header.Get("Image-Filename"), resp.Body, sizeBytes, nil
  93. }
  94. _, _, err = s.ParseJSONResponse("", resp, err)
  95. return "", nil, -1, err
  96. }