3 Star 0 Fork 0

mirrors_rancher/ui-plugin-operator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 4.31 KB
一键复制 编辑 原始数据 按行查看 历史
Diogo Souza 提交于 2024-09-09 18:47 . enabling lint
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
neturl "net/url"
"strings"
"github.com/gorilla/mux"
"github.com/rancher/ui-plugin-operator/pkg/controllers"
"github.com/rancher/ui-plugin-operator/pkg/controllers/plugin"
"github.com/rancher/ui-plugin-operator/pkg/crd"
"github.com/rancher/ui-plugin-operator/pkg/version"
command "github.com/rancher/wrangler-cli"
_ "github.com/rancher/wrangler/v2/pkg/generated/controllers/apiextensions.k8s.io"
_ "github.com/rancher/wrangler/v2/pkg/generated/controllers/networking.k8s.io"
"github.com/rancher/wrangler/v2/pkg/kubeconfig"
"github.com/rancher/wrangler/v2/pkg/ratelimit"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
debugConfig command.DebugConfig
)
type PluginOperator struct {
Kubeconfig string `usage:"Kubeconfig file" env:"KUBECONFIG"`
Namespace string `usage:"Namespace to watch for UIPlugins" default:"cattle-ui-plugin-system" env:"NAMESPACE"`
ControllerName string `usage:"Unique name to identify this controller that is added to all UIPlugins tracked by this controller" default:"ui-plugin-operator" env:"CONTROLLER_NAME"`
NodeName string `usage:"Name of the node this controller is running on" env:"NODE_NAME"`
}
func (a *PluginOperator) Run(cmd *cobra.Command, _ []string) error {
if len(a.Namespace) == 0 {
return fmt.Errorf("helm-locker can only be started in a single namespace")
}
go func() {
logrus.Println(http.ListenAndServe(":6060", nil))
}()
debugConfig.MustSetupDebug()
cfg := kubeconfig.GetNonInteractiveClientConfig(a.Kubeconfig)
clientConfig, err := cfg.ClientConfig()
if err != nil {
return err
}
clientConfig.RateLimiter = ratelimit.None
ctx := cmd.Context()
if err := crd.Create(ctx, clientConfig); err != nil {
return err
}
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/index.json", http.StatusPermanentRedirect)
})
r.HandleFunc("/index.json", indexHandler)
r.HandleFunc("/{name}/{version}/{rest:.*}", pluginHandler)
http.Handle("/", r)
go func() {
log.Println(http.ListenAndServe(":8080", nil))
}()
if err := controllers.Register(ctx, a.Namespace, a.ControllerName, a.NodeName, cfg); err != nil {
return err
}
<-cmd.Context().Done()
return nil
}
func main() {
cmd := command.Command(&PluginOperator{}, cobra.Command{
Version: version.FriendlyVersion(),
})
cmd = command.AddDebug(cmd, &debugConfig)
command.Main(cmd)
}
func indexHandler(w http.ResponseWriter, _ *http.Request) {
index, err := json.Marshal(&plugin.Index)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logrus.Error(err)
}
w.Write(index)
}
func pluginHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
logrus.Debugf("http request vars %s", vars)
entry, ok := plugin.Index.Entries[vars["name"]]
if !ok || entry.Version != vars["version"] {
msg := fmt.Sprintf("plugin [name: %s version: %s] does not exist in index", vars["name"], vars["version"])
http.Error(w, msg, http.StatusNotFound)
logrus.Debug(msg)
return
}
if entry.NoCache {
logrus.Debugf("[noCache: %v] proxying request to [endpoint: %v]\n", entry.NoCache, entry.Endpoint)
proxyRequest(entry.Endpoint, vars["rest"], w, r)
} else {
logrus.Debugf("[noCache: %v] serving plugin files from filesystem cache\n", entry.NoCache)
http.FileServer(http.Dir(plugin.FSCacheRootDir)).ServeHTTP(w, r)
}
}
func proxyRequest(target, path string, w http.ResponseWriter, r *http.Request) {
url, err := neturl.Parse(target)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse url [%s]", target), http.StatusInternalServerError)
return
}
if denylist(url.Host) {
http.Error(w, fmt.Sprintf("url [%s] is forbidden", target), http.StatusForbidden)
return
}
proxy := httputil.NewSingleHostReverseProxy(url)
r.URL.Host = url.Host
r.URL.Scheme = url.Scheme
r.URL.Path = path
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
r.Host = url.Host
proxy.ServeHTTP(w, r)
}
func denylist(host string) bool {
// temp: is there a way to check if an IP equivalent to localhost is being used?
denied := map[string]struct{}{
"localhost": {},
"127.0.0.1": {},
"0.0.0.0": {},
"": {},
}
hostWithoutPort := strings.Split(host, ":")[0]
_, isDenied := denied[hostWithoutPort]
return isDenied
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_rancher/ui-plugin-operator.git
git@gitee.com:mirrors_rancher/ui-plugin-operator.git
mirrors_rancher
ui-plugin-operator
ui-plugin-operator
main

搜索帮助