Skip to content

Commit

Permalink
Improve error message when deleting resources (#618)
Browse files Browse the repository at this point in the history
- Log the resource
- Log the events associated with such resource

Signed-off-by: Pierangelo Di Pilato <[email protected]>
Co-authored-by: Pierangelo Di Pilato <[email protected]>
  • Loading branch information
knative-prow-robot and pierDipi authored Oct 23, 2023
1 parent 7d36fe9 commit 785e0bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func DeleteResources(ctx context.Context, t T, refs []corev1.ObjectReference) er
}
}

var lastResource corev1.ObjectReference // One still present resource

err := wait.Poll(time.Second, 4*time.Minute, func() (bool, error) {
for _, ref := range refs {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
Expand All @@ -248,13 +250,15 @@ func DeleteResources(ctx context.Context, t T, refs []corev1.ObjectReference) er
return false, fmt.Errorf("failed to get resource %+v %s/%s: %w", resource, ref.Namespace, ref.Name, err)
}

lastResource = ref
t.Logf("Resource %+v %s/%s still present", resource, ref.Namespace, ref.Name)
return false, nil
}

return true, nil
})
if err != nil {
LogReferences(lastResource)(ctx, t)
return fmt.Errorf("failed to wait for resources to be deleted: %v", err)
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/feature/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/injection/clients/dynamicclient"
)

Expand Down Expand Up @@ -62,13 +63,26 @@ func logReference(ref corev1.ObjectReference) StepFn {
return
}

b, err := json.MarshalIndent(r, "", " ")
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
t.Logf("Failed to marshal %s: %v\n", resourceStr, err)
return
}

t.Logf("%s\n%s", resourceStr, string(b))
// Get events for the given resource
events, _ := kubeclient.Get(ctx).EventsV1().
Events(ref.Namespace).
List(ctx, metav1.ListOptions{
TypeMeta: metav1.TypeMeta{
Kind: ref.Kind,
APIVersion: ref.APIVersion,
},
FieldSelector: fmt.Sprintf("involvedObject.name=%s", ref.Name),
Limit: 50,
})
eBytes, _ := json.MarshalIndent(events, "", " ")

t.Logf("%s\n%s\nEvents:\n%s\n", resourceStr, string(b), string(eBytes))

// Recursively log owners
for _, or := range r.GetOwnerReferences() {
Expand Down

0 comments on commit 785e0bd

Please sign in to comment.