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

Update getFinalOwner method to handle not found error #4713

Open
wants to merge 1 commit into
base: main
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
4 changes: 4 additions & 0 deletions pkg/podownercache/pod_owner_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (s *PodOwnerCache) getFinalOwner(obj metav1.Object) (*OwnerInfo, error) {
logger.Sugar().Debugf("forbidden to get owner of pod %s/%s", obj.GetNamespace(), obj.GetName())
return nil, nil
}
if errors.IsNotFound(err) {
logger.Sugar().Debugf("owner not found for pod %s/%s", obj.GetNamespace(), obj.GetName())
return nil, nil
}
return nil, err
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/podownercache/pod_owner_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,48 @@ func TestGetFinalOwnerForbidden(t *testing.T) {
t.Fatalf("expected ownerInfo to be nil, got %v", ownerInfo)
}
}

// MockNotFoundClient is a custom mock implementation of the client.Reader interface
type MockNotFoundClient struct{}

func (m *MockNotFoundClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return errors.NewNotFound(schema.GroupResource{Group: "apps", Resource: "replicasets"}, key.Name)
}

func (m *MockNotFoundClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return nil
}

// TestGetFinalOwnerNotFound tests the getFinalOwner method when the Get call returns a not found error.
func TestGetFinalOwnerNotFound(t *testing.T) {
logger = logutils.Logger.Named("PodOwnerCache")

mockClient := &MockNotFoundClient{}
cache := &PodOwnerCache{
ctx: context.Background(),
apiReader: mockClient,
}

// Create a mock pod object
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "test-ns",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Kind: "ReplicaSet",
Name: "test-rs",
},
},
},
}

ownerInfo, err := cache.getFinalOwner(pod)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if ownerInfo != nil {
t.Fatalf("expected ownerInfo to be nil, got %v", ownerInfo)
}
}
Loading