From 598e8a2f94d6283a09b0c144ad8bd53c79781ec3 Mon Sep 17 00:00:00 2001 From: Sebastian Florek Date: Tue, 4 Feb 2025 15:27:28 +0100 Subject: [PATCH] fix broken readonly feature in project and catalog controllers --- .../controller/bootstraptoken_controller.go | 44 +++++++++---------- .../internal/controller/catalog_controller.go | 17 ++++--- .../internal/controller/project_controller.go | 4 ++ 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/go/controller/internal/controller/bootstraptoken_controller.go b/go/controller/internal/controller/bootstraptoken_controller.go index b695cca2df..7245424faf 100644 --- a/go/controller/internal/controller/bootstraptoken_controller.go +++ b/go/controller/internal/controller/bootstraptoken_controller.go @@ -82,14 +82,14 @@ func (in *BootstrapTokenReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // Check if token already exists and return early. - if !lo.IsEmpty(bootstrapToken.ConsoleID()) { + if bootstrapToken.Status.HasID() { utils.MarkCondition(bootstrapToken.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "") utils.MarkCondition(bootstrapToken.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionTrue, v1alpha1.SynchronizedConditionReason, "") return ctrl.Result{}, nil } // Create token and generate secret - apiBootstrapToken, err := in.ensure(ctx, bootstrapToken) + apiBootstrapToken, err := in.sync(ctx, bootstrapToken) if err != nil { if goerrors.Is(err, operrors.ErrRetriable) { utils.MarkCondition(bootstrapToken.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error()) @@ -107,25 +107,6 @@ func (in *BootstrapTokenReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, nil } -func (in *BootstrapTokenReconciler) getProject(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) (*v1alpha1.Project, error) { - logger := log.FromContext(ctx) - project := &v1alpha1.Project{} - if err := in.Get(ctx, client.ObjectKey{Name: bootstrapToken.Spec.ProjectRef.Name}, project); err != nil { - return project, err - } - - if project.Status.ID == nil { - logger.Info("Project is not ready") - return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, bootstrapToken.Spec.ProjectRef.Name) - } - - if err := controllerutil.SetOwnerReference(project, bootstrapToken, in.Scheme); err != nil { - return project, fmt.Errorf("could not set bootstrapToken owner reference, got error: %+v", err) - } - - return project, nil -} - func (in *BootstrapTokenReconciler) addOrRemoveFinalizer(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) *ctrl.Result { // If object is not being deleted and if it does not have our finalizer, // then lets add the finalizer. This is equivalent to registering our finalizer. @@ -159,7 +140,26 @@ func (in *BootstrapTokenReconciler) addOrRemoveFinalizer(ctx context.Context, bo return &ctrl.Result{} } -func (in *BootstrapTokenReconciler) ensure(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) (*consoleapi.BootstrapTokenBase, error) { +func (in *BootstrapTokenReconciler) getProject(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) (*v1alpha1.Project, error) { + logger := log.FromContext(ctx) + project := &v1alpha1.Project{} + if err := in.Get(ctx, client.ObjectKey{Name: bootstrapToken.Spec.ProjectRef.Name}, project); err != nil { + return project, err + } + + if project.Status.ID == nil { + logger.Info("Project is not ready") + return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, bootstrapToken.Spec.ProjectRef.Name) + } + + if err := controllerutil.SetOwnerReference(project, bootstrapToken, in.Scheme); err != nil { + return project, fmt.Errorf("could not set bootstrapToken owner reference, got error: %+v", err) + } + + return project, nil +} + +func (in *BootstrapTokenReconciler) sync(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) (*consoleapi.BootstrapTokenBase, error) { attributes := consoleapi.BootstrapTokenAttributes{} // Configure optional user binding diff --git a/go/controller/internal/controller/catalog_controller.go b/go/controller/internal/controller/catalog_controller.go index 44e7b1c075..bff54100cf 100644 --- a/go/controller/internal/controller/catalog_controller.go +++ b/go/controller/internal/controller/catalog_controller.go @@ -5,11 +5,6 @@ import ( goerrors "errors" "fmt" - "github.com/pluralsh/console/go/controller/api/v1alpha1" - "github.com/pluralsh/console/go/controller/internal/cache" - consoleclient "github.com/pluralsh/console/go/controller/internal/client" - operrors "github.com/pluralsh/console/go/controller/internal/errors" - "github.com/pluralsh/console/go/controller/internal/utils" "github.com/samber/lo" "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -20,6 +15,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/pluralsh/console/go/controller/api/v1alpha1" + "github.com/pluralsh/console/go/controller/internal/cache" + consoleclient "github.com/pluralsh/console/go/controller/internal/client" + operrors "github.com/pluralsh/console/go/controller/internal/errors" + "github.com/pluralsh/console/go/controller/internal/utils" ) const ( @@ -177,6 +178,10 @@ func (r *CatalogReconciler) handleExistingResource(ctx context.Context, catalog } func (r *CatalogReconciler) isAlreadyExists(ctx context.Context, catalog *v1alpha1.Catalog) (bool, error) { + if catalog.Status.HasReadonlyCondition() { + return catalog.Status.IsReadonly(), nil + } + _, err := r.ConsoleClient.GetCatalog(ctx, nil, lo.ToPtr(catalog.CatalogName())) if errors.IsNotFound(err) { return false, nil @@ -186,7 +191,7 @@ func (r *CatalogReconciler) isAlreadyExists(ctx context.Context, catalog *v1alph } if !catalog.Status.HasID() { - log.FromContext(ctx).Info("Catalog already exists r the API, running r read-only mode") + log.FromContext(ctx).Info("Catalog already exists in the API, running in read-only mode") return true, nil } diff --git a/go/controller/internal/controller/project_controller.go b/go/controller/internal/controller/project_controller.go index 06d0afdb9e..ef4d1acc8a 100644 --- a/go/controller/internal/controller/project_controller.go +++ b/go/controller/internal/controller/project_controller.go @@ -161,6 +161,10 @@ func (in *ProjectReconciler) addOrRemoveFinalizer(ctx context.Context, project * } func (in *ProjectReconciler) isAlreadyExists(ctx context.Context, project *v1alpha1.Project) (bool, error) { + if project.Status.HasReadonlyCondition() { + return project.Status.IsReadonly(), nil + } + _, err := in.ConsoleClient.GetProject(ctx, nil, lo.ToPtr(project.ConsoleName())) if errors.IsNotFound(err) { return false, nil