37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
link.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
package namespace
import (
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/resourcelink"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
printers2 "k8s.io/cli-runtime/pkg/genericclioptions/printers"
"k8s.io/kubernetes/pkg/printers"
)
var ExportPrinters = map[string]printers.ResourcePrinter{
"json": &printers2.JSONPrinter{},
"yaml": &printers2.YAMLPrinter{},
}
func NewLinkHandler(next types.RequestHandler, manager *clustermanager.Manager) types.RequestHandler {
lh := &yamlLinkHandler{
next: next,
clusterManagement: manager,
}
return lh.LinkHandler
}
type yamlLinkHandler struct {
next types.RequestHandler
clusterManagement *clustermanager.Manager
}
func (s *yamlLinkHandler) callNext(apiContext *types.APIContext, next types.RequestHandler) error {
if s.next != nil {
return s.next(apiContext, next)
} else if next != nil {
return next(apiContext, nil)
}
return httperror.NewAPIError(httperror.NotFound, "link not found")
}
func (s *yamlLinkHandler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
if apiContext.Link != "yaml" {
return s.callNext(apiContext, next)
}
clusterName := s.clusterManagement.ClusterName(apiContext)
userContext, err := s.clusterManagement.UserContext(clusterName)
if err != nil {
return err
}
ns := apiContext.ID
result := &unstructured.UnstructuredList{}
result.SetAPIVersion("v1")
result.SetKind("List")
resources := apiContext.Request.URL.Query()["resource"]
toExportResourceMappings := getResourcePrefixMap(resources)
for kind, prefix := range toExportResourceMappings {
req := userContext.UnversionedClient.Get().Prefix(prefix).Namespace(ns).Resource(kind)
for k, v := range apiContext.Request.URL.Query() {
req.Param(k, strings.Join(v, ","))
}
for k, v := range apiContext.Request.Header {
if k == "Authorization" {
continue
}
req.SetHeader(k, v...)
}
req.SetHeader("Accept", "*/*")
r, err := req.Do().Get()
if err != nil {
if e, ok := err.(*apierrors.StatusError); ok && e.Status().Code == 403 {
continue
}
return err
}
if list, ok := r.(*unstructured.UnstructuredList); ok {
for _, item := range list.Items {
if len(item.GetOwnerReferences()) == 0 {
result.Items = append(result.Items, item)
}
}
}
}
printer := ExportPrinters["json"]
apiContext.Response.Header().Set("content-type", "application/json")
if apiContext.Request.Header.Get("Accept") == "application/yaml" {
printer = ExportPrinters["yaml"]
apiContext.Response.Header().Set("content-type", "application/yaml")
}
return printer.PrintObj(result, apiContext.Response)
}
//getResourcePrefixMap converts resource path like `/api/v1/pods` to kind-prefix mappings
func getResourcePrefixMap(resources []string) map[string]string {
if len(resources) == 0 {
return resourcelink.ExportResourcePrefixMappings
}
m := map[string]string{}
for _, r := range resources {
idx := strings.LastIndex(r, "/")
if idx == -1 {
m[r] = ""
} else {
m[r[idx+1:]] = r[:idx]
}
}
return m
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.3

搜索帮助

0d507c66 1850385 C8b1a773 1850385