Skip to content

Commit

Permalink
fix: Remove plugin registry pod if openVSX configured (#1913)
Browse files Browse the repository at this point in the history
* fix: Remove plugin registry pod if openVSX configured

Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha authored Oct 8, 2024
1 parent 6960b81 commit 891a727
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 16 deletions.
9 changes: 6 additions & 3 deletions api/v2/checluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,11 +1034,14 @@ func (c *CheCluster) IsCheFlavor() bool {
// IsEmbeddedOpenVSXRegistryConfigured returns true if the Open VSX Registry is configured to be embedded
// only if only the `Spec.Components.PluginRegistry.OpenVSXURL` is empty.
func (c *CheCluster) IsEmbeddedOpenVSXRegistryConfigured() bool {
openVSXURL := defaults.GetPluginRegistryOpenVSXURL()
if c.Spec.Components.PluginRegistry.OpenVSXURL != nil {
openVSXURL = *c.Spec.Components.PluginRegistry.OpenVSXURL
return *c.Spec.Components.PluginRegistry.OpenVSXURL == ""
}
return openVSXURL == ""
return defaults.GetPluginRegistryOpenVSXURL() == ""
}

func (c *CheCluster) IsInternalPluginRegistryDisabled() bool {
return c.Spec.Components.PluginRegistry.DisableInternalRegistry || !c.IsEmbeddedOpenVSXRegistryConfigured()
}

// IsCheBeingInstalled returns true if the Che version is not set in the status.
Expand Down
7 changes: 0 additions & 7 deletions pkg/deploy/dashboard/dashboard_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,6 @@ func TestDashboardDeploymentEnvVars(t *testing.T) {
Name: "CHE_DASHBOARD_INTERNAL_URL",
Value: fmt.Sprintf("http://%s-dashboard.eclipse-che.svc:8080", defaults.GetCheFlavor()),
})
assert.Contains(
t,
deployment.Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "CHE_WORKSPACE_PLUGIN__REGISTRY__INTERNAL__URL",
Value: "http://plugin-registry.eclipse-che.svc:8080/v3",
})
assert.Contains(
t,
deployment.Spec.Template.Spec.Containers[0].Env,
Expand Down
2 changes: 1 addition & 1 deletion pkg/deploy/dashboard/deployment_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (d *DashboardReconciler) getDashboardDeploymentSpec(ctx *chetypes.DeployCon
Value: fmt.Sprintf("http://%s.%s.svc:8080/api", deploy.CheServiceName, ctx.CheCluster.Namespace)},
)

if !ctx.CheCluster.Spec.Components.PluginRegistry.DisableInternalRegistry {
if !ctx.CheCluster.IsInternalPluginRegistryDisabled() {
envVars = append(envVars,
corev1.EnvVar{
Name: "CHE_WORKSPACE_PLUGIN__REGISTRY__INTERNAL__URL",
Expand Down
3 changes: 3 additions & 0 deletions pkg/deploy/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"strings"
"testing"

"k8s.io/utils/pointer"

"github.com/devfile/devworkspace-operator/pkg/infrastructure"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
Expand Down Expand Up @@ -170,6 +172,7 @@ func TestOauthProxyConfigUnauthorizedPaths(t *testing.T) {
Components: chev2.CheClusterComponents{
PluginRegistry: chev2.PluginRegistry{
DisableInternalRegistry: false,
OpenVSXURL: pointer.String(""),
},
}},
}, nil)
Expand Down
2 changes: 1 addition & 1 deletion pkg/deploy/gateway/oauth_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ cookie_domains = "%s"

func skipAuthConfig(instance *chev2.CheCluster) string {
var skipAuthPaths []string
if !instance.Spec.Components.PluginRegistry.DisableInternalRegistry {
if !instance.IsInternalPluginRegistryDisabled() {
skipAuthPaths = append(skipAuthPaths, "^/"+constants.PluginRegistryName)
}
skipAuthPaths = append(skipAuthPaths, "^/$")
Expand Down
10 changes: 9 additions & 1 deletion pkg/deploy/pluginregistry/pluginregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"fmt"
"strings"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/constants"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
Expand All @@ -34,7 +37,12 @@ func NewPluginRegistryReconciler() *PluginRegistryReconciler {
}

func (p *PluginRegistryReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
if ctx.CheCluster.Spec.Components.PluginRegistry.DisableInternalRegistry {
if ctx.CheCluster.IsInternalPluginRegistryDisabled() {
_, _ = deploy.DeleteNamespacedObject(ctx, constants.PluginRegistryName, &corev1.Service{})
_, _ = deploy.DeleteNamespacedObject(ctx, constants.PluginRegistryName, &corev1.ConfigMap{})
_, _ = deploy.DeleteNamespacedObject(ctx, gateway.GatewayConfigMapNamePrefix+constants.PluginRegistryName, &corev1.ConfigMap{})
_, _ = deploy.DeleteNamespacedObject(ctx, constants.PluginRegistryName, &appsv1.Deployment{})

if ctx.CheCluster.Status.PluginRegistryURL != "" {
ctx.CheCluster.Status.PluginRegistryURL = ""
err := deploy.UpdateCheCRStatus(ctx, "PluginRegistryURL", "")
Expand Down
115 changes: 113 additions & 2 deletions pkg/deploy/pluginregistry/pluginregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@
package pluginregistry

import (
"os"

"github.com/devfile/devworkspace-operator/pkg/infrastructure"
chev2 "github.com/eclipse-che/che-operator/api/v2"
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
"github.com/eclipse-che/che-operator/pkg/common/test"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

"testing"
)

func TestPluginRegistryReconcile(t *testing.T) {
func TestShouldDeployPluginRegistryIfOpenVSXIsEmpty(t *testing.T) {
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)

ctx := test.GetDeployContext(nil, []runtime.Object{})
ctx := test.GetDeployContext(&chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "eclipse-che",
Namespace: "eclipse-che",
},
Spec: chev2.CheClusterSpec{
Components: chev2.CheClusterComponents{
PluginRegistry: chev2.PluginRegistry{
OpenVSXURL: pointer.String(""),
},
},
},
}, []runtime.Object{})

pluginregistry := NewPluginRegistryReconciler()
_, done, err := pluginregistry.Reconcile(ctx)
Expand All @@ -39,3 +57,96 @@ func TestPluginRegistryReconcile(t *testing.T) {
assert.True(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &appsv1.Deployment{}))
assert.NotEmpty(t, ctx.CheCluster.Status.PluginRegistryURL)
}

func TestShouldDeployPluginRegistryIfOpenVSXIsEmptyByDefault(t *testing.T) {
defaultOpenVSXURL := os.Getenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL")

err := os.Unsetenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL")
assert.NoError(t, err)

defer func() {
_ = os.Setenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL", defaultOpenVSXURL)
}()

// re initialize defaults with new env var
defaults.Initialize()

infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)

ctx := test.GetDeployContext(&chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "eclipse-che",
Namespace: "eclipse-che",
},
}, []runtime.Object{})

pluginregistry := NewPluginRegistryReconciler()
_, done, err := pluginregistry.Reconcile(ctx)
assert.True(t, done)
assert.Nil(t, err)

assert.True(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.Service{}))
assert.True(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.ConfigMap{}))
assert.True(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &appsv1.Deployment{}))
assert.NotEmpty(t, ctx.CheCluster.Status.PluginRegistryURL)
}

func TestShouldNotDeployPluginRegistryIfOpenVSXConfigured(t *testing.T) {
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)

ctx := test.GetDeployContext(&chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "eclipse-che",
Namespace: "eclipse-che",
},
Spec: chev2.CheClusterSpec{
Components: chev2.CheClusterComponents{
PluginRegistry: chev2.PluginRegistry{
OpenVSXURL: pointer.String("https://openvsx.org"),
},
},
},
}, []runtime.Object{})

pluginregistry := NewPluginRegistryReconciler()
_, done, err := pluginregistry.Reconcile(ctx)
assert.True(t, done)
assert.Nil(t, err)

assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.Service{}))
assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.ConfigMap{}))
assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &appsv1.Deployment{}))
assert.Empty(t, ctx.CheCluster.Status.PluginRegistryURL)
}

func TestShouldNotDeployPluginRegistryIfOpenVSXConfiguredByDefault(t *testing.T) {
defaultOpenVSXURL := os.Getenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL")
err := os.Setenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL", "https://openvsx.org")
assert.NoError(t, err)

defer func() {
_ = os.Setenv("CHE_DEFAULT_SPEC_COMPONENTS_PLUGINREGISTRY_OPENVSXURL", defaultOpenVSXURL)
}()

// re initialize defaults with new env var
defaults.Initialize()

infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)

ctx := test.GetDeployContext(&chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "eclipse-che",
Namespace: "eclipse-che",
},
}, []runtime.Object{})

pluginregistry := NewPluginRegistryReconciler()
_, done, err := pluginregistry.Reconcile(ctx)
assert.True(t, done)
assert.Nil(t, err)

assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.Service{}))
assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &corev1.ConfigMap{}))
assert.False(t, test.IsObjectExists(ctx.ClusterAPI.Client, types.NamespacedName{Name: "plugin-registry", Namespace: "eclipse-che"}, &appsv1.Deployment{}))
assert.Empty(t, ctx.CheCluster.Status.PluginRegistryURL)
}
2 changes: 1 addition & 1 deletion pkg/deploy/server/server_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *CheServerReconciler) getCheConfigMapData(ctx *chetypes.DeployContext) (
cheAPI := "https://" + ctx.CheHost + "/api"
var pluginRegistryInternalURL string

if !ctx.CheCluster.Spec.Components.PluginRegistry.DisableInternalRegistry {
if !ctx.CheCluster.IsInternalPluginRegistryDisabled() {
pluginRegistryInternalURL = fmt.Sprintf("http://%s.%s.svc:8080/v3", constants.PluginRegistryName, ctx.CheCluster.Namespace)
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/deploy/server/server_configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package server
import (
"testing"

"k8s.io/utils/pointer"

"github.com/devfile/devworkspace-operator/pkg/infrastructure"
"github.com/eclipse-che/che-operator/pkg/common/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -349,6 +351,14 @@ func TestShouldSetUpCorrectlyPluginRegistryURL(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "eclipse-che",
},
Spec: chev2.CheClusterSpec{
Components: chev2.CheClusterComponents{
PluginRegistry: chev2.PluginRegistry{
DisableInternalRegistry: false,
OpenVSXURL: pointer.String(""),
},
},
},
Status: chev2.CheClusterStatus{
PluginRegistryURL: "internal-plugin-registry",
},
Expand Down

0 comments on commit 891a727

Please sign in to comment.