container_registry.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "io"
  17. "os"
  18. "strings"
  19. "github.com/cheggaaa/pb/v3"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  24. o "yunion.io/x/onecloud/pkg/mcclient/options/k8s"
  25. )
  26. func initContainerRegistry() {
  27. cmd := NewK8sResourceCmd(k8s.ContainerRegistries)
  28. cmd.List(new(o.RegistryListOptions))
  29. cmd.Show(new(o.RegistryGetOptions))
  30. cmd.Delete(new(o.RegistryGetOptions))
  31. cmd.Create(new(o.RegistryCreateOptions))
  32. cmd.Get("images", new(o.RegistryGetImagesOptions))
  33. cmd.Get("image-tags", new(o.RegistryGetImageTagsOptions))
  34. cmd.BatchPerform("public", new(o.RegistryPublicOptions))
  35. cmd.Perform("private", new(o.RegistryGetOptions))
  36. type UploadOptions struct {
  37. REGISTRY string `help:"The name or id of registry" json:"-"`
  38. FILE string `help:"The container tar image" json:"-"`
  39. Name string `help:"Override image name" json:"name`
  40. Tag string `help:"Override image tag" json:"tag"`
  41. }
  42. R(new(UploadOptions), "k8s-container-registry-upload-image", "Upload a docker image", func(s *mcclient.ClientSession, args *UploadOptions) error {
  43. f, err := os.Open(args.FILE)
  44. if err != nil {
  45. return err
  46. }
  47. defer f.Close()
  48. finfo, err := f.Stat()
  49. if err != nil {
  50. return err
  51. }
  52. size := finfo.Size()
  53. bar := pb.Full.Start64(size)
  54. barReader := bar.NewProxyReader(f)
  55. param := jsonutils.Marshal(args)
  56. img, err := k8s.ContainerRegistries.UploadImage(s, args.REGISTRY, param, barReader, size)
  57. if err != nil {
  58. return err
  59. }
  60. printObject(img)
  61. return nil
  62. })
  63. type DownloadOptions struct {
  64. NAME string `help:"The name of image, e.g. 'influxdb:1.7.7'"`
  65. Registry string `help:"The name or id of registry" json:"-"`
  66. Output string `help:"Saved file path"`
  67. Insecure bool `help:"Set insecure"`
  68. Username string `help:"Image registry username, effective only when --registry is not specified"`
  69. Password string `help:"Image registry password, effective only when --registry is not specified"`
  70. }
  71. R(new(DownloadOptions), "k8s-container-registry-download-image", "Download container image to a file", func(s *mcclient.ClientSession, args *DownloadOptions) error {
  72. var (
  73. fileName string
  74. src io.Reader
  75. size int64
  76. err error
  77. )
  78. if args.Registry != "" {
  79. parts := strings.Split(args.NAME, ":")
  80. if len(parts) != 2 {
  81. return errors.Errorf("invalid NAME %q, use format <name>:<tag>", args.NAME)
  82. }
  83. name := parts[0]
  84. tag := parts[1]
  85. fileName, src, size, err = k8s.ContainerRegistries.DownloadImage(s, args.Registry, name, tag)
  86. if err != nil {
  87. return errors.Wrap(err, "download chart")
  88. }
  89. } else {
  90. fileName, src, size, err = k8s.ContainerRegistries.DownloadImageByManager(s, &k8s.DownloadImageByManagerInput{
  91. Insecure: args.Insecure,
  92. Image: args.NAME,
  93. Username: args.Username,
  94. Password: args.Password,
  95. })
  96. }
  97. output := args.Output
  98. if output == "" && fileName != "" {
  99. output = fileName
  100. }
  101. if output == "" {
  102. return errors.Errorf("--output filepath must provide")
  103. }
  104. f, err := os.Create(output)
  105. if err != nil {
  106. return errors.Wrapf(err, "create saved file: %q", args.Output)
  107. }
  108. defer f.Close()
  109. var sink io.Writer = f
  110. bar := pb.Full.Start64(size)
  111. barReader := bar.NewProxyReader(src)
  112. if _, err := io.Copy(sink, barReader); err != nil {
  113. return errors.Wrap(err, "save chart")
  114. }
  115. return nil
  116. })
  117. }