fix ScopeDefinition not found blocking finalizer

Signed-off-by: roy wang <seiwy2010@gmail.com>
This commit is contained in:
roy wang
2021-02-10 15:44:15 +09:00
parent 9d47a85f80
commit 062df46e47
2 changed files with 63 additions and 0 deletions

View File

@@ -378,6 +378,8 @@ func (a *workloads) applyScope(ctx context.Context, wl Workload, s unstructured.
return nil
}
// applyScopeRemoval remove the workload reference from the scope's reference list.
// If the scope or scope definition is not found(deleted), it's still regarded as remove successfully.
func (a *workloads) applyScopeRemoval(ctx context.Context, namespace string, wr runtimev1alpha1.TypedReference, s v1alpha2.WorkloadScope) error {
scopeObject := unstructured.Unstructured{}
scopeObject.SetAPIVersion(s.Reference.APIVersion)
@@ -394,6 +396,11 @@ func (a *workloads) applyScopeRemoval(ctx context.Context, namespace string, wr
scopeDefinition, err := util.FetchScopeDefinition(ctx, a.rawClient, a.dm, &scopeObject)
if err != nil {
if apierrors.IsNotFound(err) {
// if the scope definition is deleted
// treat it as removal done to avoid blocking AppConfig finalizer
return nil
}
return errors.Wrapf(err, errFmtGetScopeDefinition, scopeObject.GetAPIVersion(), scopeObject.GetKind(), scopeObject.GetName())
}

View File

@@ -27,9 +27,11 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -325,6 +327,60 @@ func TestApplyWorkloads(t *testing.T) {
},
},
},
"SuccessRemovingWhenScopeDefinitionNotFound": {
reason: "ScopeDefinition not found should not block dereference",
applicator: ApplyFn(func(_ context.Context, o runtime.Object, _ ...apply.ApplyOption) error { return nil }),
rawClient: &test.MockClient{
MockGet: func(_ context.Context, key client.ObjectKey, obj runtime.Object) error {
if key.Name == scope.GetName() {
scope := obj.(*unstructured.Unstructured)
refs := []interface{}{
map[string]interface{}{
"apiVersion": workload.GetAPIVersion(),
"kind": workload.GetKind(),
"name": workload.GetName(),
},
}
if err := fieldpath.Pave(scope.UnstructuredContent()).SetValue("spec.workloadRefs", refs); err == nil {
return err
}
return nil
}
if _, ok := obj.(*v1alpha2.ScopeDefinition); ok {
return apierrors.NewNotFound(schema.GroupResource{}, "test")
}
return nil
},
},
args: args{
w: []Workload{{
Workload: workload,
Traits: []*Trait{{Object: *trait.DeepCopy()}},
Scopes: []unstructured.Unstructured{},
}},
ws: []v1alpha2.WorkloadStatus{
{
Reference: v1alpha1.TypedReference{
APIVersion: workload.GetAPIVersion(),
Kind: workload.GetKind(),
Name: workload.GetName(),
},
Scopes: []v1alpha2.WorkloadScope{
{
Reference: v1alpha1.TypedReference{
APIVersion: scope.GetAPIVersion(),
Kind: scope.GetKind(),
Name: scope.GetName(),
},
},
},
},
},
},
},
}
for name, tc := range cases {