Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
+ add cluster support for deploy service api
Browse files Browse the repository at this point in the history
  • Loading branch information
kebe7jun committed Jun 21, 2022
1 parent 8492dab commit 0a36c40
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
20 changes: 14 additions & 6 deletions api/extend/deploy2service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package extend
import (
"strings"

"github.com/gorilla/mux"
v1 "k8s.io/api/core/v1"

"github.com/DaoCloud/ckube/api"
"github.com/DaoCloud/ckube/common"
"github.com/DaoCloud/ckube/page"
"github.com/DaoCloud/ckube/store"
"github.com/DaoCloud/ckube/utils"
"github.com/gorilla/mux"
v1 "k8s.io/api/core/v1"
)

func Deploy2Service(r *api.ReqContext) interface{} {
cluster := mux.Vars(r.Request)["cluster"]
ns := mux.Vars(r.Request)["namespace"]
dep := mux.Vars(r.Request)["deployment"]
services := []*v1.Service{}
Expand All @@ -25,11 +28,14 @@ func Deploy2Service(r *api.ReqContext) interface{} {
Version: "v1",
Resource: "services",
}
if cluster == "" {
cluster = common.GetConfig().DefaultCluster
}
p := page.Paginate{Search: "name=" + dep}
p.Clusters([]string{cluster})
res := r.Store.Query(podGvr, store.Query{
Namespace: ns,
Paginate: page.Paginate{
Search: "name=" + dep,
},
Paginate: p,
})
if res.Error != nil {
return res.Error
Expand All @@ -44,9 +50,11 @@ func Deploy2Service(r *api.ReqContext) interface{} {
}
}
}
p = page.Paginate{}
p.Clusters([]string{cluster})
res = r.Store.Query(svcGvr, store.Query{
Namespace: ns,
Paginate: page.Paginate{},
Paginate: p,
})
if res.Error != nil {
return res.Error
Expand Down
25 changes: 14 additions & 11 deletions api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import (
"strings"
"time"

"github.com/DaoCloud/ckube/common"
"github.com/DaoCloud/ckube/common/constants"
"github.com/DaoCloud/ckube/kube"
"github.com/DaoCloud/ckube/log"
"github.com/DaoCloud/ckube/page"
"github.com/DaoCloud/ckube/store"
"github.com/gorilla/mux"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8labels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"

"github.com/DaoCloud/ckube/common"
"github.com/DaoCloud/ckube/common/constants"
"github.com/DaoCloud/ckube/kube"
"github.com/DaoCloud/ckube/log"
"github.com/DaoCloud/ckube/page"
"github.com/DaoCloud/ckube/store"
)

func getGVRFromReq(req *http.Request) store.GroupVersionResource {
Expand Down Expand Up @@ -100,12 +101,14 @@ func parsePaginateAndLabelsAndClean(r *http.Request) (*page.Paginate, *v1.LabelS
var paginate page.Paginate
var labelSelectorStr string
clusterPrefix := constants.ClusterPrefix
cluster := ""
cluster := common.GetConfig().DefaultCluster
query := r.URL.Query()
for k, v := range query {
switch k {
case "labelSelector": // For List options
labelSelectorStr = v[0]
case "cluster":
cluster = v[0]
case "fieldManager", "resourceVersion": // For Get Create Patch Update actions.
if strings.HasPrefix(v[0], clusterPrefix) {
cluster = v[0][len(clusterPrefix):]
Expand Down Expand Up @@ -176,12 +179,12 @@ func parsePaginateAndLabelsAndClean(r *http.Request) (*page.Paginate, *v1.LabelS
}

func Proxy(r *ReqContext) interface{} {
//version := mux.Vars(r.Request)["version"]
// version := mux.Vars(r.Request)["version"]
namespace := mux.Vars(r.Request)["namespace"]
resourceName := mux.Vars(r.Request)["resource"]
paginate, labels, cluster, err := parsePaginateAndLabelsAndClean(r.Request)
if err != nil {
return proxyPass(r, common.GetConfig().DefaultCluster)
return proxyPass(r, cluster)
}
if cluster == "" {
cluster = common.GetConfig().DefaultCluster
Expand Down Expand Up @@ -472,11 +475,11 @@ func getRequest(r *ReqContext, cluster string, timeout time.Duration) *rest.Requ
log.Errorf("unexpected method: %s", r.Request.Method)
return nil
}
//for k, v := range r.Request.Header {
// for k, v := range r.Request.Header {
// if len(v) > 0 {
// req = req.SetHeader(k, v[0])
// }
//}
// }
req = req.Body(r.Request.Body)
return req
}
Expand Down
7 changes: 7 additions & 0 deletions server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ var (
authRequired: true,
successStatus: 200,
},
{
path: "/custom/v1/clusters/{cluster}/namespaces/{namespace}/deployments/{deployment}/services",
method: "GET",
handler: extend.Deploy2Service,
authRequired: true,
successStatus: 200,
},
{
path: "/apis/{group}/{version}/namespaces/{namespace}/{resourceType}",
method: "GET",
Expand Down

0 comments on commit 0a36c40

Please sign in to comment.