package v1alpha2 import ( "fmt" "github.com/emicklei/go-restful" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/kubernetes" "k8s.io/klog" "kubesphere.io/kubesphere/pkg/api" auditingv1alpha1 "kubesphere.io/kubesphere/pkg/api/auditing/v1alpha1" eventsv1alpha1 "kubesphere.io/kubesphere/pkg/api/events/v1alpha1" loggingv1alpha2 "kubesphere.io/kubesphere/pkg/api/logging/v1alpha2" tenantv1alpha2 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha2" "kubesphere.io/kubesphere/pkg/apiserver/query" "kubesphere.io/kubesphere/pkg/apiserver/request" kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" "kubesphere.io/kubesphere/pkg/informers" "kubesphere.io/kubesphere/pkg/models/tenant" servererr "kubesphere.io/kubesphere/pkg/server/errors" "kubesphere.io/kubesphere/pkg/simple/client/auditing" "kubesphere.io/kubesphere/pkg/simple/client/events" "kubesphere.io/kubesphere/pkg/simple/client/logging" ) type tenantHandler struct { tenant tenant.Interface } func newTenantHandler(factory informers.InformerFactory, k8sclient kubernetes.Interface, ksclient kubesphere.Interface, evtsClient events.Client, loggingClient logging.Interface, auditingclient auditing.Client) *tenantHandler { return &tenantHandler{ tenant: tenant.New(factory, k8sclient, ksclient, evtsClient, loggingClient, auditingclient), } } func (h *tenantHandler) ListWorkspaces(req *restful.Request, resp *restful.Response) { user, ok := request.UserFrom(req.Request.Context()) queryParam := query.ParseQueryParameter(req) if !ok { err := fmt.Errorf("cannot obtain user info") klog.Errorln(err) api.HandleForbidden(resp, nil, err) return } result, err := h.tenant.ListWorkspaces(user, queryParam) if err != nil { api.HandleInternalError(resp, nil, err) return } resp.WriteEntity(result) } func (h *tenantHandler) ListNamespaces(req *restful.Request, resp *restful.Response) { user, ok := request.UserFrom(req.Request.Context()) queryParam := query.ParseQueryParameter(req) if !ok { err := fmt.Errorf("cannot obtain user info") klog.Errorln(err) api.HandleForbidden(resp, nil, err) return } workspace := req.PathParameter("workspace") result, err := h.tenant.ListNamespaces(user, workspace, queryParam) if err != nil { api.HandleInternalError(resp, nil, err) return } resp.WriteEntity(result) } func (h *tenantHandler) CreateNamespace(request *restful.Request, response *restful.Response) { workspace := request.PathParameter("workspace") var namespace corev1.Namespace err := request.ReadEntity(&namespace) if err != nil { klog.Error(err) api.HandleBadRequest(response, request, err) return } created, err := h.tenant.CreateNamespace(workspace, &namespace) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } api.HandleBadRequest(response, request, err) return } response.WriteEntity(created) } func (h *tenantHandler) CreateWorkspace(request *restful.Request, response *restful.Response) { var workspace tenantv1alpha2.WorkspaceTemplate err := request.ReadEntity(&workspace) if err != nil { klog.Error(err) api.HandleBadRequest(response, request, err) return } created, err := h.tenant.CreateWorkspace(&workspace) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } api.HandleBadRequest(response, request, err) return } response.WriteEntity(created) } func (h *tenantHandler) DeleteWorkspace(request *restful.Request, response *restful.Response) { workspace := request.PathParameter("workspace") err := h.tenant.DeleteWorkspace(workspace) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } api.HandleBadRequest(response, request, err) return } response.WriteEntity(servererr.None) } func (h *tenantHandler) UpdateWorkspace(request *restful.Request, response *restful.Response) { workspaceName := request.PathParameter("workspace") var workspace tenantv1alpha2.WorkspaceTemplate err := request.ReadEntity(&workspace) if err != nil { klog.Error(err) api.HandleBadRequest(response, request, err) return } if workspaceName != workspace.Name { err := fmt.Errorf("the name of the object (%s) does not match the name on the URL (%s)", workspace.Name, workspaceName) klog.Errorf("%+v", err) api.HandleBadRequest(response, request, err) return } updated, err := h.tenant.UpdateWorkspace(&workspace) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } if errors.IsBadRequest(err) { api.HandleBadRequest(response, request, err) return } api.HandleInternalError(response, request, err) return } response.WriteEntity(updated) } func (h *tenantHandler) DescribeWorkspace(request *restful.Request, response *restful.Response) { workspaceName := request.PathParameter("workspace") workspace, err := h.tenant.DescribeWorkspace(workspaceName) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } api.HandleInternalError(response, request, err) return } response.WriteEntity(workspace) } func (h *tenantHandler) ListWorkspaceClusters(request *restful.Request, response *restful.Response) { workspaceName := request.PathParameter("workspace") result, err := h.tenant.ListWorkspaceClusters(workspaceName) if err != nil { klog.Error(err) if errors.IsNotFound(err) { api.HandleNotFound(response, request, err) return } api.HandleInternalError(response, request, err) return } response.WriteEntity(result) } func (h *tenantHandler) Events(req *restful.Request, resp *restful.Response) { user, ok := request.UserFrom(req.Request.Context()) if !ok { err := fmt.Errorf("cannot obtain user info") klog.Errorln(err) api.HandleForbidden(resp, req, err) return } queryParam, err := eventsv1alpha1.ParseQueryParameter(req) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } result, err := h.tenant.Events(user, queryParam) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } resp.WriteEntity(result) } func (h *tenantHandler) QueryLogs(req *restful.Request, resp *restful.Response) { user, ok := request.UserFrom(req.Request.Context()) if !ok { err := fmt.Errorf("cannot obtain user info") klog.Errorln(err) api.HandleForbidden(resp, req, err) return } queryParam, err := loggingv1alpha2.ParseQueryParameter(req) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } if queryParam.Operation == loggingv1alpha2.OperationExport { resp.Header().Set(restful.HEADER_ContentType, "text/plain") resp.Header().Set("Content-Disposition", "attachment") err := h.tenant.ExportLogs(user, queryParam, resp) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } } else { result, err := h.tenant.QueryLogs(user, queryParam) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } resp.WriteAsJson(result) } } func (h *tenantHandler) Auditing(req *restful.Request, resp *restful.Response) { user, ok := request.UserFrom(req.Request.Context()) if !ok { err := fmt.Errorf("cannot obtain user info") klog.Errorln(err) api.HandleForbidden(resp, req, err) return } queryParam, err := auditingv1alpha1.ParseQueryParameter(req) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } result, err := h.tenant.Auditing(user, queryParam) if err != nil { klog.Errorln(err) api.HandleInternalError(resp, req, err) return } _ = resp.WriteEntity(result) }