Skip to content

Commit 22768c9

Browse files
committed
move tenant to users pkg
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent 1d19ef7 commit 22768c9

File tree

85 files changed

+368
-396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+368
-396
lines changed

pkg/alertmanager/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/cortexproject/cortex/pkg/util"
2222
"github.com/cortexproject/cortex/pkg/util/concurrency"
2323
util_log "github.com/cortexproject/cortex/pkg/util/log"
24-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
24+
"github.com/cortexproject/cortex/pkg/util/users"
2525
)
2626

2727
const (
@@ -67,7 +67,7 @@ type UserConfig struct {
6767
func (am *MultitenantAlertmanager) GetUserConfig(w http.ResponseWriter, r *http.Request) {
6868
logger := util_log.WithContext(r.Context(), am.logger)
6969

70-
userID, err := tenant.TenantID(r.Context())
70+
userID, err := users.TenantID(r.Context())
7171
if err != nil {
7272
level.Error(logger).Log("msg", errNoOrgID, "err", err.Error())
7373
http.Error(w, fmt.Sprintf("%s: %s", errNoOrgID, err.Error()), http.StatusUnauthorized)
@@ -107,7 +107,7 @@ func (am *MultitenantAlertmanager) GetUserConfig(w http.ResponseWriter, r *http.
107107

108108
func (am *MultitenantAlertmanager) SetUserConfig(w http.ResponseWriter, r *http.Request) {
109109
logger := util_log.WithContext(r.Context(), am.logger)
110-
userID, err := tenant.TenantID(r.Context())
110+
userID, err := users.TenantID(r.Context())
111111
if err != nil {
112112
level.Error(logger).Log("msg", errNoOrgID, "err", err.Error())
113113
http.Error(w, fmt.Sprintf("%s: %s", errNoOrgID, err.Error()), http.StatusUnauthorized)
@@ -167,7 +167,7 @@ func (am *MultitenantAlertmanager) SetUserConfig(w http.ResponseWriter, r *http.
167167
// Note that if no config exists for a user, StatusOK is returned.
168168
func (am *MultitenantAlertmanager) DeleteUserConfig(w http.ResponseWriter, r *http.Request) {
169169
logger := util_log.WithContext(r.Context(), am.logger)
170-
userID, err := tenant.TenantID(r.Context())
170+
userID, err := users.TenantID(r.Context())
171171
if err != nil {
172172
level.Error(logger).Log("msg", errNoOrgID, "err", err.Error())
173173
http.Error(w, fmt.Sprintf("%s: %s", errNoOrgID, err.Error()), http.StatusUnauthorized)

pkg/alertmanager/distributor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/cortexproject/cortex/pkg/util"
2525
util_log "github.com/cortexproject/cortex/pkg/util/log"
2626
"github.com/cortexproject/cortex/pkg/util/services"
27-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
27+
"github.com/cortexproject/cortex/pkg/util/users"
2828
)
2929

3030
// Distributor forwards requests to individual alertmanagers.
@@ -97,11 +97,11 @@ func (d *Distributor) isQuorumReadPath(p string) (bool, merger.Merger) {
9797
// In case of reads, it proxies the request to one of the alertmanagers.
9898
// DistributeRequest assumes that the caller has verified IsPathSupported returns
9999
// true for the route.
100-
func (d *Distributor) DistributeRequest(w http.ResponseWriter, r *http.Request, allowedTenants *tenant.AllowedTenants) {
100+
func (d *Distributor) DistributeRequest(w http.ResponseWriter, r *http.Request, allowedTenants *users.AllowedTenants) {
101101
d.requestsInFlight.Add(1)
102102
defer d.requestsInFlight.Done()
103103

104-
userID, err := tenant.TenantID(r.Context())
104+
userID, err := users.TenantID(r.Context())
105105
if err != nil {
106106
http.Error(w, err.Error(), http.StatusUnauthorized)
107107
return

pkg/alertmanager/distributor_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
util_log "github.com/cortexproject/cortex/pkg/util/log"
3232
"github.com/cortexproject/cortex/pkg/util/services"
3333
"github.com/cortexproject/cortex/pkg/util/test"
34-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
34+
"github.com/cortexproject/cortex/pkg/util/users"
3535
)
3636

3737
func TestDistributor_DistributeRequest(t *testing.T) {
@@ -262,9 +262,9 @@ func TestDistributor_DistributeRequest(t *testing.T) {
262262
req.Method = http.MethodDelete
263263
}
264264
req.RequestURI = url
265-
var allowedTenants *tenant.AllowedTenants
265+
var allowedTenants *users.AllowedTenants
266266
if c.isTenantDisabled {
267-
allowedTenants = tenant.NewAllowedTenants(nil, []string{"1"})
267+
allowedTenants = users.NewAllowedTenants(nil, []string{"1"})
268268
}
269269

270270
w := httptest.NewRecorder()

pkg/alertmanager/multitenant.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/cortexproject/cortex/pkg/util/flagext"
3939
util_log "github.com/cortexproject/cortex/pkg/util/log"
4040
"github.com/cortexproject/cortex/pkg/util/services"
41-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
41+
"github.com/cortexproject/cortex/pkg/util/users"
4242
)
4343

4444
const (
@@ -284,7 +284,7 @@ type MultitenantAlertmanager struct {
284284

285285
limits Limits
286286

287-
allowedTenants *tenant.AllowedTenants
287+
allowedTenants *users.AllowedTenants
288288

289289
registry prometheus.Registerer
290290
ringCheckErrors prometheus.Counter
@@ -377,7 +377,7 @@ func createMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, fallbackC
377377
logger: log.With(logger, "component", "MultiTenantAlertmanager"),
378378
registry: registerer,
379379
limits: limits,
380-
allowedTenants: tenant.NewAllowedTenants(cfg.EnabledTenants, cfg.DisabledTenants),
380+
allowedTenants: users.NewAllowedTenants(cfg.EnabledTenants, cfg.DisabledTenants),
381381
ringCheckErrors: promauto.With(registerer).NewCounter(prometheus.CounterOpts{
382382
Name: "cortex_alertmanager_ring_check_errors_total",
383383
Help: "Number of errors that have occurred when checking the ring for ownership.",
@@ -1048,7 +1048,7 @@ func (am *MultitenantAlertmanager) HandleRequest(ctx context.Context, in *httpgr
10481048

10491049
// serveRequest serves the Alertmanager's web UI and API.
10501050
func (am *MultitenantAlertmanager) serveRequest(w http.ResponseWriter, req *http.Request) {
1051-
userID, err := tenant.TenantID(req.Context())
1051+
userID, err := users.TenantID(req.Context())
10521052
if err != nil {
10531053
http.Error(w, err.Error(), http.StatusUnauthorized)
10541054
return
@@ -1197,7 +1197,7 @@ func (am *MultitenantAlertmanager) ReadFullStateForUser(ctx context.Context, use
11971197

11981198
// UpdateState implements the Alertmanager service.
11991199
func (am *MultitenantAlertmanager) UpdateState(ctx context.Context, part *clusterpb.Part) (*alertmanagerpb.UpdateStateResponse, error) {
1200-
userID, err := tenant.TenantID(ctx)
1200+
userID, err := users.TenantID(ctx)
12011201
if err != nil {
12021202
return nil, err
12031203
}
@@ -1307,7 +1307,7 @@ func (am *MultitenantAlertmanager) getPerUserDirectories() map[string]string {
13071307

13081308
// UpdateState implements the Alertmanager service.
13091309
func (am *MultitenantAlertmanager) ReadState(ctx context.Context, req *alertmanagerpb.ReadStateRequest) (*alertmanagerpb.ReadStateResponse, error) {
1310-
userID, err := tenant.TenantID(ctx)
1310+
userID, err := users.TenantID(ctx)
13111311
if err != nil {
13121312
return nil, err
13131313
}

pkg/compactor/blocks_cleaner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
util_log "github.com/cortexproject/cortex/pkg/util/log"
2727
"github.com/cortexproject/cortex/pkg/util/services"
2828
"github.com/cortexproject/cortex/pkg/util/users"
29-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
3029
)
3130

3231
const (
@@ -536,7 +535,7 @@ func (c *BlocksCleaner) deleteUserMarkedForDeletion(ctx context.Context, userLog
536535
}
537536
level.Info(userLogger).Log("msg", "completed deleting blocks for tenant marked for deletion", "duration", time.Since(begin), "duration_ms", time.Since(begin).Milliseconds())
538537

539-
mark, err := tenant.ReadTenantDeletionMark(ctx, c.bucketClient, userID, userLogger)
538+
mark, err := users.ReadTenantDeletionMark(ctx, c.bucketClient, userID, userLogger)
540539
if err != nil {
541540
return errors.Wrap(err, "failed to read tenant deletion mark")
542541
}
@@ -549,7 +548,7 @@ func (c *BlocksCleaner) deleteUserMarkedForDeletion(ctx context.Context, userLog
549548
if deletedBlocks.Load() > 0 || mark.FinishedTime == 0 {
550549
level.Info(userLogger).Log("msg", "updating finished time in tenant deletion mark")
551550
mark.FinishedTime = time.Now().Unix()
552-
return errors.Wrap(tenant.WriteTenantDeletionMark(ctx, c.bucketClient, userID, mark), "failed to update tenant deletion mark")
551+
return errors.Wrap(users.WriteTenantDeletionMark(ctx, c.bucketClient, userID, mark), "failed to update tenant deletion mark")
553552
}
554553
if time.Since(time.Unix(mark.FinishedTime, 0)) < c.cfg.TenantCleanupDelay {
555554
return nil
@@ -567,7 +566,7 @@ func (c *BlocksCleaner) deleteUserMarkedForDeletion(ctx context.Context, userLog
567566
} else if deleted > 0 {
568567
level.Info(userLogger).Log("msg", "deleted marker files for tenant marked for deletion", "count", deleted, "duration", time.Since(begin), "duration_ms", time.Since(begin).Milliseconds())
569568
}
570-
if err := tenant.DeleteTenantDeletionMark(ctx, c.bucketClient, userID); err != nil {
569+
if err := users.DeleteTenantDeletionMark(ctx, c.bucketClient, userID); err != nil {
571570
return errors.Wrap(err, "failed to delete tenant deletion mark")
572571
}
573572
return nil

pkg/compactor/blocks_cleaner_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/cortexproject/cortex/pkg/util/services"
2929
cortex_testutil "github.com/cortexproject/cortex/pkg/util/testutil"
3030
"github.com/cortexproject/cortex/pkg/util/users"
31-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
3231
)
3332

3433
type testBlocksCleanerOptions struct {
@@ -165,15 +164,15 @@ func testBlocksCleanerWithOptions(t *testing.T, options testBlocksCleanerOptions
165164
createDeletionMark(t, bucketClient, "user-2", block7, now.Add(-deletionDelay).Add(-time.Hour)) // Block reached the deletion threshold.
166165

167166
// Blocks for user-3, tenant marked for deletion.
168-
require.NoError(t, tenant.WriteTenantDeletionMark(context.Background(), bucketClient, "user-3", tenant.NewTenantDeletionMark(time.Now())))
167+
require.NoError(t, users.WriteTenantDeletionMark(context.Background(), bucketClient, "user-3", users.NewTenantDeletionMark(time.Now())))
169168
block9 := createTSDBBlock(t, bucketClient, "user-3", 10, 30, nil)
170169
block10 := createTSDBBlock(t, bucketClient, "user-3", 30, 50, nil)
171170
createParquetMarker(t, bucketClient, "user-3", block10)
172171

173172
// User-4 with no more blocks, but couple of mark and debug files. Should be fully deleted.
174-
user4Mark := tenant.NewTenantDeletionMark(time.Now())
173+
user4Mark := users.NewTenantDeletionMark(time.Now())
175174
user4Mark.FinishedTime = time.Now().Unix() - 60 // Set to check final user cleanup.
176-
require.NoError(t, tenant.WriteTenantDeletionMark(context.Background(), bucketClient, "user-4", user4Mark))
175+
require.NoError(t, users.WriteTenantDeletionMark(context.Background(), bucketClient, "user-4", user4Mark))
177176
user4DebugMetaFile := path.Join("user-4", block.DebugMetas, "meta.json")
178177
require.NoError(t, bucketClient.Upload(context.Background(), user4DebugMetaFile, strings.NewReader("some random content here")))
179178

@@ -271,7 +270,7 @@ func testBlocksCleanerWithOptions(t *testing.T, options testBlocksCleanerOptions
271270
{"user-3", true},
272271
{"user-4", options.user4FilesExist},
273272
} {
274-
exists, err := tenant.TenantDeletionMarkExists(ctx, bucketClient, tc.user)
273+
exists, err := users.TenantDeletionMarkExists(ctx, bucketClient, tc.user)
275274
require.NoError(t, err)
276275
assert.Equal(t, tc.expectedExists, exists, tc.user)
277276
}

pkg/compactor/compactor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math/rand"
1010
"os"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314
"time"
1415

@@ -39,7 +40,6 @@ import (
3940
util_log "github.com/cortexproject/cortex/pkg/util/log"
4041
"github.com/cortexproject/cortex/pkg/util/services"
4142
"github.com/cortexproject/cortex/pkg/util/users"
42-
"github.com/cortexproject/cortex/pkg/util/users/tenant"
4343
"github.com/cortexproject/cortex/pkg/util/validation"
4444
)
4545

@@ -367,7 +367,7 @@ func (cfg *Config) Validate(limits validation.Limits) error {
367367
}
368368

369369
// Make sure a valid sharding strategy is being used
370-
if !util.StringsContain(supportedShardingStrategies, cfg.ShardingStrategy) {
370+
if !slices.Contains(supportedShardingStrategies, cfg.ShardingStrategy) {
371371
return errInvalidShardingStrategy
372372
}
373373

@@ -378,7 +378,7 @@ func (cfg *Config) Validate(limits validation.Limits) error {
378378
}
379379

380380
// Make sure a valid compaction strategy is being used
381-
if !util.StringsContain(supportedCompactionStrategies, cfg.CompactionStrategy) {
381+
if !slices.Contains(supportedCompactionStrategies, cfg.CompactionStrategy) {
382382
return errInvalidCompactionStrategy
383383
}
384384

@@ -405,7 +405,7 @@ type Compactor struct {
405405
logger log.Logger
406406
parentLogger log.Logger
407407
registerer prometheus.Registerer
408-
allowedTenants *tenant.AllowedTenants
408+
allowedTenants *users.AllowedTenants
409409
limits *validation.Overrides
410410

411411
// Functions that creates bucket client, grouper, planner and compactor using the context.
@@ -543,7 +543,7 @@ func newCompactor(
543543
blocksCompactorFactory: blocksCompactorFactory,
544544
blockDeletableCheckerFactory: blockDeletableCheckerFactory,
545545
compactionLifecycleCallbackFactory: compactionLifecycleCallbackFactory,
546-
allowedTenants: tenant.NewAllowedTenants(compactorCfg.EnabledTenants, compactorCfg.DisabledTenants),
546+
allowedTenants: users.NewAllowedTenants(compactorCfg.EnabledTenants, compactorCfg.DisabledTenants),
547547

548548
CompactorStartDurationSeconds: promauto.With(registerer).NewGauge(prometheus.GaugeOpts{
549549
Name: "cortex_compactor_start_duration_seconds",
@@ -896,7 +896,7 @@ func (c *Compactor) compactUsers(ctx context.Context) {
896896

897897
ownedUsers[userID] = struct{}{}
898898

899-
if markedForDeletion, err := tenant.TenantDeletionMarkExists(ctx, c.bucketClient, userID); err != nil {
899+
if markedForDeletion, err := users.TenantDeletionMarkExists(ctx, c.bucketClient, userID); err != nil {
900900
c.CompactionRunSkippedTenants.Inc()
901901
level.Warn(c.logger).Log("msg", "unable to check if user is marked for deletion", "user", userID, "err", err)
902902
continue

0 commit comments

Comments
 (0)