Skip to content

CLOUDP-273419: Don't delete teams when not assigned to a project #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/api/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const (
SearchIndexesNotReady = "SearchIndexesNotReady"
)

// Atlas Teams condition types
const (
TeamUnmanaged ConditionType = "TeamUnmanaged"
)

// Generic condition type
const (
ResourceVersionStatus ConditionType = "ResourceVersionIsValid"
Expand Down
8 changes: 3 additions & 5 deletions pkg/controller/atlasproject/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ func TestDelete(t *testing.T) {
&akov2.AtlasTeam{ObjectMeta: metav1.ObjectMeta{Name: teamName}},
},
},
"should delete team from Atlas when AtlasProject with finalizer is deleted": {
"should update team status when project is deleted": {
atlasClientMocker: func() *mongodbatlas.Client {
projectsMock := &atlasmocks.ProjectsClientMock{
GetProjectTeamsAssignedFunc: func(projectID string) (*mongodbatlas.TeamsAssigned, *mongodbatlas.Response, error) {
Expand All @@ -956,10 +956,6 @@ func TestDelete(t *testing.T) {
},
}
teamsMock := &atlasmocks.TeamsClientMock{
RemoveTeamFromOrganizationFunc: func(orgID string, teamID string) (*mongodbatlas.Response, error) {
return nil, nil
},
RemoveTeamFromOrganizationRequests: map[string]struct{}{},
ListFunc: func(orgID string) ([]mongodbatlas.Team, *mongodbatlas.Response, error) {
return []mongodbatlas.Team{
{
Expand Down Expand Up @@ -1073,6 +1069,7 @@ func TestDelete(t *testing.T) {
k8sClient := fake.NewClientBuilder().
WithScheme(testScheme).
WithObjects(tt.objects...).
WithStatusSubresource(tt.objects...).
WithIndex(
instancesIndexer.Object(),
instancesIndexer.Name(),
Expand All @@ -1091,6 +1088,7 @@ func TestDelete(t *testing.T) {
},
},
projectService: tt.projectServiceMocker(),
EventRecorder: record.NewFakeRecorder(1),
}
ctx := &workflow.Context{
Context: context.Background(),
Expand Down
25 changes: 9 additions & 16 deletions pkg/controller/atlasproject/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,19 @@ func (r *AtlasProjectReconciler) updateTeamState(ctx *workflow.Context, project
conditions := akov2.InitCondition(team, api.FalseCondition(api.ReadyType))
teamCtx := workflow.NewContext(log, conditions, ctx.Context)

atlasClient, orgID, err := r.AtlasProvider.Client(teamCtx.Context, project.ConnectionSecretObjectKey(), log)
if err != nil {
return err
}
teamCtx.Client = atlasClient

if len(assignedProjects) == 0 {
if customresource.IsResourcePolicyKeepOrDefault(project, r.ObjectDeletionProtection) {
log.Debug("team %s has no project associated, "+
"skipping deletion from Atlas due to ObjectDeletionProtection being set", team.Spec.Name)
} else {
log.Debugf("team %s has no project associated to it. removing from atlas.", team.Spec.Name)
_, err = teamCtx.Client.Teams.RemoveTeamFromOrganization(ctx.Context, orgID, team.Status.ID)
if err != nil {
return err
}
if err = customresource.ManageFinalizer(ctx.Context, r.Client, team, customresource.UnsetFinalizer); err != nil {
return err
}

teamCtx.SetConditionTrueMsg(api.TeamUnmanaged, "This resource is only reconciled when associated to a project")
} else {
if err = customresource.ManageFinalizer(ctx.Context, r.Client, team, customresource.SetFinalizer); err != nil {
return err
}
teamCtx.EnsureStatusOption(status.AtlasTeamUnsetID())
}

teamCtx.SetConditionTrue(api.ReadyType)
teamCtx.EnsureStatusOption(status.AtlasTeamSetProjects(assignedProjects))
statushandler.Update(teamCtx, r.Client, r.EventRecorder, team)

Expand Down
Loading
Loading