climc: add k8s federated resource cmd

This commit is contained in:
Zexi Li
2020-09-16 21:30:43 +08:00
parent d10f7a6d7a
commit 742e455717
49 changed files with 1724 additions and 91 deletions
+17
View File
@@ -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
}
@@ -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"),
}
}
@@ -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)
}
@@ -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"),
}
}
@@ -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)
}
+36
View File
@@ -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"),
}
}
@@ -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)
}
+39
View File
@@ -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,
}
}
+36
View File
@@ -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"),
}
}
@@ -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)
}
@@ -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"),
}
}
@@ -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)
}
+5 -1
View File
@@ -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 {
+26 -13
View File
@@ -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 {
+38 -13
View File
@@ -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"`
@@ -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
}
+44
View File
@@ -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
}
+101
View File
@@ -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
}
@@ -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)
}
})
}
}
@@ -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"`
}
@@ -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
}
+42
View File
@@ -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"`
}
@@ -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"`
}
+82
View File
@@ -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"`
}
+91
View File
@@ -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
}
+107
View File
@@ -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"`
}
+25
View File
@@ -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()
}
+2 -2
View File
@@ -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
}
+3 -2
View File
@@ -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")
}
+39
View File
@@ -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
}
+2 -2
View File
@@ -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
}
+2 -2
View File
@@ -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
}
+2 -1
View File
@@ -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