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

chore: Refactoring #1236

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 26 additions & 174 deletions controllers/che/checluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"time"

"github.com/eclipse-che/che-operator/pkg/deploy"
"github.com/eclipse-che/che-operator/pkg/deploy/consolelink"
"github.com/eclipse-che/che-operator/pkg/deploy/dashboard"
devworkspace "github.com/eclipse-che/che-operator/pkg/deploy/dev-workspace"
"github.com/eclipse-che/che-operator/pkg/deploy/devfileregistry"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
identityprovider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
imagepuller "github.com/eclipse-che/che-operator/pkg/deploy/image-puller"
"github.com/eclipse-che/che-operator/pkg/deploy/migration"
openshiftoauth "github.com/eclipse-che/che-operator/pkg/deploy/openshift-oauth"
Expand All @@ -30,7 +32,6 @@ import (
"github.com/eclipse-che/che-operator/pkg/deploy/server"
"github.com/eclipse-che/che-operator/pkg/deploy/tls"

identity_provider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
"github.com/eclipse-che/che-operator/pkg/util"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
Expand All @@ -46,7 +47,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

orgv1 "github.com/eclipse-che/che-operator/api/v1"
Expand All @@ -71,7 +71,6 @@ type CheClusterReconciler struct {
// A discovery client to check for the existence of certain APIs registered
// in the API Server
discoveryClient discovery.DiscoveryInterface
tests bool
reconcileManager *deploy.ReconcileManager
// the namespace to which to limit the reconciliation. If empty, all namespaces are considered
namespace string
Expand Down Expand Up @@ -103,6 +102,22 @@ func NewReconciler(
reconcileManager.RegisterReconciler(rbac.NewCheServerPermissionsReconciler())
reconcileManager.RegisterReconciler(rbac.NewGatewayPermissionsReconciler())
reconcileManager.RegisterReconciler(rbac.NewWorkspacePermissionsReconciler())
reconcileManager.RegisterReconciler(server.NewDefaultValuesReconciler())

// we have to expose che endpoint independently of syncing other server
// resources since che host is used for dashboard deployment and che config map
reconcileManager.RegisterReconciler(server.NewCheHostReconciler())
reconcileManager.RegisterReconciler(postgres.NewPostgresReconciler())
reconcileManager.RegisterReconciler(identityprovider.NewIdentityProviderReconciler())
reconcileManager.RegisterReconciler(devfileregistry.NewDevfileRegistryReconciler())
reconcileManager.RegisterReconciler(pluginregistry.NewPluginRegistryReconciler())
reconcileManager.RegisterReconciler(dashboard.NewDashboardReconciler())
reconcileManager.RegisterReconciler(gateway.NewGatewayReconciler())
reconcileManager.RegisterReconciler(server.NewCheServerReconciler())

if util.IsOpenShift4 {
reconcileManager.RegisterReconciler(consolelink.NewConsoleLinkReconciler())
}

return &CheClusterReconciler{
Scheme: scheme,
Expand Down Expand Up @@ -241,6 +256,7 @@ func (r *CheClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)

if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("CheCluster Custom Resource not found.")
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
Expand Down Expand Up @@ -298,183 +314,19 @@ func (r *CheClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
result, done, err := r.reconcileManager.ReconcileAll(deployContext)
if !done {
return result, err
// TODO: uncomment when all items added to ReconcilerManager
// } else {
// logrus.Info("Successfully reconciled.")
// return ctrl.Result{}, nil
} else {
logrus.Info("Successfully reconciled.")
return ctrl.Result{}, nil
}
} else {
r.reconcileManager.FinalizeAll(deployContext)
}

// Reconcile finalizers before CR is deleted
// TODO remove in favor of r.reconcileManager.FinalizeAll(deployContext)
r.reconcileFinalizers(deployContext)

if err := r.GenerateAndSaveFields(deployContext); err != nil {
_ = deploy.ReloadCheClusterCR(deployContext)
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 1}, err
}

if !deployContext.CheCluster.Spec.Database.ExternalDb {
postgres := postgres.NewPostgres(deployContext)
done, err := postgres.SyncAll()
if !done {
if err != nil {
logrus.Error(err)
}
return ctrl.Result{}, err
}
}

// we have to expose che endpoint independently of syncing other server
// resources since che host is used for dashboard deployment and che config map
server := server.NewServer(deployContext)
done, err := server.ExposeCheServiceAndEndpoint()
if !done {
if err != nil {
logrus.Error(err)
}
return ctrl.Result{}, err
}

// create and provision Keycloak related objects
if !checluster.Spec.Auth.ExternalIdentityProvider {
provisioned, err := identity_provider.SyncIdentityProviderToCluster(deployContext)
if !provisioned {
if err != nil {
logrus.Errorf("Error provisioning the identity provider to cluster: %v", err)
}
return ctrl.Result{}, err
}
} else {
keycloakURL := checluster.Spec.Auth.IdentityProviderURL
if checluster.Status.KeycloakURL != keycloakURL {
checluster.Status.KeycloakURL = keycloakURL
if err := deploy.UpdateCheCRStatus(deployContext, "status: Keycloak URL", keycloakURL); err != nil {
return reconcile.Result{}, err
}
}
}

devfileRegistry := devfileregistry.NewDevfileRegistry(deployContext)
if !checluster.Spec.Server.ExternalDevfileRegistry {
done, err := devfileRegistry.SyncAll()
if !done {
if err != nil {
logrus.Error(err)
}
return ctrl.Result{}, err
}
}

if !checluster.Spec.Server.ExternalPluginRegistry {
pluginRegistry := pluginregistry.NewPluginRegistry(deployContext)
done, err := pluginRegistry.SyncAll()
if !done {
if err != nil {
logrus.Error(err)
}
return ctrl.Result{}, err
}
} else {
if checluster.Spec.Server.PluginRegistryUrl != checluster.Status.PluginRegistryURL {
checluster.Status.PluginRegistryURL = checluster.Spec.Server.PluginRegistryUrl
if err := deploy.UpdateCheCRStatus(deployContext, "status: Plugin Registry URL", checluster.Spec.Server.PluginRegistryUrl); err != nil {
return reconcile.Result{}, err
}
}
}

d := dashboard.NewDashboard(deployContext)
done, err = d.Reconcile()
if !done {
if err != nil {
logrus.Errorf("Error provisioning '%s' to cluster: %v", d.GetComponentName(), err)
}
return ctrl.Result{}, err
}

err = gateway.SyncGatewayToCluster(deployContext)
if err != nil {
logrus.Errorf("Failed to create the Server Gateway: %s", err)
return ctrl.Result{}, err
}

done, err = server.SyncAll()
if !done {
if err != nil {
logrus.Error(err)
}
return reconcile.Result{}, err
}

// we can now try to create consolelink, after che instance is available
done, err = deploy.ReconcileConsoleLink(deployContext)
if !done {
if err != nil {
logrus.Error(err)
}
// We should `Requeue` since we created cluster object
return ctrl.Result{RequeueAfter: time.Second}, err
}

// Delete OpenShift identity provider if OpenShift oAuth is false in spec
// but OpenShiftoAuthProvisioned is true in CR status, e.g. when oAuth has been turned on and then turned off
deleted, err := identity_provider.ReconcileIdentityProvider(deployContext)
if deleted {
// ignore error
deploy.DeleteFinalizer(deployContext, deploy.OAuthFinalizerName)
for {
checluster.Status.OpenShiftoAuthProvisioned = false
if err := deploy.UpdateCheCRStatus(deployContext, "status: provisioned with OpenShift identity provider", "false"); err != nil &&
errors.IsConflict(err) {
_ = deploy.ReloadCheClusterCR(deployContext)
continue
}
break
}
for {
checluster.Spec.Auth.OAuthSecret = ""
checluster.Spec.Auth.OAuthClientName = ""
if err := deploy.UpdateCheCRStatus(deployContext, "clean oAuth secret name and client name", ""); err != nil &&
errors.IsConflict(err) {
_ = deploy.ReloadCheClusterCR(deployContext)
continue
}
break
}
}

logrus.Info("Successfully reconciled.")
return ctrl.Result{}, nil
}

func (r *CheClusterReconciler) reconcileFinalizers(deployContext *deploy.DeployContext) {
if util.IsOpenShift && deployContext.CheCluster.IsOpenShiftOAuthEnabled() {
if err := deploy.ReconcileOAuthClientFinalizer(deployContext); err != nil {
logrus.Error(err)
}
}

if err := deploy.ReconcileConsoleLinkFinalizer(deployContext); err != nil {
logrus.Error(err)
}

if !deployContext.CheCluster.ObjectMeta.DeletionTimestamp.IsZero() {
done, err := dashboard.NewDashboard(deployContext).Finalize()
if !done {
logrus.Error(err)
}
}
}

func (r *CheClusterReconciler) GetCR(request ctrl.Request) (instance *orgv1.CheCluster, err error) {
instance = &orgv1.CheCluster{}
err = r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
r.Log.Error(err, "Failed to get %s CR: %s", "Cluster name", instance.Name)
return nil, err
}
return instance, nil
func (r *CheClusterReconciler) GetCR(request ctrl.Request) (*orgv1.CheCluster, error) {
checluster := &orgv1.CheCluster{}
err := r.client.Get(context.TODO(), request.NamespacedName, checluster)
return checluster, err
}
Loading