diff --git a/Gopkg.lock b/Gopkg.lock index 99299205d0..b67bf4ca82 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -383,6 +383,14 @@ pruneopts = "UT" revision = "8e809c8a86450a29b90dcc9efbf062d0fe6d9746" +[[projects]] + branch = "master" + digest = "1:09970bb8daf100bdf11b48b72c71ef980cd2b50efbd903989b3d182956be0b83" + name = "github.com/hako/durafmt" + packages = ["."] + pruneopts = "UT" + revision = "7b7ae1e72eade09dbc9c2cfba3e6c4bae7b8bcac" + [[projects]] digest = "1:8eb1de8112c9924d59bf1d3e5c26f5eaa2bfc2a5fcbb92dc1c2e4546d695f277" name = "github.com/imdario/mergo" @@ -1143,6 +1151,7 @@ "github.com/go-sql-driver/mysql", "github.com/golang-plus/uuid", "github.com/gosuri/uitable", + "github.com/hako/durafmt", "github.com/jinzhu/gorm", "github.com/jinzhu/gorm/dialects/mysql", "github.com/json-iterator/go", diff --git a/Gopkg.toml b/Gopkg.toml index 007f3aec01..1762493a14 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -81,3 +81,7 @@ branch = "master" name = "github.com/vmware/govmomi" + +[[constraint]] + branch = "master" + name = "github.com/hako/durafmt" diff --git a/cmd/climc/shell/events.go b/cmd/climc/shell/events.go index f458f62e7c..bf5537554f 100644 --- a/cmd/climc/shell/events.go +++ b/cmd/climc/shell/events.go @@ -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.ResourceManager, s, args) } func doComputeEventList(s *mcclient.ClientSession, args *EventListOptions) error { diff --git a/cmd/climc/shell/k8s/charts.go b/cmd/climc/shell/k8s/charts.go index 8cb609642a..d13e9685a5 100644 --- a/cmd/climc/shell/k8s/charts.go +++ b/cmd/climc/shell/k8s/charts.go @@ -1,8 +1,6 @@ package k8s import ( - "fmt" - json "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/mcclient" @@ -43,21 +41,8 @@ func initChart() { if err != nil { return err } - PrintHelmListResult(charts, ChartPrinter{}) + + PrintListResultTable(charts, k8s.Charts, s) return nil }) } - -type ChartPrinter struct{} - -func (p ChartPrinter) Columns() []interface{} { - return []interface{}{"REPO/NAME", "VERSION", "DESCRIPTION"} -} - -func (p ChartPrinter) RowKeys(obj *json.JSONDict) []interface{} { - repo, _ := obj.GetString("repo") - name, _ := obj.GetString("chart", "metadata", "name") - version, _ := obj.GetString("chart", "metadata", "version") - desc, _ := obj.GetString("chart", "metadata", "description") - return []interface{}{fmt.Sprintf("%s/%s", repo, name), version, desc} -} diff --git a/cmd/climc/shell/k8s/clusters.go b/cmd/climc/shell/k8s/clusters.go index d85e2303a0..96d203d294 100644 --- a/cmd/climc/shell/k8s/clusters.go +++ b/cmd/climc/shell/k8s/clusters.go @@ -2,8 +2,11 @@ package k8s import ( "fmt" + "io/ioutil" + "strings" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/util/sets" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules/k8s" @@ -30,7 +33,7 @@ func initCluster() { type createOpt struct { NAME string `help:"Name of cluster"` Mode string `help:"Cluster mode" choices:"internal"` - K8sVersion string `help:"Cluster kubernetes components version" choices:"v1.8.10|v1.9.5|v1.10.0" default:"v1.9.5"` + K8sVersion string `help:"Cluster kubernetes components version" choices:"v1.8.10|v1.9.5|v1.10.0"` InfraImage string `help:"Cluster kubelet infra container image"` Cidr string `help:"Cluster service CIDR, e.g. 10.43.0.0/16"` Domain string `help:"Cluster pod domain, e.g. cluster.local"` @@ -61,6 +64,29 @@ func initCluster() { return nil }) + type importOpt struct { + NAME string `help:"Name of cluster to import"` + Kubeconfig string `help:"Kubernetes auth config"` + } + R(&importOpt{}, cmdN("import"), "Import exists YKE deployed kubernetes cluster", func(s *mcclient.ClientSession, args *importOpt) error { + if args.Kubeconfig == "" { + return fmt.Errorf("Kubeconfig file must provide") + } + kubeconfig, err := ioutil.ReadFile(args.Kubeconfig) + if err != nil { + return fmt.Errorf("Read kube config %q error: %v", args.Kubeconfig, err) + } + + params := jsonutils.NewDict() + params.Add(jsonutils.NewString(string(kubeconfig)), "kube_config") + cluster, err := k8s.Clusters.PerformAction(s, args.NAME, "import", params) + if err != nil { + return err + } + printObject(cluster) + return nil + }) + type updateOpt struct { NAME string `help:"Name of cluster"` K8sVersion string `help:"Cluster kubernetes components version" choices:"v1.8.10|v1.9.5|v1.10.0" default:"v1.9.5"` @@ -169,4 +195,64 @@ func initCluster() { fmt.Println(url) return nil }) + + type addNodesOpt struct { + identOpt + NodeConfig []string `help:"Node spec, 'host:[roles]' e.g: --node-config host01:controlplane,etcd,worker --node-config host02:worker"` + AutoDeploy bool `help:"Auto deploy"` + } + R(&addNodesOpt{}, cmdN("addnodes"), "Add nodes to cluster", func(s *mcclient.ClientSession, args *addNodesOpt) error { + params := jsonutils.NewDict() + if args.AutoDeploy { + params.Add(jsonutils.JSONTrue, "auto_deploy") + } + nodesArray := jsonutils.NewArray() + for _, config := range args.NodeConfig { + opt, err := parseNodeAddConfigStr(config) + if err != nil { + return err + } + nodesArray.Add(jsonutils.Marshal(opt)) + } + params.Add(nodesArray, "nodes") + ret, err := k8s.Clusters.PerformAction(s, args.ID, "add-nodes", params) + if err != nil { + return err + } + printObject(ret) + return nil + }) +} + +type dockerConfig struct { + RegistryMirrors []string `json:"registry-mirrors"` + InsecureRegistries []string `json:"insecure-registries"` +} + +type nodeAddConfig struct { + Host string `json:"host"` + Roles []string `json:"roles"` + Name string `json:"name"` + HostnameOverride string `json:"hostname_override"` + DockerdConfig dockerConfig `json:"dockerd_config"` +} + +func parseNodeAddConfigStr(config string) (nodeAddConfig, error) { + ret := nodeAddConfig{} + parts := strings.Split(config, ":") + if len(parts) != 2 { + return ret, fmt.Errorf("Invalid config: %q", config) + } + host := parts[0] + roleStr := parts[1] + ret.Host = host + roles := []string{} + for _, role := range strings.Split(roleStr, ",") { + if !sets.NewString("etcd", "controlplane", "worker").Has(role) { + return ret, fmt.Errorf("Invalid role: %q", role) + } + roles = append(roles, role) + } + ret.Roles = roles + return ret, nil } diff --git a/cmd/climc/shell/k8s/deployment.go b/cmd/climc/shell/k8s/deployment.go index dfdc392bba..6063b59739 100644 --- a/cmd/climc/shell/k8s/deployment.go +++ b/cmd/climc/shell/k8s/deployment.go @@ -6,6 +6,7 @@ import ( "strings" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/util/regutils" "yunion.io/x/pkg/util/sets" "yunion.io/x/onecloud/pkg/mcclient" @@ -41,6 +42,7 @@ func initDeployment() { Labels string `help:"Comma separated labels to apply to the pod(s)"` Env []string `help:"Environment variables to set in container"` Port []string `help:"Port for the service that is created, format is :: e.g. tcp:80:3000"` + Net string `help:"Network config, e.g. net1, net1:10.168.222.171"` } R(&createOpt{}, cmdN("create"), "Create deployment resource", func(s *mcclient.ClientSession, args *createOpt) error { params := jsonutils.NewDict() @@ -65,6 +67,13 @@ func initDeployment() { } params.Add(portMappings, "portMappings") } + if args.Net != "" { + net, err := parseNetConfig(args.Net) + if err != nil { + return err + } + params.Add(net, "networkConfig") + } ret, err := k8s.Deployments.CreateInContexts(s, params, args.ClusterContext()) if err != nil { return err @@ -143,3 +152,15 @@ func parsePortMappings(ports []string) (*jsonutils.JSONArray, error) { } return ret, nil } + +func parseNetConfig(net string) (*jsonutils.JSONDict, error) { + ret := jsonutils.NewDict() + for _, p := range strings.Split(net, ":") { + if regutils.MatchIP4Addr(p) { + ret.Add(jsonutils.NewString(p), "address") + } else { + ret.Add(jsonutils.NewString(p), "network") + } + } + return ret, nil +} diff --git a/cmd/climc/shell/k8s/helm_list.go b/cmd/climc/shell/k8s/helm_list.go deleted file mode 100644 index df1055a29e..0000000000 --- a/cmd/climc/shell/k8s/helm_list.go +++ /dev/null @@ -1,51 +0,0 @@ -package k8s - -import ( - "fmt" - - "github.com/gosuri/uitable" - - "yunion.io/x/jsonutils" - - "yunion.io/x/onecloud/pkg/mcclient/modules" -) - -type ListPrinter interface { - Columns() []interface{} - RowKeys(obj *jsonutils.JSONDict) []interface{} -} - -func ListerTable(res *modules.ListResult, printer ListPrinter) *uitable.Table { - min := func(x, y int) int { - if x < y { - return x - } - return y - } - table := uitable.New() - table.MaxColWidth = 80 - table.AddRow(printer.Columns()...) - var idx int - for ; idx < min(res.Limit, res.Total); idx++ { - table.AddRow(printer.RowKeys(res.Data[idx].(*jsonutils.JSONDict))...) - } - return table -} - -func PrintHelmListResult(res *modules.ListResult, printer ListPrinter) { - fmt.Println(ListerTable(res, printer)) - - table := uitable.New() - total := res.Total - offset := res.Offset - limit := res.Limit - page := (offset / limit) + 1 - pages := total / limit - if pages*limit < total { - pages += 1 - } - table.AddRow("") - table.AddRow("Total", "Pages", "Limit", "Offset", "Page") - table.AddRow(total, pages, limit, offset, page) - fmt.Println(table) -} diff --git a/cmd/climc/shell/k8s/k8s.go b/cmd/climc/shell/k8s/k8s.go index d7b46f482a..9449483ddf 100644 --- a/cmd/climc/shell/k8s/k8s.go +++ b/cmd/climc/shell/k8s/k8s.go @@ -99,7 +99,7 @@ var ( printBatchResults = printutils.PrintJSONBatchResults ) -func FetchPagingParams(o BaseListOptions) jsonutils.JSONObject { +func FetchPagingParams(o BaseListOptions) *jsonutils.JSONDict { return shell.FetchPagingParams(shell.BaseListOptions(o)) } diff --git a/cmd/climc/shell/k8s/list.go b/cmd/climc/shell/k8s/list.go new file mode 100644 index 0000000000..d36adac8f8 --- /dev/null +++ b/cmd/climc/shell/k8s/list.go @@ -0,0 +1,83 @@ +package k8s + +import ( + "fmt" + "reflect" + "strings" + + "github.com/gosuri/uitable" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/modules" + "yunion.io/x/onecloud/pkg/mcclient/modules/k8s" +) + +func getPrinterRowValue(printer k8s.ListPrinter, obj jsonutils.JSONObject, col string) interface{} { + getFuncName := fmt.Sprintf("Get%s", strings.Title(col)) + manValue := reflect.ValueOf(printer) + funcValue := manValue.MethodByName(getFuncName) + if !funcValue.IsValid() || funcValue.IsNil() { + log.Errorf("Can't get function: %q of manager: %#v", getFuncName, printer) + return nil + } + params := []reflect.Value{ + reflect.ValueOf(obj), + } + outs := funcValue.Call(params) + if len(outs) != 1 { + log.Errorf("Invalid return value of function: %q", getFuncName) + return nil + } + return outs[0].Interface() +} + +func getPrinterRowValues(printer k8s.ListPrinter, obj jsonutils.JSONObject, cols []string) []interface{} { + ret := make([]interface{}, 0) + for _, col := range cols { + ret = append(ret, getPrinterRowValue(printer, obj, col)) + } + return ret +} + +func ListerTable(res *modules.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) *uitable.Table { + min := func(x, y int) int { + if x < y { + return x + } + return y + } + table := uitable.New() + table.MaxColWidth = 80 + cols := printer.GetColumns(s) + colsI := make([]interface{}, len(cols)) + for i, v := range cols { + colsI[i] = v + } + table.AddRow(colsI...) + var idx int + for ; idx < min(res.Limit, res.Total-res.Offset); idx++ { + table.AddRow(getPrinterRowValues(printer, res.Data[idx], cols)...) + } + return table +} + +func PrintListResultTable(res *modules.ListResult, printer k8s.ListPrinter, s *mcclient.ClientSession) { + fmt.Println(ListerTable(res, printer, s)) + + table := uitable.New() + total := res.Total + offset := res.Offset + limit := res.Limit + page := (offset / limit) + 1 + pages := total / limit + if pages*limit < total { + pages += 1 + } + table.AddRow("") + table.AddRow("Total", "Pages", "Limit", "Offset", "Page") + table.AddRow(total, pages, limit, offset, page) + fmt.Println(table) +} diff --git a/cmd/climc/shell/k8s/nodes.go b/cmd/climc/shell/k8s/nodes.go index 9c3f840616..9ad1510472 100644 --- a/cmd/climc/shell/k8s/nodes.go +++ b/cmd/climc/shell/k8s/nodes.go @@ -15,10 +15,14 @@ func initNode() { } type listOpt struct { BaseListOptions + Cluster string `help:"Filter by cluster"` } R(&listOpt{}, cmdN("list"), "List k8s node", func(s *mcclient.ClientSession, args *listOpt) error { args.Details = true params := FetchPagingParams(args.BaseListOptions) + if args.Cluster != "" { + params.Add(jsonutils.NewString(args.Cluster), "cluster") + } result, err := k8s.Nodes.List(s, params) if err != nil { return err @@ -27,11 +31,6 @@ func initNode() { 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"` @@ -115,6 +114,12 @@ func initNode() { return nil }) + R(&identsOpt{}, cmdN("purge"), "Purge a node record in database, not actually do deletion", func(s *mcclient.ClientSession, args *identsOpt) error { + ret := k8s.Nodes.BatchPerformAction(s, args.ID, "purge", nil) + printBatchResults(ret, k8s.Nodes.GetColumns(s)) + 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 { diff --git a/cmd/climc/shell/k8s/pods.go b/cmd/climc/shell/k8s/pods.go index ee8803c414..5620609826 100644 --- a/cmd/climc/shell/k8s/pods.go +++ b/cmd/climc/shell/k8s/pods.go @@ -23,7 +23,7 @@ func initPod() { if err != nil { return err } - printList(ret, k8s.Pods.GetColumns(s)) + PrintListResultTable(ret, k8s.Pods, s) return nil }) diff --git a/cmd/climc/shell/k8s/service.go b/cmd/climc/shell/k8s/service.go index 3260b28b88..3dc22e94c4 100644 --- a/cmd/climc/shell/k8s/service.go +++ b/cmd/climc/shell/k8s/service.go @@ -21,7 +21,7 @@ func initService() { if err != nil { return err } - printList(ret, k8s.Services.GetColumns(s)) + PrintListResultTable(ret, k8s.Services, s) return nil }) } diff --git a/pkg/mcclient/modules/k8s/base.go b/pkg/mcclient/modules/k8s/base.go index 3b5fc20b61..dd49d5eb84 100644 --- a/pkg/mcclient/modules/k8s/base.go +++ b/pkg/mcclient/modules/k8s/base.go @@ -1,17 +1,59 @@ package k8s import ( + "yunion.io/x/jsonutils" "yunion.io/x/pkg/util/sets" + "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules" ) -func NewManager(keyword, keywordPlural string, columns, adminColumns *Columns) *modules.ResourceManager { - return &modules.ResourceManager{ +type ResourceManager struct { + *modules.ResourceManager +} + +func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Columns) *ResourceManager { + man := &modules.ResourceManager{ BaseManager: *modules.NewBaseManager("k8s", "", "", columns.Array(), adminColumns.Array()), Keyword: keyword, KeywordPlural: keywordPlural, } + return &ResourceManager{man} +} + +type ClusterResourceManager struct { + *ResourceManager +} + +func NewClusterResourceManager(keyword, keywordPlural string, columns, adminColumns *Columns) *ClusterResourceManager { + newAdminCols := NewClusterCols(adminColumns.Array()...) + man := NewResourceManager(keyword, keywordPlural, columns, newAdminCols) + return &ClusterResourceManager{man} +} + +func (man ClusterResourceManager) GetCluster(obj jsonutils.JSONObject) interface{} { + cluster, _ := obj.GetString("cluster") + return cluster +} + +type NamespaceResourceManager struct { + *ClusterResourceManager +} + +func NewNamespaceResourceManager(kw, kwp string, columns, adminColumns *Columns) *NamespaceResourceManager { + newCols := NewNamespaceCols(columns.Array()...) + man := NewClusterResourceManager(kw, kwp, newCols, adminColumns) + return &NamespaceResourceManager{man} +} + +func (m NamespaceResourceManager) GetName(obj jsonutils.JSONObject) interface{} { + name, _ := obj.GetString("name") + return name +} + +func (m NamespaceResourceManager) GetNamespace(obj jsonutils.JSONObject) interface{} { + ns, _ := obj.GetString("namespace") + return ns } type Columns struct { @@ -44,13 +86,17 @@ func (c Columns) Array() []string { } func NewNamespaceCols(col ...string) *Columns { - return NewColumns("name", "namespace").Add(col...) + return NewColumns("Name", "Namespace").Add(col...) } func NewClusterCols(col ...string) *Columns { - return NewColumns("cluster").Add(col...) + return NewColumns("Cluster").Add(col...) } func NewResourceCols(col ...string) *Columns { - return NewColumns("name", "id").Add(col...) + return NewColumns("Name", "Id").Add(col...) +} + +type ListPrinter interface { + GetColumns(*mcclient.ClientSession) []string } diff --git a/pkg/mcclient/modules/k8s/cluster.go b/pkg/mcclient/modules/k8s/cluster.go index 46b7bb4fea..afcddc2ef5 100644 --- a/pkg/mcclient/modules/k8s/cluster.go +++ b/pkg/mcclient/modules/k8s/cluster.go @@ -5,12 +5,12 @@ import ( ) var ( - Clusters *modules.ResourceManager + Clusters *ResourceManager ) func init() { - Clusters = NewManager("kube_cluster", "kube_clusters", + Clusters = NewResourceManager("kube_cluster", "kube_clusters", NewResourceCols("mode", "k8s_version", "status", "api_endpoint"), - NewColumns()) + NewColumns("is_public")) modules.Register(Clusters) } diff --git a/pkg/mcclient/modules/k8s/configmap.go b/pkg/mcclient/modules/k8s/configmap.go index 4bfcbbafdf..fb4c847fb7 100644 --- a/pkg/mcclient/modules/k8s/configmap.go +++ b/pkg/mcclient/modules/k8s/configmap.go @@ -7,14 +7,13 @@ import ( var ConfigMaps *ConfigMapManager type ConfigMapManager struct { - modules.ResourceManager + *NamespaceResourceManager } func init() { ConfigMaps = &ConfigMapManager{ - ResourceManager: *NewManager( + NamespaceResourceManager: NewNamespaceResourceManager( "configmap", "configmaps", - NewNamespaceCols(), - NewClusterCols())} + NewColumns(), NewColumns())} modules.Register(ConfigMaps) } diff --git a/pkg/mcclient/modules/k8s/deployment.go b/pkg/mcclient/modules/k8s/deployment.go index f56a2947ef..74c95753c1 100644 --- a/pkg/mcclient/modules/k8s/deployment.go +++ b/pkg/mcclient/modules/k8s/deployment.go @@ -7,14 +7,12 @@ import ( var Deployments *DeploymentManager type DeploymentManager struct { - modules.ResourceManager + *NamespaceResourceManager } func init() { Deployments = &DeploymentManager{ - ResourceManager: *NewManager( - "deployment", "deployments", - NewNamespaceCols("labels"), - NewClusterCols())} + NewNamespaceResourceManager("deployment", "deployments", + NewColumns("labels"), NewColumns())} modules.Register(Deployments) } diff --git a/pkg/mcclient/modules/k8s/event.go b/pkg/mcclient/modules/k8s/event.go index 584ddbc376..36551a381a 100644 --- a/pkg/mcclient/modules/k8s/event.go +++ b/pkg/mcclient/modules/k8s/event.go @@ -5,11 +5,11 @@ import ( ) var ( - Logs *modules.ResourceManager + Logs *ResourceManager ) func init() { - Logs = NewManager("kube_event", "kube_events", + Logs = NewResourceManager("kube_event", "kube_events", NewColumns("id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"), NewColumns()) modules.Register(Logs) diff --git a/pkg/mcclient/modules/k8s/helm.go b/pkg/mcclient/modules/k8s/helm.go index b3e04f21ea..1a5f765cc4 100644 --- a/pkg/mcclient/modules/k8s/helm.go +++ b/pkg/mcclient/modules/k8s/helm.go @@ -5,11 +5,11 @@ import ( ) var ( - Repos *modules.ResourceManager + Repos *ResourceManager ) func init() { - Repos = NewManager("repo", "repos", + Repos = NewResourceManager("repo", "repos", NewResourceCols("url", "is_public", "source"), NewColumns(), ) diff --git a/pkg/mcclient/modules/k8s/helm_chart.go b/pkg/mcclient/modules/k8s/helm_chart.go index 522628793f..6abefed566 100644 --- a/pkg/mcclient/modules/k8s/helm_chart.go +++ b/pkg/mcclient/modules/k8s/helm_chart.go @@ -1,16 +1,40 @@ package k8s import ( + "fmt" + + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/modules" ) var ( - Charts *modules.ResourceManager + Charts *ChartManager ) +type ChartManager struct { + *ResourceManager +} + func init() { - Charts = NewManager("chart", "charts", - NewResourceCols(), - NewColumns()) + Charts = &ChartManager{NewResourceManager("chart", "charts", + NewColumns("RepoWithName", "Version", "Description"), + NewColumns())} modules.Register(Charts) } + +func (m ChartManager) GetRepoWithName(obj jsonutils.JSONObject) interface{} { + repo, _ := obj.GetString("repo") + name, _ := obj.GetString("chart", "name") + return fmt.Sprintf("%s/%s", repo, name) +} + +func (m ChartManager) GetVersion(obj jsonutils.JSONObject) interface{} { + version, _ := obj.GetString("chart", "version") + return version +} + +func (m ChartManager) GetDescription(obj jsonutils.JSONObject) interface{} { + desc, _ := obj.GetString("chart", "description") + return desc +} diff --git a/pkg/mcclient/modules/k8s/helm_release.go b/pkg/mcclient/modules/k8s/helm_release.go index ef254c0a45..0df6b7fba4 100644 --- a/pkg/mcclient/modules/k8s/helm_release.go +++ b/pkg/mcclient/modules/k8s/helm_release.go @@ -9,15 +9,11 @@ var ( ) type ReleaseManager struct { - modules.ResourceManager + *NamespaceResourceManager } func init() { Releases = &ReleaseManager{ - ResourceManager: *NewManager( - "release", "releases", - NewNamespaceCols(""), - NewColumns(), - )} + NewNamespaceResourceManager("release", "releases", NewColumns(), NewColumns())} modules.Register(Releases) } diff --git a/pkg/mcclient/modules/k8s/helm_tiller.go b/pkg/mcclient/modules/k8s/helm_tiller.go index ddb442561f..112e097dc4 100644 --- a/pkg/mcclient/modules/k8s/helm_tiller.go +++ b/pkg/mcclient/modules/k8s/helm_tiller.go @@ -9,12 +9,12 @@ var ( ) type TillerManager struct { - modules.ResourceManager + *ResourceManager } func init() { Tiller = &TillerManager{ - ResourceManager: *NewManager( + ResourceManager: NewResourceManager( "tiller", "tiller", NewColumns(), NewColumns(), diff --git a/pkg/mcclient/modules/k8s/node.go b/pkg/mcclient/modules/k8s/node.go index 630c0ffb8c..14a551303a 100644 --- a/pkg/mcclient/modules/k8s/node.go +++ b/pkg/mcclient/modules/k8s/node.go @@ -5,10 +5,10 @@ import ( ) var ( - Nodes *modules.ResourceManager + Nodes *ResourceManager ) func init() { - Nodes = NewManager("kube_node", "kube_nodes", NewResourceCols("cluster", "roles", "address", "status"), NewColumns()) + Nodes = NewResourceManager("kube_node", "kube_nodes", NewResourceCols("cluster", "roles", "address", "status"), NewColumns()) modules.Register(Nodes) } diff --git a/pkg/mcclient/modules/k8s/pod.go b/pkg/mcclient/modules/k8s/pod.go index 218ef6a243..c816e39555 100644 --- a/pkg/mcclient/modules/k8s/pod.go +++ b/pkg/mcclient/modules/k8s/pod.go @@ -1,20 +1,59 @@ package k8s import ( + "fmt" + "strings" + + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient/modules" ) var Pods *PodManager type PodManager struct { - modules.ResourceManager + *NamespaceResourceManager } func init() { Pods = &PodManager{ - ResourceManager: *NewManager( - "pod", "pods", - NewNamespaceCols("podIP", "status", "restartCount", "labels"), - NewClusterCols("nodeName"))} + NewNamespaceResourceManager("pod", "pods", + NewNamespaceCols("IP", "Status", "Restarts", "Labels"), + NewClusterCols("Node"))} + modules.Register(Pods) } + +func (m PodManager) GetIP(obj jsonutils.JSONObject) interface{} { + ip, _ := obj.GetString("podIP") + return ip +} + +func (m PodManager) GetStatus(obj jsonutils.JSONObject) interface{} { + status, _ := obj.GetString("status") + return status +} + +func (m PodManager) GetRestarts(obj jsonutils.JSONObject) interface{} { + count, _ := obj.Int("restartCount") + return count +} + +func (m PodManager) GetLabels(obj jsonutils.JSONObject) interface{} { + labels, _ := obj.GetMap("labels") + str := "" + ls := []string{} + for k, v := range labels { + vs, _ := v.GetString() + ls = append(ls, fmt.Sprintf("%s=%s", k, vs)) + } + if len(ls) != 0 { + str = strings.Join(ls, ",") + } + return str +} + +func (m PodManager) GetNode(obj jsonutils.JSONObject) interface{} { + node, _ := obj.GetString("nodeName") + return node +} diff --git a/pkg/mcclient/modules/k8s/service.go b/pkg/mcclient/modules/k8s/service.go index 7a153c4bc4..bf7a189d72 100644 --- a/pkg/mcclient/modules/k8s/service.go +++ b/pkg/mcclient/modules/k8s/service.go @@ -1,20 +1,75 @@ package k8s import ( + "fmt" + "strings" + "time" + + "github.com/hako/durafmt" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/mcclient/modules" ) var Services *ServiceManager type ServiceManager struct { - modules.ResourceManager + *NamespaceResourceManager } func init() { - Services = &ServiceManager{ - ResourceManager: *NewManager( - "k8s_service", "k8s_services", - NewNamespaceCols("clusterIP", "selector", "internalEndpoint", "externalEndpoints"), - NewClusterCols())} + Services = &ServiceManager{NewNamespaceResourceManager( + "k8s_service", "k8s_services", + NewNamespaceCols("Type", "ClusterIP", "Ports", "Age", "Selector"), + NewColumns())} modules.Register(Services) } + +func (s ServiceManager) GetType(obj jsonutils.JSONObject) interface{} { + typ, _ := obj.GetString("type") + return typ +} + +func (s ServiceManager) GetClusterIP(obj jsonutils.JSONObject) interface{} { + clusterIp, _ := obj.GetString("clusterIP") + return clusterIp +} + +func (s ServiceManager) GetAge(obj jsonutils.JSONObject) interface{} { + creationTimestamp, err := obj.GetString("creationTimestamp") + if err != nil { + log.Errorf("Get creationTimestamp error: %v", err) + return nil + } + t, _ := time.Parse(time.RFC3339, creationTimestamp) + dur := time.Since(t) + return durafmt.ParseShort(dur).String() +} + +func (s ServiceManager) GetSelector(obj jsonutils.JSONObject) interface{} { + selectorObj, _ := obj.GetMap("selector") + var selectors []string + for k, obj := range selectorObj { + val, _ := obj.GetString() + selectors = append(selectors, fmt.Sprintf("%s=%s", k, val)) + } + selectorStr := strings.Join(selectors, ",") + return selectorStr +} + +func (s ServiceManager) GetPorts(obj jsonutils.JSONObject) interface{} { + var ports []string + var portsStr string + portObjs, _ := obj.GetArray("internalEndpoint", "ports") + if len(portObjs) != 0 { + for _, obj := range portObjs { + port, _ := obj.Int("port") + proto, _ := obj.GetString("protocol") + ports = append(ports, fmt.Sprintf("%d/%s", port, proto)) + } + portsStr = strings.Join(ports, ",") + } + return portsStr +} diff --git a/vendor/github.com/hako/durafmt/.gitignore b/vendor/github.com/hako/durafmt/.gitignore new file mode 100644 index 0000000000..daf913b1b3 --- /dev/null +++ b/vendor/github.com/hako/durafmt/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/hako/durafmt/.travis.yml b/vendor/github.com/hako/durafmt/.travis.yml new file mode 100644 index 0000000000..77f8c12ca6 --- /dev/null +++ b/vendor/github.com/hako/durafmt/.travis.yml @@ -0,0 +1,21 @@ +language: go + +go: + - 1.5 + - 1.6 + - 1.7 + - 1.8 + - 1.9 + - tip + +before_install: + - go get golang.org/x/tools/cmd/cover + +script: + - GOARCH=386 go test # test 32bit architectures. + - go test -coverprofile=coverage.txt -covermode=atomic + +after_success: + - bash <(curl -s https://codecov.io/bash) + +sudo: false diff --git a/vendor/github.com/hako/durafmt/CODE_OF_CONDUCT.md b/vendor/github.com/hako/durafmt/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000..19a95e513c --- /dev/null +++ b/vendor/github.com/hako/durafmt/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at wesley@hakobaito.co.uk. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/vendor/github.com/hako/durafmt/CONTRIBUTING.md b/vendor/github.com/hako/durafmt/CONTRIBUTING.md new file mode 100644 index 0000000000..bdfb376fcd --- /dev/null +++ b/vendor/github.com/hako/durafmt/CONTRIBUTING.md @@ -0,0 +1,9 @@ +# Contributing + +Contributions are welcome! Fork this repo and add your changes and submit a PR. + +If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section. + +You can run tests by runnning `go test`. Running `go test; go vet; golint` is recommended. + +durafmt is also tested against `gometalinter`. diff --git a/vendor/github.com/hako/durafmt/LICENSE b/vendor/github.com/hako/durafmt/LICENSE new file mode 100644 index 0000000000..ccb595049d --- /dev/null +++ b/vendor/github.com/hako/durafmt/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Wesley Hill + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/hako/durafmt/README.md b/vendor/github.com/hako/durafmt/README.md new file mode 100644 index 0000000000..6b26faaf35 --- /dev/null +++ b/vendor/github.com/hako/durafmt/README.md @@ -0,0 +1,122 @@ +# durafmt + +[![Build Status](https://travis-ci.org/hako/durafmt.svg?branch=master)](https://travis-ci.org/hako/durafmt) [![Go Report Card](https://goreportcard.com/badge/github.com/hako/durafmt)](https://goreportcard.com/report/github.com/hako/durafmt) [![codecov](https://codecov.io/gh/hako/durafmt/branch/master/graph/badge.svg)](https://codecov.io/gh/hako/durafmt) [![GoDoc](https://godoc.org/github.com/hako/durafmt?status.svg)](https://godoc.org/github.com/hako/durafmt) +[![Open Source Helpers](https://www.codetriage.com/hako/durafmt/badges/users.svg)](https://www.codetriage.com/hako/durafmt) + + + +durafmt is a tiny Go library that formats `time.Duration` strings into a human readable format. + +``` +go get github.com/hako/durafmt +``` + +# Why + +If you've worked with `time.Duration` in Go, you most likely have come across this: + +``` +53m28.587093086s // :) +``` + +The above seems very easy to read, unless your duration looks like this: + +``` +354h22m3.24s // :S +``` + +# Usage + +### durafmt.ParseString() + +```go +package main + +import ( + "fmt" + "github.com/hako/durafmt" +) + +func main() { + duration, err := durafmt.ParseString("354h22m3.24s") + if err != nil { + fmt.Println(err) + } + fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds + // duration.String() // String representation. "2 weeks 18 hours 22 minutes 3 seconds" +} +``` + +### durafmt.ParseStringShort() + +Version of `durafmt.ParseString()` that only returns the first part of the duration string. + +```go +package main + +import ( + "fmt" + "github.com/hako/durafmt" +) + +func main() { + duration, err := durafmt.ParseStringShort("354h22m3.24s") + if err != nil { + fmt.Println(err) + } + fmt.Println(duration) // 2 weeks + // duration.String() // String short representation. "2 weeks" +} +``` + +### durafmt.Parse() + +```go +package main + +import ( + "fmt" + "time" + "github.com/hako/durafmt" +) + +func main() { + timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second) + duration := durafmt.Parse(timeduration).String() + fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds +} +``` + +### durafmt.ParseShort() + +Version of `durafmt.Parse()` that only returns the first part of the duration string. + +```go +package main + +import ( + "fmt" + "time" + "github.com/hako/durafmt" +) + +func main() { + timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second) + duration := durafmt.ParseShort(timeduration).String() + fmt.Println(duration) // 2 weeks +} +``` + +# Contributing + +Contributions are welcome! Fork this repo and add your changes and submit a PR. + +If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section. + +You can run tests by runnning `go test`. Running `go test; go vet; golint` is recommended. + +durafmt is also tested against `gometalinter`. + +# License + +MIT \ No newline at end of file diff --git a/vendor/github.com/hako/durafmt/durafmt.go b/vendor/github.com/hako/durafmt/durafmt.go new file mode 100644 index 0000000000..730237d57a --- /dev/null +++ b/vendor/github.com/hako/durafmt/durafmt.go @@ -0,0 +1,147 @@ +// Package durafmt formats time.Duration into a human readable format. +package durafmt + +import ( + "errors" + "strconv" + "strings" + "time" +) + +var ( + units = []string{"years", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"} +) + +// Durafmt holds the parsed duration and the original input duration. +type Durafmt struct { + duration time.Duration + input string // Used as reference. + short bool +} + +// Parse creates a new *Durafmt struct, returns error if input is invalid. +func Parse(dinput time.Duration) *Durafmt { + input := dinput.String() + return &Durafmt{dinput, input, false} +} + +// ParseShort creates a new *Durafmt struct, short form, returns error if input is invalid. +func ParseShort(dinput time.Duration) *Durafmt { + input := dinput.String() + return &Durafmt{dinput, input, true} +} + +// ParseString creates a new *Durafmt struct from a string. +// returns an error if input is invalid. +func ParseString(input string) (*Durafmt, error) { + if input == "0" || input == "-0" { + return nil, errors.New("durafmt: missing unit in duration " + input) + } + duration, err := time.ParseDuration(input) + if err != nil { + return nil, err + } + return &Durafmt{duration, input, false}, nil +} + +// ParseStringShort creates a new *Durafmt struct from a string, short form +// returns an error if input is invalid. +func ParseStringShort(input string) (*Durafmt, error) { + if input == "0" || input == "-0" { + return nil, errors.New("durafmt: missing unit in duration " + input) + } + duration, err := time.ParseDuration(input) + if err != nil { + return nil, err + } + return &Durafmt{duration, input, true}, nil +} + +// String parses d *Durafmt into a human readable duration. +func (d *Durafmt) String() string { + var duration string + + // Check for minus durations. + if string(d.input[0]) == "-" { + duration += "-" + d.duration = -d.duration + } + + // Convert duration. + seconds := int64(d.duration.Seconds()) % 60 + minutes := int64(d.duration.Minutes()) % 60 + hours := int64(d.duration.Hours()) % 24 + days := int64(d.duration/(24*time.Hour)) % 365 % 7 + + // Edge case between 364 and 365 days. + // We need to calculate weeks from what is left from years + leftYearDays := int64(d.duration/(24*time.Hour)) % 365 + weeks := leftYearDays / 7 + if leftYearDays >= 364 && leftYearDays < 365 { + weeks = 52 + } + + years := int64(d.duration/(24*time.Hour)) / 365 + milliseconds := int64(d.duration/time.Millisecond) - + (seconds * 1000) - (minutes * 60000) - (hours * 3600000) - + (days * 86400000) - (weeks * 604800000) - (years * 31536000000) + + // Create a map of the converted duration time. + durationMap := map[string]int64{ + "milliseconds": milliseconds, + "seconds": seconds, + "minutes": minutes, + "hours": hours, + "days": days, + "weeks": weeks, + "years": years, + } + + // Construct duration string. + for _, u := range units { + v := durationMap[u] + strval := strconv.FormatInt(v, 10) + switch { + // add to the duration string if v > 1. + case v > 1: + duration += strval + " " + u + " " + // remove the plural 's', if v is 1. + case v == 1: + duration += strval + " " + strings.TrimRight(u, "s") + " " + // omit any value with 0s or 0. + case d.duration.String() == "0" || d.duration.String() == "0s": + // note: milliseconds and minutes have the same suffix (m) + // so we have to check if the units match with the suffix. + + // check for a suffix that is NOT the milliseconds suffix. + if strings.HasSuffix(d.input, string(u[0])) && !strings.Contains(d.input, "ms") { + // if it happens that the units are milliseconds, skip. + if u == "milliseconds" { + continue + } + duration += strval + " " + u + } + // process milliseconds here. + if u == "milliseconds" { + if strings.Contains(d.input, "ms") { + duration += strval + " " + u + break + } + } + break + // omit any value with 0. + case v == 0: + continue + } + } + // trim any remaining spaces. + duration = strings.TrimSpace(duration) + + // if more than 2 spaces present return the first 2 strings + // if short version is requested + if d.short { + duration = strings.Join(strings.Split(duration, " ")[:2], " ") + } + + return duration +}