未验证 提交 13ffbe89 编写于 作者: H hongming

fix tenant resource list API

Signed-off-by: Nhongming <talonwan@yunify.com>
上级 b71ce477
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/emicklei/go-restful" "github.com/emicklei/go-restful"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/klog" "k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api" "kubesphere.io/kubesphere/pkg/api"
...@@ -56,9 +57,10 @@ func (h *tenantHandler) ListWorkspaces(req *restful.Request, resp *restful.Respo ...@@ -56,9 +57,10 @@ func (h *tenantHandler) ListWorkspaces(req *restful.Request, resp *restful.Respo
} }
func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *restful.Response) { func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *restful.Response) {
user, ok := request.UserFrom(req.Request.Context()) workspace := req.PathParameter("workspace")
queryParam := query.ParseQueryParameter(req) queryParam := query.ParseQueryParameter(req)
workspaceMember, ok := request.UserFrom(req.Request.Context())
if !ok { if !ok {
err := fmt.Errorf("cannot obtain user info") err := fmt.Errorf("cannot obtain user info")
klog.Errorln(err) klog.Errorln(err)
...@@ -66,10 +68,7 @@ func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *rest ...@@ -66,10 +68,7 @@ func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *rest
return return
} }
workspace := req.PathParameter("workspace") result, err := h.tenant.ListFederatedNamespaces(workspaceMember, workspace, queryParam)
result, err := h.tenant.ListFederatedNamespaces(user, workspace, queryParam)
if err != nil { if err != nil {
api.HandleInternalError(resp, nil, err) api.HandleInternalError(resp, nil, err)
return return
...@@ -79,20 +78,26 @@ func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *rest ...@@ -79,20 +78,26 @@ func (h *tenantHandler) ListFederatedNamespaces(req *restful.Request, resp *rest
} }
func (h *tenantHandler) ListNamespaces(req *restful.Request, resp *restful.Response) { func (h *tenantHandler) ListNamespaces(req *restful.Request, resp *restful.Response) {
user, ok := request.UserFrom(req.Request.Context()) workspace := req.PathParameter("workspace")
queryParam := query.ParseQueryParameter(req) queryParam := query.ParseQueryParameter(req)
if !ok { var workspaceMember user.Info
err := fmt.Errorf("cannot obtain user info") if username := req.PathParameter("workspacemember"); username != "" {
klog.Errorln(err) workspaceMember = &user.DefaultInfo{
api.HandleForbidden(resp, nil, err) Name: username,
return }
} else {
requestUser, ok := request.UserFrom(req.Request.Context())
if !ok {
err := fmt.Errorf("cannot obtain user info")
klog.Errorln(err)
api.HandleForbidden(resp, nil, err)
return
}
workspaceMember = requestUser
} }
workspace := req.PathParameter("workspace") result, err := h.tenant.ListNamespaces(workspaceMember, workspace, queryParam)
result, err := h.tenant.ListNamespaces(user, workspace, queryParam)
if err != nil { if err != nil {
api.HandleInternalError(resp, nil, err) api.HandleInternalError(resp, nil, err)
return return
...@@ -102,19 +107,26 @@ func (h *tenantHandler) ListNamespaces(req *restful.Request, resp *restful.Respo ...@@ -102,19 +107,26 @@ func (h *tenantHandler) ListNamespaces(req *restful.Request, resp *restful.Respo
} }
func (h *tenantHandler) ListDevOpsProjects(req *restful.Request, resp *restful.Response) { func (h *tenantHandler) ListDevOpsProjects(req *restful.Request, resp *restful.Response) {
user, ok := request.UserFrom(req.Request.Context()) workspace := req.PathParameter("workspace")
queryParam := query.ParseQueryParameter(req) queryParam := query.ParseQueryParameter(req)
if !ok { var workspaceMember user.Info
err := fmt.Errorf("cannot obtain user info") if username := req.PathParameter("workspacemember"); username != "" {
klog.Errorln(err) workspaceMember = &user.DefaultInfo{
api.HandleForbidden(resp, nil, err) Name: username,
return }
} else {
requestUser, ok := request.UserFrom(req.Request.Context())
if !ok {
err := fmt.Errorf("cannot obtain user info")
klog.Errorln(err)
api.HandleForbidden(resp, nil, err)
return
}
workspaceMember = requestUser
} }
workspace := req.PathParameter("workspace") result, err := h.tenant.ListDevOpsProjects(workspaceMember, workspace, queryParam)
result, err := h.tenant.ListDevOpsProjects(user, workspace, queryParam)
if err != nil { if err != nil {
api.HandleInternalError(resp, nil, err) api.HandleInternalError(resp, nil, err)
......
...@@ -130,6 +130,14 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, k8s ...@@ -130,6 +130,14 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, k8s
Doc("List the devops projects of the specified workspace for the current user"). Doc("List the devops projects of the specified workspace for the current user").
Returns(http.StatusOK, api.StatusOK, api.ListResult{}). Returns(http.StatusOK, api.StatusOK, api.ListResult{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag})) Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag}))
ws.Route(ws.GET("/workspaces/{workspace}/workspacemembers/{workspacemember}/devops").
To(handler.ListDevOpsProjects).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("workspacemember", "workspacemember username")).
Doc("List the devops projects of specified workspace for the workspace member").
Reads(corev1.Namespace{}).
Returns(http.StatusOK, api.StatusOK, corev1.Namespace{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag}))
ws.Route(ws.GET("/workspaces/{workspace}/namespaces/{namespace}"). ws.Route(ws.GET("/workspaces/{workspace}/namespaces/{namespace}").
To(handler.DescribeNamespace). To(handler.DescribeNamespace).
Param(ws.PathParameter("workspace", "workspace name")). Param(ws.PathParameter("workspace", "workspace name")).
...@@ -149,6 +157,14 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, k8s ...@@ -149,6 +157,14 @@ func AddToContainer(c *restful.Container, factory informers.InformerFactory, k8s
Reads(corev1.Namespace{}). Reads(corev1.Namespace{}).
Returns(http.StatusOK, api.StatusOK, corev1.Namespace{}). Returns(http.StatusOK, api.StatusOK, corev1.Namespace{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag})) Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag}))
ws.Route(ws.GET("/workspaces/{workspace}/workspacemembers/{workspacemember}/namespaces").
To(handler.ListNamespaces).
Param(ws.PathParameter("workspace", "workspace name")).
Param(ws.PathParameter("workspacemember", "workspacemember username")).
Doc("List the namespaces of the specified workspace for the workspace member").
Reads(corev1.Namespace{}).
Returns(http.StatusOK, api.StatusOK, corev1.Namespace{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.TenantResourcesTag}))
ws.Route(ws.PUT("/workspaces/{workspace}/namespaces/{namespace}"). ws.Route(ws.PUT("/workspaces/{workspace}/namespaces/{namespace}").
To(handler.UpdateNamespace). To(handler.UpdateNamespace).
Param(ws.PathParameter("workspace", "workspace name")). Param(ws.PathParameter("workspace", "workspace name")).
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册