-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update project getter in bootstrap token, catalog and global service …
…reconcilers
- Loading branch information
1 parent
9bbc419
commit 300db5a
Showing
4 changed files
with
43 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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()) | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters