Skip to content

Commit

Permalink
Merge pull request #654 from jhadvig/CONSOLE-3162
Browse files Browse the repository at this point in the history
CONSOLE-3162: Implement check for the new i18n annotation for dynamic plugins
  • Loading branch information
openshift-ci[bot] committed Jun 14, 2022
2 parents 805bbac + 2ce2457 commit 54b362c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pkg/api/api.go
Expand Up @@ -54,6 +54,8 @@ const (
CreateOAuthClientManagedClusterActionName = "console-create-oauth-client"
OAuthServerCertManagedClusterViewName = "console-oauth-server-cert"

PluginI18nAnnotation = "console.openshift.io/use-i18n"

ManagedClusterViewAPIGroup = "view.open-cluster-management.io"
ManagedClusterViewAPIVersion = "v1beta1"
ManagedClusterViewResource = "managedclusterviews"
Expand Down
19 changes: 15 additions & 4 deletions pkg/console/subresource/configmap/configmap.go
Expand Up @@ -75,8 +75,9 @@ func DefaultConfigMap(
OAuthServingCert(useDefaultCAFile).
APIServerURL(getApiUrl(infrastructureConfig)).
TopologyMode(infrastructureConfig.Status.ControlPlaneTopology).
Plugins(GetPluginsEndpointMap(availablePlugins)).
Proxy(GetPluginsProxyServices(availablePlugins)).
Plugins(getPluginsEndpointMap(availablePlugins)).
I18nNamespaces(pluginsWithI18nNamespace(availablePlugins)).
Proxy(getPluginsProxyServices(availablePlugins)).
CustomLogoFile(operatorConfig.Spec.Customization.CustomLogoFile.Key).
CustomProductName(operatorConfig.Spec.Customization.CustomProductName).
CustomDeveloperCatalog(operatorConfig.Spec.Customization.DeveloperCatalog).
Expand Down Expand Up @@ -119,15 +120,25 @@ func DefaultConfigMap(
return configMap, willMergeConfigOverrides, nil
}

func GetPluginsEndpointMap(availablePlugins []*v1alpha1.ConsolePlugin) map[string]string {
func pluginsWithI18nNamespace(availablePlugins []*v1alpha1.ConsolePlugin) []string {
i18nNamespaces := []string{}
for _, plugin := range availablePlugins {
if plugin.Annotations[api.PluginI18nAnnotation] == "true" {
i18nNamespaces = append(i18nNamespaces, fmt.Sprintf("plugin__%s", plugin.Name))
}
}
return i18nNamespaces
}

func getPluginsEndpointMap(availablePlugins []*v1alpha1.ConsolePlugin) map[string]string {
pluginsEndpointMap := map[string]string{}
for _, plugin := range availablePlugins {
pluginsEndpointMap[plugin.Name] = getServiceURL(plugin)
}
return pluginsEndpointMap
}

func GetPluginsProxyServices(availablePlugins []*v1alpha1.ConsolePlugin) []consoleserver.ProxyService {
func getPluginsProxyServices(availablePlugins []*v1alpha1.ConsolePlugin) []consoleserver.ProxyService {
proxyServices := []consoleserver.ProxyService{}
for _, plugin := range availablePlugins {
for _, proxy := range plugin.Spec.Proxy {
Expand Down
25 changes: 19 additions & 6 deletions pkg/console/subresource/configmap/configmap_test.go
Expand Up @@ -663,8 +663,10 @@ managedClusterConfigFile: 'test'
useDefaultCAFile: true,
inactivityTimeoutSeconds: 0,
availablePlugins: []*v1alpha1.ConsolePlugin{
testPlugins("pluginName1", "serviceName1", "serviceNamespace1"),
testPluginsWithProxy("pluginName2", "serviceName2", "serviceNamespace2"),
testPlugins("plugin1", "service1", "service-namespace1"),
testPluginsWithProxy("plugin2", "service2", "service-namespace2"),
testPluginsWithI18nAnnotation("plugin3", "service3", "service-namespace3", "false"),
testPluginsWithI18nAnnotation("plugin4", "service4", "service-namespace4", "true"),
},
managedClusterConfigFile: "",
},
Expand All @@ -691,14 +693,18 @@ clusterInfo:
customization:
branding: ` + DEFAULT_BRAND + `
documentationBaseURL: ` + DEFAULT_DOC_URL + `
i18nNamespaces:
- plugin__plugin4
servingInfo:
bindAddress: https://[::]:8443
certFile: /var/serving-cert/tls.crt
keyFile: /var/serving-cert/tls.key
providers: {}
plugins:
pluginName1: https://serviceName1.serviceNamespace1.svc.cluster.local:8443/
pluginName2: https://serviceName2.serviceNamespace2.svc.cluster.local:8443/
plugin1: https://service1.service-namespace1.svc.cluster.local:8443/
plugin2: https://service2.service-namespace2.svc.cluster.local:8443/
plugin3: https://service3.service-namespace3.svc.cluster.local:8443/
plugin4: https://service4.service-namespace4.svc.cluster.local:8443/
proxy:
services:
- authorize: true
Expand All @@ -717,8 +723,8 @@ HQ4EFgQU0vhI4OPGEOqT+VAWwxdhVvcmgdIwHwYDVR0jBBgwFoAU0vhI4OPGEOqT` + "\n" + `
nV5cXbp9W1bC12Tc8nnNXn4ypLE2JTQAvyp51zoZ8hQoSnRVx/VCY55Yu+br8gQZ` + "\n" + `
+tW+O/PoE7B3tuY=` + "\n" + `
-----END CERTIFICATE-----'
consoleAPIPath: /api/proxy/plugin/pluginName2/test-alias/
endpoint: https://proxy-serviceName2.proxy-serviceNamespace2.svc.cluster.local:9991
consoleAPIPath: /api/proxy/plugin/plugin2/test-alias/
endpoint: https://proxy-service2.proxy-service-namespace2.svc.cluster.local:9991
`,
},
},
Expand Down Expand Up @@ -818,6 +824,7 @@ providers: {}

// compare the configs
if diff := deep.Equal(exampleConfig, actualConfig); diff != nil {
fmt.Printf("\n EXAMPLE: %#v\n\n ACTUAL: %#v\n", exampleConfig, actualConfig)
t.Error(diff)
}

Expand Down Expand Up @@ -923,6 +930,12 @@ func TestTelemetryConfiguration(t *testing.T) {
}
}

func testPluginsWithI18nAnnotation(pluginName, serviceName, serviceNamespace, annotationValue string) *v1alpha1.ConsolePlugin {
plugin := testPlugins(pluginName, serviceName, serviceNamespace)
plugin.SetAnnotations(map[string]string{api.PluginI18nAnnotation: annotationValue})
return plugin
}

func TestStub(t *testing.T) {
tests := []struct {
name string
Expand Down
11 changes: 11 additions & 0 deletions pkg/console/subresource/consoleserver/config_builder.go
Expand Up @@ -47,6 +47,7 @@ type ConsoleServerCLIConfigBuilder struct {
customHostnameRedirectPort int
inactivityTimeoutSeconds int
pluginsList map[string]string
i18nNamespaceList []string
proxyServices []ProxyService
managedClusterConfigFile string
telemetry map[string]string
Expand Down Expand Up @@ -135,6 +136,11 @@ func (b *ConsoleServerCLIConfigBuilder) Plugins(plugins map[string]string) *Cons
return b
}

func (b *ConsoleServerCLIConfigBuilder) I18nNamespaces(i18nNamespaces []string) *ConsoleServerCLIConfigBuilder {
b.i18nNamespaceList = i18nNamespaces
return b
}

func (b *ConsoleServerCLIConfigBuilder) Proxy(proxyServices []ProxyService) *ConsoleServerCLIConfigBuilder {
b.proxyServices = proxyServices
return b
Expand All @@ -160,6 +166,7 @@ func (b *ConsoleServerCLIConfigBuilder) Config() Config {
ServingInfo: b.servingInfo(),
Providers: b.providers(),
Plugins: b.plugins(),
I18nNamespaces: b.i18nNamespaces(),
Proxy: b.proxy(),
ManagedClusterConfigFile: b.managedClusterConfigFile,
Telemetry: b.telemetry,
Expand Down Expand Up @@ -301,6 +308,10 @@ func (b *ConsoleServerCLIConfigBuilder) plugins() map[string]string {
return b.pluginsList
}

func (b *ConsoleServerCLIConfigBuilder) i18nNamespaces() []string {
return b.i18nNamespaceList
}

func (b *ConsoleServerCLIConfigBuilder) proxy() Proxy {
return Proxy{
Services: b.proxyServices,
Expand Down
7 changes: 6 additions & 1 deletion pkg/console/subresource/consoleserver/config_builder_test.go
Expand Up @@ -350,7 +350,8 @@ func TestConsoleServerCLIConfigBuilder(t *testing.T) {
Plugins(map[string]string{
"plugin1": "plugin1_url",
"plugin2": "plugin2_url",
})
}).
I18nNamespaces([]string{"plugin__plugin1"})
return b.Config()
},
output: Config{
Expand Down Expand Up @@ -382,6 +383,7 @@ func TestConsoleServerCLIConfigBuilder(t *testing.T) {
"plugin1": "plugin1_url",
"plugin2": "plugin2_url",
},
I18nNamespaces: []string{"plugin__plugin1"},
},
},
{
Expand Down Expand Up @@ -664,6 +666,7 @@ providers: {}
"plugin1": "plugin1_url",
"plugin2": "plugin2_url",
}).
I18nNamespaces([]string{"plugin__plugin1"}).
OAuthServingCert(true)
return b.ConfigYAML()
},
Expand All @@ -689,6 +692,8 @@ providers:
plugins:
plugin1: plugin1_url
plugin2: plugin2_url
i18nNamespaces:
- plugin__plugin1
`,
},
{
Expand Down
1 change: 1 addition & 0 deletions pkg/console/subresource/consoleserver/types.go
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Customization `yaml:"customization"`
Providers `yaml:"providers"`
Plugins map[string]string `yaml:"plugins,omitempty"`
I18nNamespaces []string `yaml:"i18nNamespaces,omitempty"`
Proxy Proxy `yaml:"proxy,omitempty"`
ManagedClusterConfigFile string `yaml:"managedClusterConfigFile,omitempty"`
Telemetry map[string]string `yaml:"telemetry,omitempty"`
Expand Down
16 changes: 11 additions & 5 deletions test/e2e/plugins_test.go
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"

"github.com/openshift/console-operator/pkg/api"
consoleapi "github.com/openshift/console-operator/pkg/api"
"github.com/openshift/console-operator/pkg/console/subresource/consoleserver"
"github.com/openshift/console-operator/test/e2e/framework"
Expand All @@ -41,13 +42,15 @@ func cleanupPluginsTestCase(t *testing.T, client *framework.ClientSet) {
// Only plugin that is available on the cluster will be set with his endpoint into the console-config ConfigMap.
func TestAddPlugins(t *testing.T) {
expectedPlugins := map[string]string{availablePluginName: "https://test-plugin-service-name.test-plugin-service-namespace.svc.cluster.local:8443/manifest"}
expertedI18nNamespaces := []string{"plugin__test-plugin"}
currentPlugins := map[string]string{}
client, _ := setupPluginsTestCase(t)
defer cleanupPluginsTestCase(t, client)

plugin := &consolev1alpha.ConsolePlugin{
ObjectMeta: v1.ObjectMeta{
Name: availablePluginName,
Name: availablePluginName,
Annotations: map[string]string{api.PluginI18nAnnotation: "true"},
},
Spec: consolev1alpha.ConsolePluginSpec{
DisplayName: "TestPlugin",
Expand All @@ -68,8 +71,11 @@ func TestAddPlugins(t *testing.T) {
setOperatorConfigPlugins(t, client, enabledPlugins)

err = wait.Poll(1*time.Second, pollTimeout, func() (stop bool, err error) {
currentPlugins = getConsolePluginsField(t, client)
if reflect.DeepEqual(expectedPlugins, currentPlugins) {
consoleConfig := getConsoleConfig(t, client)
if reflect.DeepEqual(expectedPlugins, consoleConfig.Plugins) {
return true, nil
}
if reflect.DeepEqual(expertedI18nNamespaces, consoleConfig.I18nNamespaces) {
return true, nil
}
return false, nil
Expand Down Expand Up @@ -102,7 +108,7 @@ func setOperatorConfigPlugins(t *testing.T, client *framework.ClientSet, pluginN
}
}

func getConsolePluginsField(t *testing.T, client *framework.ClientSet) map[string]string {
func getConsoleConfig(t *testing.T, client *framework.ClientSet) consoleserver.Config {
cm, err := framework.GetConsoleConfigMap(client)
if err != nil {
t.Fatalf("error: %s", err)
Expand All @@ -113,5 +119,5 @@ func getConsolePluginsField(t *testing.T, client *framework.ClientSet) map[strin
t.Fatalf("could not unmarshal console-config.yaml: %v", err)
}

return consoleConfig.Plugins
return consoleConfig
}

0 comments on commit 54b362c

Please sign in to comment.