add swagger ui

This commit is contained in:
richardxz
2018-07-11 17:08:57 +08:00
parent 5091a87c1e
commit a4a48eb4fc
20 changed files with 396 additions and 18 deletions

View File

@@ -21,14 +21,23 @@ import (
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/models"
)
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.GET(subPath).To(getClusterQuota).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath + "/namespaces/{namespace}").To(getNamespaceQuota).Produces(restful.MIME_JSON))
tags := []string{"quota"}
ws.Route(ws.GET(subPath).To(getClusterQuota).Produces(restful.MIME_JSON).Doc("get whole "+
"cluster's resource usage").Writes(models.ResourceQuota{}).Metadata(restfulspec.KeyOpenAPITags, tags))
ws.Route(ws.GET(subPath+"/namespaces/{namespace}").Doc("get specified namespace's resource "+
"quota and usage").Param(ws.PathParameter("namespace",
"namespace's name").DataType("string")).Writes(models.ResourceQuota{}).
Metadata(restfulspec.KeyOpenAPITags, tags).To(getNamespaceQuota).Produces(restful.MIME_JSON))
}

View File

@@ -21,13 +21,21 @@ import (
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/models"
)
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.GET(subPath + "/{resource}").To(listResource).Produces(restful.MIME_JSON))
tags := []string{"resources"}
ws.Route(ws.GET(subPath+"/{resource}").To(listResource).Produces(restful.MIME_JSON).Metadata(restfulspec.KeyOpenAPITags, tags).Doc("Get resource" +
" list").Param(ws.PathParameter("resource", "resource name").DataType(
"string")).Param(ws.QueryParameter("conditions", "search "+
"conditions").DataType("string")).Param(ws.QueryParameter("paging",
"support paging function").DataType("string")).Writes(models.ResourceList{}))
}

View File

@@ -22,6 +22,8 @@ import (
"github.com/emicklei/go-restful"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/emicklei/go-restful-openapi"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/models"
"kubesphere.io/kubesphere/pkg/models/iam"
@@ -29,10 +31,17 @@ import (
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.POST(subPath).To(createUser).Consumes("*/*").Produces(restful.MIME_JSON))
ws.Route(ws.DELETE(subPath).To(delUser).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath + "/kubectl").To(getKubectl).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath + "/kubeconfig").To(getKubeconfig).Produces(restful.MIME_JSON))
tags := []string{"users"}
ws.Route(ws.POST(subPath).Doc("create user").Param(ws.PathParameter("user",
"the username to be created").DataType("string")).Metadata(restfulspec.KeyOpenAPITags, tags).
To(createUser).Consumes("*/*").Produces(restful.MIME_JSON))
ws.Route(ws.DELETE(subPath).Doc("delete user").Param(ws.PathParameter("user",
"the username to be deleted").DataType("string")).Metadata(restfulspec.KeyOpenAPITags, tags).To(delUser).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath+"/kubectl").Doc("get user's kubectl pod").Param(ws.PathParameter("user",
"username").DataType("string")).Metadata(restfulspec.KeyOpenAPITags, tags).To(getKubectl).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath+"/kubeconfig").Doc("get users' kubeconfig").Param(ws.PathParameter("user",
"username").DataType("string")).Metadata(restfulspec.KeyOpenAPITags, tags).To(getKubeconfig).Produces(restful.MIME_JSON))
}

View File

@@ -21,14 +21,19 @@ import (
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/models"
)
func Register(ws *restful.WebService, subPath string) {
ws.Route(ws.GET(subPath).To(getClusterStatus).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath + "/namespaces/{namespace}").To(getNamespaceStatus).Produces(restful.MIME_JSON))
tags := []string{"workloadStatus"}
ws.Route(ws.GET(subPath).Doc("get abnormal workloads' count of whole cluster").Metadata(restfulspec.KeyOpenAPITags, tags).To(getClusterStatus).Produces(restful.MIME_JSON))
ws.Route(ws.GET(subPath+"/namespaces/{namespace}").Doc("get abnormal workloads' count of specified namespace").Param(ws.PathParameter("namespace",
"the name of namespace").DataType("string")).Metadata(restfulspec.KeyOpenAPITags, tags).To(getNamespaceStatus).Produces(restful.MIME_JSON))
}

View File

@@ -28,6 +28,9 @@ import (
"net"
"net/http"
"github.com/emicklei/go-restful-openapi"
"github.com/go-openapi/spec"
_ "kubesphere.io/kubesphere/pkg/apis/v1alpha"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/constants"
@@ -74,6 +77,28 @@ func preCheck() error {
_, err = k8sClient.CoreV1().Namespaces().Create(&namespace)
return err
}
func registerSwagger() {
config := restfulspec.Config{
WebServices: restful.RegisteredWebServices(), // you control what services are visible
APIPath: "/swagger-ui/api.json",
PostBuildSwaggerObjectHandler: enrichSwaggerObject}
restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(config))
http.Handle("/swagger-ui/", http.StripPrefix("/swagger-ui/", http.FileServer(http.Dir("/usr/lib/kubesphere/swagger-ui"))))
}
func enrichSwaggerObject(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "KubeSphere",
Description: "The extend apis of kubesphere",
Version: "v1.0-alpha",
},
}
swo.Tags = []spec.Tag{spec.Tag{TagProps: spec.TagProps{
Name: "extend apis"}}}
}
func (server *kubeSphereServer) run() {
err := preCheck()
if err != nil {
@@ -83,6 +108,8 @@ func (server *kubeSphereServer) run() {
go controllers.Run()
registerSwagger()
if len(server.certFile) > 0 && len(server.keyFile) > 0 {
servingCert, err := tls.LoadX509KeyPair(server.certFile, server.keyFile)
if err != nil {

View File

@@ -45,7 +45,7 @@ var resourceMap = map[string]string{daemonsetsKey: controllers.Daemonsets, deplo
statefulsetsKey: controllers.Statefulsets, persistentvolumeclaimsKey: controllers.PersistentVolumeClaim, podsKey: controllers.Pods,
namespaceKey: controllers.Namespaces, storageClassesKey: controllers.StorageClasses, clusterRolesKey: controllers.ClusterRoles}
type resourceQuota struct {
type ResourceQuota struct {
NameSpace string `json:"namespace"`
Data v1.ResourceQuotaStatus `json:"data"`
}
@@ -58,7 +58,7 @@ func getUsage(namespace, resource string) int {
return ctl.Count(namespace)
}
func GetClusterQuota() (*resourceQuota, error) {
func GetClusterQuota() (*ResourceQuota, error) {
quota := v1.ResourceQuotaStatus{Hard: make(v1.ResourceList), Used: make(v1.ResourceList)}
for k, v := range resourceMap {
@@ -68,11 +68,11 @@ func GetClusterQuota() (*resourceQuota, error) {
quota.Used[v1.ResourceName(k)] = quantity
}
return &resourceQuota{NameSpace: "\"\"", Data: quota}, nil
return &ResourceQuota{NameSpace: "\"\"", Data: quota}, nil
}
func GetNamespaceQuota(namespace string) (*resourceQuota, error) {
func GetNamespaceQuota(namespace string) (*ResourceQuota, error) {
quota, err := getNamespaceResourceQuota(namespace)
if err != nil {
glog.Error(err)
@@ -95,7 +95,7 @@ func GetNamespaceQuota(namespace string) (*resourceQuota, error) {
}
}
return &resourceQuota{NameSpace: namespace, Data: *quota}, nil
return &ResourceQuota{NameSpace: namespace, Data: *quota}, nil
}
func updateNamespaceQuota(tmpResourceList, resourceList v1.ResourceList) {