Skip to content
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

feat: Add immediate deletion of groups #168

Merged
merged 2 commits into from
Dec 4, 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
10 changes: 10 additions & 0 deletions apis/groups/v1alpha1/group_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ type GroupParameters struct {
// SharedWithGroups create links for sharing a group with another group.
// +optional
SharedWithGroups []SharedWithGroups `json:"sharedWithGroups,omitempty"`

// Force the immediate deletion of the group when removed. In GitLab Premium and Ultimate a group is by default
// just marked for deletion and removed permanently after seven days. Defaults to false.
// +optional
PermanentlyRemove *bool `json:"permanentlyRemove,omitempty"`

// Full path of group to delete permanently. Only required if PermanentlyRemove is set to true.
// GitLab Premium and Ultimate only.
// +optional
FullPathToRemove *string `json:"fullPathToRemove,omitempty"`
}

// AccessLevelValue represents a permission level within GitLab.
Expand Down
10 changes: 10 additions & 0 deletions apis/groups/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion build
10 changes: 10 additions & 0 deletions package/crds/groups.gitlab.crossplane.io_groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ spec:
description: Extra pipeline minutes quota for this group (purchased
in addition to the minutes included in the plan).
type: integer
fullPathToRemove:
description: |-
Full path of group to delete permanently. Only required if PermanentlyRemove is set to true.
GitLab Premium and Ultimate only.
type: string
lfsEnabled:
description: Enable/disable Large File Storage (LFS) for the projects
in this group.
Expand Down Expand Up @@ -194,6 +199,11 @@ spec:
path:
description: The path of the group.
type: string
permanentlyRemove:
description: |-
Force the immediate deletion of the group when removed. In GitLab Premium and Ultimate a group is by default
just marked for deletion and removed permanently after seven days. Defaults to false.
type: boolean
projectCreationLevel:
description: |-
developers can create projects in the group.
Expand Down
14 changes: 14 additions & 0 deletions pkg/controller/groups/groups/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package groups
import (
"context"
"strconv"
"strings"
"time"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
Expand Down Expand Up @@ -245,6 +246,19 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
}

_, err := e.client.DeleteGroup(meta.GetExternalName(cr), &gitlab.DeleteGroupOptions{}, gitlab.WithContext(ctx))
// if the group is for some reason already marked for deletion, we ignore the error and continue to delete the group permanently
if err != nil && !strings.Contains(err.Error(), "Group has been already marked for deletion") {
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteFailed)
}

// permanent deletion is only available on subgroups; when executed against top-level groups the backend will return an error
isSubGroup := cr.Status.AtProvider.FullPath != nil && *cr.Status.AtProvider.FullPath != cr.Spec.ForProvider.Path
if cr.Spec.ForProvider.PermanentlyRemove != nil && *cr.Spec.ForProvider.PermanentlyRemove && isSubGroup {
_, err = e.client.DeleteGroup(meta.GetExternalName(cr), &gitlab.DeleteGroupOptions{
PermanentlyRemove: cr.Spec.ForProvider.PermanentlyRemove,
FullPath: cr.Spec.ForProvider.FullPathToRemove,
}, gitlab.WithContext(ctx))
}
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteFailed)
}

Expand Down
87 changes: 82 additions & 5 deletions pkg/controller/groups/groups/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func withPath(s string) groupModifier {
return func(r *v1alpha1.Group) { r.Spec.ForProvider.Path = s }
}

func withPermanentlyRemove(b *bool) groupModifier {
return func(r *v1alpha1.Group) { r.Spec.ForProvider.PermanentlyRemove = b }
}

func withFullPathToRemove(s *string) groupModifier {
return func(r *v1alpha1.Group) { r.Spec.ForProvider.FullPathToRemove = s }
}

func withDescription(s *string) groupModifier {
return func(r *v1alpha1.Group) { r.Spec.ForProvider.Description = s }
}
Expand Down Expand Up @@ -844,9 +852,16 @@ func TestUpdate(t *testing.T) {
}

func TestDelete(t *testing.T) {
type deleteGroupCalls struct {
Pid interface{}
Opt *gitlab.DeleteGroupOptions
}
var recordedCalls []deleteGroupCalls

type want struct {
cr resource.Managed
err error
cr resource.Managed
calls []deleteGroupCalls
err error
}

cases := map[string]struct {
Expand All @@ -866,21 +881,23 @@ func TestDelete(t *testing.T) {
args: args{
group: &fake.MockClient{
MockDeleteGroup: func(pid interface{}, opt *gitlab.DeleteGroupOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
recordedCalls = append(recordedCalls, deleteGroupCalls{Pid: pid, Opt: opt})
return &gitlab.Response{}, nil
},
},
cr: group(withExternalName("0")),
},
want: want{
cr: group(withExternalName("0")),
err: nil,
cr: group(withExternalName("0")),
calls: []deleteGroupCalls{{Pid: "0", Opt: &gitlab.DeleteGroupOptions{}}},
err: nil,
},
},
"FailedDeletion": {
args: args{
group: &fake.MockClient{
MockDeleteGroup: func(pid interface{}, opt *gitlab.DeleteGroupOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return &gitlab.Response{}, errBoom
return nil, errBoom
},
},
cr: group(),
Expand All @@ -890,9 +907,66 @@ func TestDelete(t *testing.T) {
err: errors.Wrap(errBoom, errDeleteFailed),
},
},
"SuccessfulPermanentlyDeletion": {
args: args{
group: &fake.MockClient{
MockDeleteGroup: func(pid interface{}, opt *gitlab.DeleteGroupOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
recordedCalls = append(recordedCalls, deleteGroupCalls{Pid: pid, Opt: opt})
return &gitlab.Response{}, nil
},
},
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("group"),
withFullPathToRemove(gitlab.Ptr("path/to/group")),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("path/to/group")})),
},
want: want{
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("group"),
withFullPathToRemove(gitlab.Ptr("path/to/group")),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("path/to/group")})),
calls: []deleteGroupCalls{
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{}},
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{PermanentlyRemove: gitlab.Ptr(true), FullPath: gitlab.Ptr("path/to/group")}},
},
err: nil,
},
},
"SuccessfulPermanentlyTopLevelGroupDeletion": {
args: args{
group: &fake.MockClient{
MockDeleteGroup: func(pid interface{}, opt *gitlab.DeleteGroupOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
recordedCalls = append(recordedCalls, deleteGroupCalls{Pid: pid, Opt: opt})
return &gitlab.Response{}, nil
},
},
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("top-level-group"),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("top-level-group")})),
},
want: want{
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("top-level-group"),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("top-level-group")}),
),
calls: []deleteGroupCalls{
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{}},
},
err: nil,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
recordedCalls = nil
e := &external{kube: tc.kube, client: tc.group}
_, err := e.Delete(context.Background(), tc.args.cr)

Expand All @@ -902,6 +976,9 @@ func TestDelete(t *testing.T) {
if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
if diff := cmp.Diff(tc.want.calls, recordedCalls, test.EquateConditions()); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
})
}
}
Loading