Skip to content

Commit a83fcb9

Browse files
authored
Fix group migration when runconfig is corrupt (#1506)
1 parent dc2e3e3 commit a83fcb9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/migration/default_group.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stacklok/toolhive/pkg/config"
88
"github.com/stacklok/toolhive/pkg/groups"
99
"github.com/stacklok/toolhive/pkg/logger"
10+
"github.com/stacklok/toolhive/pkg/runner"
1011
"github.com/stacklok/toolhive/pkg/workloads"
1112
)
1213

@@ -95,6 +96,21 @@ func (m *DefaultGroupMigrator) migrateWorkloadsToDefaultGroup(ctx context.Contex
9596

9697
migratedCount := 0
9798
for _, workloadName := range workloadsWithoutGroup {
99+
// Check the runconfig to validate if the workload is not in a group already
100+
// ListWorkloadsInGroup doesn't check the runconfig, so we need an additional check here
101+
runConfig, err := runner.LoadState(ctx, workloadName)
102+
if err != nil {
103+
logger.Warnf("Failed to migrate workload %s to default group due to missing or corrupt"+
104+
" run configuration: %v. The workload may be unhealthy and need to be deleted.", workloadName, err)
105+
continue
106+
}
107+
108+
// Check if the workload is actually in the specified group
109+
if runConfig.Group != "" {
110+
logger.Debugf("Workload %s is already in group %s, skipping migration", workloadName, runConfig.Group)
111+
continue
112+
}
113+
98114
// Move workload to default group
99115
if err := m.workloadsManager.MoveToGroup(ctx, []string{workloadName}, "", groups.DefaultGroup); err != nil {
100116
logger.Warnf("Failed to migrate workload %s to default group: %v", workloadName, err)

pkg/workloads/statuses/file_status.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ func (f *fileStatusManager) ListWorkloads(ctx context.Context, listAll bool, lab
127127
return nil, fmt.Errorf("failed to get workloads from files: %w", err)
128128
}
129129

130+
// TODO: Fetch the runconfig if present to populate additional fields like package, tool type, group etc.
131+
// There's currently an import cycle between this package and the runconfig package
132+
130133
// Create a map of runtime workloads by name for easy lookup
131134
workloadMap := f.mergeRuntimeAndFileWorkloads(ctx, runtimeContainers, fileWorkloads)
132135

0 commit comments

Comments
 (0)