diff --git a/cmd/climc/shell/k8s/clusters.go b/cmd/climc/shell/k8s/clusters.go index 6580be7cc3..795696217f 100644 --- a/cmd/climc/shell/k8s/clusters.go +++ b/cmd/climc/shell/k8s/clusters.go @@ -26,38 +26,15 @@ import ( func initKubeCluster() { cmdN := func(action string) string { - return fmt.Sprintf("kubecluster-%s", action) + return fmt.Sprintf("k8s-cluster-%s", action) } - R(&o.ClusterListOptions{}, cmdN("list"), "List k8s clusters", func(s *mcclient.ClientSession, args *o.ClusterListOptions) error { - result, err := k8s.KubeClusters.List(s, args.Params()) - if err != nil { - return err - } - printList(result, k8s.KubeClusters.GetColumns(s)) - return nil - }) - - R(&o.IdentOptions{}, cmdN("show"), "Show details of a cluster", func(s *mcclient.ClientSession, args *o.IdentOptions) error { - result, err := k8s.KubeClusters.Get(s, args.ID, nil) - if err != nil { - return err - } - printObject(result) - return nil - }) - - R(&o.KubeClusterCreateOptions{}, cmdN("create"), "Create k8s cluster", func(s *mcclient.ClientSession, args *o.KubeClusterCreateOptions) error { - params, err := args.Params() - if err != nil { - return err - } - cluster, err := k8s.KubeClusters.Create(s, params) - if err != nil { - return err - } - printObject(cluster) - return nil - }) + cmd := NewK8sResourceCmd(k8s.KubeClusters).SetKeyword("cluster") + cmd.List(new(o.ClusterListOptions)) + cmd.Show(new(o.IdentOptions)) + cmd.Create(new(o.KubeClusterCreateOptions)) + cmd.Get("api-resources", new(o.IdentOptions)) + cmd.Get("cluster-users", new(o.IdentOptions)) + cmd.Get("cluster-user-groups", new(o.IdentOptions)) R(&o.KubeClusterImportOptions{}, cmdN("import"), "Import k8s cluster", func(s *mcclient.ClientSession, args *o.KubeClusterImportOptions) error { params, err := args.Params() @@ -201,24 +178,6 @@ func initKubeCluster() { return nil }) - R(&o.IdentOptions{}, cmdN("public"), "Make cluster public", func(s *mcclient.ClientSession, args *o.IdentOptions) error { - ret, err := k8s.KubeClusters.PerformAction(s, args.ID, "public", nil) - if err != nil { - return err - } - printObject(ret) - return nil - }) - - R(&o.IdentOptions{}, cmdN("private"), "Make cluster private", func(s *mcclient.ClientSession, args *o.IdentOptions) error { - ret, err := k8s.KubeClusters.PerformAction(s, args.ID, "private", nil) - if err != nil { - return err - } - printObject(ret) - return nil - }) - R(&o.IdentOptions{}, cmdN("components-status"), "Get cluster component status", func(s *mcclient.ClientSession, args *o.IdentOptions) error { ret, err := k8s.KubeClusters.GetSpecific(s, args.ID, "components-status", nil) if err != nil { diff --git a/cmd/climc/shell/k8s/configmap.go b/cmd/climc/shell/k8s/configmap.go index 1f1d1607e1..a655ec16ee 100644 --- a/cmd/climc/shell/k8s/configmap.go +++ b/cmd/climc/shell/k8s/configmap.go @@ -16,8 +16,11 @@ package k8s import ( "yunion.io/x/onecloud/pkg/mcclient/modules/k8s" + o "yunion.io/x/onecloud/pkg/mcclient/options/k8s" ) func initConfigMap() { initK8sNamespaceResource("configmap", k8s.ConfigMaps) + cmd := NewK8sResourceCmd(k8s.ConfigMaps) + cmd.Create(&o.ConfigMapCreateOptions{}) } diff --git a/cmd/climc/shell/k8s/fed_clusterrole.go b/cmd/climc/shell/k8s/fed_clusterrole.go new file mode 100644 index 0000000000..f5b2067740 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_clusterrole.go @@ -0,0 +1,35 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := newFedResourceCmd(k8s.FederatedClusterRoles) + cmd.Create(new(options.FedClusterRoleCreateOptions)). + List(new(options.FedResourceListOptions)). + Show(new(options.IdentOptions)). + Delete(new(options.IdentOptions)). + AttachCluster(new(options.FedResourceJointClusterAttachOptions)). + DetachCluster(new(options.FedResourceJointClusterDetachOptions)). + SyncCluster(new(options.FedResourceJointClusterDetachOptions)) + + cmd.ClassShow(new(options.FedApiResourecesOptions)) + cmd.ClassShow(new(options.FedClusterUsersOptions)) + cmd.ClassShow(new(options.FedClusterUserGroupsOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_clusterrole_cluster.go b/cmd/climc/shell/k8s/fed_clusterrole_cluster.go new file mode 100644 index 0000000000..e3184dd6d4 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_clusterrole_cluster.go @@ -0,0 +1,27 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := NewK8sJointCmd(&k8s.FederatedClusterRoleClusters) + + cmd.List(new(options.FedJointClusterBaseListOptions)) + cmd.Show(new(options.FedResourceJointClusterShowOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_clusterrolebinding.go b/cmd/climc/shell/k8s/fed_clusterrolebinding.go new file mode 100644 index 0000000000..586dd390f0 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_clusterrolebinding.go @@ -0,0 +1,31 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := newFedResourceCmd(k8s.FederatedClusterRoleBindings) + cmd.Create(new(options.FedClusterRoleBindingCreatOpt)). + List(new(options.FedResourceListOptions)). + Show(new(options.IdentOptions)). + Delete(new(options.IdentOptions)). + AttachCluster(new(options.FedResourceJointClusterAttachOptions)). + DetachCluster(new(options.FedResourceJointClusterDetachOptions)). + SyncCluster(new(options.FedResourceJointClusterDetachOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_helper.go b/cmd/climc/shell/k8s/fed_helper.go new file mode 100644 index 0000000000..80608127e9 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_helper.go @@ -0,0 +1,66 @@ +// 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/onecloud/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/mcclient/modulebase" +) + +type fedResourceCmd struct { + *shell.ResourceCmd +} + +func newFedResourceCmd(manager modulebase.IBaseManager) *fedResourceCmd { + cmd := NewK8sResourceCmd(manager) + return &fedResourceCmd{ + ResourceCmd: cmd, + } +} + +func (c *fedResourceCmd) List(args shell.IListOpt) *fedResourceCmd { + c.ResourceCmd.List(args) + return c +} + +func (c *fedResourceCmd) Show(args shell.IShowOpt) *fedResourceCmd { + c.ResourceCmd.Show(args) + return c +} + +func (c *fedResourceCmd) Create(args shell.ICreateOpt) *fedResourceCmd { + c.ResourceCmd.Create(args) + return c +} + +func (c *fedResourceCmd) Delete(args shell.IDeleteOpt) *fedResourceCmd { + c.ResourceCmd.Delete(args) + return c +} + +func (c *fedResourceCmd) AttachCluster(args shell.IPerformOpt) *fedResourceCmd { + c.ResourceCmd.Perform("attach-cluster", args) + return c +} + +func (c *fedResourceCmd) DetachCluster(args shell.IPerformOpt) *fedResourceCmd { + c.ResourceCmd.Perform("detach-cluster", args) + return c +} + +func (c *fedResourceCmd) SyncCluster(args shell.IPerformOpt) *fedResourceCmd { + c.ResourceCmd.Perform("sync-cluster", args) + return c +} diff --git a/cmd/climc/shell/k8s/fed_namespace.go b/cmd/climc/shell/k8s/fed_namespace.go new file mode 100644 index 0000000000..b4898b90b0 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_namespace.go @@ -0,0 +1,32 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := newFedResourceCmd(k8s.FederatedNamespaces) + + cmd.List(new(options.FedNamespaceListOptions)). + Show(new(options.IdentOptions)). + Create(new(options.FedNamespaceCreateOptions)). + Delete(new(options.IdentOptions)). + AttachCluster(new(options.FedResourceJointClusterAttachOptions)). + DetachCluster(new(options.FedResourceJointClusterDetachOptions)). + SyncCluster(new(options.FedResourceJointClusterDetachOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_namespace_cluster.go b/cmd/climc/shell/k8s/fed_namespace_cluster.go new file mode 100644 index 0000000000..69a577ca0f --- /dev/null +++ b/cmd/climc/shell/k8s/fed_namespace_cluster.go @@ -0,0 +1,27 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := NewK8sJointCmd(&k8s.FederatedNamespaceClusters) + + cmd.List(new(options.FedNamespaceClusterListOptions)) + cmd.Show(new(options.FedResourceJointClusterShowOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_role.go b/cmd/climc/shell/k8s/fed_role.go new file mode 100644 index 0000000000..24497f324f --- /dev/null +++ b/cmd/climc/shell/k8s/fed_role.go @@ -0,0 +1,35 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := newFedResourceCmd(k8s.FederatedRoles) + cmd.Create(new(options.FedRoleCreateOptions)). + List(new(options.FedRoleListOptions)). + Show(new(options.IdentOptions)). + Delete(new(options.IdentOptions)). + AttachCluster(new(options.FedResourceJointClusterAttachOptions)). + DetachCluster(new(options.FedResourceJointClusterDetachOptions)). + SyncCluster(new(options.FedResourceJointClusterDetachOptions)) + + cmd.ClassShow(new(options.FedApiResourecesOptions)) + cmd.ClassShow(new(options.FedClusterUsersOptions)) + cmd.ClassShow(new(options.FedClusterUserGroupsOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_role_cluster.go b/cmd/climc/shell/k8s/fed_role_cluster.go new file mode 100644 index 0000000000..f23224b603 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_role_cluster.go @@ -0,0 +1,27 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := NewK8sJointCmd(&k8s.FederatedRoleClusters) + + cmd.List(new(options.FedNamespaceJointClusterListOpt)) + cmd.Show(new(options.FedResourceJointClusterShowOptions)) +} diff --git a/cmd/climc/shell/k8s/fed_rolebinding.go b/cmd/climc/shell/k8s/fed_rolebinding.go new file mode 100644 index 0000000000..08d6722424 --- /dev/null +++ b/cmd/climc/shell/k8s/fed_rolebinding.go @@ -0,0 +1,32 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := newFedResourceCmd(k8s.FederatedRoleBindings) + + cmd.Create(new(options.FedRoleBindingCreateOpt)). + List(new(options.FedNamespaceResourceListOptions)). + Show(new(options.IdentOptions)). + Delete(new(options.IdentOptions)). + AttachCluster(new(options.FedResourceJointClusterAttachOptions)). + DetachCluster(new(options.FedResourceJointClusterDetachOptions)). + SyncCluster(new(options.FedResourceJointClusterDetachOptions)) +} diff --git a/cmd/climc/shell/k8s/helper.go b/cmd/climc/shell/k8s/helper.go new file mode 100644 index 0000000000..a52ac76556 --- /dev/null +++ b/cmd/climc/shell/k8s/helper.go @@ -0,0 +1,30 @@ +// 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/onecloud/cmd/climc/shell" + "yunion.io/x/onecloud/pkg/mcclient/modulebase" +) + +func NewK8sResourceCmd(manager modulebase.IBaseManager) *shell.ResourceCmd { + return shell.NewResourceCmd(manager).SetPrefix("k8s") +} + +func NewK8sJointCmd(manager modulebase.JointManager) *shell.JointCmd { + cmd := shell.NewJointCmd(manager) + cmd.SetPrefix("k8s") + return cmd +} diff --git a/cmd/climc/shell/k8s/k8s.go b/cmd/climc/shell/k8s/k8s.go index 52b741e536..475a05de00 100644 --- a/cmd/climc/shell/k8s/k8s.go +++ b/cmd/climc/shell/k8s/k8s.go @@ -219,7 +219,11 @@ func NewK8sNsResourceGetCmd(cmdN CmdNameFactory, manager modulebase.Manager) *Cm cmdN.Do("show"), fmt.Sprintf("Show k8s %s", cmdN.Kind), func(s *mcclient.ClientSession, args *o.NamespaceResourceGetOptions) error { - ret, err := manager.Get(s, args.NAME, args.Params()) + params, err := args.Params() + if err != nil { + return err + } + ret, err := manager.Get(s, args.NAME, params) if err != nil { return err } @@ -235,7 +239,11 @@ func NewK8sNsResourceGetRawCmd(cmdN CmdNameFactory, manager k8s.IClusterResource cmdN.Do("show-raw"), fmt.Sprintf("Show k8s %s raw data", cmdN.Kind), func(s *mcclient.ClientSession, args *o.NamespaceResourceGetOptions) error { - ret, err := manager.GetRaw(s, args.NAME, args.Params()) + params, err := args.Params() + if err != nil { + return err + } + ret, err := manager.GetRaw(s, args.NAME, params.(*jsonutils.JSONDict)) if err != nil { return err } @@ -251,7 +259,11 @@ func NewK8sResourceEditRawCmd(cmdN CmdNameFactory, manager k8s.IClusterResourceM cmdN.Do("edit-raw"), fmt.Sprintf("Edit and update k8s %s raw data", cmdN.Kind), func(s *mcclient.ClientSession, args *o.NamespaceResourceGetOptions) error { - rawData, err := manager.GetRaw(s, args.NAME, args.Params()) + params, err := args.Params() + if err != nil { + return err + } + rawData, err := manager.GetRaw(s, args.NAME, params.(*jsonutils.JSONDict)) if err != nil { return err } @@ -286,7 +298,11 @@ func NewK8sResourceEditRawCmd(cmdN CmdNameFactory, manager k8s.IClusterResourceM if err != nil { return err } - if _, err := manager.UpdateRaw(s, args.NAME, args.Params(), body.(*jsonutils.JSONDict)); err != nil { + params, err = args.Params() + if err != nil { + return err + } + if _, err := manager.UpdateRaw(s, args.NAME, params.(*jsonutils.JSONDict), body.(*jsonutils.JSONDict)); err != nil { return err } return nil @@ -313,7 +329,11 @@ func NewK8sNsResourceDeleteCmd(cmdN CmdNameFactory, manager modulebase.Manager) cmdN.Do("delete"), fmt.Sprintf("Delete k8s %s", cmdN.Kind), func(s *mcclient.ClientSession, args *o.NamespaceResourceDeleteOptions) error { - ret := manager.BatchDelete(s, args.NAME, args.Params()) + params, err := args.Params() + if err != nil { + return err + } + ret := manager.BatchDelete(s, args.NAME, params) printBatchResults(ret, manager.GetColumns(s)) return nil }, diff --git a/cmd/climc/shell/k8s/namespace.go b/cmd/climc/shell/k8s/namespace.go index 67f41d7e71..255f81e06e 100644 --- a/cmd/climc/shell/k8s/namespace.go +++ b/cmd/climc/shell/k8s/namespace.go @@ -16,8 +16,12 @@ package k8s import ( "yunion.io/x/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" ) func initNamespace() { initK8sClusterResource("namespace", k8s.Namespaces) + + cmd := NewK8sResourceCmd(k8s.Namespaces) + cmd.Create(new(options.NamespaceCreateOptions)) } diff --git a/cmd/climc/shell/k8s/releases.go b/cmd/climc/shell/k8s/releases.go index 7059d1fb87..e08333beab 100644 --- a/cmd/climc/shell/k8s/releases.go +++ b/cmd/climc/shell/k8s/releases.go @@ -38,7 +38,11 @@ func initRelease() { }) R(&o.NamespaceResourceGetOptions{}, cmdN("show"), "Get helm release details", func(s *mcclient.ClientSession, args *o.NamespaceResourceGetOptions) error { - ret, err := k8s.Releases.Get(s, args.NAME, args.Params()) + params, err := args.Params() + if err != nil { + return err + } + ret, err := k8s.Releases.Get(s, args.NAME, params) if err != nil { return err } diff --git a/cmd/climc/shell/k8s/role.go b/cmd/climc/shell/k8s/role.go new file mode 100644 index 0000000000..b7869f900b --- /dev/null +++ b/cmd/climc/shell/k8s/role.go @@ -0,0 +1,28 @@ +// 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/onecloud/pkg/mcclient/modules/k8s" + options "yunion.io/x/onecloud/pkg/mcclient/options/k8s" +) + +func init() { + cmd := NewK8sResourceCmd(k8s.RbacRoles).SetKeyword("role") + cmd.Create(new(options.RoleCreateOpt)) + cmd.List(new(options.NamespaceResourceListOptions)) + cmd.Show(new(options.NamespaceResourceGetOptions)) + cmd.BatchDelete(new(options.ResourceIdsOptions)) +} diff --git a/pkg/mcclient/modules/k8s/base.go b/pkg/mcclient/modules/k8s/base.go index e6cd251eb4..cd3ca6143d 100644 --- a/pkg/mcclient/modules/k8s/base.go +++ b/pkg/mcclient/modules/k8s/base.go @@ -35,6 +35,14 @@ func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Co return &ResourceManager{man} } +func NewJointManager(keyword, keywordPlural string, columns, adminColumns *Columns, master, slave modulebase.Manager) modulebase.JointResourceManager { + return modulebase.JointResourceManager{ + ResourceManager: *NewResourceManager(keyword, keywordPlural, columns, adminColumns).ResourceManager, + Master: master, + Slave: slave, + } +} + func (m ResourceManager) GetBaseManager() modulebase.ResourceManager { return *m.ResourceManager } @@ -141,6 +149,15 @@ func NewResourceCols(col ...string) *Columns { return NewNameCols("ID").Add(col...) } +func NewFedJointClusterCols(col ...string) *Columns { + return NewColumns( + "Federatedresource_ID", "Federatedresource", + "Cluster_ID", "Cluster", + "Namespace_ID", "Namespace", + "Resource_ID", "Resource", + ) +} + type ListPrinter interface { GetColumns(*mcclient.ClientSession) []string } diff --git a/pkg/mcclient/modules/k8s/fed_clusterrole.go b/pkg/mcclient/modules/k8s/fed_clusterrole.go new file mode 100644 index 0000000000..7756e4672c --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_clusterrole.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modules" +) + +var FederatedClusterRoles *FederatedClusterRoleManager + +func init() { + FederatedClusterRoles = NewFederatedClusterRoleManager() + modules.Register(FederatedClusterRoles) +} + +type FederatedClusterRoleManager struct { + *FederatedResourceManager +} + +func NewFederatedClusterRoleManager() *FederatedClusterRoleManager { + return &FederatedClusterRoleManager{ + FederatedResourceManager: NewFederatedResourceManager("federatedclusterrole", "federatedclusterroles"), + } +} diff --git a/pkg/mcclient/modules/k8s/fed_clusterrole_cluster.go b/pkg/mcclient/modules/k8s/fed_clusterrole_cluster.go new file mode 100644 index 0000000000..fd0224adb4 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_clusterrole_cluster.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + FederatedClusterRoleClusters modulebase.JointResourceManager +) + +func init() { + FederatedClusterRoleClusters = NewJointManager( + "federatedclusterrolecluster", + "federatedclusterroleclusters", + NewFedJointClusterCols(), + NewColumns(), + FederatedClusterRoles, + KubeClusters, + ) + modules.Register(&FederatedClusterRoleClusters) +} diff --git a/pkg/mcclient/modules/k8s/fed_clusterrolebinding.go b/pkg/mcclient/modules/k8s/fed_clusterrolebinding.go new file mode 100644 index 0000000000..1a294a11d5 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_clusterrolebinding.go @@ -0,0 +1,34 @@ +// 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/onecloud/pkg/mcclient/modules" + +var FederatedClusterRoleBindings *FederatedClusterRoleBindingManager + +func init() { + FederatedClusterRoleBindings = NewFederatedClusterRoleBindingManager() + modules.Register(FederatedClusterRoleBindings) +} + +type FederatedClusterRoleBindingManager struct { + *FederatedResourceManager +} + +func NewFederatedClusterRoleBindingManager() *FederatedClusterRoleBindingManager { + return &FederatedClusterRoleBindingManager{ + FederatedResourceManager: NewFederatedResourceManager("federatedclusterrolebinding", "federatedclusterrolebindings"), + } +} diff --git a/pkg/mcclient/modules/k8s/fed_clusterrolebinding_cluster.go b/pkg/mcclient/modules/k8s/fed_clusterrolebinding_cluster.go new file mode 100644 index 0000000000..7114b13580 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_clusterrolebinding_cluster.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + FederatedClusterRoleBindingClusters modulebase.JointResourceManager +) + +func init() { + FederatedClusterRoleBindingClusters = NewJointManager( + "federatedclusterrolebindingcluster", + "federatedclusterrolebindingclusters", + NewFedJointClusterCols(), + NewColumns(), + FederatedClusterRoleBindings, + KubeClusters, + ) + modules.Register(&FederatedClusterRoleBindingClusters) +} diff --git a/pkg/mcclient/modules/k8s/fed_namespace.go b/pkg/mcclient/modules/k8s/fed_namespace.go new file mode 100644 index 0000000000..139db42fc0 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_namespace.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modules" +) + +var FederatedNamespaces *FederatedNamespaceManager + +func init() { + FederatedNamespaces = NewFederatedNamespaceManager() + modules.Register(FederatedNamespaces) +} + +type FederatedNamespaceManager struct { + *FederatedResourceManager +} + +func NewFederatedNamespaceManager() *FederatedNamespaceManager { + return &FederatedNamespaceManager{ + FederatedResourceManager: NewFederatedResourceManager("federatednamespace", "federatednamespaces"), + } +} diff --git a/pkg/mcclient/modules/k8s/fed_namespace_cluster.go b/pkg/mcclient/modules/k8s/fed_namespace_cluster.go new file mode 100644 index 0000000000..83cc43addc --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_namespace_cluster.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + FederatedNamespaceClusters modulebase.JointResourceManager +) + +func init() { + FederatedNamespaceClusters = NewJointManager( + "federatednamespacecluster", + "federatednamespaceclusters", + NewFedJointClusterCols(), + NewColumns(), + FederatedNamespaces, + KubeClusters, + ) + modules.Register(&FederatedNamespaceClusters) +} diff --git a/pkg/mcclient/modules/k8s/fed_resource.go b/pkg/mcclient/modules/k8s/fed_resource.go new file mode 100644 index 0000000000..cd7268384e --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_resource.go @@ -0,0 +1,39 @@ +// 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 + +type FederatedResourceManager struct { + *ResourceManager +} + +func NewFederatedResourceManager(keyword, keywordPlural string) *FederatedResourceManager { + m := FederatedResourceManager{ + ResourceManager: NewResourceManager(keyword, keywordPlural, NewColumns(), NewColumns()), + } + return &m +} + +type FederatedNamespaceResourceManager struct { + *FederatedResourceManager +} + +func NewFederatedNamespaceResourceManager(keyword, keywordPlural string) *FederatedNamespaceResourceManager { + bm := FederatedResourceManager{ + ResourceManager: NewResourceManager(keyword, keywordPlural, NewColumns("federatednamespace_id"), NewColumns()), + } + return &FederatedNamespaceResourceManager{ + FederatedResourceManager: &bm, + } +} diff --git a/pkg/mcclient/modules/k8s/fed_role.go b/pkg/mcclient/modules/k8s/fed_role.go new file mode 100644 index 0000000000..7539dad785 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_role.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modules" +) + +var FederatedRoles *FederatedRoleManager + +func init() { + FederatedRoles = NewFederatedRoleManager() + modules.Register(FederatedRoles) +} + +type FederatedRoleManager struct { + *FederatedNamespaceResourceManager +} + +func NewFederatedRoleManager() *FederatedRoleManager { + return &FederatedRoleManager{ + FederatedNamespaceResourceManager: NewFederatedNamespaceResourceManager("federatedrole", "federatedroles"), + } +} diff --git a/pkg/mcclient/modules/k8s/fed_role_cluster.go b/pkg/mcclient/modules/k8s/fed_role_cluster.go new file mode 100644 index 0000000000..2fd41acac2 --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_role_cluster.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + FederatedRoleClusters modulebase.JointResourceManager +) + +func init() { + FederatedRoleClusters = NewJointManager( + "federatedrolecluster", + "federatedroleclusters", + NewFedJointClusterCols(), + NewColumns(), + FederatedRoles, + KubeClusters, + ) + modules.Register(&FederatedRoleClusters) +} diff --git a/pkg/mcclient/modules/k8s/fed_rolebinding.go b/pkg/mcclient/modules/k8s/fed_rolebinding.go new file mode 100644 index 0000000000..3587034f2e --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_rolebinding.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modules" +) + +var FederatedRoleBindings *FederatedRoleBindingManager + +func init() { + FederatedRoleBindings = NewFederatedRoleBindingManager() + modules.Register(FederatedRoleBindings) +} + +type FederatedRoleBindingManager struct { + *FederatedNamespaceResourceManager +} + +func NewFederatedRoleBindingManager() *FederatedRoleBindingManager { + return &FederatedRoleBindingManager{ + FederatedNamespaceResourceManager: NewFederatedNamespaceResourceManager("federatedrolebinding", "federatedrolebindings"), + } +} diff --git a/pkg/mcclient/modules/k8s/fed_rolebinding_cluster.go b/pkg/mcclient/modules/k8s/fed_rolebinding_cluster.go new file mode 100644 index 0000000000..a52c5b5ffe --- /dev/null +++ b/pkg/mcclient/modules/k8s/fed_rolebinding_cluster.go @@ -0,0 +1,36 @@ +// 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/onecloud/pkg/mcclient/modulebase" + "yunion.io/x/onecloud/pkg/mcclient/modules" +) + +var ( + FederatedRoleBindingClusters modulebase.JointResourceManager +) + +func init() { + FederatedRoleBindingClusters = NewJointManager( + "federatedrolebindingcluster", + "federatedrolebindingclusters", + NewFedJointClusterCols(), + NewColumns(), + FederatedRoles, + KubeClusters, + ) + modules.Register(&FederatedRoleBindingClusters) +} diff --git a/pkg/mcclient/options/k8s/app.go b/pkg/mcclient/options/k8s/app.go index 06f35bed48..96f0e892e4 100644 --- a/pkg/mcclient/options/k8s/app.go +++ b/pkg/mcclient/options/k8s/app.go @@ -113,7 +113,11 @@ type K8sAppCreateFromFileOptions struct { } func (o K8sAppCreateFromFileOptions) Params() (*jsonutils.JSONDict, error) { - params := o.NamespaceResourceGetOptions.Params() + ret, err := o.NamespaceResourceGetOptions.Params() + if err != nil { + return nil, err + } + params := ret.(*jsonutils.JSONDict) params.Add(jsonutils.NewString(o.NAME), "name") content, err := ioutil.ReadFile(o.FILE) if err != nil { diff --git a/pkg/mcclient/options/k8s/base.go b/pkg/mcclient/options/k8s/base.go index 59e169c7f2..8eeab31f3b 100644 --- a/pkg/mcclient/options/k8s/base.go +++ b/pkg/mcclient/options/k8s/base.go @@ -33,16 +33,13 @@ func (o ClusterBaseOptions) Params() *jsonutils.JSONDict { } type ClusterResourceBaseOptions struct { - ClusterBaseOptions - NAME string `help:"Name of resource"` + Cluster string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"` + NAME string `help:"Name of resource"` } -type ClusterResourceCreateOptions struct { - ClusterResourceBaseOptions -} - -func (o ClusterResourceCreateOptions) Params() *jsonutils.JSONDict { - params := o.ClusterBaseOptions.Params() +func (o ClusterResourceBaseOptions) Params() jsonutils.JSONObject { + params := jsonutils.NewDict() + params.Add(jsonutils.NewString(o.Cluster), "cluster") params.Add(jsonutils.NewString(o.NAME), "name") return params } @@ -87,6 +84,10 @@ func (o ResourceGetOptions) Params() *jsonutils.JSONDict { return params } +func (o ResourceGetOptions) GetId() string { + return o.NAME +} + type ResourceDeleteOptions struct { ClusterBaseOptions NAME []string `help:"Name ident of the resources"` @@ -97,13 +98,25 @@ func (o ResourceDeleteOptions) Params() *jsonutils.JSONDict { return params } +type ResourceIdsOptions struct { + ID []string `help:"Resource id"` +} + +func (o ResourceIdsOptions) GetIds() []string { + return o.ID +} + +func (o ResourceIdsOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + type NamespaceResourceListOptions struct { ResourceListOptions Namespace string `help:"Namespace of this resource"` AllNamespace bool `help:"Show resource in all namespace"` } -func (o NamespaceResourceListOptions) Params() (*jsonutils.JSONDict, error) { +func (o NamespaceResourceListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.ResourceListOptions.Params() if err != nil { return nil, err @@ -135,10 +148,10 @@ type NamespaceResourceGetOptions struct { NamespaceOptions } -func (o NamespaceResourceGetOptions) Params() *jsonutils.JSONDict { +func (o NamespaceResourceGetOptions) Params() (jsonutils.JSONObject, error) { params := o.ResourceGetOptions.Params() params.Update(o.NamespaceOptions.Params()) - return params + return params, nil } type NamespaceResourceDeleteOptions struct { @@ -146,10 +159,10 @@ type NamespaceResourceDeleteOptions struct { NamespaceOptions } -func (o NamespaceResourceDeleteOptions) Params() *jsonutils.JSONDict { +func (o NamespaceResourceDeleteOptions) Params() (jsonutils.JSONObject, error) { params := o.ResourceDeleteOptions.Params() params.Update(o.NamespaceOptions.Params()) - return params + return params, nil } type NamespaceWithClusterOptions struct { diff --git a/pkg/mcclient/options/k8s/cluster.go b/pkg/mcclient/options/k8s/cluster.go index d9db496e69..4114494623 100644 --- a/pkg/mcclient/options/k8s/cluster.go +++ b/pkg/mcclient/options/k8s/cluster.go @@ -27,15 +27,25 @@ import ( type ClusterListOptions struct { options.BaseListOptions + // federated resource keyword, e.g: federatednamespace + FederatedKeyword string `json:"federated_keyword"` + FederatedResourceId string `json:"federated_resource_id"` + FederatedUsed *bool `json:"-"` + FederatedUnused *bool `json:"-"` } -func (o ClusterListOptions) Params() *jsonutils.JSONDict { - o.Details = options.Bool(true) - params, err := o.BaseListOptions.Params() +func (o *ClusterListOptions) Params() (jsonutils.JSONObject, error) { + params, err := options.ListStructToParams(o) if err != nil { - panic(err) + return nil, err } - return params + if o.FederatedUnused != nil { + params.Add(jsonutils.JSONFalse, "federated_used") + } + if o.FederatedUsed != nil { + params.Add(jsonutils.JSONTrue, "federated_used") + } + return params, nil } type AddMachineOptions struct { @@ -64,11 +74,6 @@ type KubeClusterCreateOptions struct { AddMachineOptions } -type KubeClusterImportOptions struct { - NAME string `help:"Name of cluster"` - KUBECONFIG string `help:"Cluster kubeconfig file path"` -} - func parseMachineDesc(desc string, disk string, netConf string, ncpu int, memorySize string, hypervisor string) (*MachineCreateOptions, error) { matchType := func(p string) bool { switch p { @@ -111,7 +116,7 @@ func parseMachineDesc(desc string, disk string, netConf string, ncpu int, memory return mo, nil } -func (o KubeClusterCreateOptions) Params() (*jsonutils.JSONDict, error) { +func (o KubeClusterCreateOptions) Params() (jsonutils.JSONObject, error) { params := jsonutils.NewDict() params.Add(jsonutils.NewString(o.NAME), "name") if o.ClusterType != "" { @@ -154,7 +159,13 @@ func (o KubeClusterCreateOptions) Params() (*jsonutils.JSONDict, error) { return params, nil } -func (o KubeClusterImportOptions) Params() (*jsonutils.JSONDict, error) { +type KubeClusterImportOptions struct { + NAME string `help:"Name of cluster"` + KUBECONFIG string `help:"Cluster kubeconfig file path"` + Distro string `help:"Kubernetes distribution, e.g. openshift"` +} + +func (o KubeClusterImportOptions) Params() (jsonutils.JSONObject, error) { kubeconfig, err := ioutil.ReadFile(o.KUBECONFIG) if err != nil { return nil, fmt.Errorf("Read kube config %q error: %v", o.KUBECONFIG, err) @@ -162,9 +173,15 @@ func (o KubeClusterImportOptions) Params() (*jsonutils.JSONDict, error) { params := jsonutils.NewDict() params.Add(jsonutils.NewString(o.NAME), "name") params.Add(jsonutils.NewString("import"), "mode") - params.Add(jsonutils.NewString(string(kubeconfig)), "kubeconfig") params.Add(jsonutils.NewString("external"), "provider") params.Add(jsonutils.NewString("unknown"), "resource_type") + + importData := jsonutils.NewDict() + importData.Add(jsonutils.NewString(string(kubeconfig)), "kubeconfig") + if o.Distro != "" { + importData.Add(jsonutils.NewString(o.Distro), "distribution") + } + params.Add(importData, "import_data") return params, nil } @@ -172,6 +189,14 @@ type IdentOptions struct { ID string `help:"ID or name of the model"` } +func (o IdentOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +func (o IdentOptions) GetId() string { + return o.ID +} + type ClusterSyncOptions struct { IdentOptions Force bool `help:"force sync"` diff --git a/pkg/mcclient/options/k8s/cluster_resource.go b/pkg/mcclient/options/k8s/cluster_resource.go new file mode 100644 index 0000000000..b285b96e0d --- /dev/null +++ b/pkg/mcclient/options/k8s/cluster_resource.go @@ -0,0 +1,46 @@ +// 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/apis" +) + +type ClusterResourceCreateOptions struct { + CLUSTER string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"` + NAME string `help:"Name of resource"` +} + +func (o ClusterResourceCreateOptions) Params() (jsonutils.JSONObject, error) { + params := jsonutils.NewDict() + params.Add(jsonutils.NewString(o.CLUSTER), "cluster_id") + params.Add(jsonutils.NewString(o.NAME), "name") + return params, nil +} + +type NamespaceResourceCreateOptions struct { + apis.DomainLevelResourceCreateInput + CLUSTER string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"` + NAMESPACE string `help:"Namespace of resource"` +} + +func (o NamespaceResourceCreateOptions) Params() (jsonutils.JSONObject, error) { + params := jsonutils.Marshal(o.DomainLevelResourceCreateInput).(*jsonutils.JSONDict) + params.Add(jsonutils.NewString(o.CLUSTER), "cluster_id") + params.Add(jsonutils.NewString(o.NAMESPACE), "namespace_id") + return params, nil +} diff --git a/pkg/mcclient/options/k8s/configmap.go b/pkg/mcclient/options/k8s/configmap.go new file mode 100644 index 0000000000..befbc48085 --- /dev/null +++ b/pkg/mcclient/options/k8s/configmap.go @@ -0,0 +1,44 @@ +// 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/pkg/errors" +) + +type ConfigMapCreateOptions struct { + NamespaceResourceCreateOptions + DataKey []string `help:"configmap data key"` + DataValue []string `help:"configmap data value"` +} + +func (o *ConfigMapCreateOptions) Params() (jsonutils.JSONObject, error) { + params, err := o.NamespaceResourceCreateOptions.Params() + if err != nil { + return nil, err + } + if len(o.DataKey) != len(o.DataValue) { + return nil, errors.Error("data key and value must paired") + } + data := jsonutils.NewDict() + for i := range o.DataKey { + key := o.DataKey[i] + val := o.DataValue[i] + data.Add(jsonutils.NewString(val), key) + } + params.(*jsonutils.JSONDict).Add(data, "data") + return params, nil +} diff --git a/pkg/mcclient/options/k8s/fed_clusterrole.go b/pkg/mcclient/options/k8s/fed_clusterrole.go new file mode 100644 index 0000000000..7929d33589 --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_clusterrole.go @@ -0,0 +1,101 @@ +// 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 ( + "strings" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" +) + +type FedClusterRoleCreateOptions struct { + FedResourceCreateOptions + // Spec FedClusterRoleSpec `json:"spec"` + Rule []string `help:"role rule, e.g: 'apps/v1:deployments:get,watch,list'"` +} + +type FedClusterRoleCreateInput struct { + FedResourceCreateOptions + Spec FedClusterRoleSpec `json:"spec"` +} + +type FedClusterRoleSpec struct { + Template ClusterRoleTemplate `json:"template"` +} + +type ClusterRoleTemplate struct { + Rules []PolicyRule `json:"rules"` +} + +type PolicyRule struct { + APIGroups []string `json:"apiGroups"` + Resources []string `json:"resources"` + Verbs []string `json:"verbs"` +} + +func parsePolicyRules(rules []string) ([]PolicyRule, error) { + ret := make([]PolicyRule, 0) + for _, rule := range rules { + r, err := parsePolicyRule(rule) + if err != nil { + return nil, err + } + ret = append(ret, *r) + } + return ret, nil +} + +func parsePolicyRule(rule string) (*PolicyRule, error) { + parts := strings.Split(rule, ":") + if len(parts) != 3 { + return nil, errors.Errorf("Invalid rule format: %q", rule) + } + groupStr := parts[0] + resourceStr := parts[1] + verbStr := parts[2] + + ret := &PolicyRule{ + Verbs: []string{}, + APIGroups: []string{}, + Resources: []string{}, + } + for _, group := range strings.Split(groupStr, ",") { + ret.APIGroups = append(ret.APIGroups, group) + } + for _, res := range strings.Split(resourceStr, ",") { + ret.Resources = append(ret.Resources, res) + } + for _, verb := range strings.Split(verbStr, ",") { + ret.Verbs = append(ret.Verbs, verb) + } + return ret, nil +} + +func (o *FedClusterRoleCreateOptions) Params() (jsonutils.JSONObject, error) { + rules, err := parsePolicyRules(o.Rule) + if err != nil { + return nil, err + } + input := FedClusterRoleCreateInput{ + FedResourceCreateOptions: o.FedResourceCreateOptions, + Spec: FedClusterRoleSpec{ + Template: ClusterRoleTemplate{ + Rules: rules, + }, + }, + } + return input.JSON(input), nil +} diff --git a/pkg/mcclient/options/k8s/fed_clusterrole_test.go b/pkg/mcclient/options/k8s/fed_clusterrole_test.go new file mode 100644 index 0000000000..0b7889298a --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_clusterrole_test.go @@ -0,0 +1,72 @@ +// 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 ( + "reflect" + "testing" +) + +func Test_parsePolicyRule(t *testing.T) { + tests := []struct { + name string + rule string + want *PolicyRule + wantErr bool + }{ + { + name: "apps/v1:deployments,daemonsets:get,watch", + rule: "apps/v1:deployments,daemonsets:get,watch", + want: &PolicyRule{ + APIGroups: []string{"apps/v1"}, + Resources: []string{"deployments", "daemonsets"}, + Verbs: []string{"get", "watch"}, + }, + wantErr: false, + }, + { + name: "*:*:get,watch", + rule: "*:*:get,watch", + want: &PolicyRule{ + APIGroups: []string{"*"}, + Resources: []string{"*"}, + Verbs: []string{"get", "watch"}, + }, + wantErr: false, + }, + { + name: ":pods,configmaps:*", + rule: ":pods,configmaps:*", + want: &PolicyRule{ + APIGroups: []string{""}, + Resources: []string{"pods", "configmaps"}, + Verbs: []string{"*"}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parsePolicyRule(tt.rule) + if (err != nil) != tt.wantErr { + t.Errorf("parsePolicyRule() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("parsePolicyRule() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/mcclient/options/k8s/fed_clusterrolebinding.go b/pkg/mcclient/options/k8s/fed_clusterrolebinding.go new file mode 100644 index 0000000000..e15da07c1f --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_clusterrolebinding.go @@ -0,0 +1,58 @@ +// 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" +) + +type FedClusterRoleBindingCreatOpt struct { + FedResourceCreateOptions + RoleRef RoleRef `json:"roleRef"` + Subject Subject `help:"Subject is role bind subject, e.g: User=jane"` +} + +func (o *FedClusterRoleBindingCreatOpt) Params() (jsonutils.JSONObject, error) { + input, err := o.ToInput() + if err != nil { + return nil, err + } + return input.JSON(input), nil +} + +func (o *FedClusterRoleBindingCreatOpt) ToInput() (*FedClusterRoleBindingCreateInput, error) { + input := &FedClusterRoleBindingCreateInput{ + FedResourceCreateOptions: o.FedResourceCreateOptions, + } + if err := validateRoleRef(&o.RoleRef); err != nil { + return nil, err + } + if err := validateSubject(&o.Subject); err != nil { + return nil, err + } + input.Spec.Template.RoleRef = o.RoleRef + subs := []Subject{o.Subject} + input.Spec.Template.Subjects = subs + return input, nil +} + +type FedClusterRoleBindingCreateInput struct { + FedResourceCreateOptions + Spec FedClusterRoleBindingSpec `json:"spec"` +} + +type FedClusterRoleBindingSpec struct { + Template RoleBindingTemplate `json:"template"` +} diff --git a/pkg/mcclient/options/k8s/fed_joint_resource.go b/pkg/mcclient/options/k8s/fed_joint_resource.go new file mode 100644 index 0000000000..313cc8530b --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_joint_resource.go @@ -0,0 +1,85 @@ +// 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 FedJointClusterBaseListOptions struct { + options.BaseListOptions + FederatedResourceId string `help:"ID or Name of federated resource" json:"federatedresource_id"` + ClusterId string `help:"ID or Name of cluster"` + NamespaceId string `help:"ID or Name of namespace"` + ResourceId string `help:"ID or Name of resource"` +} + +func (o *FedJointClusterBaseListOptions) GetMasterOpt() string { + return o.FederatedResourceId +} + +func (o *FedJointClusterBaseListOptions) GetSlaveOpt() string { + return o.ClusterId +} + +func (o *FedJointClusterBaseListOptions) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +type FedNamespaceJointClusterListOpt struct { + FedJointClusterBaseListOptions + FederatednamespaceId string `help:"ID or Name of federatednamespace"` +} + +func (o *FedNamespaceJointClusterListOpt) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +type FedResourceJointClusterShowOptions struct { + FedResourceIdOptions + FedResourceClusterShowOptions +} + +func (o FedResourceJointClusterShowOptions) GetMasterId() string { + return o.FEDRESOURCE +} + +func (o FedResourceJointClusterShowOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type FedResourceIdOptions struct { + FEDRESOURCE string `help:"ID or Name of federated resource"` +} + +type FedResourceJointClusterAttachOptions struct { + FedResourceIdOptions + FedResourceClusterJointOptions +} + +func (o *FedResourceJointClusterAttachOptions) GetId() string { + return o.FEDRESOURCE +} + +type FedResourceJointClusterDetachOptions struct { + FedResourceIdOptions + FedResourceClusterJointOptions +} + +func (o *FedResourceJointClusterDetachOptions) GetId() string { + return o.FEDRESOURCE +} diff --git a/pkg/mcclient/options/k8s/fed_namespace.go b/pkg/mcclient/options/k8s/fed_namespace.go new file mode 100644 index 0000000000..2e73f84f9e --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_namespace.go @@ -0,0 +1,42 @@ +// 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 ( + corev1 "k8s.io/api/core/v1" + + "yunion.io/x/jsonutils" +) + +type FedNamespaceListOptions struct { + FedResourceListOptions +} + +type FedNamespaceCreateOptions struct { + FedResourceCreateOptions + Spec FedNamespaceSpec `json:"spec"` +} + +func (o *FedNamespaceCreateOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(o), nil +} + +type FedNamespaceSpec struct { + Template NamespaceTemplate `json:"template"` +} + +type NamespaceTemplate struct { + Spec corev1.NamespaceSpec `json:"spec"` +} diff --git a/pkg/mcclient/options/k8s/fed_namespace_cluster.go b/pkg/mcclient/options/k8s/fed_namespace_cluster.go new file mode 100644 index 0000000000..9968071fc0 --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_namespace_cluster.go @@ -0,0 +1,23 @@ +// 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 + +type FedNamespaceClusterListOptions struct { + FedJointClusterBaseListOptions +} + +type FedNamespaceIdOptions struct { + NAMESPACE string `help:"ID or Name of federated namespace"` +} diff --git a/pkg/mcclient/options/k8s/fed_resource.go b/pkg/mcclient/options/k8s/fed_resource.go new file mode 100644 index 0000000000..cce2d86f1c --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_resource.go @@ -0,0 +1,82 @@ +// 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/apis" + "yunion.io/x/onecloud/pkg/mcclient/options" +) + +type FedResourceListOptions struct { + options.BaseListOptions +} + +func (o *FedResourceListOptions) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +type FedNamespaceResourceListOptions struct { + FedResourceListOptions + Federatednamespace string `json:"federatednamespace"` +} + +func (o *FedNamespaceResourceListOptions) Params() (jsonutils.JSONObject, error) { + return options.ListStructToParams(o) +} + +type FedResourceCreateOptions struct { + apis.DomainLevelResourceCreateInput +} + +type FedResourceClusterJointOptions struct { + ClusterId string `json:"cluster_id" positional:"true" required:"true" help:"ID or name of cluster"` +} + +func (o FedResourceClusterJointOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(o), nil +} + +type FedResourceClusterShowOptions struct { + CLUSTER string `help:"ID or Name of cluster"` +} + +func (o FedResourceClusterShowOptions) GetSlaveId() string { + return o.CLUSTER +} + +type FedNamespaceResourceCreateOptions struct { + FEDNAMESPACE string `help:"Federatednamespace id or name"` + FedResourceCreateOptions +} + +func (o FedNamespaceResourceCreateOptions) Params() (jsonutils.JSONObject, error) { + params := jsonutils.Marshal(o.FedResourceCreateOptions) + params.(*jsonutils.JSONDict).Add(jsonutils.NewString(o.FEDNAMESPACE), "federatednamespace_id") + return params, nil +} + +func (o FedNamespaceResourceCreateOptions) ToInput() FedNamespaceResourceCreateInput { + return FedNamespaceResourceCreateInput{ + FedResourceCreateOptions: o.FedResourceCreateOptions, + FederatednamespaceId: o.FEDNAMESPACE, + } +} + +type FedNamespaceResourceCreateInput struct { + FedResourceCreateOptions + FederatednamespaceId string `json:"federatednamespace_id"` +} diff --git a/pkg/mcclient/options/k8s/fed_role.go b/pkg/mcclient/options/k8s/fed_role.go new file mode 100644 index 0000000000..d3c1118666 --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_role.go @@ -0,0 +1,91 @@ +// 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" +) + +type FedApiResourecesOptions struct{} + +func (opts *FedApiResourecesOptions) GetId() string { + return "api-resources" +} + +func (opts *FedApiResourecesOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type FedClusterUsersOptions struct{} + +func (opts *FedClusterUsersOptions) GetId() string { + return "cluster-users" +} + +func (opts *FedClusterUsersOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type FedClusterUserGroupsOptions struct{} + +func (opts *FedClusterUserGroupsOptions) GetId() string { + return "cluster-user-groups" +} + +func (opts *FedClusterUserGroupsOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +} + +type FedRoleListOptions struct { + FedNamespaceResourceListOptions +} + +type FedRoleCreateOptions struct { + FedNamespaceResourceCreateOptions + Rule []string `help:"role rule, e.g: 'apps/v1:deployments:get,watch,list'"` +} + +type FedRoleCreateInput struct { + FedNamespaceResourceCreateInput + Spec FedRoleSpec `json:"spec"` +} + +type FedRoleSpec struct { + Template RoleTemplate `json:"template"` +} + +type RoleTemplate struct { + Rules []PolicyRule `json:"rules"` +} + +func (o *FedRoleCreateOptions) Params() (jsonutils.JSONObject, error) { + rules := make([]PolicyRule, 0) + for _, rule := range o.Rule { + ret, err := parsePolicyRule(rule) + if err != nil { + return nil, err + } + rules = append(rules, *ret) + } + input := FedRoleCreateInput{ + FedNamespaceResourceCreateInput: o.FedNamespaceResourceCreateOptions.ToInput(), + Spec: FedRoleSpec{ + Template: RoleTemplate{ + Rules: rules, + }, + }, + } + return input.JSON(input), nil +} diff --git a/pkg/mcclient/options/k8s/fed_rolebinding.go b/pkg/mcclient/options/k8s/fed_rolebinding.go new file mode 100644 index 0000000000..ca1bbd416c --- /dev/null +++ b/pkg/mcclient/options/k8s/fed_rolebinding.go @@ -0,0 +1,107 @@ +// 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/pkg/errors" +) + +const ( + RBACAPIGroup = "rbac.authorization.k8s.io" +) + +type FedRoleBindingCreateOpt struct { + FedNamespaceResourceCreateOptions + RoleRef RoleRef `json:"roleRef"` + Subject Subject `help:"Subject is role bind subject, e.g: User=jane"` +} + +func (o *FedRoleBindingCreateOpt) ToInput() (*FedRoleBindingCreateInput, error) { + input := &FedRoleBindingCreateInput{ + FedNamespaceResourceCreateOptions: o.FedNamespaceResourceCreateOptions, + } + if err := validateRoleRef(&o.RoleRef); err != nil { + return nil, err + } + if err := validateSubject(&o.Subject); err != nil { + return nil, err + } + input.Spec.Template.RoleRef = o.RoleRef + subs := []Subject{o.Subject} + input.Spec.Template.Subjects = subs + return input, nil +} + +func (o *FedRoleBindingCreateOpt) Params() (jsonutils.JSONObject, error) { + input, err := o.ToInput() + if err != nil { + return nil, err + } + return input.JSON(input), nil +} + +func validateRoleRef(ref *RoleRef) error { + if ref.APIGroup == "" { + ref.APIGroup = RBACAPIGroup + } + if ref.Kind == "" { + return errors.Errorf("roleRef kind must specified") + } + if ref.Name == "" { + return errors.Errorf("roleRef name must specified") + } + return nil +} + +func validateSubject(sub *Subject) error { + if sub.APIGroup == "" { + sub.APIGroup = RBACAPIGroup + } + if sub.Kind == "" { + return errors.Errorf("subject kind must specified") + } + if sub.Name == "" { + return errors.Errorf("subject name must specified") + } + return nil +} + +type RoleRef struct { + Kind string `help:"Role kind" choices:"ClusterRole|Role" json:"kind"` + Name string `help:"Name is the name of role" json:"name"` + APIGroup string `json:"apiGroup"` +} + +type FedRoleBindingCreateInput struct { + FedNamespaceResourceCreateOptions + Spec FedRoleBindingSpec `json:"spec"` +} + +type FedRoleBindingSpec struct { + Template RoleBindingTemplate `json:"template"` +} + +type RoleBindingTemplate struct { + RoleRef RoleRef `json:"roleRef"` + Subjects []Subject `json:"subjects"` +} + +type Subject struct { + Kind string `json:"kind"` + APIGroup string `json:"apiGroup"` + Name string `json:"name"` + Namespace string `json:"namespace"` +} diff --git a/pkg/mcclient/options/k8s/namespace.go b/pkg/mcclient/options/k8s/namespace.go new file mode 100644 index 0000000000..28f49e39c9 --- /dev/null +++ b/pkg/mcclient/options/k8s/namespace.go @@ -0,0 +1,25 @@ +// 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" + +type NamespaceCreateOptions struct { + ClusterResourceCreateOptions +} + +func (o NamespaceCreateOptions) Params() (jsonutils.JSONObject, error) { + return o.ClusterResourceCreateOptions.Params() +} diff --git a/pkg/mcclient/options/k8s/pvc.go b/pkg/mcclient/options/k8s/pvc.go index 2984fe12d7..b6def6c31e 100644 --- a/pkg/mcclient/options/k8s/pvc.go +++ b/pkg/mcclient/options/k8s/pvc.go @@ -23,13 +23,13 @@ type PVCListOptions struct { Unused bool `help:"Filter unused pvc"` } -func (o PVCListOptions) Params() (*jsonutils.JSONDict, error) { +func (o PVCListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.NamespaceResourceListOptions.Params() if err != nil { return nil, err } if o.Unused { - params.Add(jsonutils.JSONTrue, "unused") + params.(*jsonutils.JSONDict).Add(jsonutils.JSONTrue, "unused") } return params, nil } diff --git a/pkg/mcclient/options/k8s/release.go b/pkg/mcclient/options/k8s/release.go index af321fb341..68933f717f 100644 --- a/pkg/mcclient/options/k8s/release.go +++ b/pkg/mcclient/options/k8s/release.go @@ -32,11 +32,12 @@ type ReleaseListOptions struct { Type string `help:"Release type" choices:"internal|external"` } -func (o ReleaseListOptions) Params() (*jsonutils.JSONDict, error) { - params, err := o.NamespaceResourceListOptions.Params() +func (o ReleaseListOptions) Params() (jsonutils.JSONObject, error) { + ret, err := o.NamespaceResourceListOptions.Params() if err != nil { return nil, err } + params := ret.(*jsonutils.JSONDict) if o.Namespace != "" { params.Add(jsonutils.NewString(o.Namespace), "namespace") } diff --git a/pkg/mcclient/options/k8s/role.go b/pkg/mcclient/options/k8s/role.go new file mode 100644 index 0000000000..69731756c3 --- /dev/null +++ b/pkg/mcclient/options/k8s/role.go @@ -0,0 +1,39 @@ +// 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" +) + +type RoleCreateOpt struct { + NamespaceResourceCreateOptions + + Rule []string `help:"role rule, e.g: 'apps/v1:deployments:get,watch,list'"` +} + +func (o *RoleCreateOpt) Params() (jsonutils.JSONObject, error) { + ret, err := o.NamespaceResourceCreateOptions.Params() + if err != nil { + return nil, err + } + params := ret.(*jsonutils.JSONDict) + rules, err := parsePolicyRules(o.Rule) + if err != nil { + return nil, err + } + params.Add(jsonutils.Marshal(rules), "rules") + return params, nil +} diff --git a/pkg/mcclient/options/k8s/secret.go b/pkg/mcclient/options/k8s/secret.go index 73904fc0b1..c8085a6a16 100644 --- a/pkg/mcclient/options/k8s/secret.go +++ b/pkg/mcclient/options/k8s/secret.go @@ -23,13 +23,13 @@ type SecretListOptions struct { Type string `help:"Secret type"` } -func (o SecretListOptions) Params() (*jsonutils.JSONDict, error) { +func (o SecretListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.NamespaceResourceListOptions.Params() if err != nil { return nil, err } if o.Type != "" { - params.Add(jsonutils.NewString(o.Type), "type") + params.(*jsonutils.JSONDict).Add(jsonutils.NewString(o.Type), "type") } return params, nil } diff --git a/pkg/mcclient/options/k8s/service.go b/pkg/mcclient/options/k8s/service.go index 4722a6297b..85d48796aa 100644 --- a/pkg/mcclient/options/k8s/service.go +++ b/pkg/mcclient/options/k8s/service.go @@ -82,13 +82,13 @@ type ServiceListOptions struct { Type string `help:"Service type" choices:"ClusterIP|LoadBalancer|NodePort|ExternalName"` } -func (o ServiceListOptions) Params() (*jsonutils.JSONDict, error) { +func (o ServiceListOptions) Params() (jsonutils.JSONObject, error) { params, err := o.NamespaceResourceListOptions.Params() if err != nil { return nil, err } if o.Type != "" { - params.Add(jsonutils.NewString(o.Type), "type") + params.(*jsonutils.JSONDict).Add(jsonutils.NewString(o.Type), "type") } return params, nil } diff --git a/pkg/mcclient/options/k8s/storageclass.go b/pkg/mcclient/options/k8s/storageclass.go index 6ea9415e87..ddbd11e1bb 100644 --- a/pkg/mcclient/options/k8s/storageclass.go +++ b/pkg/mcclient/options/k8s/storageclass.go @@ -22,7 +22,8 @@ type StorageClassCreateOptions struct { } func (o *StorageClassCreateOptions) Params(provisioner string) *jsonutils.JSONDict { - params := o.ClusterBaseOptions.Params() + paramsObj, _ := o.ClusterResourceCreateOptions.Params() + params := paramsObj.(*jsonutils.JSONDict) params.Add(jsonutils.NewString(o.NAME), "name") params.Add(jsonutils.NewString(provisioner), "provisioner") return params