Skip to content

Commit 96cc0e8

Browse files
committed
fix
1 parent 4bb57a1 commit 96cc0e8

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

cmd/core/helpers.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func VMConfigFromFlags(cmd *cobra.Command, image string) (*types.VMConfig, error
133133
storStr, _ := cmd.Flags().GetString("storage")
134134

135135
if vmName == "" {
136-
vmName = fmt.Sprintf("cocoon-%s", image)
136+
vmName = sanitizeVMName(image)
137137
}
138138

139139
memBytes, err := units.RAMInBytes(memStr)
@@ -176,3 +176,16 @@ func FormatSize(bytes int64) string {
176176
func IsURL(ref string) bool {
177177
return strings.HasPrefix(ref, "http://") || strings.HasPrefix(ref, "https://")
178178
}
179+
180+
// sanitizeVMName derives a safe VM name from an image reference.
181+
// Strips registry prefix (everything before the last '/'), replaces ':' with '-',
182+
// and prepends "cocoon-".
183+
// e.g. "ghcr.io/foo/ubuntu:24.04" → "cocoon-ubuntu-24.04"
184+
func sanitizeVMName(image string) string {
185+
name := image
186+
if i := strings.LastIndex(name, "/"); i >= 0 {
187+
name = name[i+1:]
188+
}
189+
name = strings.ReplaceAll(name, ":", "-")
190+
return "cocoon-" + name
191+
}

hypervisor/cloudhypervisor/cloudhypervisor.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,20 @@ func (ch *CloudHypervisor) Delete(ctx context.Context, refs []string, force bool
100100
}
101101

102102
// resolveRefs batch-resolves refs to exact VM IDs under a single lock.
103+
// Duplicate refs that resolve to the same ID are silently deduplicated.
103104
func (ch *CloudHypervisor) resolveRefs(ctx context.Context, refs []string) ([]string, error) {
104105
var ids []string
105106
return ids, ch.store.With(ctx, func(idx *hypervisor.VMIndex) error {
107+
seen := make(map[string]struct{}, len(refs))
106108
for _, ref := range refs {
107109
id, err := hypervisor.ResolveVMRef(idx, ref)
108110
if err != nil {
109111
return fmt.Errorf("resolve %q: %w", ref, err)
110112
}
113+
if _, ok := seen[id]; ok {
114+
continue
115+
}
116+
seen[id] = struct{}{}
111117
ids = append(ids, id)
112118
}
113119
return nil

0 commit comments

Comments
 (0)