37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
shell.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
package clusteregistrationtokens
import (
"fmt"
"net/http"
"net/url"
"github.com/rancher/norman/condition"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/transport"
)
type ShellLinkHandler struct {
Proxy http.Handler
ClusterManager *clustermanager.Manager
}
func (s *ShellLinkHandler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
context, err := s.ClusterManager.UserContext(apiContext.ID)
if err != nil {
return err
}
userManager := context.Management.UserManager
userID := userManager.GetUser(apiContext)
token, err := userManager.EnsureToken("kubectl-shell-"+userID, "Access to kubectl shell in the browser", userID)
if err != nil {
return err
}
pods, err := context.K8sClient.CoreV1().Pods("cattle-system").List(v1.ListOptions{
LabelSelector: "app=cattle-agent",
})
if err != nil {
return err
}
for _, pod := range pods.Items {
if condition.Cond(corev1.PodReady).IsTrue(&pod) {
vars := url.Values{}
vars.Add("container", "agent")
vars.Add("stdout", "1")
vars.Add("stdin", "1")
vars.Add("stderr", "1")
vars.Add("tty", "1")
vars.Add("command", "kubectl-shell.sh")
vars.Add("command", token)
vars.Add("command", context.ClusterName)
path := fmt.Sprintf("/k8s/clusters/%s/api/v1/namespaces/%s/pods/%s/exec", context.ClusterName, "cattle-system", pod.Name)
req := apiContext.Request
req.URL.Path = path
req.URL.RawQuery = vars.Encode()
// we want to run this as a the system user
req.Header.Del(transport.ImpersonateUserHeader)
req.Header.Del(transport.ImpersonateGroupHeader)
req.Header.Del(transport.ImpersonateUserExtraHeaderPrefix)
s.Proxy.ServeHTTP(apiContext.Response, req)
return nil
}
}
return httperror.NewAPIError(httperror.NotFound, "failed to find kubectl pod")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.1-rc6

搜索帮助

0d507c66 1850385 C8b1a773 1850385