Skip to content

Commit

Permalink
Delete project via operator (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jul 8, 2024
1 parent 9fc475a commit 233b48b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 14 deletions.
2 changes: 1 addition & 1 deletion controller/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.1
// Dependencies
require (
github.com/Yamashou/gqlgenc v0.18.1
github.com/pluralsh/console-client-go v0.11.1
github.com/pluralsh/console-client-go v0.11.3
github.com/pluralsh/polly v0.1.7
github.com/samber/lo v1.39.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions controller/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ github.com/pluralsh/console-client-go v0.11.0 h1:Yk6d7V0tqz5TUHPwynbHrRKIcD2vJ25
github.com/pluralsh/console-client-go v0.11.0/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.11.1 h1:ljvJxCCyJSGCWFr9DYbfMxeJALu0BsUh8dwN3yQ6gvo=
github.com/pluralsh/console-client-go v0.11.1/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.11.3 h1:6HP0Tetebk0ZFRCALWjdy14Nzc8hci9/Jn0NuhQ2BKg=
github.com/pluralsh/console-client-go v0.11.3/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/polly v0.1.7 h1:MUuTb6rCUV1doisaFGC+iz+33ZPU4FZHOb/kFwSDkjw=
github.com/pluralsh/polly v0.1.7/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
1 change: 1 addition & 0 deletions controller/internal/client/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type ConsoleClient interface {
GetProject(ctx context.Context, id, name *string) (*console.ProjectFragment, error)
UpdateProject(ctx context.Context, id string, attributes console.ProjectAttributes) (*console.ProjectFragment, error)
IsProjectExists(ctx context.Context, name string) (bool, error)
DeleteProject(ctx context.Context, id string) error
UseCredentials(namespace string, credentialsCache credentials.NamespaceCredentialsCache) (string, error)
}

Expand Down
5 changes: 5 additions & 0 deletions controller/internal/client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (c *client) UpdateProject(ctx context.Context, id string, attributes consol
return response.UpdateProject, nil
}

func (c *client) DeleteProject(ctx context.Context, id string) error {
_, err := c.consoleClient.DeleteProject(ctx, id)
return err
}

func (c *client) IsProjectExists(ctx context.Context, name string) (bool, error) {
scm, err := c.GetProject(ctx, nil, &name)
if errors.IsNotFound(err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *NamespaceCredentialsReconciler) Reconcile(ctx context.Context, req reco
// Try to add namespace credentials to cache.
if err := r.CredentialsCache.AddNamespaceCredentials(nc); err != nil {
utils.MarkFalse(nc.SetCondition, v1alpha1.SynchronizedConditionType, v1alpha1.SynchronizedConditionReasonError, err.Error())
return ctrl.Result{}, err
return requeue, nil
}

utils.MarkTrue(nc.SetCondition, v1alpha1.SynchronizedConditionType, v1alpha1.SynchronizedConditionReason, "")
Expand Down
26 changes: 19 additions & 7 deletions controller/internal/controller/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (in *ProjectReconciler) Reconcile(ctx context.Context, req reconcile.Reques
utils.MarkCondition(project.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionFalse, v1alpha1.ReadyConditionReason, "")

// Handle proper resource deletion via finalizer
result := in.addOrRemoveFinalizer(project)
result := in.addOrRemoveFinalizer(ctx, project)
if result != nil {
return *result, nil
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (in *ProjectReconciler) Reconcile(ctx context.Context, req reconcile.Reques
return requeue, nil
}

func (in *ProjectReconciler) addOrRemoveFinalizer(project *v1alpha1.Project) *ctrl.Result {
func (in *ProjectReconciler) addOrRemoveFinalizer(ctx context.Context, project *v1alpha1.Project) *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.
if project.ObjectMeta.DeletionTimestamp.IsZero() && !controllerutil.ContainsFinalizer(project, ProjectProtectionFinalizerName) {
Expand All @@ -127,6 +127,22 @@ func (in *ProjectReconciler) addOrRemoveFinalizer(project *v1alpha1.Project) *ct
// currently to delete project from Console API, so we simply detach
// and only remove the CRD.
if !project.ObjectMeta.DeletionTimestamp.IsZero() {
exists, err := in.ConsoleClient.IsProjectExists(ctx, project.Spec.Name)
if err != nil {
return &requeue
}

// Remove Pipeline from Console API if it exists.
if exists {
if err := in.ConsoleClient.DeleteProject(ctx, project.Status.GetID()); err != nil {
// If it fails to delete the external dependency here, return with error
// so that it can be retried.
utils.MarkCondition(project.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionFalse, v1alpha1.SynchronizedConditionReasonError, err.Error())
return &ctrl.Result{}
}

// project deletion is synchronous so can just fall back to removing the finalizer and reconciling
}
// Stop reconciliation as the item is being deleted
controllerutil.RemoveFinalizer(project, ProjectProtectionFinalizerName)
return &ctrl.Result{}
Expand All @@ -136,10 +152,6 @@ func (in *ProjectReconciler) addOrRemoveFinalizer(project *v1alpha1.Project) *ct
}

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 Expand Up @@ -177,7 +189,7 @@ func (in *ProjectReconciler) handleExistingProject(ctx context.Context, project
project.Status.ID = &apiProject.ID

utils.MarkCondition(project.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionTrue, v1alpha1.SynchronizedConditionReason, "")
utils.MarkCondition(project.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "")
// utils.MarkCondition(project.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "")

return requeue, nil
}
Expand Down
10 changes: 5 additions & 5 deletions controller/internal/controller/service_account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ func (r *ServiceAccountReconciler) handleExistingServiceAccount(ctx context.Cont

sa.Status.ID = &apiServiceAccount.ID

utils.MarkCondition(sa.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionTrue, v1alpha1.SynchronizedConditionReason, "")
utils.MarkCondition(sa.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "")
// utils.MarkCondition(sa.SetCondition, v1alpha1.SynchronizedConditionType, v1.ConditionTrue, v1alpha1.SynchronizedConditionReason, "")
// utils.MarkCondition(sa.SetCondition, v1alpha1.ReadyConditionType, v1.ConditionTrue, v1alpha1.ReadyConditionReason, "")

return requeue, nil
}

func (r *ServiceAccountReconciler) isAlreadyExists(ctx context.Context, sa *v1alpha1.ServiceAccount) (bool, error) {
if sa.Status.HasReadonlyCondition() {
return sa.Status.IsReadonly(), nil
}
// if sa.Status.HasReadonlyCondition() {
// return sa.Status.IsReadonly(), nil
// }

_, err := r.ConsoleClient.GetServiceAccount(ctx, sa.Spec.Email)
if errors.IsNotFound(err) {
Expand Down
47 changes: 47 additions & 0 deletions controller/internal/test/mocks/ConsoleClient_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 233b48b

Please sign in to comment.