Skip to content

Commit baf29b5

Browse files
feat(variables): early return on deletion skips secret retrieval (#238)
Signed-off-by: Markus Siebert <[email protected]>
1 parent 7e8dacc commit baf29b5

File tree

8 files changed

+174
-0
lines changed

8 files changed

+174
-0
lines changed

pkg/cluster/controller/groups/variables/zz_controller.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/controller/groups/variables/zz_controller_test.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/controller/projects/variables/zz_controller.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/controller/projects/variables/zz_controller_test.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/namespaced/controller/groups/variables/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
131131
return managed.ExternalObservation{}, errors.Wrap(err, errGetFailed)
132132
}
133133

134+
// Deleting: only need to determine external resource still exists.
135+
if !cr.ObjectMeta.DeletionTimestamp.IsZero() {
136+
return managed.ExternalObservation{ResourceExists: true}, nil
137+
}
138+
134139
if cr.Spec.ForProvider.ValueSecretRef != nil {
135140
if err = e.updateVariableFromSecret(mg, ctx, cr.Spec.ForProvider.ValueSecretRef, &cr.Spec.ForProvider); err != nil {
136141
return managed.ExternalObservation{}, errors.Wrap(err, errUpdateFailed)

pkg/namespaced/controller/groups/variables/controller_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/google/go-cmp/cmp"
2929
gitlab "gitlab.com/gitlab-org/api/client-go"
3030
corev1 "k8s.io/api/core/v1"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
3233

3334
"github.com/crossplane-contrib/provider-gitlab/apis/namespaced/groups/v1alpha1"
@@ -133,6 +134,14 @@ func withEnvironmentScope(scope string) variableModifier {
133134
}
134135
}
135136

137+
var deletionTime = metav1.Now()
138+
139+
func withDeletionTimestamp() variableModifier {
140+
return func(r *v1alpha1.Variable) {
141+
r.ObjectMeta.DeletionTimestamp = &deletionTime
142+
}
143+
}
144+
136145
func variable(m ...variableModifier) *v1alpha1.Variable {
137146
cr := &v1alpha1.Variable{}
138147
for _, f := range m {
@@ -360,6 +369,36 @@ func TestObserve(t *testing.T) {
360369
err: errors.Wrap(errors.New(common.ErrSecretKeyNotFound), errGetFailed),
361370
},
362371
},
372+
373+
"DeletingEarlyReturnSkipsSecret": {
374+
args: args{
375+
kube: &test.MockClient{
376+
MockGet: func(_ context.Context, key client.ObjectKey, obj client.Object) error { return errBoom },
377+
},
378+
variable: &fake.MockClient{
379+
MockGetGroupVariable: func(gid interface{}, key string, opt *gitlab.GetGroupVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
380+
return &pv, &gitlab.Response{}, nil
381+
},
382+
},
383+
cr: variable(
384+
withGroupID(groupID),
385+
withKey(variableKey),
386+
withValueSecretRef(common.TestCreateLocalSecretKeySelector("something", "blah")),
387+
withDeletionTimestamp(),
388+
withConditions(xpv1.Deleting()),
389+
),
390+
},
391+
want: want{
392+
cr: variable(
393+
withGroupID(groupID),
394+
withKey(variableKey),
395+
withValueSecretRef(common.TestCreateLocalSecretKeySelector("something", "blah")),
396+
withDeletionTimestamp(),
397+
withConditions(xpv1.Deleting()),
398+
),
399+
result: managed.ExternalObservation{ResourceExists: true},
400+
},
401+
},
363402
}
364403

365404
for name, tc := range cases {

pkg/namespaced/controller/projects/variables/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
131131
return managed.ExternalObservation{}, errors.Wrap(err, errGetFailed)
132132
}
133133

134+
// Deleting: only need to determine external resource still exists.
135+
if !cr.ObjectMeta.DeletionTimestamp.IsZero() {
136+
return managed.ExternalObservation{ResourceExists: true}, nil
137+
}
138+
134139
if cr.Spec.ForProvider.ValueSecretRef != nil {
135140
if err = e.updateVariableFromSecret(mg, ctx, cr.Spec.ForProvider.ValueSecretRef, &cr.Spec.ForProvider); err != nil {
136141
return managed.ExternalObservation{}, errors.Wrap(err, errUpdateFailed)

pkg/namespaced/controller/projects/variables/controller_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/google/go-cmp/cmp"
2929
gitlab "gitlab.com/gitlab-org/api/client-go"
3030
corev1 "k8s.io/api/core/v1"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
3233

3334
"github.com/crossplane-contrib/provider-gitlab/apis/namespaced/projects/v1alpha1"
@@ -133,6 +134,14 @@ func withEnvironmentScope(scope string) variableModifier {
133134
}
134135
}
135136

137+
var deletionTime = metav1.Now()
138+
139+
func withDeletionTimestamp() variableModifier {
140+
return func(r *v1alpha1.Variable) {
141+
r.ObjectMeta.DeletionTimestamp = &deletionTime
142+
}
143+
}
144+
136145
func variable(m ...variableModifier) *v1alpha1.Variable {
137146
cr := &v1alpha1.Variable{}
138147
for _, f := range m {
@@ -360,6 +369,35 @@ func TestObserve(t *testing.T) {
360369
err: errors.Wrap(errors.New(common.ErrSecretKeyNotFound), errGetFailed),
361370
},
362371
},
372+
"DeletingEarlyReturnSkipsSecret": {
373+
args: args{
374+
kube: &test.MockClient{
375+
MockGet: func(_ context.Context, key client.ObjectKey, obj client.Object) error { return errBoom },
376+
},
377+
variable: &fake.MockClient{
378+
MockGetVariable: func(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
379+
return &pv, &gitlab.Response{}, nil
380+
},
381+
},
382+
cr: variable(
383+
withProjectID(projectID),
384+
withKey(variableKey),
385+
withValueSecretRef(common.TestCreateLocalSecretKeySelector("", "blah")),
386+
withDeletionTimestamp(),
387+
withConditions(xpv1.Deleting()),
388+
),
389+
},
390+
want: want{
391+
cr: variable(
392+
withProjectID(projectID),
393+
withKey(variableKey),
394+
withValueSecretRef(common.TestCreateLocalSecretKeySelector("", "blah")),
395+
withDeletionTimestamp(),
396+
withConditions(xpv1.Deleting()),
397+
),
398+
result: managed.ExternalObservation{ResourceExists: true},
399+
},
400+
},
363401
}
364402

365403
for name, tc := range cases {

0 commit comments

Comments
 (0)