mirror of https://github.com/grafana/loki
avoid making delete requests if deletes are disabled for a user (#6583)
parent
1c8ebaff61
commit
a92e048ec3
@ -0,0 +1,33 @@ |
||||
package deletion |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/loki/pkg/storage/stores/shipper/compactor/retention" |
||||
) |
||||
|
||||
type perTenantDeleteRequestsClient struct { |
||||
client DeleteRequestsClient |
||||
limits retention.Limits |
||||
} |
||||
|
||||
func NewPerTenantDeleteRequestsClient(c DeleteRequestsClient, l retention.Limits) DeleteRequestsClient { |
||||
return &perTenantDeleteRequestsClient{ |
||||
client: c, |
||||
limits: l, |
||||
} |
||||
} |
||||
|
||||
func (c *perTenantDeleteRequestsClient) GetAllDeleteRequestsForUser(ctx context.Context, userID string) ([]DeleteRequest, error) { |
||||
allLimits := c.limits.AllByUserID() |
||||
userLimits, ok := allLimits[userID] |
||||
if ok && userLimits.CompactorDeletionEnabled || c.limits.DefaultLimits().CompactorDeletionEnabled { |
||||
return c.client.GetAllDeleteRequestsForUser(ctx, userID) |
||||
} |
||||
|
||||
return nil, nil |
||||
} |
||||
|
||||
func (c *perTenantDeleteRequestsClient) Stop() { |
||||
c.client.Stop() |
||||
} |
||||
@ -0,0 +1,62 @@ |
||||
package deletion |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func TestTenantDeleteRequestsClient(t *testing.T) { |
||||
fakeClient := &fakeRequestsClient{ |
||||
reqs: []DeleteRequest{{ |
||||
RequestID: "test-request", |
||||
}}, |
||||
} |
||||
perTenantClient := NewPerTenantDeleteRequestsClient(fakeClient, limits) |
||||
|
||||
t.Run("tenant enabled", func(t *testing.T) { |
||||
reqs, err := perTenantClient.GetAllDeleteRequestsForUser(context.Background(), "1") |
||||
require.Nil(t, err) |
||||
require.Equal(t, []DeleteRequest{{RequestID: "test-request"}}, reqs) |
||||
}) |
||||
|
||||
t.Run("tenant disabled", func(t *testing.T) { |
||||
reqs, err := perTenantClient.GetAllDeleteRequestsForUser(context.Background(), "2") |
||||
require.Nil(t, err) |
||||
require.Empty(t, reqs) |
||||
}) |
||||
|
||||
t.Run("default is enabled", func(t *testing.T) { |
||||
limits.defaultLimit.compactorDeletionEnabled = true |
||||
reqs, err := perTenantClient.GetAllDeleteRequestsForUser(context.Background(), "3") |
||||
require.Nil(t, err) |
||||
require.Equal(t, []DeleteRequest{{RequestID: "test-request"}}, reqs) |
||||
}) |
||||
|
||||
t.Run("default is disabled", func(t *testing.T) { |
||||
limits.defaultLimit.compactorDeletionEnabled = false |
||||
reqs, err := perTenantClient.GetAllDeleteRequestsForUser(context.Background(), "3") |
||||
require.Nil(t, err) |
||||
require.Empty(t, reqs) |
||||
}) |
||||
} |
||||
|
||||
type fakeRequestsClient struct { |
||||
DeleteRequestsClient |
||||
|
||||
reqs []DeleteRequest |
||||
} |
||||
|
||||
func (c *fakeRequestsClient) GetAllDeleteRequestsForUser(_ context.Context, userID string) ([]DeleteRequest, error) { |
||||
return c.reqs, nil |
||||
} |
||||
|
||||
var ( |
||||
limits = &fakeLimits{ |
||||
perTenant: map[string]retentionLimit{ |
||||
"1": {compactorDeletionEnabled: true}, |
||||
"2": {compactorDeletionEnabled: false}, |
||||
}, |
||||
} |
||||
) |
||||
Loading…
Reference in new issue