Skip to content

Commit 2548973

Browse files
milosgajdosthaJeztah
authored andcommitted
Enable Go build tags
This enables go build tags so the GCS and OSS driver support is available in the binary distributed via the image build by Dockerfile. This led to quite a few fixes in the GCS and OSS packages raised as warning by golang-ci linter. Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com> (cherry picked from commit 6b388b1) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 8728c52 commit 2548973

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

BUILDING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ the registry binary generated in the "./bin" directory:
114114
### Optional build tags
115115

116116
Optional [build tags](http://golang.org/pkg/go/build/) can be provided using
117-
the environment variable `DOCKER_BUILDTAGS`.
117+
the environment variable `BUILDTAGS`.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ RUN --mount=target=. \
2222
FROM base AS build
2323
ARG TARGETPLATFORM
2424
ARG LDFLAGS="-s -w"
25-
ARG BUILDTAGS="include_oss include_gcs"
25+
ARG BUILDTAGS="include_oss,include_gcs"
2626
RUN --mount=type=bind,target=/go/src/github.com/docker/distribution,rw \
2727
--mount=type=cache,target=/root/.cache/go-build \
2828
--mount=target=/go/pkg/mod,type=cache \
2929
--mount=type=bind,source=/tmp/.ldflags,target=/tmp/.ldflags,from=version \
30-
set -x ; xx-go build -trimpath -ldflags "$(cat /tmp/.ldflags) ${LDFLAGS}" -tags="${BUILDTAGS}" -o /usr/bin/registry ./cmd/registry \
30+
set -x ; xx-go build -tags "${BUILDTAGS}" -trimpath -ldflags "$(cat /tmp/.ldflags) ${LDFLAGS}" -o /usr/bin/registry ./cmd/registry \
3131
&& xx-verify --static /usr/bin/registry
3232

3333
FROM scratch AS binary

registry/storage/driver/gcs/gcs.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
//go:build include_gcs
2+
// +build include_gcs
3+
14
// Package gcs provides a storagedriver.StorageDriver implementation to
25
// store blobs in Google cloud storage.
36
//
47
// This package leverages the google.golang.org/cloud/storage client library
5-
//for interfacing with gcs.
8+
// for interfacing with gcs.
69
//
710
// Because gcs is a key, value store the Stat call does not support last modification
811
// time for directories (directories are an abstraction for key, value stores)
912
//
1013
// Note that the contents of incomplete uploads are not accessible even though
1114
// Stat returns their length
12-
//
13-
//go:build include_gcs
14-
// +build include_gcs
15-
1615
package gcs
1716

1817
import (
@@ -62,7 +61,6 @@ var rangeHeader = regexp.MustCompile(`^bytes=([0-9])+-([0-9]+)$`)
6261
// driverParameters is a struct that encapsulates all of the driver parameters after all values have been set
6362
type driverParameters struct {
6463
bucket string
65-
config *jwt.Config
6664
email string
6765
privateKey []byte
6866
client *http.Client
@@ -88,6 +86,8 @@ func (factory *gcsDriverFactory) Create(parameters map[string]interface{}) (stor
8886
return FromParameters(parameters)
8987
}
9088

89+
var _ storagedriver.StorageDriver = &driver{}
90+
9191
// driver is a storagedriver.StorageDriver implementation backed by GCS
9292
// Objects are stored at absolute keys in the provided bucket.
9393
type driver struct {
@@ -298,7 +298,7 @@ func (d *driver) Reader(context context.Context, path string, offset int64) (io.
298298
if err != nil {
299299
return nil, err
300300
}
301-
if offset == int64(obj.Size) {
301+
if offset == obj.Size {
302302
return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
303303
}
304304
return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
@@ -434,7 +434,6 @@ func putContentsClose(wc *storage.Writer, contents []byte) error {
434434
}
435435
}
436436
if err != nil {
437-
wc.CloseWithError(err)
438437
return err
439438
}
440439
return wc.Close()
@@ -614,10 +613,10 @@ func (d *driver) Stat(context context.Context, path string) (storagedriver.FileI
614613
//try to get as folder
615614
dirpath := d.pathToDirKey(path)
616615

617-
var query *storage.Query
618-
query = &storage.Query{}
619-
query.Prefix = dirpath
620-
query.MaxResults = 1
616+
query := &storage.Query{
617+
Prefix: dirpath,
618+
MaxResults: 1,
619+
}
621620

622621
objects, err := storageListObjects(gcsContext, d.bucket, query)
623622
if err != nil {
@@ -639,12 +638,12 @@ func (d *driver) Stat(context context.Context, path string) (storagedriver.FileI
639638
}
640639

641640
// List returns a list of the objects that are direct descendants of the
642-
//given path.
641+
// given path.
643642
func (d *driver) List(context context.Context, path string) ([]string, error) {
644-
var query *storage.Query
645-
query = &storage.Query{}
646-
query.Delimiter = "/"
647-
query.Prefix = d.pathToDirKey(path)
643+
query := &storage.Query{
644+
Delimiter: "/",
645+
Prefix: d.pathToDirKey(path),
646+
}
648647
list := make([]string, 0, 64)
649648
for {
650649
objects, err := storageListObjects(d.context(context), d.bucket, query)

registry/storage/driver/gcs/gcs_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func init() {
5959
panic(fmt.Sprintf("Error reading JWT config : %s", err))
6060
}
6161
email = jwtConfig.Email
62-
privateKey = []byte(jwtConfig.PrivateKey)
62+
privateKey = jwtConfig.PrivateKey
6363
if len(privateKey) == 0 {
6464
panic("Error reading JWT config : missing private_key property")
6565
}
@@ -260,13 +260,19 @@ func TestEmptyRootList(t *testing.T) {
260260
}
261261
}()
262262
keys, err := emptyRootDriver.List(ctx, "/")
263+
if err != nil {
264+
t.Fatalf("unexpected error listing empty root content: %v", err)
265+
}
263266
for _, path := range keys {
264267
if !storagedriver.PathRegexp.MatchString(path) {
265268
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
266269
}
267270
}
268271

269272
keys, err = slashRootDriver.List(ctx, "/")
273+
if err != nil {
274+
t.Fatalf("unexpected error listing slash root content: %v", err)
275+
}
270276
for _, path := range keys {
271277
if !storagedriver.PathRegexp.MatchString(path) {
272278
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)

registry/storage/driver/oss/oss.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build include_oss
2+
// +build include_oss
3+
14
// Package oss provides a storagedriver.StorageDriver implementation to
25
// store blobs in Aliyun OSS cloud storage.
36
//
@@ -6,10 +9,6 @@
69
//
710
// Because OSS is a key, value store the Stat call does not support last modification
811
// time for directories (directories are an abstraction for key, value stores)
9-
//
10-
//go:build include_oss
11-
// +build include_oss
12-
1312
package oss
1413

1514
import (
@@ -68,6 +67,8 @@ func (factory *ossDriverFactory) Create(parameters map[string]interface{}) (stor
6867
return FromParameters(parameters)
6968
}
7069

70+
var _ storagedriver.StorageDriver = &driver{}
71+
7172
type driver struct {
7273
Client *oss.Client
7374
Bucket *oss.Bucket
@@ -498,11 +499,6 @@ func parseError(path string, err error) error {
498499
return err
499500
}
500501

501-
func hasCode(err error, code string) bool {
502-
ossErr, ok := err.(*oss.Error)
503-
return ok && ossErr.Code == code
504-
}
505-
506502
func (d *driver) getOptions() oss.Options {
507503
return oss.Options{ServerSideEncryption: d.Encrypt}
508504
}

registry/storage/driver/oss/oss_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,19 @@ func TestEmptyRootList(t *testing.T) {
128128
defer rootedDriver.Delete(ctx, filename)
129129

130130
keys, err := emptyRootDriver.List(ctx, "/")
131+
if err != nil {
132+
t.Fatalf("unexpected error listing empty root content: %v", err)
133+
}
131134
for _, path := range keys {
132135
if !storagedriver.PathRegexp.MatchString(path) {
133136
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)
134137
}
135138
}
136139

137140
keys, err = slashRootDriver.List(ctx, "/")
141+
if err != nil {
142+
t.Fatalf("unexpected error listing slash root content: %v", err)
143+
}
138144
for _, path := range keys {
139145
if !storagedriver.PathRegexp.MatchString(path) {
140146
t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp)

0 commit comments

Comments
 (0)