From ab8415f20e71404a97f0fc7226c06340331023dc Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 6 Jul 2023 01:15:35 +0800 Subject: [PATCH] feat(climc): add container registry cmd for k8s service (#17447) --- cmd/climc/shell/k8s/container_registry.go | 64 +++++++++++++ cmd/climc/shell/k8s/k8s.go | 3 + .../modules/k8s/container_registry.go | 64 +++++++++++++ .../options/k8s/container_registry.go | 93 +++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 cmd/climc/shell/k8s/container_registry.go create mode 100644 pkg/mcclient/modules/k8s/container_registry.go create mode 100644 pkg/mcclient/options/k8s/container_registry.go diff --git a/cmd/climc/shell/k8s/container_registry.go b/cmd/climc/shell/k8s/container_registry.go new file mode 100644 index 0000000000..789a94d1e9 --- /dev/null +++ b/cmd/climc/shell/k8s/container_registry.go @@ -0,0 +1,64 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package k8s + +import ( + "os" + + "github.com/cheggaaa/pb/v3" + + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules/k8s" + o "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func initContainerRegistry() { + cmd := NewK8sResourceCmd(k8s.ContainerRegistries) + cmd.List(new(o.RegistryListOptions)) + cmd.Show(new(o.RegistryGetOptions)) + cmd.Create(new(o.RegistryCreateOptions)) + cmd.Get("images", new(o.RegistryGetImagesOptions)) + cmd.Get("image-tags", new(o.RegistryGetImageTagsOptions)) + + type UploadOptions struct { + REGISTRY string `help:"The name or id of registry" json:"-"` + FILE string `help:"The container tar image" json:"-"` + Name string `help:"Override image name" json:"name` + Tag string `help:"Override image tag" json:"tag"` + } + R(new(UploadOptions), "k8s-container-registry-upload-image", "Upload a docker image", func(s *mcclient.ClientSession, args *UploadOptions) error { + f, err := os.Open(args.FILE) + if err != nil { + return err + } + defer f.Close() + finfo, err := f.Stat() + if err != nil { + return err + } + size := finfo.Size() + bar := pb.Full.Start64(size) + barReader := bar.NewProxyReader(f) + param := jsonutils.Marshal(args) + img, err := k8s.ContainerRegistries.UploadImage(s, args.REGISTRY, param, barReader, size) + if err != nil { + return err + } + printObject(img) + return nil + }) +} diff --git a/cmd/climc/shell/k8s/k8s.go b/cmd/climc/shell/k8s/k8s.go index 83e4628685..d48b7beed9 100644 --- a/cmd/climc/shell/k8s/k8s.go +++ b/cmd/climc/shell/k8s/k8s.go @@ -59,6 +59,9 @@ func init() { initRbac() initApp() + + // container registry + initContainerRegistry() } var ( diff --git a/pkg/mcclient/modules/k8s/container_registry.go b/pkg/mcclient/modules/k8s/container_registry.go new file mode 100644 index 0000000000..fddc442a42 --- /dev/null +++ b/pkg/mcclient/modules/k8s/container_registry.go @@ -0,0 +1,64 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package k8s + +import ( + "fmt" + "io" + "net/http" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/util/httputils" + + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + ContainerRegistries *ContainerRegistryManager +) + +func init() { + ContainerRegistries = NewContainerRegistryManager() + modules.Register(ContainerRegistries) +} + +type ContainerRegistryManager struct { + *ResourceManager +} + +func NewContainerRegistryManager() *ContainerRegistryManager { + return &ContainerRegistryManager{ + ResourceManager: NewResourceManager("container_registry", "container_registries", + NewResourceCols("Url", "Type"), + NewColumns()), + } +} + +func (m *ContainerRegistryManager) UploadImage(s *mcclient.ClientSession, id string, params jsonutils.JSONObject, body io.Reader, size int64) (jsonutils.JSONObject, error) { + path := fmt.Sprintf("/%s/%s/upload-image", m.URLPath(), id) + headers := http.Header{} + headers.Add("Content-Type", "application/octet-stream") + if size > 0 { + headers.Add("Content-Length", fmt.Sprintf("%d", size)) + } + resp, err := modulebase.RawRequest(*m.ResourceManager.ResourceManager, s, httputils.POST, path, headers, body) + _, json, err := s.ParseJSONResponse("", resp, err) + if err != nil { + return nil, err + } + return json, nil +} diff --git a/pkg/mcclient/options/k8s/container_registry.go b/pkg/mcclient/options/k8s/container_registry.go new file mode 100644 index 0000000000..e44f0b981e --- /dev/null +++ b/pkg/mcclient/options/k8s/container_registry.go @@ -0,0 +1,93 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package k8s + +import ( + "yunion.io/x/jsonutils" + + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type RegistryListOptions struct { + options.BaseListOptions + Type string `help:"Container registry type" json:"type" choices:"harbor"` +} + +func (o *RegistryListOptions) Params() (jsonutils.JSONObject, error) { + params, err := o.BaseListOptions.Params() + if err != nil { + return nil, err + } + if o.Type != "" { + params.Add(jsonutils.NewString(o.Type), "type") + } + return params, nil +} + +type RegistryGetOptions struct { + NAME string `help:"ID or name of the repo" json:"name"` +} + +func (o *RegistryGetOptions) GetId() string { + return o.NAME +} + +func (o *RegistryGetOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type RegistryCreateCommonOptions struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type RegistryCreateHarborOptions struct { + RegistryCreateCommonOptions +} + +type RegistryCreateConfigOptions struct { + Common RegistryCreateHarborOptions `json:"common"` + Harbor RegistryCreateHarborOptions `json:"harbor"` +} + +type RegistryCreateOptions struct { + RegistryGetOptions + TYPE string `help:"Repository type" choices:"common|harbor" json:"type"` + URL string `help:"Repository url" json:"url"` + Config RegistryCreateConfigOptions `json:"config"` +} + +func (o RegistryCreateOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(o), nil +} + +type RegistryGetImagesOptions struct { + RegistryGetOptions +} + +func (o RegistryGetImagesOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type RegistryGetImageTagsOptions struct { + RegistryGetOptions + REPOSITORY string `help:"image repository, e.g. 'yunion/region'"` +} + +func (o RegistryGetImageTagsOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(map[string]interface{}{ + "repository": o.REPOSITORY, + }), nil +}