@@ -6,12 +6,15 @@ import (
66 "encoding/json"
77 "fmt"
88 "io"
9+ "slices"
910 "strings"
1011
12+ "github.com/distribution/reference"
1113 "github.com/docker/docker/api/types/mount"
1214 "github.com/pkg/errors"
1315 "github.com/ptah-sh/ptah-agent/internal/app/ptah-agent/busybox"
1416 "github.com/ptah-sh/ptah-agent/internal/app/ptah-agent/docker/config"
17+ "github.com/ptah-sh/ptah-agent/internal/app/ptah-agent/registry"
1518 t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
1619)
1720
@@ -145,3 +148,78 @@ func (e *taskExecutor) pullImage(ctx context.Context, req *t.PullImageReq) (*t.P
145148
146149 return & t.PullImageRes {Output : output }, nil
147150}
151+
152+ func (e * taskExecutor ) pruneDockerRegistry (ctx context.Context , req * t.PruneDockerRegistryReq ) (* t.PruneDockerRegistryRes , error ) {
153+ log := Logger (ctx )
154+
155+ var result t.PruneDockerRegistryRes
156+
157+ tagsToKeep := make (map [string ][]string )
158+ for _ , imageRef := range req .KeepImages {
159+ ref , err := reference .ParseNamed (imageRef )
160+ if err != nil {
161+ return nil , fmt .Errorf ("prune docker registry: %w" , err )
162+ }
163+
164+ repo := reference .Path (ref )
165+
166+ repoTags , ok := tagsToKeep [repo ]
167+ if ! ok {
168+ repoTags = make ([]string , 0 )
169+ }
170+
171+ taggedRef , ok := reference .TagNameOnly (ref ).(reference.Tagged )
172+ if ! ok {
173+ return nil , fmt .Errorf ("prune docker registry: can not get tag from ref: %s" , imageRef )
174+ }
175+
176+ tagsToKeep [repo ] = append (repoTags , taggedRef .Tag ())
177+
178+ log .Debug ("parsed image ref" , "ref" , ref , "repo" , repo , "tag" , taggedRef .Tag ())
179+ }
180+
181+ registry := registry .New ("http://registry.ptah.local:5050" )
182+
183+ catalog , err := registry .Catalog (ctx )
184+ if err != nil {
185+ return nil , fmt .Errorf ("prune docker registry: %w" , err )
186+ }
187+
188+ for _ , repo := range catalog .Repositories {
189+ log .Debug ("processing repo" , "repo" , repo )
190+
191+ tags , err := registry .TagsList (ctx , repo )
192+ if err != nil {
193+ return nil , fmt .Errorf ("prune docker registry: %w" , err )
194+ }
195+
196+ for _ , tag := range tags .Tags {
197+ log .Debug ("processing tag" , "tag" , tag )
198+
199+ if _ , ok := tagsToKeep [repo ]; ! ok || ! slices .Contains (tagsToKeep [repo ], tag ) {
200+ manifest , err := registry .ManifestHead (ctx , repo , tag )
201+ if err != nil {
202+ return nil , fmt .Errorf ("prune docker registry: %w" , err )
203+ }
204+
205+ log .Debug ("deleting image" , "repo" , repo , "tag" , tag , "digest" , manifest .Digest )
206+
207+ if err := registry .ManifestDelete (ctx , repo , manifest .Digest ); err != nil {
208+ return nil , fmt .Errorf ("prune docker registry: %w" , err )
209+ }
210+ }
211+ }
212+ }
213+
214+ // ref, err := reference.ParseNamed("ptah/test")
215+ // if err != nil {
216+ // return nil, fmt.Errorf("prune docker registry: %w", err)
217+ // }
218+
219+ // info, err := dockerRegistry.ParseRepositoryInfo(ref)
220+ // if err != nil {
221+ // return nil, fmt.Errorf("prune docker registry: %w", err)
222+ // }
223+
224+ return & result , nil
225+ }
0 commit comments