Skip to content

Commit

Permalink
update project getter in bootstrap token, catalog and global service …
Browse files Browse the repository at this point in the history
…reconcilers
  • Loading branch information
maciaszczykm committed Jan 29, 2025
1 parent 9bbc419 commit 300db5a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go/controller/api/v1alpha1/bootstraptoken_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BootstrapTokenSpec struct {
// +kubebuilder:validation:Optional
User *string `json:"user,omitempty"`

// ProjectRef is the optional project that all clusters spawned by generated bootstrap token will belong to
// ProjectRef is the project that all clusters spawned by generated bootstrap token will belong to
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Project is immutable"
// +kubebuilder:validation:Required
ProjectRef v1.ObjectReference `json:"projectRef,omitempty"`
Expand Down
41 changes: 17 additions & 24 deletions go/controller/internal/controller/bootstraptoken_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -51,8 +49,6 @@ type BootstrapTokenReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (in *BootstrapTokenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, retErr error) {
logger := log.FromContext(ctx)

bootstrapToken := new(v1alpha1.BootstrapToken)
if err := in.Get(ctx, req.NamespacedName, bootstrapToken); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand All @@ -75,8 +71,7 @@ func (in *BootstrapTokenReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}()

// Handle proper resource deletion via finalizer
result := in.addOrRemoveFinalizer(ctx, bootstrapToken)
if result != nil {
if result := in.addOrRemoveFinalizer(ctx, bootstrapToken); result != nil {
return *result, nil
}

Expand All @@ -87,8 +82,13 @@ func (in *BootstrapTokenReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, nil
}

project, result, err := in.getProject(ctx, bootstrapToken)
if result != nil || err != nil {
return handleRequeue(result, err, bootstrapToken.SetCondition)
}

// Create token and generate secret
apiBootstrapToken, err := in.ensure(ctx, bootstrapToken)
apiBootstrapToken, err := in.ensure(ctx, bootstrapToken, *project)
if err != nil {
if goerrors.Is(err, operrors.ErrRetriable) {
utils.MarkCondition(bootstrapToken.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
Expand All @@ -106,23 +106,25 @@ 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)
func (in *BootstrapTokenReconciler) getProject(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) (*v1alpha1.Project, *ctrl.Result, error) {
project := &v1alpha1.Project{}
if err := in.Get(ctx, client.ObjectKey{Name: bootstrapToken.Spec.ProjectRef.Name}, project); err != nil {
return project, err
if errors.IsNotFound(err) {
return nil, &waitForResources, err
}

return nil, nil, 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)
return nil, &waitForResources, fmt.Errorf("project is not ready yet")
}

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 nil, nil, fmt.Errorf("could not set owner reference: %+v", err)
}

return project, nil
return project, nil, nil
}

func (in *BootstrapTokenReconciler) addOrRemoveFinalizer(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken) *ctrl.Result {
Expand Down Expand Up @@ -158,7 +160,7 @@ 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) ensure(ctx context.Context, bootstrapToken *v1alpha1.BootstrapToken, project v1alpha1.Project) (*consoleapi.BootstrapTokenBase, error) {
attributes := consoleapi.BootstrapTokenAttributes{}

// Configure optional user binding
Expand All @@ -176,15 +178,6 @@ func (in *BootstrapTokenReconciler) ensure(ctx context.Context, bootstrapToken *
}

// Configure required project binding
project, err := in.getProject(ctx, bootstrapToken)
if errors.IsNotFound(err) {
return nil, operrors.ErrRetriable
}

if err != nil {
return nil, err
}

attributes.ProjectID = lo.FromPtr(project.ConsoleID())

// Create the token
Expand Down
30 changes: 13 additions & 17 deletions go/controller/internal/controller/catalog_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import (
"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"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -104,15 +102,11 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, err
}
if changed {
project, err := r.getProject(ctx, catalog)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(catalog.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return waitForResources, nil
}
utils.MarkCondition(catalog.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, err.Error())
return ctrl.Result{}, err
project, res, err := r.getProject(ctx, catalog)
if res != nil || err != nil {
return handleRequeue(res, err, catalog.SetCondition)
}

apiCatalog, err := r.ConsoleClient.UpsertCatalog(ctx, catalog.Attributes(project.Status.ID))
if err != nil {
logger.Error(err, "unable to create or update catalog")
Expand All @@ -128,25 +122,27 @@ func (r *CatalogReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_
return ctrl.Result{}, nil
}

func (r *CatalogReconciler) getProject(ctx context.Context, catalog *v1alpha1.Catalog) (*v1alpha1.Project, error) {
logger := log.FromContext(ctx)
func (r *CatalogReconciler) getProject(ctx context.Context, catalog *v1alpha1.Catalog) (*v1alpha1.Project, *ctrl.Result, error) {
project := &v1alpha1.Project{}
if catalog.Spec.ProjectRef != nil {
if err := r.Get(ctx, client.ObjectKey{Name: catalog.Spec.ProjectRef.Name}, project); err != nil {
return project, err
if errors.IsNotFound(err) {
return nil, &waitForResources, err
}

return nil, nil, err
}

if project.Status.ID == nil {
logger.Info("Project is not ready")
return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, catalog.Spec.ProjectRef.Name)
return nil, &waitForResources, fmt.Errorf("project is not ready yet")
}

if err := controllerutil.SetOwnerReference(project, catalog, r.Scheme); err != nil {
return project, fmt.Errorf("could not set catalog owner reference, got error: %+v", err)
return nil, nil, fmt.Errorf("could not set owner reference: %+v", err)
}
}

return project, nil
return project, nil, nil
}

func (r *CatalogReconciler) handleExistingResource(ctx context.Context, catalog *v1alpha1.Catalog) (ctrl.Result, error) {
Expand Down
27 changes: 12 additions & 15 deletions go/controller/internal/controller/globalservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,9 @@ func (r *GlobalServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}

project, err := r.getProject(ctx, globalService)
if err != nil {
if errors.IsNotFound(err) {
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, notFoundOrReadyErrorMessage(err))
return waitForResources, nil
}
utils.MarkCondition(globalService.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReason, err.Error())
return ctrl.Result{}, err
project, result, err := r.getProject(ctx, globalService)
if result != nil || err != nil {
return handleRequeue(result, err, globalService.SetCondition)
}

attr := globalService.Attributes(provider.Status.ID, project.Status.ID)
Expand Down Expand Up @@ -239,25 +234,27 @@ func (r *GlobalServiceReconciler) getProvider(ctx context.Context, globalService
return provider, nil
}

func (r *GlobalServiceReconciler) getProject(ctx context.Context, globalService *v1alpha1.GlobalService) (*v1alpha1.Project, error) {
logger := log.FromContext(ctx)
func (r *GlobalServiceReconciler) getProject(ctx context.Context, globalService *v1alpha1.GlobalService) (*v1alpha1.Project, *ctrl.Result, error) {
project := &v1alpha1.Project{}
if globalService.Spec.ProjectRef != nil {
if err := r.Get(ctx, client.ObjectKey{Name: globalService.Spec.ProjectRef.Name}, project); err != nil {
return project, err
if errors.IsNotFound(err) {
return nil, &waitForResources, err
}

return nil, nil, err
}

if project.Status.ID == nil {
logger.Info("Project is not ready")
return project, apierrors.NewNotFound(schema.GroupResource{Resource: "Project", Group: "deployments.plural.sh"}, globalService.Spec.ProjectRef.Name)
return nil, &waitForResources, fmt.Errorf("project is not ready yet")
}

if err := controllerutil.SetOwnerReference(project, globalService, r.Scheme); err != nil {
return project, fmt.Errorf("could not set global service owner reference, got error: %+v", err)
return nil, nil, fmt.Errorf("could not set owner reference: %+v", err)
}
}

return project, nil
return project, nil, nil
}

func (r *GlobalServiceReconciler) handleCreate(sha string, global *v1alpha1.GlobalService, svc *v1alpha1.ServiceDeployment, attrs console.GlobalServiceAttributes) error {
Expand Down

0 comments on commit 300db5a

Please sign in to comment.