Skip to content

Commit

Permalink
add finalizer, fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ranakan19 committed Apr 10, 2024
1 parent 0987ad3 commit b1816ef
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 26 deletions.
11 changes: 0 additions & 11 deletions controllers/nstemplateset/cluster_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,6 @@ func (r *clusterResourcesManager) deleteClusterResource(ctx context.Context, nsT
if err := r.setStatusUpdatingIfNotProvisioning(ctx, nsTmplSet); err != nil {
return false, err
}
//remove finalizer before deleting
toDelete.SetFinalizers(nil)
if err := r.Client.Update(ctx, toDelete); err != nil {
return false, errs.Wrapf(err, "failed to remove finalizer on cluster resource of name '%s' and gvk '%v'",
toDelete.GetName(), toDelete.GetObjectKind().GroupVersionKind())
}
if err := r.Client.Delete(ctx, toDelete); err != nil {
return false, errs.Wrapf(err, "failed to delete an existing redundant cluster resource of name '%s' and gvk '%v'",
toDelete.GetName(), toDelete.GetObjectKind().GroupVersionKind())
Expand Down Expand Up @@ -322,11 +316,6 @@ func (r *clusterResourcesManager) delete(ctx context.Context, nsTmplSet *toolcha
}

log.FromContext(ctx).Info("deleting cluster resource", "name", toDelete.GetName(), "kind", toDelete.GetObjectKind().GroupVersionKind().Kind)
// remove finalizer before deleting
toDelete.SetFinalizers(nil)
if err := r.Client.Update(ctx, toDelete); err != nil {
return false, r.wrapErrorWithStatusUpdate(ctx, nsTmplSet, r.setStatusTerminatingFailed, err, "failed to remove finalizer from cluster resource '%s'", toDelete.GetName())
}
if err := r.Client.Delete(ctx, toDelete); err != nil && errors.IsNotFound(err) {
// ignore case where the resource did not exist anymore, move to the next one to delete
continue
Expand Down
2 changes: 1 addition & 1 deletion controllers/nstemplateset/cluster_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func TestDeleteClusterResources(t *testing.T) {
t.Run("delete the second ClusterResourceQuota since the first one has deletion timestamp set", func(t *testing.T) {
// given
nsTmplSet := newNSTmplSet(namespaceName, spacename, "withemptycrq", withNamespaces("abcde11", "dev"), withClusterResources("abcde11"))
crq := newClusterResourceQuota(spacename, "withemptycrq")
crq := newClusterResourceQuota(spacename, "withemptycrq", withFinalizer())
deletionTS := metav1.NewTime(time.Now())
crq.SetDeletionTimestamp(&deletionTS)
emptyCrq := newClusterResourceQuota("empty", "withemptycrq")
Expand Down
12 changes: 5 additions & 7 deletions controllers/nstemplateset/nstemplateset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,8 @@ func TestDeleteNSTemplateSet(t *testing.T) {
// given an NSTemplateSet resource and 1 active user namespaces ("dev")
nsTmplSet := newNSTmplSet(namespaceName, spacename, "advanced", withNamespaces("abcde11", "dev", "stage"), withDeletionTs(), withClusterResources("abcde11"))
nsTmplSet.SetDeletionTimestamp(&metav1.Time{Time: time.Now().Add(-61 * time.Second)})
devNS := newNamespace("advanced", spacename, "dev", withTemplateRefUsingRevision("abcde11"), withFinalizer(toolchainv1alpha1.FinalizerName))
stageNS := newNamespace("advanced", spacename, "stage", withTemplateRefUsingRevision("abcde11"), withFinalizer(toolchainv1alpha1.FinalizerName))
devNS := newNamespace("advanced", spacename, "dev", withTemplateRefUsingRevision("abcde11"), withFinalizer())
stageNS := newNamespace("advanced", spacename, "stage", withTemplateRefUsingRevision("abcde11"), withFinalizer())

r, _ := prepareController(t, nsTmplSet, devNS, stageNS)
req := newReconcileRequest(namespaceName, spacename)
Expand All @@ -1391,7 +1391,7 @@ func TestDeleteNSTemplateSet(t *testing.T) {
t.Run("NSTemplateSet not deleted until namespace is deleted", func(t *testing.T) {
// given an NSTemplateSet resource and 2 user namespaces ("dev" and "stage")
nsTmplSet := newNSTmplSet(namespaceName, spacename, "advanced", withNamespaces("abcde11", "dev", "stage"), withDeletionTs(), withClusterResources("abcde11"))
devNS := newNamespace("advanced", spacename, "dev", withTemplateRefUsingRevision("abcde11"), withFinalizer(toolchainv1alpha1.FinalizerName))
devNS := newNamespace("advanced", spacename, "dev", withTemplateRefUsingRevision("abcde11"), withFinalizer())
stageNS := newNamespace("advanced", spacename, "stage", withTemplateRefUsingRevision("abcde11"))

r, fakeClient := prepareController(t, nsTmplSet, devNS, stageNS)
Expand Down Expand Up @@ -1679,7 +1679,6 @@ func newTektonClusterRoleBinding(spacename, tier string) *rbacv1.ClusterRoleBind
},
Name: spacename + "-tekton-view",
Generation: int64(1),
Finalizers: []string{toolchainv1alpha1.FinalizerName},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Expand Down Expand Up @@ -1711,7 +1710,6 @@ func newClusterResourceQuota(spacename, tier string, options ...objectMetaOption
Annotations: map[string]string{},
Name: "for-" + spacename,
Generation: int64(1),
Finalizers: []string{toolchainv1alpha1.FinalizerName},
},
Spec: quotav1.ClusterResourceQuotaSpec{
Quota: corev1.ResourceQuotaSpec{
Expand Down Expand Up @@ -1786,9 +1784,9 @@ func withLastAppliedSpaceRoles(nsTmplSet *toolchainv1alpha1.NSTemplateSet) objec
}
}

func withFinalizer(finalizer string) objectMetaOption {
func withFinalizer() objectMetaOption {
return func(meta metav1.ObjectMeta, tier, typeName string) metav1.ObjectMeta {
meta.Finalizers = append(meta.Finalizers, finalizer)
meta.Finalizers = append(meta.Finalizers, toolchainv1alpha1.FinalizerName)
return meta
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ require (

replace (
github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9 => github.com/ranakan19/api v0.0.0-20240229043418-fb48d723557f
github.com/codeready-toolchain/toolchain-common v0.0.0-20240403070054-183e7d407080 => github.com/ranakan19/toolchain-common v0.0.0-20240320153453-bfa0fdfef982
github.com/codeready-toolchain/toolchain-common v0.0.0-20240404090512-046d250d7d78 => github.com/ranakan19/toolchain-common v0.0.0-20240405195257-0986552a3444
)

go 1.20
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9 h1:Lm7bFLrzfJzrUiRGVqtsSaZMpj+akLiR/fvAFjjE9gM=
github.com/codeready-toolchain/api v0.0.0-20240322110702-5ab3840476e9/go.mod h1:cfNN6YPX4TORvhhZXMSjSPesqAHlB3nD/WAfGe4WLKQ=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240404090512-046d250d7d78 h1:4jFHu6xN/T+hml9egbiZyJmptrMO0azWfF94liICmrM=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240404090512-046d250d7d78/go.mod h1:OJ3L9aaTRMGjxr2WeH/9l6m5OjExwEK3Bp/+P+efoGg=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -511,8 +507,8 @@ github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/ranakan19/api v0.0.0-20240229043418-fb48d723557f h1:bqPhEnvaZDlH12o0CFODM18l2/1vr351YXoN2e+NkLE=
github.com/ranakan19/api v0.0.0-20240229043418-fb48d723557f/go.mod h1:InMOgwgGYp6RYMhdEYBvB6gR9hIvdLAXmdnqF3lSFpg=
github.com/ranakan19/toolchain-common v0.0.0-20240320153453-bfa0fdfef982 h1:ItZ3MYmmgj1D3ZfluaaOj7KMwBAERXdNOzK1lHIEhqc=
github.com/ranakan19/toolchain-common v0.0.0-20240320153453-bfa0fdfef982/go.mod h1:KcgADoBgrF6b+r5ta4yL4osTC+wUmPkAbhGPxHc8iW0=
github.com/ranakan19/toolchain-common v0.0.0-20240405195257-0986552a3444 h1:1Bsl8kkjthsMi4wLNpyGBDwQu6EPAGd1ktvZ4gWNaPU=
github.com/ranakan19/toolchain-common v0.0.0-20240405195257-0986552a3444/go.mod h1:3d6K8hEUooGh/Lp10SGOAwZneO5JZfRZUzgc+S4dltw=
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf h1:fsZiv9XuFo8G7IyzFWjG02vqzJG7kSqFvD1Wiq3V/o8=
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf/go.mod h1:FfTyeSCu+e2VLgeMh/1RFG8TSkVjKRPEyR6EmDt0RIw=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down

0 comments on commit b1816ef

Please sign in to comment.