()
| 46 | } |
| 47 | |
| 48 | func (h *Handler) KubernetesAPIProxy() iris.Handler { |
| 49 | return func(ctx *context.Context) { |
| 50 | // 解析参数 |
| 51 | name := ctx.Params().GetString("name") |
| 52 | proxyPath := ensureProxyPathValid(ctx.Params().GetString("p")) |
| 53 | namespace := ctx.URLParam("namespace") |
| 54 | keywords := ctx.URLParam("keywords") |
| 55 | search := false |
| 56 | if ctx.URLParamExists("search") { |
| 57 | search, _ = ctx.URLParamBool("search") |
| 58 | } |
| 59 | |
| 60 | requestMethod := ctx.Request().Method |
| 61 | // 获取当亲集群 |
| 62 | c, err := h.clusterService.Get(name, common.DBOptions{}) |
| 63 | if err != nil { |
| 64 | ctx.StatusCode(iris.StatusInternalServerError) |
| 65 | ctx.Values().Set("message", fmt.Sprintf("get cluster failed: %s", err.Error())) |
| 66 | return |
| 67 | } |
| 68 | // 获取session |
| 69 | u := ctx.Values().Get("profile") |
| 70 | profile := u.(session.UserProfile) |
| 71 | // 生成transport |
| 72 | ts, err := h.generateTLSTransport(c, profile) |
| 73 | if err != nil { |
| 74 | ctx.StatusCode(iris.StatusInternalServerError) |
| 75 | ctx.Values().Set("message", err) |
| 76 | return |
| 77 | } |
| 78 | // 生成httpClient |
| 79 | httpClient := http.Client{Transport: ts} |
| 80 | k := kubernetes.NewKubernetes(c) |
| 81 | clusterVersionMinor, err := k.VersionMinor() |
| 82 | if err != nil { |
| 83 | ctx.StatusCode(iris.StatusInternalServerError) |
| 84 | ctx.Values().Set("message", err) |
| 85 | return |
| 86 | } |
| 87 | compatibleClusterVersion(clusterVersionMinor, &proxyPath) |
| 88 | |
| 89 | //判断是否已经包含了namespace的查询 |
| 90 | hasNsFilter := hasNamespaceFilter(proxyPath) |
| 91 | resourceName, err := parseResourceName(proxyPath) |
| 92 | if err != nil { |
| 93 | ctx.StatusCode(iris.StatusInternalServerError) |
| 94 | ctx.Values().Set("message", err) |
| 95 | return |
| 96 | } |
| 97 | // 判断资源类型是否是namespace级别的 |
| 98 | namespaced, err := k.IsNamespacedResource(resourceName) |
| 99 | if err != nil { |
| 100 | ctx.StatusCode(iris.StatusInternalServerError) |
| 101 | ctx.Values().Set("message", err) |
| 102 | return |
| 103 | } |
| 104 | if strings.Contains(proxyPath, "namespaces") { |
| 105 | namespaced = false |
no test coverage detected