From f1c1c9e6e41f5af50f49a9272b9c39408258ea36 Mon Sep 17 00:00:00 2001 From: richardxz Date: Wed, 26 Sep 2018 22:22:38 -0400 Subject: [PATCH] add hpa api --- pkg/apis/v1alpha/hpa/hpa.go | 57 +++++++++++++++++++++++++++++++++++++ pkg/apis/v1alpha/install.go | 2 ++ 2 files changed, 59 insertions(+) create mode 100644 pkg/apis/v1alpha/hpa/hpa.go diff --git a/pkg/apis/v1alpha/hpa/hpa.go b/pkg/apis/v1alpha/hpa/hpa.go new file mode 100644 index 000000000..f67cb26fb --- /dev/null +++ b/pkg/apis/v1alpha/hpa/hpa.go @@ -0,0 +1,57 @@ +/* +Copyright 2018 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 hpa + +import ( + "net/http" + + "github.com/emicklei/go-restful" + "github.com/emicklei/go-restful-openapi" + "k8s.io/api/autoscaling/v1" + "k8s.io/apimachinery/pkg/api/errors" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "kubesphere.io/kubesphere/pkg/client" +) + +func Register(ws *restful.WebService, subPath string) { + + tags := []string{"horizontalpodautoscalers"} + + ws.Route(ws.GET(subPath).To(getHpa).Consumes("*/*").Metadata(restfulspec.KeyOpenAPITags, tags).Doc( + "get horizontalpodautoscalers").Param(ws.PathParameter("namespace", + "horizontalpodautoscalers's namespace").DataType("string")).Param(ws.PathParameter( + "horizontalpodautoscaler", "horizontalpodautoscaler's name")).Writes(v1.HorizontalPodAutoscaler{})) +} + +func getHpa(req *restful.Request, resp *restful.Response) { + hpa := req.PathParameter("horizontalpodautoscaler") + namespace := req.PathParameter("namespace") + client := client.NewK8sClient() + + res, err := client.AutoscalingV1().HorizontalPodAutoscalers(namespace).Get(hpa, metaV1.GetOptions{}) + + if err != nil { + if errors.IsNotFound(err) { + resp.WriteHeaderAndEntity(http.StatusOK, nil) + } else { + resp.WriteHeaderAndEntity(http.StatusInternalServerError, nil) + } + } + + resp.WriteEntity(res) +} diff --git a/pkg/apis/v1alpha/install.go b/pkg/apis/v1alpha/install.go index 5a0534a40..48db15ea6 100644 --- a/pkg/apis/v1alpha/install.go +++ b/pkg/apis/v1alpha/install.go @@ -23,6 +23,7 @@ import ( "kubesphere.io/kubesphere/pkg/apis/v1alpha/containers" "kubesphere.io/kubesphere/pkg/apis/v1alpha/daemonsets" "kubesphere.io/kubesphere/pkg/apis/v1alpha/deployments" + "kubesphere.io/kubesphere/pkg/apis/v1alpha/hpa" "kubesphere.io/kubesphere/pkg/apis/v1alpha/iam" "kubesphere.io/kubesphere/pkg/apis/v1alpha/jobs" "kubesphere.io/kubesphere/pkg/apis/v1alpha/nodes" @@ -58,6 +59,7 @@ func init() { terminal.Register(ws, "/namespaces/{namespace}/pod/{pod}/shell/{container}") workloadstatus.Register(ws, "/status") quota.Register(ws, "/quota") + hpa.Register(ws, "/namespaces/{namespace}/horizontalpodautoscalers/{horizontalpodautoscaler}") jobs.Register(ws, "/namespaces/{namespace}/jobs/{job}") deployments.Register(ws, "/namespaces/{namespace}/deployments/{deployment}/revisions/{revision}") daemonsets.Register(ws, "/namespaces/{namespace}/daemonsets/{daemonset}/revisions/{revision}")