37 Star 396 Fork 71

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
link.go 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
package yaml
import (
"fmt"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
)
func NewLinkHandler(proxy http.Handler, manager *clustermanager.Manager, next types.RequestHandler) types.RequestHandler {
lh := &yamlLinkHandler{
Proxy: proxy,
ClusterManager: manager,
next: next,
}
return lh.LinkHandler
}
type yamlLinkHandler struct {
Proxy http.Handler
ClusterManager *clustermanager.Manager
next types.RequestHandler
}
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.ClusterManager.ClusterName(apiContext)
if clusterName == "" {
return httperror.NewAPIError(httperror.NotFound, "cluster not found")
}
schema := apiContext.Schemas.Schema(apiContext.Version, apiContext.Type)
if schema == nil {
return fmt.Errorf("failed to find schema " + apiContext.Type)
}
data, err := schema.Store.ByID(apiContext, schema, apiContext.ID)
if err != nil {
return err
}
link, _ := data[".selfLink"].(string)
if link == "" {
return httperror.NewAPIError(httperror.NotFound, "self link not found")
}
apiContext.Request.URL.Path = fmt.Sprintf("/k8s/clusters/%s%s", clusterName, link)
s.Proxy.ServeHTTP(apiContext.Response, apiContext.Request)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.16

搜索帮助

344bd9b3 5694891 D2dac590 5694891