feat: kubesphere 4.0 (#6115)

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

---------

Signed-off-by: ci-bot <ci-bot@kubesphere.io>
Co-authored-by: ks-ci-bot <ks-ci-bot@example.com>
Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
KubeSphere CI Bot
2024-09-06 11:05:52 +08:00
committed by GitHub
parent b5015ec7b9
commit 447a51f08b
8557 changed files with 546695 additions and 1146174 deletions

View File

@@ -1,18 +1,7 @@
/*
Copyright 2019 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
// Package resources contains resources API versions
package resources

View File

@@ -1,105 +1,43 @@
/*
Copyright 2020 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v1alpha2
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/emicklei/go-restful/v3"
v1 "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/models/components"
"kubesphere.io/kubesphere/pkg/models/git"
"kubesphere.io/kubesphere/pkg/models/kubeconfig"
"kubesphere.io/kubesphere/pkg/models/kubectl"
"kubesphere.io/kubesphere/pkg/models/quotas"
"kubesphere.io/kubesphere/pkg/models/registries"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2/resource"
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
"kubesphere.io/kubesphere/pkg/models/revisions"
"kubesphere.io/kubesphere/pkg/models/routers"
"kubesphere.io/kubesphere/pkg/server/errors"
"kubesphere.io/kubesphere/pkg/server/params"
)
type resourceHandler struct {
resourcesGetter *resource.ResourceGetter
componentsGetter components.ComponentsGetter
type handler struct {
componentsGetter components.Getter
resourceQuotaGetter quotas.ResourceQuotaGetter
revisionGetter revisions.RevisionGetter
routerOperator routers.RouterOperator
gitVerifier git.GitVerifier
registryGetter registries.RegistryGetter
kubeconfigOperator kubeconfig.Interface
kubectlOperator kubectl.Interface
resourceGetter *resourcev1alpha3.Getter
}
func newResourceHandler(k8sClient kubernetes.Interface, factory informers.InformerFactory, masterURL string) *resourceHandler {
return &resourceHandler{
resourcesGetter: resource.NewResourceGetter(factory),
componentsGetter: components.NewComponentsGetter(factory.KubernetesSharedInformerFactory()),
resourceQuotaGetter: quotas.NewResourceQuotaGetter(factory.KubernetesSharedInformerFactory()),
revisionGetter: revisions.NewRevisionGetter(factory.KubernetesSharedInformerFactory()),
routerOperator: routers.NewRouterOperator(k8sClient, factory.KubernetesSharedInformerFactory()),
gitVerifier: git.NewGitVerifier(factory.KubernetesSharedInformerFactory()),
registryGetter: registries.NewRegistryGetter(factory.KubernetesSharedInformerFactory()),
kubeconfigOperator: kubeconfig.NewReadOnlyOperator(factory.KubernetesSharedInformerFactory().Core().V1().ConfigMaps().Lister(), masterURL),
kubectlOperator: kubectl.NewOperator(nil, factory.KubernetesSharedInformerFactory().Apps().V1().Deployments(),
factory.KubernetesSharedInformerFactory().Core().V1().Pods(),
factory.KubeSphereSharedInformerFactory().Iam().V1alpha2().Users(), ""),
}
}
func (r *resourceHandler) handleListNamespaceResources(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
resource := request.PathParameter("resources")
orderBy := params.GetStringValueWithDefault(request, params.OrderByParam, v1alpha2.CreateTime)
limit, offset := params.ParsePaging(request)
reverse := params.GetBoolValueWithDefault(request, params.ReverseParam, false)
conditions, err := params.ParseConditions(request)
if err != nil {
klog.Error(err)
api.HandleBadRequest(response, request, err)
return
}
result, err := r.resourcesGetter.ListResources(namespace, resource, conditions, orderBy, reverse, limit, offset)
if err != nil {
klog.Error(err)
api.HandleInternalError(response, nil, err)
return
}
response.WriteEntity(result)
}
func (r *resourceHandler) handleGetSystemHealthStatus(_ *restful.Request, response *restful.Response) {
result, err := r.componentsGetter.GetSystemHealthStatus()
func (h *handler) GetSystemHealthStatus(_ *restful.Request, response *restful.Response) {
result, err := h.componentsGetter.GetSystemHealthStatus()
if err != nil {
api.HandleInternalError(response, nil, err)
@@ -109,9 +47,9 @@ func (r *resourceHandler) handleGetSystemHealthStatus(_ *restful.Request, respon
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetComponentStatus(request *restful.Request, response *restful.Response) {
func (h *handler) GetComponentStatus(request *restful.Request, response *restful.Response) {
component := request.PathParameter("component")
result, err := r.componentsGetter.GetComponentStatus(component)
result, err := h.componentsGetter.GetComponentStatus(component)
if err != nil {
api.HandleInternalError(response, nil, err)
@@ -121,8 +59,8 @@ func (r *resourceHandler) handleGetComponentStatus(request *restful.Request, res
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetComponents(_ *restful.Request, response *restful.Response) {
result, err := r.componentsGetter.GetAllComponentsStatus()
func (h *handler) GetComponents(_ *restful.Request, response *restful.Response) {
result, err := h.componentsGetter.GetAllComponentsStatus()
if err != nil {
api.HandleInternalError(response, nil, err)
@@ -132,8 +70,8 @@ func (r *resourceHandler) handleGetComponents(_ *restful.Request, response *rest
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetClusterQuotas(_ *restful.Request, response *restful.Response) {
result, err := r.resourceQuotaGetter.GetClusterQuota()
func (h *handler) GetClusterQuotas(_ *restful.Request, response *restful.Response) {
result, err := h.resourceQuotaGetter.GetClusterQuota()
if err != nil {
api.HandleInternalError(response, nil, err)
return
@@ -142,9 +80,9 @@ func (r *resourceHandler) handleGetClusterQuotas(_ *restful.Request, response *r
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetNamespaceQuotas(request *restful.Request, response *restful.Response) {
func (h *handler) GetNamespaceQuotas(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
quota, err := r.resourceQuotaGetter.GetNamespaceQuota(namespace)
quota, err := h.resourceQuotaGetter.GetNamespaceQuota(namespace)
if err != nil {
api.HandleInternalError(response, nil, err)
@@ -154,7 +92,7 @@ func (r *resourceHandler) handleGetNamespaceQuotas(request *restful.Request, res
response.WriteAsJson(quota)
}
func (r *resourceHandler) handleGetDaemonSetRevision(request *restful.Request, response *restful.Response) {
func (h *handler) GetDaemonSetRevision(request *restful.Request, response *restful.Response) {
daemonset := request.PathParameter("daemonset")
namespace := request.PathParameter("namespace")
revision, err := strconv.Atoi(request.PathParameter("revision"))
@@ -164,7 +102,7 @@ func (r *resourceHandler) handleGetDaemonSetRevision(request *restful.Request, r
return
}
result, err := r.revisionGetter.GetDaemonSetRevision(namespace, daemonset, revision)
result, err := h.revisionGetter.GetDaemonSetRevision(namespace, daemonset, revision)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
@@ -174,13 +112,12 @@ func (r *resourceHandler) handleGetDaemonSetRevision(request *restful.Request, r
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetDeploymentRevision(request *restful.Request, response *restful.Response) {
func (h *handler) GetDeploymentRevision(request *restful.Request, response *restful.Response) {
deploy := request.PathParameter("deployment")
namespace := request.PathParameter("namespace")
revision := request.PathParameter("revision")
result, err := r.revisionGetter.GetDeploymentRevision(namespace, deploy, revision)
result, err := h.revisionGetter.GetDeploymentRevision(namespace, deploy, revision)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
return
@@ -189,7 +126,7 @@ func (r *resourceHandler) handleGetDeploymentRevision(request *restful.Request,
response.WriteAsJson(result)
}
func (r *resourceHandler) handleGetStatefulSetRevision(request *restful.Request, response *restful.Response) {
func (h *handler) GetStatefulSetRevision(request *restful.Request, response *restful.Response) {
statefulset := request.PathParameter("statefulset")
namespace := request.PathParameter("namespace")
revision, err := strconv.Atoi(request.PathParameter("revision"))
@@ -198,7 +135,7 @@ func (r *resourceHandler) handleGetStatefulSetRevision(request *restful.Request,
return
}
result, err := r.revisionGetter.GetStatefulSetRevision(namespace, statefulset, revision)
result, err := h.revisionGetter.GetStatefulSetRevision(namespace, statefulset, revision)
if err != nil {
api.HandleInternalError(response, nil, err)
return
@@ -206,83 +143,7 @@ func (r *resourceHandler) handleGetStatefulSetRevision(request *restful.Request,
response.WriteAsJson(result)
}
// Get ingress controller service for specified namespace
func (r *resourceHandler) handleGetRouter(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
router, err := r.routerOperator.GetRouter(namespace)
if err != nil {
if k8serr.IsNotFound(err) {
response.WriteHeaderAndEntity(http.StatusNotFound, errors.Wrap(err))
} else {
api.HandleInternalError(response, nil, err)
}
return
}
response.WriteAsJson(router)
}
// Create ingress controller and related services
func (r *resourceHandler) handleCreateRouter(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
newRouter := api.Router{}
err := request.ReadEntity(&newRouter)
if err != nil {
response.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(fmt.Errorf("wrong annotations, missing key or value")))
return
}
routerType := v1.ServiceTypeNodePort
if strings.Compare(strings.ToLower(newRouter.RouterType), "loadbalancer") == 0 {
routerType = v1.ServiceTypeLoadBalancer
}
router, err := r.routerOperator.CreateRouter(namespace, routerType, newRouter.Annotations)
if err != nil {
api.HandleInternalError(response, nil, err)
return
}
response.WriteAsJson(router)
}
// Delete ingress controller and services
func (r *resourceHandler) handleDeleteRouter(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
router, err := r.routerOperator.DeleteRouter(namespace)
if err != nil {
api.HandleInternalError(response, nil, err)
return
}
response.WriteAsJson(router)
}
func (r *resourceHandler) handleUpdateRouter(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
newRouter := api.Router{}
err := request.ReadEntity(&newRouter)
if err != nil {
response.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
return
}
var routerType = v1.ServiceTypeNodePort
if strings.Compare(strings.ToLower(newRouter.RouterType), "loadbalancer") == 0 {
routerType = v1.ServiceTypeLoadBalancer
}
router, err := r.routerOperator.UpdateRouter(namespace, routerType, newRouter.Annotations)
if err != nil {
api.HandleInternalError(response, nil, err)
return
}
response.WriteAsJson(router)
}
func (r *resourceHandler) handleVerifyGitCredential(request *restful.Request, response *restful.Response) {
func (h *handler) VerifyGitCredential(request *restful.Request, response *restful.Response) {
var credential api.GitCredential
err := request.ReadEntity(&credential)
if err != nil {
@@ -294,7 +155,7 @@ func (r *resourceHandler) handleVerifyGitCredential(request *restful.Request, re
namespace = credential.SecretRef.Namespace
secretName = credential.SecretRef.Name
}
err = r.gitVerifier.VerifyGitCredential(credential.RemoteUrl, namespace, secretName)
err = h.gitVerifier.VerifyGitCredential(credential.RemoteUrl, namespace, secretName)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
return
@@ -302,7 +163,7 @@ func (r *resourceHandler) handleVerifyGitCredential(request *restful.Request, re
response.WriteAsJson(errors.None)
}
func (r *resourceHandler) handleVerifyRegistryCredential(request *restful.Request, response *restful.Response) {
func (h *handler) VerifyRegistryCredential(request *restful.Request, response *restful.Response) {
var credential api.RegistryCredential
err := request.ReadEntity(&credential)
if err != nil {
@@ -310,7 +171,7 @@ func (r *resourceHandler) handleVerifyRegistryCredential(request *restful.Reques
return
}
err = r.registryGetter.VerifyRegistryCredential(credential)
err = h.registryGetter.VerifyRegistryCredential(credential)
if err != nil {
api.HandleBadRequest(response, nil, err)
return
@@ -319,13 +180,13 @@ func (r *resourceHandler) handleVerifyRegistryCredential(request *restful.Reques
response.WriteHeader(http.StatusOK)
}
func (r *resourceHandler) handleGetRegistryEntry(request *restful.Request, response *restful.Response) {
func (h *handler) GetRegistryEntry(request *restful.Request, response *restful.Response) {
imageName := request.QueryParameter("image")
namespace := request.QueryParameter("namespace")
secretName := request.QueryParameter("secret")
insecure := request.QueryParameter("insecure") == "true"
detail, err := r.registryGetter.GetEntry(namespace, secretName, imageName, insecure)
detail, err := h.registryGetter.GetEntry(namespace, secretName, imageName, insecure)
if err != nil {
api.HandleBadRequest(response, nil, err)
return
@@ -334,7 +195,7 @@ func (r *resourceHandler) handleGetRegistryEntry(request *restful.Request, respo
response.WriteAsJson(detail)
}
func (r *resourceHandler) handleGetNamespacedAbnormalWorkloads(request *restful.Request, response *restful.Response) {
func (h *handler) GetNamespacedAbnormalWorkloads(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
result := api.Workloads{
@@ -347,14 +208,17 @@ func (r *resourceHandler) handleGetNamespacedAbnormalWorkloads(request *restful.
switch workloadType {
case api.ResourceKindPersistentVolumeClaim:
notReadyStatus = strings.Join([]string{v1alpha2.StatusPending, v1alpha2.StatusLost}, "|")
notReadyStatus = strings.Join([]string{"pending", "lost"}, "|")
case api.ResourceKindJob:
notReadyStatus = v1alpha2.StatusFailed
notReadyStatus = "failed"
default:
notReadyStatus = v1alpha2.StatusUpdating
notReadyStatus = "updating"
}
res, err := r.resourcesGetter.ListResources(namespace, workloadType, &params.Conditions{Match: map[string]string{v1alpha2.Status: notReadyStatus}}, "", false, -1, 0)
q := query.New()
q.Filters[query.FieldStatus] = query.Value(notReadyStatus)
res, err := h.resourceGetter.List(workloadType, namespace, q)
if err != nil {
api.HandleInternalError(response, nil, err)
}
@@ -363,27 +227,12 @@ func (r *resourceHandler) handleGetNamespacedAbnormalWorkloads(request *restful.
}
response.WriteAsJson(result)
}
func (r *resourceHandler) GetKubectlPod(request *restful.Request, response *restful.Response) {
user := request.PathParameter("user")
func (h *handler) GetKubeconfig(request *restful.Request, response *restful.Response) {
username := request.PathParameter("user")
kubectlPod, err := r.kubectlOperator.GetKubectlPod(user)
if err != nil {
klog.Errorln(err)
response.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
return
}
response.WriteEntity(kubectlPod)
}
func (r *resourceHandler) GetKubeconfig(request *restful.Request, response *restful.Response) {
user := request.PathParameter("user")
kubectlConfig, err := r.kubeconfigOperator.GetKubeConfig(user)
kubectlConfig, err := h.kubeconfigOperator.GetKubeConfig(request.Request.Context(), username)
if err != nil {
klog.Error(err)

View File

@@ -1,41 +1,35 @@
/*
Copyright 2019 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v1alpha2
import (
"net/http"
"github.com/Masterminds/semver/v3"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/api/resource/v1alpha2"
"kubesphere.io/kubesphere/pkg/apiserver/rest"
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models"
"kubesphere.io/kubesphere/pkg/models/components"
"kubesphere.io/kubesphere/pkg/models/git"
gitmodel "kubesphere.io/kubesphere/pkg/models/git"
"kubesphere.io/kubesphere/pkg/models/kubeconfig"
"kubesphere.io/kubesphere/pkg/models/quotas"
"kubesphere.io/kubesphere/pkg/models/registries"
registriesmodel "kubesphere.io/kubesphere/pkg/models/registries"
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
"kubesphere.io/kubesphere/pkg/models/revisions"
"kubesphere.io/kubesphere/pkg/models/terminal"
"kubesphere.io/kubesphere/pkg/server/errors"
"kubesphere.io/kubesphere/pkg/server/params"
)
const (
@@ -44,197 +38,152 @@ const (
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}
func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, factory informers.InformerFactory, masterURL string) error {
webservice := runtime.NewWebService(GroupVersion)
handler := newResourceHandler(k8sClient, factory, masterURL)
func NewHandler(cacheClient runtimeclient.Client, k8sVersion *semver.Version, masterURL string, options *terminal.Options) rest.Handler {
return &handler{
resourceGetter: resourcev1alpha3.NewResourceGetter(cacheClient, k8sVersion),
componentsGetter: components.NewComponentsGetter(cacheClient),
resourceQuotaGetter: quotas.NewResourceQuotaGetter(cacheClient, k8sVersion),
revisionGetter: revisions.NewRevisionGetter(cacheClient),
gitVerifier: git.NewGitVerifier(cacheClient),
registryGetter: registries.NewRegistryGetter(cacheClient),
kubeconfigOperator: kubeconfig.NewReadOnlyOperator(cacheClient, masterURL),
}
}
webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
To(handler.handleListNamespaceResources).
Deprecate().
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Doc("Namespace level resource query").
Param(webservice.PathParameter("namespace", "the name of the project")).
Param(webservice.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
Param(webservice.QueryParameter(params.ConditionsParam, "query conditions,connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
Required(false).
DataFormat("key=%s,key~%s")).
Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
Required(false).
DataFormat("limit=%d,page=%d").
DefaultValue("limit=10,page=1")).
Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}))
func NewFakeHandler() rest.Handler {
return &handler{}
}
webservice.Route(webservice.GET("/{resources}").
To(handler.handleListNamespaceResources).
Deprecate().
Returns(http.StatusOK, api.StatusOK, models.PageableResponse{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
Doc("Cluster level resources").
Param(webservice.PathParameter("resources", "cluster level resource type, e.g. nodes,workspaces,storageclasses,clusterrole.")).
Param(webservice.QueryParameter(params.ConditionsParam, "query conditions, connect multiple conditions with commas, equal symbol for exact query, wave symbol for fuzzy query e.g. name~a").
Required(false).
DataFormat("key=value,key~value").
DefaultValue("")).
Param(webservice.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1").
Required(false).
DataFormat("limit=%d,page=%d").
DefaultValue("limit=10,page=1")).
Param(webservice.QueryParameter(params.ReverseParam, "sort parameters, e.g. reverse=true")).
Param(webservice.QueryParameter(params.OrderByParam, "sort parameters, e.g. orderBy=createTime")))
func (h *handler) AddToContainer(c *restful.Container) error {
ws := runtime.NewWebService(GroupVersion)
webservice.Route(webservice.GET("/users/{user}/kubectl").
To(handler.GetKubectlPod).
Doc("get user's kubectl pod").
Param(webservice.PathParameter("user", "username")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ToolboxTag}).
Returns(http.StatusOK, api.StatusOK, models.PodInfo{}))
webservice.Route(webservice.GET("/users/{user}/kubeconfig").
ws.Route(ws.GET("/users/{user}/kubeconfig").
Produces("text/plain", restful.MIME_JSON).
To(handler.GetKubeconfig).
Doc("get users' kubeconfig").
Param(webservice.PathParameter("user", "username")).
Returns(http.StatusOK, api.StatusOK, "").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ToolboxTag}))
To(h.GetKubeconfig).
Deprecate().
Doc("Get users' kubeconfig").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagUserRelatedResources}).
Param(ws.PathParameter("user", "username")).
Returns(http.StatusOK, api.StatusOK, ""))
webservice.Route(webservice.GET("/components").
To(handler.handleGetComponents).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("List the system components.").
ws.Route(ws.GET("/components").
To(h.GetComponents).
Deprecate().
Doc("List the system components").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Operation("get-components-v1alpha2").
Returns(http.StatusOK, api.StatusOK, []v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/components/{component}").
To(handler.handleGetComponentStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Describe the specified system component.").
Param(webservice.PathParameter("component", "component name")).
ws.Route(ws.GET("/components/{component}").
To(h.GetComponentStatus).
Deprecate().
Doc("Describe the specified system component").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Operation("get-components-status-v1alpha2").
Param(ws.PathParameter("component", "component name")).
Returns(http.StatusOK, api.StatusOK, v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/componenthealth").
To(handler.handleGetSystemHealthStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Get the health status of system components.").
ws.Route(ws.GET("/componenthealth").
To(h.GetSystemHealthStatus).
Deprecate().
Doc("Get the health status of system components").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Operation("get-system-health-status-v1alpha2").
Returns(http.StatusOK, api.StatusOK, v1alpha2.HealthStatus{}))
webservice.Route(webservice.GET("/quotas").
To(handler.handleGetClusterQuotas).
Doc("get whole cluster's resource usage").
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}))
webservice.Route(webservice.GET("/namespaces/{namespace}/quotas").
Doc("get specified namespace's resource quota and usage").
Param(webservice.PathParameter("namespace", "the name of the project")).
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
To(handler.handleGetNamespaceQuotas))
webservice.Route(webservice.POST("registry/verify").
ws.Route(ws.GET("/quotas").
To(h.GetClusterQuotas).
Deprecate().
To(handler.handleVerifyRegistryCredential).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.RegistryTag}).
Doc("verify if a user has access to the docker registry").
Doc("Get whole cluster's resource usage").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagClusterResources}).
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}))
ws.Route(ws.GET("/namespaces/{namespace}/quotas").
To(h.GetNamespaceQuotas).
Deprecate().
Doc("get specified namespace's resource quota and usage").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Returns(http.StatusOK, api.StatusOK, api.ResourceQuota{}))
ws.Route(ws.POST("registry/verify").
To(h.VerifyRegistryCredential).
Deprecate().
Doc("Verify registry credential").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagAdvancedOperations}).
Reads(api.RegistryCredential{}).
Returns(http.StatusOK, api.StatusOK, errors.Error{}))
webservice.Route(webservice.GET("/registry/blob").
ws.Route(ws.GET("/registry/blob").
To(h.GetRegistryEntry).
Deprecate().
To(handler.handleGetRegistryEntry).
Param(webservice.QueryParameter("image", "query image, condition for filtering.").
Doc("Retrieve the blob from the registry").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagAdvancedOperations}).
Param(ws.QueryParameter("image", "query image, condition for filtering.").
Required(true).
DataFormat("image=%s")).
Param(webservice.QueryParameter("namespace", "namespace which secret in.").
Param(ws.QueryParameter("namespace", "namespace which secret in.").
Required(false).
DataFormat("namespace=%s")).
Param(webservice.QueryParameter("secret", "secret name").
Param(ws.QueryParameter("secret", "secret name").
Required(false).
DataFormat("secret=%s")).
Param(webservice.QueryParameter("insecure", "whether verify cert if using https repo").
Param(ws.QueryParameter("insecure", "whether verify cert if using https repo").
Required(false).
DataFormat("insecure=%s")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.RegistryTag}).
Doc("Retrieve the blob from the registry identified").
Writes(registriesmodel.ImageDetails{}).
Returns(http.StatusOK, api.StatusOK, registriesmodel.ImageDetails{}),
)
webservice.Route(webservice.POST("git/verify").
To(handler.handleVerifyGitCredential).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.GitTag}).
Doc("Verify if the kubernetes secret has read access to the git repository").
Returns(http.StatusOK, api.StatusOK, registriesmodel.ImageDetails{}))
ws.Route(ws.POST("git/verify").
To(h.VerifyGitCredential).
Deprecate().
Doc("Verify the git credential").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagAdvancedOperations}).
Reads(gitmodel.AuthInfo{}).
Returns(http.StatusOK, api.StatusOK, errors.Error{}),
)
webservice.Route(webservice.GET("/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}").
To(handler.handleGetDaemonSetRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Doc("Get the specified daemonset revision").
Param(webservice.PathParameter("daemonset", "the name of the daemonset")).
Param(webservice.PathParameter("namespace", "the namespace of the daemonset")).
Param(webservice.PathParameter("revision", "the revision of the daemonset")).
ws.Route(ws.GET("/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}").
To(h.GetDaemonSetRevision).
Deprecate().
Doc("Get the specified daemonSet revision").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("daemonset", "the name of the daemonset")).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("revision", "the revision of the daemonset")).
Returns(http.StatusOK, api.StatusOK, appsv1.DaemonSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}").
To(handler.handleGetDeploymentRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
ws.Route(ws.GET("/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}").
To(h.GetDeploymentRevision).
Deprecate().
Doc("Get the specified deployment revision").
Param(webservice.PathParameter("deployment", "the name of deployment")).
Param(webservice.PathParameter("namespace", "the namespace of the deployment")).
Param(webservice.PathParameter("revision", "the revision of the deployment")).
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("deployment", "the name of deployment")).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("revision", "the revision of the deployment")).
Returns(http.StatusOK, api.StatusOK, appsv1.ReplicaSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/statefulsets/{statefulset}/revisions/{revision}").
To(handler.handleGetStatefulSetRevision).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Doc("Get the specified statefulset revision").
Param(webservice.PathParameter("statefulset", "the name of the statefulset")).
Param(webservice.PathParameter("namespace", "the namespace of the statefulset")).
Param(webservice.PathParameter("revision", "the revision of the statefulset")).
ws.Route(ws.GET("/namespaces/{namespace}/statefulsets/{statefulset}/revisions/{revision}").
To(h.GetStatefulSetRevision).
Deprecate().
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Doc("Get the specified statefulSet revision").
Param(ws.PathParameter("statefulset", "the name of the statefulset")).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("revision", "the revision of the statefulset")).
Returns(http.StatusOK, api.StatusOK, appsv1.StatefulSet{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/router").
ws.Route(ws.GET("/abnormalworkloads").
To(h.GetNamespacedAbnormalWorkloads).
Deprecate().
To(handler.handleGetRouter).
Doc("List router of a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.DELETE("/namespaces/{namespace}/router").
Doc("Get abnormal workloads").
Operation("get-cluster-abnormal-workloads").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagClusterResources}).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}))
ws.Route(ws.GET("/namespaces/{namespace}/abnormalworkloads").
To(h.GetNamespacedAbnormalWorkloads).
Deprecate().
To(handler.handleDeleteRouter).
Doc("List router of a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.POST("/namespaces/{namespace}/router").
Deprecate().
To(handler.handleCreateRouter).
Doc("Create a router for a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.PUT("/namespaces/{namespace}/router").
Deprecate().
To(handler.handleUpdateRouter).
Doc("Update a router for a specified project").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, corev1.Service{}).
Param(webservice.PathParameter("namespace", "the name of the project")))
webservice.Route(webservice.GET("/abnormalworkloads").
Doc("get abnormal workloads' count of whole cluster").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ClusterResourcesTag}).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}).
To(handler.handleGetNamespacedAbnormalWorkloads))
webservice.Route(webservice.GET("/namespaces/{namespace}/abnormalworkloads").
Doc("get abnormal workloads' count of specified namespace").
Param(webservice.PathParameter("namespace", "the name of the project")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NamespaceResourcesTag}).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}).
To(handler.handleGetNamespacedAbnormalWorkloads))
c.Add(webservice)
Doc("Get abnormal workloads").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Operation("get-namespaced-abnormal-workloads").
Param(ws.PathParameter("namespace", "The specified namespace.")).
Returns(http.StatusOK, api.StatusOK, api.Workloads{}))
c.Add(ws)
return nil
}

View File

@@ -1,18 +1,7 @@
/*
Copyright 2020 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v1alpha3
@@ -31,50 +20,42 @@ import (
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/models/components"
v2 "kubesphere.io/kubesphere/pkg/models/registries/v2"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
resourcev1alpha2 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha2/resource"
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
"kubesphere.io/kubesphere/pkg/server/params"
"kubesphere.io/kubesphere/pkg/simple/client/overview"
)
type Handler struct {
resourceGetterV1alpha3 *resourcev1alpha3.ResourceGetter
resourcesGetterV1alpha2 *resourcev1alpha2.ResourceGetter
componentsGetter components.ComponentsGetter
registryHelper v2.RegistryHelper
}
func New(resourceGetterV1alpha3 *resourcev1alpha3.ResourceGetter, resourcesGetterV1alpha2 *resourcev1alpha2.ResourceGetter, componentsGetter components.ComponentsGetter) *Handler {
return &Handler{
resourceGetterV1alpha3: resourceGetterV1alpha3,
resourcesGetterV1alpha2: resourcesGetterV1alpha2,
componentsGetter: componentsGetter,
registryHelper: v2.NewRegistryHelper(),
var (
ClusterMetricNames = []string{
overview.NamespaceCount, overview.PodCount, overview.DeploymentCount,
overview.StatefulSetCount, overview.DaemonSetCount, overview.JobCount,
overview.CronJobCount, overview.PersistentVolumeClaimCount, overview.ServiceCount,
overview.IngressCount, overview.ClusterRoleBindingCount, overview.ClusterRoleCount,
}
NamespaceMetricNames = []string{
overview.PodCount, overview.DeploymentCount, overview.StatefulSetCount,
overview.DaemonSetCount, overview.JobCount, overview.CronJobCount,
overview.PersistentVolumeClaimCount, overview.ServiceCount,
overview.IngressCount, overview.RoleCount, overview.RoleBindingCount,
}
)
type handler struct {
resourceGetterV1alpha3 *resourcev1alpha3.Getter
componentsGetter components.Getter
registryHelper v2.RegistryHelper
counter overview.Counter
}
func (h *Handler) handleGetResources(request *restful.Request, response *restful.Response) {
func (h *handler) GetResources(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
resourceType := request.PathParameter("resources")
name := request.PathParameter("name")
// use informers to retrieve resources
result, err := h.resourceGetterV1alpha3.Get(resourceType, namespace, name)
if err == nil {
response.WriteEntity(result)
return
}
if err != resourcev1alpha3.ErrResourceNotSupported {
klog.Errorf("%s, resource type: %s", err, resourceType)
api.HandleInternalError(response, nil, err)
return
}
// fallback to v1alpha2
resultV1alpha2, err := h.resourcesGetterV1alpha2.GetResource(namespace, resourceType, name)
if err != nil {
if err == resourcev1alpha2.ErrResourceNotSupported {
if err == resourcev1alpha3.ErrResourceNotSupported {
api.HandleNotFound(response, request, err)
return
}
@@ -83,32 +64,18 @@ func (h *Handler) handleGetResources(request *restful.Request, response *restful
return
}
response.WriteEntity(resultV1alpha2)
response.WriteEntity(result)
}
// handleListResources retrieves resources
func (h *Handler) handleListResources(request *restful.Request, response *restful.Response) {
// ListResources retrieves resources
func (h *handler) ListResources(request *restful.Request, response *restful.Response) {
query := query.ParseQueryParameter(request)
resourceType := request.PathParameter("resources")
namespace := request.PathParameter("namespace")
result, err := h.resourceGetterV1alpha3.List(resourceType, namespace, query)
if err == nil {
response.WriteEntity(result)
return
}
if err != resourcev1alpha3.ErrResourceNotSupported {
klog.Errorf("%s, resource type: %s", err, resourceType)
api.HandleInternalError(response, request, err)
return
}
// fallback to v1alpha2
result, err = h.fallback(resourceType, namespace, query)
if err != nil {
if err == resourcev1alpha2.ErrResourceNotSupported {
if err == resourcev1alpha3.ErrResourceNotSupported {
api.HandleNotFound(response, request, err)
return
}
@@ -116,60 +83,11 @@ func (h *Handler) handleListResources(request *restful.Request, response *restfu
api.HandleError(response, request, err)
return
}
response.WriteEntity(result)
}
func (h *Handler) fallback(resourceType string, namespace string, q *query.Query) (*api.ListResult, error) {
orderBy := string(q.SortBy)
limit, offset := q.Pagination.Limit, q.Pagination.Offset
reverse := !q.Ascending
conditions := &params.Conditions{Match: make(map[string]string, 0), Fuzzy: make(map[string]string, 0)}
for field, value := range q.Filters {
switch field {
case query.FieldName:
conditions.Fuzzy[v1alpha2.Name] = string(value)
case query.FieldNames:
conditions.Match[v1alpha2.Name] = string(value)
case query.FieldCreationTimeStamp:
conditions.Match[v1alpha2.CreateTime] = string(value)
case query.FieldLastUpdateTimestamp:
conditions.Match[v1alpha2.UpdateTime] = string(value)
case query.FieldLabel:
values := strings.SplitN(string(value), ":", 2)
if len(values) == 2 {
conditions.Match[values[0]] = values[1]
} else {
conditions.Match[v1alpha2.Label] = values[0]
}
case query.FieldAnnotation:
values := strings.SplitN(string(value), ":", 2)
if len(values) == 2 {
conditions.Match[v1alpha2.Annotation] = values[1]
} else {
conditions.Match[v1alpha2.Annotation] = values[0]
}
case query.FieldStatus:
conditions.Match[v1alpha2.Status] = string(value)
case query.FieldOwnerReference:
conditions.Match[v1alpha2.Owner] = string(value)
default:
conditions.Match[string(field)] = string(value)
}
}
result, err := h.resourcesGetterV1alpha2.ListResources(namespace, resourceType, conditions, orderBy, reverse, limit, offset)
if err != nil {
klog.Error(err)
return nil, err
}
return &api.ListResult{
Items: result.Items,
TotalItems: result.TotalCount,
}, nil
}
func (h *Handler) handleGetComponentStatus(request *restful.Request, response *restful.Response) {
func (h *handler) GetComponentStatus(request *restful.Request, response *restful.Response) {
component := request.PathParameter("component")
result, err := h.componentsGetter.GetComponentStatus(component)
if err != nil {
@@ -180,7 +98,7 @@ func (h *Handler) handleGetComponentStatus(request *restful.Request, response *r
response.WriteEntity(result)
}
func (h *Handler) handleGetSystemHealthStatus(request *restful.Request, response *restful.Response) {
func (h *handler) GetSystemHealthStatus(request *restful.Request, response *restful.Response) {
result, err := h.componentsGetter.GetSystemHealthStatus()
if err != nil {
klog.Error(err)
@@ -192,7 +110,7 @@ func (h *Handler) handleGetSystemHealthStatus(request *restful.Request, response
}
// get all componentsHandler
func (h *Handler) handleGetComponents(request *restful.Request, response *restful.Response) {
func (h *handler) GetComponents(request *restful.Request, response *restful.Response) {
result, err := h.componentsGetter.GetAllComponentsStatus()
if err != nil {
klog.Error(err)
@@ -203,10 +121,10 @@ func (h *Handler) handleGetComponents(request *restful.Request, response *restfu
response.WriteEntity(result)
}
// handleVerifyImageRepositorySecret verifies image secret against registry, it takes k8s.io/api/core/v1/types.Secret
// VerifyImageRepositorySecret verifies image secret against registry, it takes k8s.io/api/core/v1/types.Secret
// as input, and authenticate registry with credential specified. Returns http.StatusOK if authenticate successfully,
// returns http.StatusUnauthorized if failed.
func (h *Handler) handleVerifyImageRepositorySecret(request *restful.Request, response *restful.Response) {
func (h *handler) VerifyImageRepositorySecret(request *restful.Request, response *restful.Response) {
secret := &v1.Secret{}
err := request.ReadEntity(secret)
if err != nil {
@@ -222,8 +140,8 @@ func (h *Handler) handleVerifyImageRepositorySecret(request *restful.Request, re
}
}
// handleGetImageConfig fetches container image spec described in https://github.com/opencontainers/image-spec/blob/main/manifest.md
func (h *Handler) handleGetImageConfig(request *restful.Request, response *restful.Response) {
// GetImageConfig fetches container image spec described in https://github.com/opencontainers/image-spec/blob/main/manifest.md
func (h *handler) GetImageConfig(request *restful.Request, response *restful.Response) {
secretName := request.QueryParameter("secret")
namespace := request.PathParameter("namespace")
image := request.QueryParameter("image")
@@ -247,8 +165,8 @@ func (h *Handler) handleGetImageConfig(request *restful.Request, response *restf
response.WriteHeaderAndJson(http.StatusOK, config, restful.MIME_JSON)
}
// handleGetRepositoryTags fetchs all tags of given repository, no paging.
func (h *Handler) handleGetRepositoryTags(request *restful.Request, response *restful.Response) {
// GetRepositoryTags fetchs all tags of given repository, no paging.
func (h *handler) GetRepositoryTags(request *restful.Request, response *restful.Response) {
secretName := request.QueryParameter("secret")
namespace := request.PathParameter("namespace")
repository := request.QueryParameter("repository")
@@ -285,6 +203,25 @@ func (h *Handler) handleGetRepositoryTags(request *restful.Request, response *re
response.WriteHeaderAndJson(http.StatusOK, tags, restful.MIME_JSON)
}
func (h *handler) GetClusterOverview(request *restful.Request, response *restful.Response) {
metrics, err := h.counter.GetMetrics(ClusterMetricNames, "", "", "cluster")
if err != nil {
api.HandleError(response, request, err)
return
}
_ = response.WriteEntity(metrics)
}
func (h *handler) GetNamespaceOverview(request *restful.Request, response *restful.Response) {
namespace := request.PathParameter("namespace")
metrics, err := h.counter.GetMetrics(NamespaceMetricNames, namespace, "", "namespace")
if err != nil {
api.HandleError(response, request, err)
return
}
_ = response.WriteEntity(metrics)
}
func canonicalizeRegistryError(request *restful.Request, response *restful.Response, err error) {
if strings.Contains(err.Error(), "Unauthorized") {
api.HandleUnauthorized(response, request, err)

View File

@@ -1,18 +1,7 @@
/*
Copyright 2020 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v1alpha3
@@ -25,29 +14,26 @@ import (
"testing"
"unsafe"
"github.com/Masterminds/semver/v3"
"github.com/emicklei/go-restful/v3"
"k8s.io/klog/v2"
"github.com/google/go-cmp/cmp"
fakesnapshot "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/fake"
fakeistio "istio.io/client-go/pkg/clientset/versioned/fake"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
fakeapiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakek8s "k8s.io/client-go/kubernetes/fake"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
runtimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
fakeks "kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/components"
resourcev1alpha2 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha2/resource"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
v2 "kubesphere.io/kubesphere/pkg/models/registries/v2"
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
"kubesphere.io/kubesphere/pkg/scheme"
"kubesphere.io/kubesphere/pkg/simple/client/overview"
)
func TestResourceV1alpha2Fallback(t *testing.T) {
func TestResourceV1alpha3(t *testing.T) {
tests := []struct {
description string
namespace string
@@ -71,7 +57,7 @@ func TestResourceV1alpha2Fallback(t *testing.T) {
},
expectedError: nil,
expected: &api.ListResult{
Items: []interface{}{kubesphereNamespace, defaultNamespace},
Items: []runtime.Object{kubesphereNamespace, defaultNamespace},
TotalItems: 2,
},
},
@@ -90,7 +76,7 @@ func TestResourceV1alpha2Fallback(t *testing.T) {
},
expectedError: nil,
expected: &api.ListResult{
Items: []interface{}{secretFoo2, secretFoo1},
Items: []runtime.Object{secretFoo2, secretFoo1},
TotalItems: 2,
},
},
@@ -103,7 +89,6 @@ func TestResourceV1alpha2Fallback(t *testing.T) {
for _, test := range tests {
got, err := listResources(test.namespace, test.resource, test.query, handler)
if err != test.expectedError {
t.Fatalf("expected error: %s, got: %s", test.expectedError, err)
}
@@ -113,20 +98,12 @@ func TestResourceV1alpha2Fallback(t *testing.T) {
}
}
func listResources(namespace, resourceType string, query *query.Query, h *Handler) (*api.ListResult, error) {
func listResources(namespace, resourceType string, query *query.Query, h *handler) (*api.ListResult, error) {
result, err := h.resourceGetterV1alpha3.List(resourceType, namespace, query)
if err == nil {
return result, nil
}
if err != resource.ErrResourceNotSupported {
if err != nil {
return nil, err
}
// fallback to v1alpha2
return h.fallback(resourceType, namespace, query)
return result, nil
}
var (
@@ -200,51 +177,29 @@ var (
Selector: map[string]string{"app": "ks-controller-app"},
},
}
deployments = []interface{}{redisDeployment, nginxDeployment}
namespaces = []interface{}{defaultNamespace, kubesphereNamespace}
secrets = []interface{}{secretFoo1, secretFoo2}
services = []interface{}{apiServerService, ksControllerService}
deployments = []runtime.Object{redisDeployment, nginxDeployment}
namespaces = []runtime.Object{defaultNamespace, kubesphereNamespace}
secrets = []runtime.Object{secretFoo1, secretFoo2}
services = []runtime.Object{apiServerService, ksControllerService}
)
func prepare() (*Handler, error) {
func prepare() (*handler, error) {
client := runtimefakeclient.NewClientBuilder().
WithScheme(scheme.Scheme).
WithRuntimeObjects(namespaces...).
WithRuntimeObjects(deployments...).
WithRuntimeObjects(secrets...).
WithRuntimeObjects(services...).
Build()
ksClient := fakeks.NewSimpleClientset()
k8sClient := fakek8s.NewSimpleClientset()
istioClient := fakeistio.NewSimpleClientset()
snapshotClient := fakesnapshot.NewSimpleClientset()
apiextensionsClient := fakeapiextensions.NewSimpleClientset()
k8sVersion120, _ := semver.NewVersion("1.20.0")
fakeInformerFactory := informers.NewInformerFactories(k8sClient, ksClient, istioClient, snapshotClient, apiextensionsClient, nil)
k8sInformerFactory := fakeInformerFactory.KubernetesSharedInformerFactory()
for _, namespace := range namespaces {
err := k8sInformerFactory.Core().V1().Namespaces().Informer().GetIndexer().Add(namespace)
if err != nil {
return nil, err
}
handler := &handler{
resourceGetterV1alpha3: resourcev1alpha3.NewResourceGetter(client, k8sVersion120),
componentsGetter: components.NewComponentsGetter(client),
registryHelper: v2.NewRegistryHelper(),
counter: overview.New(client),
}
for _, deployment := range deployments {
err := k8sInformerFactory.Apps().V1().Deployments().Informer().GetIndexer().Add(deployment)
if err != nil {
return nil, err
}
}
for _, secret := range secrets {
err := k8sInformerFactory.Core().V1().Secrets().Informer().GetIndexer().Add(secret)
if err != nil {
return nil, err
}
}
for _, service := range services {
err := k8sInformerFactory.Core().V1().Services().Informer().GetIndexer().Add(service)
if err != nil {
return nil, err
}
}
handler := New(resourcev1alpha3.NewResourceGetter(fakeInformerFactory, nil), resourcev1alpha2.NewResourceGetter(fakeInformerFactory), components.NewComponentsGetter(fakeInformerFactory.KubernetesSharedInformerFactory()))
return handler, nil
}
@@ -261,8 +216,7 @@ func TestHandleGetComponentStatus(t *testing.T) {
t.Fatal("init handler failed")
}
handler.handleGetComponentStatus(request, response)
handler.GetComponentStatus(request, response)
if status := response.StatusCode(); status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
@@ -278,9 +232,7 @@ func TestHandleGetComponents(t *testing.T) {
if err != nil {
t.Fatal("init handler failed")
}
handler.handleGetComponents(request, response)
handler.GetComponents(request, response)
if status := response.StatusCode(); status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)

View File

@@ -1,153 +1,181 @@
/*
Copyright 2019 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package v1alpha3
import (
"net/http"
"github.com/Masterminds/semver/v3"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/cache"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/api/resource/v1alpha2"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/apiserver/rest"
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/components"
v2 "kubesphere.io/kubesphere/pkg/models/registries/v2"
resourcev1alpha2 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha2/resource"
resourcev1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/resource"
"net/http"
"kubesphere.io/kubesphere/pkg/simple/client/overview"
)
const (
GroupName = "resources.kubesphere.io"
tagClusteredResource = "Clustered Resource"
tagComponentStatus = "Component Status"
tagNamespacedResource = "Namespaced Resource"
ok = "OK"
Version = "v1alpha3"
)
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha3"}
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}
func Resource(resource string) schema.GroupResource {
return GroupVersion.WithResource(resource).GroupResource()
}
func AddToContainer(c *restful.Container, informerFactory informers.InformerFactory, cache cache.Cache) error {
func NewHandler(cacheReader runtimeclient.Reader, counter overview.Counter, k8sVersion *semver.Version) rest.Handler {
return &handler{
resourceGetterV1alpha3: resourcev1alpha3.NewResourceGetter(cacheReader, k8sVersion),
componentsGetter: components.NewComponentsGetter(cacheReader),
registryHelper: v2.NewRegistryHelper(),
counter: counter,
}
}
webservice := runtime.NewWebService(GroupVersion)
handler := New(resourcev1alpha3.NewResourceGetter(informerFactory, cache), resourcev1alpha2.NewResourceGetter(informerFactory), components.NewComponentsGetter(informerFactory.KubernetesSharedInformerFactory()))
func NewFakeHandler() rest.Handler {
return &handler{}
}
webservice.Route(webservice.GET("/{resources}").
To(handler.handleListResources).
Metadata(restfulspec.KeyOpenAPITags, []string{tagClusteredResource}).
func (h *handler) AddToContainer(c *restful.Container) error {
ws := runtime.NewWebService(GroupVersion)
ws.Route(ws.GET("/{resources}").
To(h.ListResources).
Deprecate().
Doc("Cluster level resources").
Param(webservice.PathParameter("resources", "cluster level resource type, e.g. pods,jobs,configmaps,services.")).
Param(webservice.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
Param(webservice.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(webservice.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(webservice.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Param(webservice.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, ok, api.ListResult{}))
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagClusterResources}).
Operation("list-cluster-resources").
Param(ws.PathParameter("resources", "cluster level resource type, e.g. pods,jobs,configmaps,services.")).
Param(ws.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Returns(http.StatusOK, api.StatusOK, api.ListResult{}))
webservice.Route(webservice.GET("/{resources}/{name}").
To(handler.handleGetResources).
Metadata(restfulspec.KeyOpenAPITags, []string{tagClusteredResource}).
Doc("Cluster level resource").
Param(webservice.PathParameter("resources", "cluster level resource type, e.g. pods,jobs,configmaps,services.")).
Param(webservice.PathParameter("name", "the name of the clustered resources")).
ws.Route(ws.GET("/{resources}/{name}").
To(h.GetResources).
Deprecate().
Doc("Get cluster scope resource").
Operation("get-cluster-resource").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagClusterResources}).
Param(ws.PathParameter("resources", "cluster level resource type, e.g. pods,jobs,configmaps,services.")).
Param(ws.PathParameter("name", "the name of the clustered resources")).
Returns(http.StatusOK, api.StatusOK, nil))
webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}").
To(handler.handleListResources).
Metadata(restfulspec.KeyOpenAPITags, []string{tagNamespacedResource}).
Doc("Namespace level resource query").
Param(webservice.PathParameter("namespace", "the name of the project")).
Param(webservice.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
Param(webservice.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
Param(webservice.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(webservice.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(webservice.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Param(webservice.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Param(webservice.QueryParameter(query.ParameterFieldSelector, "field selector used for filtering, you can use the = , == and != operators with field selectors( = and == mean the same thing), e.g. fieldSelector=type=kubernetes.io/dockerconfigjson, multiple separated by comma").Required(false)).
Returns(http.StatusOK, ok, api.ListResult{}))
ws.Route(ws.GET("/namespaces/{namespace}/{resources}").
To(h.ListResources).
Deprecate().
Doc("List resources at namespace scope").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Operation("list-namespaced-resources").
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
Param(ws.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")).
Param(ws.QueryParameter(query.ParameterFieldSelector, "field selector used for filtering, you can use the = , == and != operators with field selectors( = and == mean the same thing), e.g. fieldSelector=type=kubernetes.io/dockerconfigjson, multiple separated by comma").Required(false)).
Returns(http.StatusOK, api.StatusOK, api.ListResult{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/{resources}/{name}").
To(handler.handleGetResources).
Metadata(restfulspec.KeyOpenAPITags, []string{tagNamespacedResource}).
Doc("Namespace level get resource query").
Param(webservice.PathParameter("namespace", "the name of the project")).
Param(webservice.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
Param(webservice.PathParameter("name", "the name of resource")).
Returns(http.StatusOK, ok, api.ListResult{}))
ws.Route(ws.GET("/namespaces/{namespace}/{resources}/{name}").
To(h.GetResources).
Deprecate().
Doc("Get namespace scope resource").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Operation("get-namespaced-resource").
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("resources", "namespace level resource type, e.g. pods,jobs,configmaps,services.")).
Param(ws.PathParameter("name", "the name of resource")).
Returns(http.StatusOK, api.StatusOK, api.ListResult{}))
webservice.Route(webservice.GET("/components").
To(handler.handleGetComponents).
Metadata(restfulspec.KeyOpenAPITags, []string{tagComponentStatus}).
Doc("List the system components.").
Returns(http.StatusOK, ok, []v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/components/{component}").
To(handler.handleGetComponentStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{tagComponentStatus}).
Doc("Describe the specified system component.").
Param(webservice.PathParameter("component", "component name")).
Returns(http.StatusOK, ok, v1alpha2.ComponentStatus{}))
webservice.Route(webservice.GET("/componenthealth").
To(handler.handleGetSystemHealthStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{tagComponentStatus}).
Doc("Get the health status of system components.").
Returns(http.StatusOK, ok, v1alpha2.HealthStatus{}))
ws.Route(ws.GET("/components").
To(h.GetComponents).
Deprecate().
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Doc("List the system components").
Operation("get-components-v1alpha3").
Returns(http.StatusOK, api.StatusOK, []v1alpha2.ComponentStatus{}))
ws.Route(ws.GET("/components/{component}").
To(h.GetComponentStatus).
Deprecate().
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Doc("Describe the specified system component").
Operation("get-components-status-v1alpha3").
Param(ws.PathParameter("component", "component name")).
Returns(http.StatusOK, api.StatusOK, v1alpha2.ComponentStatus{}))
ws.Route(ws.GET("/componenthealth").
To(h.GetSystemHealthStatus).
Deprecate().
Doc("Get the health status of system components").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagComponentStatus}).
Operation("get-system-health-status-v1alpha3").
Returns(http.StatusOK, api.StatusOK, v1alpha2.HealthStatus{}))
webservice.Route(webservice.POST("/namespaces/{namespace}/registrysecrets/{secret}/verify").
To(handler.handleVerifyImageRepositorySecret).
Param(webservice.PathParameter("namespace", "Namespace of the image repository secret to create.").Required(true)).
Param(webservice.PathParameter("secret", "Name of the secret name").Required(true)).
Param(webservice.BodyParameter("secretSpec", "Secret specification, definition in k8s.io/api/core/v1/types.Secret")).
ws.Route(ws.POST("/namespaces/{namespace}/registrysecrets/{secret}/verify").
To(h.VerifyImageRepositorySecret).
Deprecate().
Doc("Verify registry credential").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.PathParameter("secret", "Name of the secret.")).
Reads(v1.Secret{}).
Metadata(restfulspec.KeyOpenAPITags, []string{tagNamespacedResource}).
Doc("Verify image repostiry secret.").
Returns(http.StatusOK, ok, v1.Secret{}))
Returns(http.StatusOK, api.StatusOK, v1.Secret{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/imageconfig").
To(handler.handleGetImageConfig).
Param(webservice.PathParameter("namespace", "Namespace of the image repository secret.").Required(true)).
Param(webservice.QueryParameter("secret", "Secret name of the image repository credential, left empty means anonymous fetch.").Required(false)).
Param(webservice.QueryParameter("image", "Image name to query, e.g. kubesphere/ks-apiserver:v3.1.1").Required(true)).
Metadata(restfulspec.KeyOpenAPITags, []string{tagNamespacedResource}).
Doc("Get image config.").
Returns(http.StatusOK, ok, v2.ImageConfig{}))
ws.Route(ws.GET("/namespaces/{namespace}/imageconfig").
To(h.GetImageConfig).
Deprecate().
Doc("Get image config").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.QueryParameter("secret", "Secret name of the image repository credential, left empty means anonymous fetch.").Required(false)).
Param(ws.QueryParameter("image", "Image name to query, e.g. kubesphere/ks-apiserver:v3.1.1").Required(true)).
Returns(http.StatusOK, api.StatusOK, v2.ImageConfig{}))
webservice.Route(webservice.GET("/namespaces/{namespace}/repositorytags").
To(handler.handleGetRepositoryTags).
Param(webservice.PathParameter("namespace", "Namespace of the image repository secret.").Required(true)).
Param(webservice.QueryParameter("repository", "Repository to query, e.g. calico/cni.").Required(true)).
Param(webservice.QueryParameter("secret", "Secret name of the image repository credential, left empty means anonymous fetch.").Required(false)).
Param(webservice.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(webservice.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(webservice.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Doc("List repository tags, this is an experimental API, use it by your own caution.").
Returns(http.StatusOK, ok, v2.RepositoryTags{}))
ws.Route(ws.GET("/namespaces/{namespace}/repositorytags").
To(h.GetRepositoryTags).
Deprecate().
Doc("List image tags").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Notes("List repository tags, this is an experimental API, use it by your own caution.").
Param(ws.PathParameter("namespace", "The specified namespace.")).
Param(ws.QueryParameter("repository", "Repository to query, e.g. calico/cni.").Required(true)).
Param(ws.QueryParameter("secret", "Secret name of the image repository credential, left empty means anonymous fetch.").Required(false)).
Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")).
Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)).
Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")).
Returns(http.StatusOK, api.StatusOK, v2.RepositoryTags{}))
c.Add(webservice)
ws.Route(ws.GET("/metrics").
To(h.GetClusterOverview).
Deprecate().
Doc("Cluster summary").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagClusterResources}).
Returns(http.StatusOK, api.StatusOK, overview.MetricResults{}))
ws.Route(ws.GET("/namespaces/{namespace}/metrics").
To(h.GetNamespaceOverview).
Deprecate().
Doc("Namespace summary").
Metadata(restfulspec.KeyOpenAPITags, []string{api.TagNamespacedResources}).
Param(ws.PathParameter("namespace", "The specified namespace.")).
Returns(http.StatusOK, api.StatusOK, overview.MetricResults{}))
c.Add(ws)
return nil
}