mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 14:19:49 +08:00
Automatic merge from release/2.0.0 -> release/2.1.0
* commit '6f5845eef4acead6ff5b69bcc58964a4a6349ffb': climc: k8s api moved
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/yunionio/onecloud/cmd/climc/promputils"
|
||||
"github.com/yunionio/onecloud/cmd/climc/shell"
|
||||
_ "github.com/yunionio/onecloud/cmd/climc/shell/k8s"
|
||||
"github.com/yunionio/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ type TypeEventListOptions struct {
|
||||
}
|
||||
|
||||
func doK8sEventList(s *mcclient.ClientSession, args *EventListOptions) error {
|
||||
return doEventList(k8s.Logs, s, args)
|
||||
return doEventList(*k8s.Logs, s, args)
|
||||
}
|
||||
|
||||
func doComputeEventList(s *mcclient.ClientSession, args *EventListOptions) error {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package shell
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -9,14 +9,9 @@ import (
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func init() {
|
||||
initCluster()
|
||||
initNode()
|
||||
}
|
||||
|
||||
func initCluster() {
|
||||
cmdN := func(suffix string) string {
|
||||
return fmt.Sprintf("k8s-cluster-%s", suffix)
|
||||
return resourceCmdN("cluster", suffix)
|
||||
}
|
||||
type listOpt struct {
|
||||
BaseListOptions
|
||||
@@ -175,119 +170,3 @@ func initCluster() {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func initNode() {
|
||||
cmdN := func(suffix string) string {
|
||||
return fmt.Sprintf("k8s-node-%s", suffix)
|
||||
}
|
||||
type listOpt struct {
|
||||
BaseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s node", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
args.Details = true
|
||||
params := FetchPagingParams(args.BaseListOptions)
|
||||
result, err := k8s.Nodes.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(result, k8s.Nodes.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type dockerConfig struct {
|
||||
RegistryMirrors []string `json:"registry-mirrors"`
|
||||
InsecureRegistries []string `json:"insecure-registries"`
|
||||
}
|
||||
|
||||
type createOpt struct {
|
||||
CLUSTER string `help:"Cluster id"`
|
||||
Etcd bool `help:"Etcd role"`
|
||||
Controlplane bool `help:"Controlplane role"`
|
||||
Worker bool `help:"Worker role"`
|
||||
AllRole bool `help:"All roles"`
|
||||
HostnameOverride string `help:"Worker node overrided hostname"`
|
||||
Host string `help:"Yunion host server name or id"`
|
||||
Name string `help:"Name of node"`
|
||||
RegistryMirror []string `help:"Docker registry mirrors, e.g. 'https://registry.docker-cn.com'"`
|
||||
InsecureRegistry []string `help:"Docker insecure registry"`
|
||||
}
|
||||
R(&createOpt{}, cmdN("create"), "Create k8s cluster node", func(s *mcclient.ClientSession, args *createOpt) error {
|
||||
params := jsonutils.NewDict()
|
||||
if args.Name != "" {
|
||||
params.Add(jsonutils.NewString(args.Name), "name")
|
||||
}
|
||||
params.Add(jsonutils.NewString(args.CLUSTER), "cluster")
|
||||
|
||||
dockerConf := dockerConfig{}
|
||||
for _, rm := range args.RegistryMirror {
|
||||
dockerConf.RegistryMirrors = append(dockerConf.RegistryMirrors, rm)
|
||||
}
|
||||
for _, im := range args.InsecureRegistry {
|
||||
dockerConf.InsecureRegistries = append(dockerConf.InsecureRegistries, im)
|
||||
}
|
||||
confObj := jsonutils.Marshal(dockerConf)
|
||||
params.Add(confObj, "dockerd_config")
|
||||
|
||||
roles := jsonutils.NewArray()
|
||||
if args.AllRole {
|
||||
roles.Add(jsonutils.NewString("etcd"), jsonutils.NewString("controlplane"), jsonutils.NewString("worker"))
|
||||
} else {
|
||||
if args.Etcd {
|
||||
roles.Add(jsonutils.NewString("etcd"))
|
||||
}
|
||||
if args.Controlplane {
|
||||
roles.Add(jsonutils.NewString("controlplane"))
|
||||
}
|
||||
if args.Worker {
|
||||
roles.Add(jsonutils.NewString("worker"))
|
||||
}
|
||||
}
|
||||
params.Add(roles, "roles")
|
||||
if args.HostnameOverride != "" {
|
||||
params.Add(jsonutils.NewString(args.HostnameOverride), "hostname_override")
|
||||
}
|
||||
if args.Host != "" {
|
||||
params.Add(jsonutils.NewString(args.Host), "host")
|
||||
}
|
||||
node, err := k8s.Nodes.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(node)
|
||||
return nil
|
||||
})
|
||||
|
||||
type identOpt struct {
|
||||
ID string `help:"ID or name of the node"`
|
||||
}
|
||||
|
||||
type identsOpt struct {
|
||||
ID []string `help:"ID or name of the nodes"`
|
||||
}
|
||||
type deleteOpt struct {
|
||||
identsOpt
|
||||
}
|
||||
R(&deleteOpt{}, cmdN("delete"), "Delete node", func(s *mcclient.ClientSession, args *deleteOpt) error {
|
||||
ret := k8s.Nodes.BatchDeleteWithParam(s, args.ID, nil, nil)
|
||||
printBatchResults(ret, k8s.Nodes.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&identOpt{}, cmdN("show"), "Show node details", func(s *mcclient.ClientSession, args *identOpt) error {
|
||||
obj, err := k8s.Nodes.Get(s, args.ID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(obj)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&identOpt{}, cmdN("dockerconfig"), "Get docker daemon config", func(s *mcclient.ClientSession, args *identOpt) error {
|
||||
ret, err := k8s.Nodes.GetSpecific(s, args.ID, "docker-config", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yunionio/jsonutils"
|
||||
|
||||
"github.com/yunionio/onecloud/cmd/climc/shell"
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules"
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules/k8s"
|
||||
"github.com/yunionio/onecloud/pkg/util/printutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
initCluster()
|
||||
initNode()
|
||||
initPod()
|
||||
}
|
||||
|
||||
type BaseListOptions shell.BaseListOptions
|
||||
|
||||
type clusterBaseOptions struct {
|
||||
Cluster string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"`
|
||||
}
|
||||
|
||||
func (o clusterBaseOptions) ClusterContext() []modules.ManagerContext {
|
||||
return []modules.ManagerContext{clusterContext(o.Cluster)}
|
||||
}
|
||||
|
||||
type k8sBaseListOptions struct {
|
||||
clusterBaseOptions
|
||||
Limit int `default:"20" help:"Page limit"`
|
||||
}
|
||||
|
||||
var (
|
||||
R = shell.R
|
||||
printList = printutils.PrintJSONList
|
||||
printObject = printutils.PrintJSONObject
|
||||
printBatchResults = printutils.PrintJSONBatchResults
|
||||
)
|
||||
|
||||
func FetchPagingParams(o BaseListOptions) jsonutils.JSONObject {
|
||||
return shell.FetchPagingParams(shell.BaseListOptions(o))
|
||||
}
|
||||
|
||||
func resourceCmdN(prefix, suffix string) string {
|
||||
return fmt.Sprintf("k8s-%s-%s", prefix, suffix)
|
||||
}
|
||||
|
||||
func clusterContext(clusterId string) modules.ManagerContext {
|
||||
return modules.ManagerContext{
|
||||
InstanceManager: k8s.Clusters,
|
||||
InstanceId: clusterId,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yunionio/jsonutils"
|
||||
|
||||
"github.com/yunionio/onecloud/pkg/mcclient"
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initNode() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("node", suffix)
|
||||
}
|
||||
type listOpt struct {
|
||||
BaseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s node", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
args.Details = true
|
||||
params := FetchPagingParams(args.BaseListOptions)
|
||||
result, err := k8s.Nodes.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(result, k8s.Nodes.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type dockerConfig struct {
|
||||
RegistryMirrors []string `json:"registry-mirrors"`
|
||||
InsecureRegistries []string `json:"insecure-registries"`
|
||||
}
|
||||
|
||||
type createOpt struct {
|
||||
CLUSTER string `help:"Cluster id"`
|
||||
Etcd bool `help:"Etcd role"`
|
||||
Controlplane bool `help:"Controlplane role"`
|
||||
Worker bool `help:"Worker role"`
|
||||
AllRole bool `help:"All roles"`
|
||||
HostnameOverride string `help:"Worker node overrided hostname"`
|
||||
Host string `help:"Yunion host server name or id"`
|
||||
Name string `help:"Name of node"`
|
||||
RegistryMirror []string `help:"Docker registry mirrors, e.g. 'https://registry.docker-cn.com'"`
|
||||
InsecureRegistry []string `help:"Docker insecure registry"`
|
||||
}
|
||||
R(&createOpt{}, cmdN("create"), "Create k8s cluster node", func(s *mcclient.ClientSession, args *createOpt) error {
|
||||
params := jsonutils.NewDict()
|
||||
if args.Name != "" {
|
||||
params.Add(jsonutils.NewString(args.Name), "name")
|
||||
}
|
||||
params.Add(jsonutils.NewString(args.CLUSTER), "cluster")
|
||||
|
||||
dockerConf := dockerConfig{}
|
||||
for _, rm := range args.RegistryMirror {
|
||||
dockerConf.RegistryMirrors = append(dockerConf.RegistryMirrors, rm)
|
||||
}
|
||||
for _, im := range args.InsecureRegistry {
|
||||
dockerConf.InsecureRegistries = append(dockerConf.InsecureRegistries, im)
|
||||
}
|
||||
confObj := jsonutils.Marshal(dockerConf)
|
||||
params.Add(confObj, "dockerd_config")
|
||||
|
||||
roles := jsonutils.NewArray()
|
||||
if args.AllRole {
|
||||
roles.Add(jsonutils.NewString("etcd"), jsonutils.NewString("controlplane"), jsonutils.NewString("worker"))
|
||||
} else {
|
||||
if args.Etcd {
|
||||
roles.Add(jsonutils.NewString("etcd"))
|
||||
}
|
||||
if args.Controlplane {
|
||||
roles.Add(jsonutils.NewString("controlplane"))
|
||||
}
|
||||
if args.Worker {
|
||||
roles.Add(jsonutils.NewString("worker"))
|
||||
}
|
||||
}
|
||||
params.Add(roles, "roles")
|
||||
if args.HostnameOverride != "" {
|
||||
params.Add(jsonutils.NewString(args.HostnameOverride), "hostname_override")
|
||||
}
|
||||
if args.Host != "" {
|
||||
params.Add(jsonutils.NewString(args.Host), "host")
|
||||
}
|
||||
node, err := k8s.Nodes.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(node)
|
||||
return nil
|
||||
})
|
||||
|
||||
type identOpt struct {
|
||||
ID string `help:"ID or name of the node"`
|
||||
}
|
||||
|
||||
type identsOpt struct {
|
||||
ID []string `help:"ID or name of the nodes"`
|
||||
}
|
||||
type deleteOpt struct {
|
||||
identsOpt
|
||||
}
|
||||
R(&deleteOpt{}, cmdN("delete"), "Delete node", func(s *mcclient.ClientSession, args *deleteOpt) error {
|
||||
ret := k8s.Nodes.BatchDeleteWithParam(s, args.ID, nil, nil)
|
||||
printBatchResults(ret, k8s.Nodes.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&identOpt{}, cmdN("show"), "Show node details", func(s *mcclient.ClientSession, args *identOpt) error {
|
||||
obj, err := k8s.Nodes.Get(s, args.ID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(obj)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&identOpt{}, cmdN("dockerconfig"), "Get docker daemon config", func(s *mcclient.ClientSession, args *identOpt) error {
|
||||
ret, err := k8s.Nodes.GetSpecific(s, args.ID, "docker-config", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yunionio/onecloud/pkg/mcclient"
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initPod() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("pod", suffix)
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
k8sBaseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s pod", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
ret, err := k8s.Pods.ListInContexts(s, nil, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
func NewManager(keyword, keywordPlural string, columns, adminColumns []string) modules.ResourceManager {
|
||||
return modules.ResourceManager{
|
||||
func NewManager(keyword, keywordPlural string, columns, adminColumns []string) *modules.ResourceManager {
|
||||
return &modules.ResourceManager{
|
||||
BaseManager: *modules.NewBaseManager("k8s", "", "", columns, adminColumns),
|
||||
Keyword: keyword,
|
||||
KeywordPlural: keywordPlural,
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Clusters modules.ResourceManager
|
||||
Clusters *modules.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
Clusters = NewManager("cluster", "clusters",
|
||||
[]string{"id", "name", "mode", "k8s_version", "status", "api_endpoint"},
|
||||
[]string{})
|
||||
modules.Register(&Clusters)
|
||||
modules.Register(Clusters)
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Logs modules.ResourceManager
|
||||
Logs *modules.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
Logs = NewManager("kube_event", "kube_events",
|
||||
[]string{"id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"},
|
||||
[]string{})
|
||||
modules.Register(&Logs)
|
||||
modules.Register(Logs)
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Nodes modules.ResourceManager
|
||||
Nodes *modules.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
Nodes = NewManager("node", "nodes", []string{"id", "name", "cluster", "roles", "address", "status"}, []string{})
|
||||
modules.Register(&Nodes)
|
||||
modules.Register(Nodes)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"github.com/yunionio/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var Pods *PodManager
|
||||
|
||||
type PodManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Pods = &PodManager{
|
||||
ResourceManager: *NewManager(
|
||||
"pod", "pods",
|
||||
[]string{"id", "name", "cluster", "roles", "address", "status"},
|
||||
[]string{})}
|
||||
modules.Register(Pods)
|
||||
}
|
||||
Reference in New Issue
Block a user