Skip to content

Commit

Permalink
feat(registry): support image deletion (#29)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
heiruwu authored Jun 25, 2024
1 parent 420f969 commit fe818da
Show file tree
Hide file tree
Showing 11 changed files with 1,374 additions and 18 deletions.
1 change: 0 additions & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func main() {
// Initialize Minio client
minioClient, _ := minio.NewMinioClientAndInitBucket()


service := service.NewService(repository, minioClient, mgmtPrivateServiceClient, httpclient.NewRegistryClient(ctx))

publicGrpcS := grpc.NewServer(grpcServerOpts...)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c
github.com/knadh/koanf v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0
github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d h1:gJqn9yDo+1vyL6I9In667Y8LM0iVRg0ExH7Kd97OZvw=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240620083254-e05817f35b4d/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1 h1:Vxg7WEXNqzI9sj13SdDQP3wQC/6rRJ8mW1KnAnYiaIQ=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240624201244-e8a5b1dcc4a1/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ=
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61 h1:smPTvmXDhn/QC7y/TPXyMTqbbRd0gvzmFgWBChwTfhE=
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61/go.mod h1:/TAHs4ybuylk5icuy+MQtHRc4XUnIyXzeNKxX9qDFhw=
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c h1:a2RVkpIV2QcrGnSHAou+t/L+vBsaIfFvk5inVg5Uh4s=
Expand Down
27 changes: 27 additions & 0 deletions pkg/client/http/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ func (c *RegistryClient) ListTags(ctx context.Context, repository string) ([]str

return resp.Tags, nil
}

// DeleteTag calls the DELETE /v2/<name>/manifests/<reference> endpoint, where <name> is a
// repository, and <reference> is the digest
func (c *RegistryClient) DeleteTag(ctx context.Context, repository string, digest string) error {

deletePath := fmt.Sprintf("/v2/%s/manifests/%s", repository, digest)
r := c.R().SetContext(ctx)
if _, err := r.Delete(deletePath); err != nil {
return fmt.Errorf("couldn't delete the image with registry: %w", err)
}

return nil
}

// GetTagDigest calls the HEAD /v2/<name>/manifests/<reference> endpoint, where <name> is a
// repository, and <reference> is the tag
func (c *RegistryClient) GetTagDigest(ctx context.Context, repository string, tag string) (string, error) {

digestPath := fmt.Sprintf("/v2/%s/manifests/%s", repository, tag)
r := c.R().SetContext(ctx).SetHeader("Accept", "application/vnd.docker.distribution.manifest.v2+json")
resp, err := r.Head(digestPath)
if err != nil {
return "", fmt.Errorf("couldn't get the image digest: %w", err)
}

return resp.Header().Get("Docker-Content-Digest"), nil
}
17 changes: 17 additions & 0 deletions pkg/handler/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ func (h *PrivateHandler) CreateRepositoryTag(ctx context.Context, req *pb.Create
logger.Info("CreateRepositoryTag")
return resp, nil
}

// DeleteRepositoryTag deletes the information of a repository tag in registry.
func (h *PrivateHandler) DeleteRepositoryTag(ctx context.Context, req *pb.DeleteRepositoryTagRequest) (*pb.DeleteRepositoryTagResponse, error) {
ctx, span := tracer.Start(ctx, "DeleteRepositoryTag", trace.WithSpanKind(trace.SpanKindServer))
defer span.End()

logger, _ := logger.GetZapLogger(ctx)

resp, err := h.service.DeleteRepositoryTag(ctx, req)
if err != nil {
span.SetStatus(1, err.Error())
return nil, err
}

logger.Info("DeleteRepositoryTag")
return resp, nil
}
Loading

0 comments on commit fe818da

Please sign in to comment.