feat(climc): add container registry cmd for k8s service (#17447)

This commit is contained in:
Zexi Li
2023-07-06 01:15:35 +08:00
committed by GitHub
parent b57fdc6dfa
commit ab8415f20e
4 changed files with 224 additions and 0 deletions
+64
View File
@@ -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
})
}
+3
View File
@@ -59,6 +59,9 @@ func init() {
initRbac()
initApp()
// container registry
initContainerRegistry()
}
var (
@@ -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
}
@@ -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
}