Skip to content

Commit 765e3da

Browse files
authored
Continuously check if group migration is needed (#1485)
1 parent 418e341 commit 765e3da

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

pkg/migration/default_group.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,25 @@ func (m *DefaultGroupMigrator) Migrate(ctx context.Context) error {
2222
return fmt.Errorf("failed to initialize managers: %w", err)
2323
}
2424

25-
// Create default group
26-
if err := m.createDefaultGroup(ctx); err != nil {
27-
return fmt.Errorf("failed to create default group: %w", err)
25+
// Create default group if it doesn't exist
26+
defaultGroupExists, err := m.groupManager.Exists(ctx, groups.DefaultGroupName)
27+
if err != nil {
28+
return fmt.Errorf("failed to check if default group exists: %w", err)
29+
}
30+
if !defaultGroupExists {
31+
if err := m.createDefaultGroup(ctx); err != nil {
32+
return fmt.Errorf("failed to create default group: %w", err)
33+
}
2834
}
2935

30-
// Migrate workloads to default group
36+
// Migrate workloads to default group if they don't have a group assigned
3137
migratedCount, err := m.migrateWorkloadsToDefaultGroup(ctx)
3238
if err != nil {
3339
return fmt.Errorf("failed to migrate workloads: %w", err)
3440
}
3541

3642
if migratedCount > 0 {
3743
fmt.Printf("\nSuccessfully migrated %d workloads to default group '%s'\n", migratedCount, groups.DefaultGroupName)
38-
} else {
39-
fmt.Println("No workloads needed migration to default group")
4044
}
4145

4246
// Migrate client configurations from global config to default group
@@ -108,12 +112,10 @@ func (m *DefaultGroupMigrator) migrateClientConfigs(ctx context.Context) error {
108112

109113
// If there are no registered clients, nothing to migrate
110114
if len(appConfig.Clients.RegisteredClients) == 0 {
111-
logger.Infof("No client configurations to migrate")
115+
logger.Debugf("No client configurations to migrate")
112116
return nil
113117
}
114118

115-
fmt.Printf("Migrating %d client configurations to default group...\n", len(appConfig.Clients.RegisteredClients))
116-
117119
// Get the default group
118120
defaultGroup, err := m.groupManager.Get(ctx, groups.DefaultGroupName)
119121
if err != nil {
@@ -151,10 +153,10 @@ func (m *DefaultGroupMigrator) migrateClientConfigs(ctx context.Context) error {
151153
if err != nil {
152154
logger.Warnf("Failed to clear global client configurations after migration: %v", err)
153155
} else {
154-
logger.Infof("Cleared global client configurations")
156+
logger.Debugf("Cleared global client configurations")
155157
}
156158
} else {
157-
logger.Infof("No client configurations needed migration")
159+
logger.Debugf("No client configurations needed migration")
158160
}
159161

160162
return nil

pkg/migration/migration.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package migration
33

44
import (
55
"context"
6-
"fmt"
76
"sync"
87

9-
"github.com/stacklok/toolhive/pkg/config"
108
"github.com/stacklok/toolhive/pkg/logger"
119
)
1210

@@ -17,13 +15,6 @@ var migrationOnce sync.Once
1715
// This is called once at application startup
1816
func CheckAndPerformDefaultGroupMigration() {
1917
migrationOnce.Do(func() {
20-
appConfig := config.GetConfig()
21-
22-
// Check if default group migration has already been performed
23-
if appConfig.DefaultGroupMigration {
24-
return
25-
}
26-
2718
if err := performDefaultGroupMigration(); err != nil {
2819
logger.Errorf("Failed to perform default group migration: %v", err)
2920
return
@@ -33,9 +24,6 @@ func CheckAndPerformDefaultGroupMigration() {
3324

3425
// performDefaultGroupMigration migrates all existing workloads to the default group
3526
func performDefaultGroupMigration() error {
36-
fmt.Println("Migrating existing workloads to default group...")
37-
fmt.Println()
38-
3927
migrator := &DefaultGroupMigrator{}
4028
return migrator.Migrate(context.Background())
4129
}

0 commit comments

Comments
 (0)