Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CONSOLE-2425: Support localization of dynamic plugins #9196

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/public/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ i18n
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
backend: {
loadPath: 'static/locales/{{lng}}/{{ns}}.json',
loadPath: '/locales/resource.json?lng={{lng}}&ns={{ns}}',
},
lng: localStorage.getItem('bridge/language'),
fallbackLng: 'en',
Expand Down
55 changes: 50 additions & 5 deletions pkg/plugins/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,77 @@ import (
type PluginsHandler struct {
Client *http.Client
PluginsEndpointMap map[string]string
PublicDir string
}

func NewPluginsHandler(client *http.Client, token string, pluginsEndpointMap map[string]string) *PluginsHandler {
func NewPluginsHandler(client *http.Client, pluginsEndpointMap map[string]string, publicDir string) *PluginsHandler {
return &PluginsHandler{
Client: client,
PluginsEndpointMap: pluginsEndpointMap,
PublicDir: publicDir,
}
}

func (p *PluginsHandler) HandlePlugins(w http.ResponseWriter, r *http.Request) {
func (p *PluginsHandler) HandleI18nResources(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.Header().Set("Allow", "GET")
serverutils.SendResponse(w, http.StatusMethodNotAllowed, serverutils.ApiError{Err: "Method unsupported, the only supported methods is GET"})
return
}

query := r.URL.Query()
lang := query.Get("lng")
// In case of the dynamic plugins, the namespace should contain name of the plugin prefixed with 'plugin__' prefix.
// eg. 'plugin__helm' will fetch `locales/{lang}/plugin__helm.json` from the plugin service
namespace := query.Get("ns")
spadgett marked this conversation as resolved.
Show resolved Hide resolved
if lang == "" || namespace == "" {
errMsg := fmt.Sprintf("GET request %q is missing 'lng' or 'ns' query parameter", r.URL.String())
klog.Error(errMsg)
serverutils.SendResponse(w, http.StatusBadRequest, serverutils.ApiError{Err: errMsg})
return
}

if !strings.HasPrefix(namespace, "plugin__") {
http.ServeFile(w, r, path.Join(p.PublicDir, "locales", lang, fmt.Sprintf("%s.json", namespace)))
return
}
// In case of dynamic-plugin we need to trim the "plugin__" prefix, since we are using the ConsolePlugin CR's name
// as key when looking for the plugin's Service endpoint.
pluginName := strings.TrimPrefix(namespace, "plugin__")

pluginServiceRequestURL, err := p.getServiceRequestURL(pluginName)
if err != nil {
errMsg := err.Error()
klog.Error(errMsg)
serverutils.SendResponse(w, http.StatusBadGateway, serverutils.ApiError{Err: errMsg})
return
}
pluginServiceRequestURL.Path = path.Join(pluginServiceRequestURL.Path, "locales", lang, fmt.Sprintf("%s.json", namespace))

p.proxyPluginRequest(pluginServiceRequestURL, pluginName, w, r)
}

func (p *PluginsHandler) HandlePluginAssets(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.Header().Set("Allow", "GET")
serverutils.SendResponse(w, http.StatusMethodNotAllowed, serverutils.ApiError{Err: "Method unsupported, the only supported methods is GET"})
return
}
pluginName, pluginAssetPath := parsePluginNameAndAssetPath(r.URL.Path)
serviceRequestURL, err := p.getServiceRequestURL(pluginName)
pluginServiceRequestURL, err := p.getServiceRequestURL(pluginName)
if err != nil {
errMsg := err.Error()
klog.Error(errMsg)
serverutils.SendResponse(w, http.StatusBadGateway, serverutils.ApiError{Err: errMsg})
return
}
serviceRequestURL.Path = path.Join(serviceRequestURL.Path, pluginAssetPath)
pluginServiceRequestURL.Path = path.Join(pluginServiceRequestURL.Path, pluginAssetPath)

p.proxyPluginRequest(pluginServiceRequestURL, pluginName, w, r)
}

resp, err := p.Client.Get(serviceRequestURL.String())
func (p *PluginsHandler) proxyPluginRequest(requestURL *url.URL, pluginName string, w http.ResponseWriter, r *http.Request) {
resp, err := p.Client.Get(requestURL.String())
if err != nil {
errMsg := fmt.Sprintf("GET request for %q plugin failed: %v", pluginName, err)
klog.Error(errMsg)
Expand Down
42 changes: 22 additions & 20 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const (
gitopsEndpoint = "/api/gitops/"
devfileEndpoint = "/api/devfile/"
devfileSamplesEndpoint = "/api/devfile/samples/"
pluginsEndpoint = "/api/plugins/"
pluginAssetsEndpoint = "/api/plugins/"
localesEndpoint = "/locales/resource.json"

sha256Prefix = "sha256~"
)
Expand Down Expand Up @@ -422,26 +423,27 @@ func (s *Server) HTTPHandler() http.Handler {

helmHandlers := helmhandlerspkg.New(s.K8sProxyConfig.Endpoint.String(), s.K8sClient.Transport, s)

// No need to create plugins handler if no plugin is enabled.
if len(s.EnabledConsolePlugins) > 0 {
pluginsHandler := plugins.NewPluginsHandler(
&http.Client{
// 120 seconds matches the webpack require timeout.
// Plugins are loaded asynchronously, so this doesn't block page load.
Timeout: 120 * time.Second,
Transport: &http.Transport{TLSClientConfig: s.PluginsProxyTLSConfig},
},
s.ServiceAccountToken,
s.EnabledConsolePlugins,
)
pluginsHandler := plugins.NewPluginsHandler(
&http.Client{
// 120 seconds matches the webpack require timeout.
// Plugins are loaded asynchronously, so this doesn't block page load.
Timeout: 120 * time.Second,
Transport: &http.Transport{TLSClientConfig: s.PluginsProxyTLSConfig},
},
s.EnabledConsolePlugins,
s.PublicDir,
)

handle(pluginsEndpoint, http.StripPrefix(
proxy.SingleJoiningSlash(s.BaseURL.Path, pluginsEndpoint),
authHandler(func(w http.ResponseWriter, r *http.Request) {
pluginsHandler.HandlePlugins(w, r)
}),
))
}
handle(pluginAssetsEndpoint, http.StripPrefix(
proxy.SingleJoiningSlash(s.BaseURL.Path, pluginAssetsEndpoint),
authHandler(func(w http.ResponseWriter, r *http.Request) {
pluginsHandler.HandlePluginAssets(w, r)
}),
))

handleFunc(localesEndpoint, func(w http.ResponseWriter, r *http.Request) {
pluginsHandler.HandleI18nResources(w, r)
})

// Helm Endpoints
handle("/api/helm/template", authHandlerWithUser(helmHandlers.HandleHelmRenderManifests))
Expand Down