Skip to content

Commit 8eed56d

Browse files
committed
use interface
1 parent 8149de5 commit 8eed56d

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

pkg/v1/mutate/image.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ type image struct {
3939
digestMap map[v1.Hash]v1.Layer
4040
}
4141

42+
func (i *image) ETag() string {
43+
if e, ok := i.base.(partial.WithETag); ok {
44+
return e.ETag()
45+
}
46+
return ""
47+
}
48+
4249
var _ v1.Image = (*image)(nil)
4350

4451
func (i *image) MediaType() (types.MediaType, error) {

pkg/v1/partial/with.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import (
2525
"github.com/google/go-containerregistry/pkg/v1/types"
2626
)
2727

28+
// WithETag defines the subset of manifest types that can expose an ETag
29+
// returned by a registry server.
30+
//
31+
// If a manifest provides an ETag using this method, it will be used when
32+
// pushing updates, to prevent race conditions.
33+
type WithETag interface {
34+
// ETag returns the ETag returned by remote registry implementations.
35+
ETag() string
36+
}
37+
2838
// WithRawConfigFile defines the subset of v1.Image used by these helper methods
2939
type WithRawConfigFile interface {
3040
// RawConfigFile returns the serialized bytes of this image's config file.

pkg/v1/remote/image.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func Image(ref name.Reference, options ...Option) (v1.Image, error) {
6060
return desc.Image()
6161
}
6262

63+
func (r *remoteImage) ETag() string { return r.etag }
64+
6365
func (r *remoteImage) MediaType() (types.MediaType, error) {
6466
if string(r.mediaType) != "" {
6567
return r.mediaType, nil

pkg/v1/remote/index.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func Index(ref name.Reference, options ...Option) (v1.ImageIndex, error) {
5151
return desc.ImageIndex()
5252
}
5353

54+
func (r *remoteIndex) ETag() string { return r.etag }
55+
5456
func (r *remoteIndex) MediaType() (types.MediaType, error) {
5557
if string(r.mediaType) != "" {
5658
return r.mediaType, nil

pkg/v1/remote/write.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,8 @@ func (w *writer) commitManifest(ctx context.Context, t Taggable, ref name.Refere
612612
}
613613
req.Header.Set("Content-Type", string(desc.MediaType))
614614

615-
if ri, ok := t.(*remoteImage); ok && ri.etag != "" {
616-
req.Header.Set("If-Match", ri.etag)
617-
}
618-
if ri, ok := t.(*remoteIndex); ok && ri.etag != "" {
619-
req.Header.Set("If-Match", ri.etag)
615+
if e, ok := t.(partial.WithETag); ok && e.ETag() != "" {
616+
req.Header.Set("If-Match", e.ETag())
620617
}
621618

622619
resp, err := w.client.Do(req.WithContext(ctx))

0 commit comments

Comments
 (0)