[v3.2] Add grafana dashboard importing API (#11)

* Add API to import grafana templates to kubesphere dashboard
* Merge and fix the latest codes from kubesphere #2501

Signed-off-by: zhu733756 <talonzhu@yunify.com>
This commit is contained in:
zhu733756
2021-08-16 11:41:29 +08:00
committed by zhu733756
parent 9df6df5544
commit 242ceb54f6
217 changed files with 119028 additions and 96 deletions

View File

@@ -0,0 +1,44 @@
/*
Copyright 2021 The KubeSphere Authors.
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 restclient
import (
rest "k8s.io/client-go/rest"
iamv1alpha2 "kubesphere.io/client-go/restclient/versioned/iam/v1alpha2"
)
// NewForConfig returns a new Client using the provided config and Options.
func NewForConfig(c *rest.Config) (*RestClient, error) {
var rc RestClient
var err error
rc.iamV1alpha2, err = iamv1alpha2.NewForConfig(c)
if err != nil {
return nil, err
}
return &rc, nil
}
// RestClient is a set of restful API clients that doesn't compatible with
// Kube API machinery.
type RestClient struct {
iamV1alpha2 *iamv1alpha2.IamV1alpha2Client
}
// IamV1alpha2 retrieves the IamV1alpha2Client
func (c *RestClient) IamV1alpha2() iamv1alpha2.IamV1alpha2Interface {
return c.iamV1alpha2
}

View File

@@ -0,0 +1,65 @@
/*
Copyright 2021 The KubeSphere Authors.
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 v1alpha2
import (
"context"
resty "github.com/go-resty/resty/v2"
)
type GroupsGetter interface {
Groups() GroupInterface
}
type GroupInterface interface {
CreateBinding(ctx context.Context, workspace, group, user string) (string, error)
}
type groups struct {
client *resty.Client
}
func newGroups(c *IamV1alpha2Client) *groups {
return &groups{
client: c.client,
}
}
//TODO: to be remoted once we move kubesphere.io/apis out of kubesphere package
type groupMember struct {
UserName string `json:"userName"`
GroupName string `json:"groupName"`
}
// Create takes the representation of a group and creates it. Returns the server's representation of the group, and an error, if there is any.
func (c *groups) CreateBinding(ctx context.Context, workspace, group, user string) (result string, err error) {
members := []groupMember{{
UserName: user,
GroupName: group,
}}
resp, err := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(members).
SetPathParams(map[string]string{
"workspace": workspace,
}).
Post("/kapis/iam.kubesphere.io/v1alpha2/workspaces/{workspace}/groupbindings")
return resp.String(), err
}

View File

@@ -0,0 +1,55 @@
/*
Copyright 2020 The KubeSphere Authors.
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 v1alpha2
import (
resty "github.com/go-resty/resty/v2"
rest "k8s.io/client-go/rest"
)
type IamV1alpha2Interface interface {
GroupsGetter
RoleBindingsGetter
}
type IamV1alpha2Client struct {
client *resty.Client
}
func (c *IamV1alpha2Client) Groups() GroupInterface {
return newGroups(c)
}
func (c *IamV1alpha2Client) RoleBindings() RoleBindingInterface {
return newRoleBindings(c)
}
// NewForConfig creates a new IamV1alpha2Client for the given config.
func NewForConfig(c *rest.Config) (*IamV1alpha2Client, error) {
client := resty.New()
client.SetHostURL(c.Host)
if c.BearerToken != "" {
client.SetAuthToken(c.BearerToken)
}
if c.Username != "" {
client.SetBasicAuth(c.Username, c.Password)
}
return &IamV1alpha2Client{client}, nil
}

View File

@@ -0,0 +1,98 @@
/*
Copyright 2020 The KubeSphere Authors.
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 v1alpha2
import (
"context"
resty "github.com/go-resty/resty/v2"
)
type RoleBindingsGetter interface {
RoleBindings() RoleBindingInterface
}
type RoleBindingInterface interface {
CreateRoleBinding(ctx context.Context, namespace, role, group string) (string, error)
CreateWorkspaceRoleBinding(ctx context.Context, namespace, role, group string) (string, error)
}
type rolebindings struct {
client *resty.Client
}
func newRoleBindings(c *IamV1alpha2Client) *rolebindings {
return &rolebindings{
client: c.client,
}
}
// CreateRoleBinding assembling of a rolebinding object and creates it. Returns the server's response and an error, if there is any.
func (c *rolebindings) CreateRoleBinding(ctx context.Context, namespace, role, group string) (result string, err error) {
roles := []map[string]interface{}{{
"subjects": []map[string]interface{}{
{
"kind": "Group",
"apiGroup": "rbac.authorization.k8s.io",
"name": group,
},
},
"roleRef": map[string]interface{}{
"apiGroup": "rbac.authorization.k8s.io",
"kind": "Role",
"name": role,
},
}}
resp, err := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(roles).
SetPathParams(map[string]string{
"namespace": namespace,
}).
Post("/kapis/iam.kubesphere.io/v1alpha2/namespaces/{namespace}/rolebindings")
return resp.String(), err
}
// CreateWorkspaceRoleBinding assembling of a workspacerolebinding object and creates it. Returns the server's response, and an error, if there is any.
func (c *rolebindings) CreateWorkspaceRoleBinding(ctx context.Context, workspace, role, group string) (result string, err error) {
roles := []map[string]interface{}{{
"subjects": []map[string]interface{}{
{
"kind": "Group",
"apiGroup": "rbac.authorization.k8s.io",
"name": group,
},
},
"roleRef": map[string]interface{}{
"apiGroup": "iam.kubesphere.io/v1alpha2",
"kind": "WorkspaceRoleBinding",
"name": role,
},
}}
resp, err := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(roles).
SetPathParams(map[string]string{
"workspace": workspace,
}).
Post("/kapis/iam.kubesphere.io/v1alpha2/workspaces/{workspace}/workspacerolebindings/")
return resp.String(), err
}