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: handle pending deletion of projects, fixes #149 #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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/projects/v1alpha1/project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,16 @@ type ProjectParameters struct {
// One of disabled, private, or enabled.
// +optional
WikiAccessLevel *AccessControlValue `json:"wikiAccessLevel,omitempty"`

// Force the immediate deletion of the project when removed. In GitLab Premium and Ultimate a project 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 project to delete permanently (see pathWithNamespace). Only required if PermanentlyRemove is set to true.
// GitLab Premium and Ultimate only.
// +optional
FullPathToRemove *string `json:"fullPathToRemove,omitempty"`
}

// ProjectNamespace represents a project namespace.
Expand Down
10 changes: 10 additions & 0 deletions apis/projects/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/crossplane/crossplane-tools v0.0.0-20230925130601-628280f8bf79
github.com/google/go-cmp v0.6.0
github.com/pkg/errors v0.9.1
github.com/xanzy/go-gitlab v0.107.0
github.com/xanzy/go-gitlab v0.110.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
Expand All @@ -22,6 +22,7 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/sync v0.10.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
Expand Down Expand Up @@ -74,11 +75,11 @@ require (
go.uber.org/zap v1.27.0
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.33.0
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xanzy/go-gitlab v0.107.0 h1:P2CT9Uy9yN9lJo3FLxpMZ4xj6uWcpnigXsjvqJ6nd2Y=
github.com/xanzy/go-gitlab v0.107.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xanzy/go-gitlab v0.110.0 h1:hsFIFp01v/0D0sdUXoZfRk6CROzZbHQplk6NzKSFKhc=
github.com/xanzy/go-gitlab v0.110.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand All @@ -167,8 +167,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -187,8 +187,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
10 changes: 10 additions & 0 deletions package/crds/projects.gitlab.crossplane.io_projects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ spec:
forkingAccessLevel:
description: One of disabled, private, or enabled.
type: string
fullPathToRemove:
description: |-
Full path of project to delete permanently (see pathWithNamespace). Only required if PermanentlyRemove is set to true.
GitLab Premium and Ultimate only.
type: string
groupWithProjectTemplatesId:
description: |-
For group-level custom templates, specifies ID of group from which all the custom project templates are sourced.
Expand Down Expand Up @@ -317,6 +322,11 @@ spec:
Repository name for new project.
Generated based on name if not provided (generated as lowercase with dashes).
type: string
permanentlyRemove:
description: |-
Force the immediate deletion of the project when removed. In GitLab Premium and Ultimate a project is by default
just marked for deletion and removed permanently after seven days. Defaults to false.
type: boolean
printingMergeRequestLinkEnabled:
description: Show link to create/view merge request when pushing
from the command line.
Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/groups/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *MockClient) ListVariables(gid interface{}, opt *gitlab.ListGroupVariabl
}

// GetVariable calls the underlying MockGetGrouptVariable method.
func (c *MockClient) GetVariable(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
func (c *MockClient) GetVariable(gid interface{}, key string, opt *gitlab.GetGroupVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
return c.MockGetGroupVariable(gid, key)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/groups/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
// VariableClient defines Gitlab Variable service operations
type VariableClient interface {
ListVariables(gid interface{}, opt *gitlab.ListGroupVariablesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error)
GetVariable(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
GetVariable(gid interface{}, key string, opt *gitlab.GetGroupVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
CreateVariable(gid interface{}, opt *gitlab.CreateGroupVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
UpdateVariable(gid interface{}, key string, opt *gitlab.UpdateGroupVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
RemoveVariable(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
Expand Down
6 changes: 3 additions & 3 deletions pkg/clients/projects/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MockClient struct {
MockGetProject func(pid interface{}, opt *gitlab.GetProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
MockCreateProject func(opt *gitlab.CreateProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
MockEditProject func(pid interface{}, opt *gitlab.EditProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
MockDeleteProject func(pid interface{}, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
MockDeleteProject func(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)

MockGetHook func(pid interface{}, hook int, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectHook, *gitlab.Response, error)
MockAddHook func(pid interface{}, opt *gitlab.AddProjectHookOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectHook, *gitlab.Response, error)
Expand Down Expand Up @@ -124,8 +124,8 @@ func (c *MockClient) EditProject(pid interface{}, opt *gitlab.EditProjectOptions
}

// DeleteProject calls the underlying MockDeleteProject method
func (c *MockClient) DeleteProject(pid interface{}, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return c.MockDeleteProject(pid)
func (c *MockClient) DeleteProject(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return c.MockDeleteProject(pid, opt)
}

// GetProjectHook calls the underlying MockGetProjectHook method.
Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/projects/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Client interface {
GetProject(pid interface{}, opt *gitlab.GetProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
CreateProject(opt *gitlab.CreateProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
EditProject(pid interface{}, opt *gitlab.EditProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Project, *gitlab.Response, error)
DeleteProject(pid interface{}, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
DeleteProject(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
}

// NewProjectClient returns a new Gitlab Project service
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/groups/variables/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
variable, res, err := e.client.GetVariable(
*cr.Spec.ForProvider.GroupID,
cr.Spec.ForProvider.Key,
&gitlab.GetGroupVariableOptions{},
gitlab.WithContext(ctx))

if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion pkg/controller/projects/projects/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package projects
import (
"context"
"strconv"
"strings"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/connection"
Expand Down Expand Up @@ -187,7 +188,18 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalDelete{}, errors.New(errNotProject)
}

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

if cr.Spec.ForProvider.PermanentlyRemove != nil && *cr.Spec.ForProvider.PermanentlyRemove {
_, err = e.client.DeleteProject(meta.GetExternalName(cr), &gitlab.DeleteProjectOptions{
PermanentlyRemove: cr.Spec.ForProvider.PermanentlyRemove,
FullPath: cr.Spec.ForProvider.FullPathToRemove,
}, gitlab.WithContext(ctx))
}
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteFailed)
}

Expand Down
60 changes: 55 additions & 5 deletions pkg/controller/projects/projects/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func withPath(p *string) projectModifier {
return func(r *v1alpha1.Project) { r.Spec.ForProvider.Path = p }
}

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

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

func withExternalName(projectID string) projectModifier {
return func(r *v1alpha1.Project) { meta.SetExternalName(r, projectID) }
}
Expand Down Expand Up @@ -667,9 +675,16 @@ func TestUpdate(t *testing.T) {
}

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

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

cases := map[string]struct {
Expand All @@ -688,7 +703,7 @@ func TestDelete(t *testing.T) {
"SuccessfulDeletion": {
args: args{
project: &fake.MockClient{
MockDeleteProject: func(pid interface{}, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
MockDeleteProject: func(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return &gitlab.Response{}, nil
},
},
Expand All @@ -702,8 +717,8 @@ func TestDelete(t *testing.T) {
"FailedDeletion": {
args: args{
project: &fake.MockClient{
MockDeleteProject: func(pid interface{}, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return &gitlab.Response{}, errBoom
MockDeleteProject: func(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
return nil, errBoom
},
},
cr: project(),
Expand All @@ -713,9 +728,41 @@ func TestDelete(t *testing.T) {
err: errors.Wrap(errBoom, errDeleteFailed),
},
},
"SuccessfulPermanentlyDeletion": {
args: args{
project: &fake.MockClient{
MockDeleteProject: func(pid interface{}, opt *gitlab.DeleteProjectOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
recordedCalls = append(recordedCalls, deleteProjectCalls{Pid: pid, Opt: opt})
return &gitlab.Response{}, nil
},
},
cr: project(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath(gitlab.Ptr("project")),
withFullPathToRemove(gitlab.Ptr("path/to/project")),
withStatus(v1alpha1.ProjectObservation{PathWithNamespace: "path/to/project"}),
),
},
want: want{
cr: project(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath(gitlab.Ptr("project")),
withFullPathToRemove(gitlab.Ptr("path/to/project")),
withStatus(v1alpha1.ProjectObservation{PathWithNamespace: "path/to/project"}),
),
calls: []deleteProjectCalls{
{Pid: "0", Opt: &gitlab.DeleteProjectOptions{}},
{Pid: "0", Opt: &gitlab.DeleteProjectOptions{PermanentlyRemove: gitlab.Ptr(true), FullPath: gitlab.Ptr("path/to/project")}},
},
err: nil,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
recordedCalls = nil
e := &external{kube: tc.kube, client: tc.project}
_, err := e.Delete(context.Background(), tc.args.cr)

Expand All @@ -725,6 +772,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)
}
})
}

Expand Down
Loading