Signed-off-by: Anatolii Bazko <abazko@redhat.com>
pull/1843/head
Anatolii Bazko 2024-05-28 11:02:31 +02:00
parent e0aae8b7be
commit 1aece54cba
4 changed files with 61 additions and 107 deletions

View File

@ -16,9 +16,6 @@ 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"
@ -37,13 +34,7 @@ func NewPluginRegistryReconciler() *PluginRegistryReconciler {
}
func (p *PluginRegistryReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
if ctx.CheCluster.Spec.Components.PluginRegistry.DisableInternalRegistry ||
!ctx.CheCluster.IsEmbeddedOpenVSXRegistryConfigured() {
_, _ = 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.Spec.Components.PluginRegistry.DisableInternalRegistry {
if ctx.CheCluster.Status.PluginRegistryURL != "" {
ctx.CheCluster.Status.PluginRegistryURL = ""
err := deploy.UpdateCheCRStatus(ctx, "PluginRegistryURL", "")

View File

@ -14,12 +14,8 @@ package pluginregistry
import (
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/test"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
@ -31,24 +27,7 @@ import (
func TestPluginRegistryReconcile(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(""),
},
},
},
Status: chev2.CheClusterStatus{
CheURL: "https://che-host",
},
},
[]runtime.Object{})
ctx := test.GetDeployContext(nil, []runtime.Object{})
pluginregistry := NewPluginRegistryReconciler()
_, done, err := pluginregistry.Reconcile(ctx)

View File

@ -37,13 +37,11 @@ func NewPostgresReconciler() *PostgresReconciler {
func (p *PostgresReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
// PostgreSQL component is not used anymore
_, _ = p.syncDeployment(ctx)
_, _ = p.syncPVC(ctx)
_, _ = p.syncCredentials(ctx)
_, _ = p.syncService(ctx)
// Backup server component is not used anymore
_, _ = p.syncBackupDeployment(ctx)
_, _ = deploy.DeleteNamespacedObject(ctx, postgresComponentName, &appsv1.Deployment{})
_, _ = deploy.DeleteNamespacedObject(ctx, backupServerComponentName, &appsv1.Deployment{})
_, _ = deploy.DeleteNamespacedObject(ctx, defaultPostgresVolumeClaimName, &corev1.PersistentVolumeClaim{})
_, _ = deploy.DeleteNamespacedObject(ctx, defaultPostgresCredentialsSecret, &corev1.Secret{})
_, _ = deploy.DeleteNamespacedObject(ctx, postgresComponentName, &corev1.Service{})
return reconcile.Result{}, true, nil
}
@ -51,23 +49,3 @@ func (p *PostgresReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.R
func (p *PostgresReconciler) Finalize(ctx *chetypes.DeployContext) bool {
return true
}
func (p *PostgresReconciler) syncService(ctx *chetypes.DeployContext) (bool, error) {
return deploy.DeleteNamespacedObject(ctx, postgresComponentName, &corev1.Service{})
}
func (p *PostgresReconciler) syncPVC(ctx *chetypes.DeployContext) (bool, error) {
return deploy.DeleteNamespacedObject(ctx, defaultPostgresVolumeClaimName, &corev1.PersistentVolumeClaim{})
}
func (p *PostgresReconciler) syncDeployment(ctx *chetypes.DeployContext) (bool, error) {
return deploy.DeleteNamespacedObject(ctx, postgresComponentName, &appsv1.Deployment{})
}
func (p *PostgresReconciler) syncCredentials(ctx *chetypes.DeployContext) (bool, error) {
return deploy.DeleteNamespacedObject(ctx, defaultPostgresCredentialsSecret, &corev1.Secret{})
}
func (p *PostgresReconciler) syncBackupDeployment(ctx *chetypes.DeployContext) (bool, error) {
return deploy.DeleteNamespacedObject(ctx, backupServerComponentName, &appsv1.Deployment{})
}

View File

@ -119,52 +119,6 @@ func DeleteClusterObject(deployContext *chetypes.DeployContext, name string, obj
return DeleteByKeyWithClient(client, key, objectMeta)
}
// doUpdate updates object.
// Returns true if object is up-to-date otherwise return false
func doUpdate(
client client.Client,
deployContext *chetypes.DeployContext,
actual client.Object,
blueprint client.Object,
diffOpts ...cmp.Option,
) (bool, error) {
actualMeta, ok := actual.(metav1.Object)
if !ok {
return false, fmt.Errorf("object %T is not a metav1.Object. Cannot sync it", actualMeta)
}
diff := cmp.Diff(actual, blueprint, diffOpts...)
if len(diff) > 0 {
// don't print difference if there are no diffOpts mainly to avoid huge output
if len(diffOpts) != 0 {
fmt.Printf("Difference:\n%s", diff)
}
if isUpdateUsingDeleteCreate(actual.GetObjectKind().GroupVersionKind().Kind) {
done, err := doDeleteIgnoreIfNotFound(context.TODO(), client, actual)
if !done {
return false, err
}
return doCreate(context.TODO(), client, deployContext, blueprint, false)
} else {
err := setOwnerReferenceIfNeeded(deployContext, blueprint)
if err != nil {
return false, err
}
// to be able to update, we need to set the resource version of the object that we know of
blueprint.(metav1.Object).SetResourceVersion(actualMeta.GetResourceVersion())
err = client.Update(context.TODO(), blueprint)
if err == nil {
syncLog.Info("Object updated", "namespace", actual.GetNamespace(), "kind", GetObjectType(actual), "name", actual.GetName())
}
return false, err
}
}
return true, nil
}
func DeleteByKeyWithClient(cli client.Client, key client.ObjectKey, objectMeta client.Object) (bool, error) {
runtimeObject, ok := objectMeta.(runtime.Object)
if !ok {
@ -218,7 +172,13 @@ func GetObjectType(obj interface{}) string {
// DeleteIgnoreIfNotFound deletes object.
// Returns nil if object deleted or not found otherwise returns error.
func DeleteIgnoreIfNotFound(context context.Context, cli client.Client, key client.ObjectKey, blueprint client.Object) error {
// Return error if object cannot be deleted otherwise returns nil.
func DeleteIgnoreIfNotFound(
context context.Context,
cli client.Client,
key client.ObjectKey,
blueprint client.Object,
) error {
runtimeObj, ok := blueprint.(runtime.Object)
if !ok {
return fmt.Errorf("object %T is not a runtime.Object. Cannot sync it", runtimeObj)
@ -236,7 +196,7 @@ func DeleteIgnoreIfNotFound(context context.Context, cli client.Client, key clie
}
// doCreate creates object.
// Throws error if object cannot be created otherwise returns nil.
// Return error if object cannot be created otherwise returns nil.
func doCreate(
context context.Context,
client client.Client,
@ -299,3 +259,49 @@ func doGet(
return false, err
}
}
// doUpdate updates object.
// Returns true if object is up-to-date otherwise return false
func doUpdate(
cli client.Client,
deployContext *chetypes.DeployContext,
actual client.Object,
blueprint client.Object,
diffOpts ...cmp.Option,
) (bool, error) {
actualMeta, ok := actual.(metav1.Object)
if !ok {
return false, fmt.Errorf("object %T is not a metav1.Object. Cannot sync it", actualMeta)
}
diff := cmp.Diff(actual, blueprint, diffOpts...)
if len(diff) > 0 {
// don't print difference if there are no diffOpts mainly to avoid huge output
if len(diffOpts) != 0 {
fmt.Printf("Difference:\n%s", diff)
}
if isUpdateUsingDeleteCreate(actual.GetObjectKind().GroupVersionKind().Kind) {
done, err := doDeleteIgnoreIfNotFound(context.TODO(), cli, actual)
if !done {
return false, err
}
return doCreate(context.TODO(), cli, deployContext, blueprint, false)
} else {
err := setOwnerReferenceIfNeeded(deployContext, blueprint)
if err != nil {
return false, err
}
// to be able to update, we need to set the resource version of the object that we know of
blueprint.(metav1.Object).SetResourceVersion(actualMeta.GetResourceVersion())
err = cli.Update(context.TODO(), blueprint)
if err == nil {
syncLog.Info("Object updated", "namespace", actual.GetNamespace(), "kind", GetObjectType(actual), "name", actual.GetName())
}
return false, err
}
}
return true, nil
}