Skip to content

Commit fe818da

Browse files
authored
feat(registry): support image deletion (#29)
Because - We are going to support image deletion This commit - implement `DeleteTag` and `GetTagDigest` for registry client - implement private `DeleteRepositoryTag` - refactor `ListRepositoryTags` endpoint to create missing tag record on the fly to avoid empty `digest` field
1 parent 420f969 commit fe818da

File tree

11 files changed

+1374
-18
lines changed

11 files changed

+1374
-18
lines changed

cmd/main/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ func main() {
182182
// Initialize Minio client
183183
minioClient, _ := minio.NewMinioClientAndInitBucket()
184184

185-
186185
service := service.NewService(repository, minioClient, mgmtPrivateServiceClient, httpclient.NewRegistryClient(ctx))
187186

188187
publicGrpcS := grpc.NewServer(grpcServerOpts...)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
1616
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
1717
github.com/influxdata/influxdb-client-go/v2 v2.12.3
18-
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d
18+
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1
1919
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61
2020
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c
2121
github.com/knadh/koanf v1.5.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0
286286
github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0=
287287
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
288288
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
289-
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d h1:gJqn9yDo+1vyL6I9In667Y8LM0iVRg0ExH7Kd97OZvw=
290-
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ=
289+
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1 h1:Vxg7WEXNqzI9sj13SdDQP3wQC/6rRJ8mW1KnAnYiaIQ=
290+
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ=
291291
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61 h1:smPTvmXDhn/QC7y/TPXyMTqbbRd0gvzmFgWBChwTfhE=
292292
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61/go.mod h1:/TAHs4ybuylk5icuy+MQtHRc4XUnIyXzeNKxX9qDFhw=
293293
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c h1:a2RVkpIV2QcrGnSHAou+t/L+vBsaIfFvk5inVg5Uh4s=

pkg/client/http/registry.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,30 @@ func (c *RegistryClient) ListTags(ctx context.Context, repository string) ([]str
5757

5858
return resp.Tags, nil
5959
}
60+
61+
// DeleteTag calls the DELETE /v2/<name>/manifests/<reference> endpoint, where <name> is a
62+
// repository, and <reference> is the digest
63+
func (c *RegistryClient) DeleteTag(ctx context.Context, repository string, digest string) error {
64+
65+
deletePath := fmt.Sprintf("/v2/%s/manifests/%s", repository, digest)
66+
r := c.R().SetContext(ctx)
67+
if _, err := r.Delete(deletePath); err != nil {
68+
return fmt.Errorf("couldn't delete the image with registry: %w", err)
69+
}
70+
71+
return nil
72+
}
73+
74+
// GetTagDigest calls the HEAD /v2/<name>/manifests/<reference> endpoint, where <name> is a
75+
// repository, and <reference> is the tag
76+
func (c *RegistryClient) GetTagDigest(ctx context.Context, repository string, tag string) (string, error) {
77+
78+
digestPath := fmt.Sprintf("/v2/%s/manifests/%s", repository, tag)
79+
r := c.R().SetContext(ctx).SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json")
80+
resp, err := r.Head(digestPath)
81+
if err != nil {
82+
return "", fmt.Errorf("couldn't get the image digest: %w", err)
83+
}
84+
85+
return resp.Header().Get("Docker-Content-Digest"), nil
86+
}

pkg/handler/private.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,20 @@ func (h *PrivateHandler) CreateRepositoryTag(ctx context.Context, req *pb.Create
6262
logger.Info("CreateRepositoryTag")
6363
return resp, nil
6464
}
65+
66+
// DeleteRepositoryTag deletes the information of a repository tag in registry.
67+
func (h *PrivateHandler) DeleteRepositoryTag(ctx context.Context, req *pb.DeleteRepositoryTagRequest) (*pb.DeleteRepositoryTagResponse, error) {
68+
ctx, span := tracer.Start(ctx, "DeleteRepositoryTag", trace.WithSpanKind(trace.SpanKindServer))
69+
defer span.End()
70+
71+
logger, _ := logger.GetZapLogger(ctx)
72+
73+
resp, err := h.service.DeleteRepositoryTag(ctx, req)
74+
if err != nil {
75+
span.SetStatus(1, err.Error())
76+
return nil, err
77+
}
78+
79+
logger.Info("DeleteRepositoryTag")
80+
return resp, nil
81+
}

0 commit comments

Comments
 (0)