Skip to content

Commit

Permalink
fix broken readonly feature in project and catalog controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks committed Feb 4, 2025
1 parent 73bab5b commit 598e8a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
44 changes: 22 additions & 22 deletions go/controller/internal/controller/bootstraptoken_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions go/controller/internal/controller/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions go/controller/internal/controller/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 598e8a2

Please sign in to comment.