mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
climc: support create resource from file, raw put body fix
This commit is contained in:
@@ -2,6 +2,7 @@ package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -105,6 +106,31 @@ func initDeployment() {
|
||||
printObjectYAML(ret)
|
||||
return nil
|
||||
})
|
||||
|
||||
type createFromFileOpt struct {
|
||||
resourceGetOptions
|
||||
FILE string `help:"K8s resource YAML or JSON file"`
|
||||
}
|
||||
R(&createFromFileOpt{}, "k8s-create", "Create resource by file", func(s *mcclient.ClientSession, args *createFromFileOpt) error {
|
||||
params := args.ClusterParams()
|
||||
params.Add(jsonutils.NewString(args.NAME), "name")
|
||||
|
||||
content, err := ioutil.ReadFile(args.FILE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
namespace := args.Namespace
|
||||
if namespace != "" {
|
||||
params.Add(jsonutils.NewString(namespace), "namespace")
|
||||
}
|
||||
params.Add(jsonutils.NewString(string(content)), "content")
|
||||
ret, err := k8s.DeployFromFile.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObjectYAML(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type portMapping struct {
|
||||
|
||||
@@ -57,7 +57,10 @@ func initRaw() {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body := jsonutils.Marshal(string(content))
|
||||
body, err := jsonutils.Parse(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = k8s.RawResource.Put(s, args.KIND, args.Namespace, args.NAME, body, args.Cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -4,15 +4,28 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var Deployments *DeploymentManager
|
||||
var (
|
||||
Deployments *DeploymentManager
|
||||
DeployFromFile *DeployFromFileManager
|
||||
)
|
||||
|
||||
type DeploymentManager struct {
|
||||
*NamespaceResourceManager
|
||||
}
|
||||
|
||||
type DeployFromFileManager struct {
|
||||
*NamespaceResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Deployments = &DeploymentManager{
|
||||
NewNamespaceResourceManager("deployment", "deployments",
|
||||
NewNamespaceCols(), NewColumns())}
|
||||
|
||||
DeployFromFile = &DeployFromFileManager{
|
||||
NewNamespaceResourceManager("deployfromfile", "deployfromfiles",
|
||||
NewNamespaceCols(), NewColumns())}
|
||||
|
||||
modules.Register(Deployments)
|
||||
modules.Register(DeployFromFile)
|
||||
}
|
||||
|
||||
@@ -68,10 +68,8 @@ func (m *RawResourceManager) Get(s *mcclient.ClientSession, kind string, namespa
|
||||
}
|
||||
|
||||
func (m *RawResourceManager) Put(s *mcclient.ClientSession, kind string, namespace string, name string, body jsonutils.JSONObject, cluster string) error {
|
||||
newBody := jsonutils.NewDict()
|
||||
newBody.Add(body, "raw")
|
||||
ctx := newRawResource(kind, namespace, name, cluster)
|
||||
_, err := m.request(s, "PUT", ctx.path(), newBody)
|
||||
_, err := m.request(s, "PUT", ctx.path(), body)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user