Skip to content

Commit 4aef889

Browse files
authoredNov 22, 2019
Merge pull request #130 from njhale/fix-ignored-cachedir
Bug 1775527: Fix ignored cachedir in appregistry image builder
2 parents fb1e512 + 5bae611 commit 4aef889

18 files changed

+684
-48
lines changed
 

‎Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ container-codegen:
4343
docker rm temp-codegen
4444

4545
generate-fakes:
46-
go install -mod=vendor ./vendor/github.com/maxbrunsfeld/counterfeiter/v6
4746
go generate ./...
4847

4948
clean:

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
243243
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
244244
github.com/jackc/pgx v3.2.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
245245
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
246+
github.com/joefitzgerald/rainbow-reporter v0.1.0 h1:AuMG652zjdzI0YCCnXAqATtRBpGXMcAnrajcaTrSeuo=
246247
github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8=
247248
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
248249
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@@ -351,6 +352,7 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn
351352
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
352353
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
353354
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
355+
github.com/sclevine/spec v1.2.0 h1:1Jwdf9jSfDl9NVmt8ndHqbTZ7XCCPbh1jI3hkDBHVYA=
354356
github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
355357
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
356358
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

‎pkg/apprclient/apprclientfakes/fake_client.go

+273
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/apprclient/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Client
12
package apprclient
23

34
import (
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package appregistry
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestAppregistry(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Appregistry Suite")
13+
}

‎pkg/appregistry/appregistryfakes/fake_image_appender.go

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/appregistry/appregistryfakes/fake_manifest_downloader.go

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/appregistry/builder.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 builder.go ImageAppender
12
package appregistry
23

34
import (
@@ -46,6 +47,7 @@ type AppregistryImageBuilder struct {
4647
CleanOutput bool
4748
ManifestDir string
4849
DatabaseDir string
50+
client apprclient.Client
4951
}
5052

5153
func NewAppregistryImageBuilder(options ...AppregistryBuildOption) (*AppregistryImageBuilder, error) {
@@ -69,14 +71,11 @@ func NewAppregistryImageBuilder(options ...AppregistryBuildOption) (*Appregistry
6971
CleanOutput: config.CleanOutput,
7072
ManifestDir: config.ManifestDir,
7173
DatabaseDir: config.DatabaseDir,
74+
client: config.Client,
7275
}, nil
7376
}
7477

7578
func (b *AppregistryImageBuilder) Build() error {
76-
opts := apprclient.Options{Source: b.AppRegistryEndpoint}
77-
if b.AuthToken != "" {
78-
opts.AuthToken = b.AuthToken
79-
}
8079

8180
defer func() {
8281
if !b.CleanOutput {
@@ -87,12 +86,7 @@ func (b *AppregistryImageBuilder) Build() error {
8786
}
8887
}()
8988

90-
client, err := apprclient.New(opts)
91-
if err != nil {
92-
return err
93-
}
94-
95-
downloader := NewManifestDownloader(client)
89+
downloader := NewManifestDownloader(b.client)
9690
if err := downloader.DownloadManifests(b.ManifestDir, b.AppRegistryOrg); err != nil {
9791
return err
9892
}

‎pkg/appregistry/builder_options.go

+49-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package appregistry
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67
"path"
8+
9+
"github.com/operator-framework/operator-registry/pkg/apprclient"
710
)
811

912
type AppregistryBuildOptions struct {
@@ -20,6 +23,8 @@ type AppregistryBuildOptions struct {
2023
CleanOutput bool
2124
ManifestDir string
2225
DatabaseDir string
26+
27+
Client apprclient.Client
2328
}
2429

2530
func (o *AppregistryBuildOptions) Validate() error {
@@ -46,18 +51,31 @@ func (o *AppregistryBuildOptions) Validate() error {
4651
if o.DatabaseDir == "" {
4752
return fmt.Errorf("local database directory required")
4853
}
54+
if o.Client == nil {
55+
return fmt.Errorf("app-registry client must not be nil")
56+
}
4957

5058
return nil
5159
}
5260

5361
func (o *AppregistryBuildOptions) Complete() error {
54-
// if a user has specified a specific cache dir, don't clean it after run
55-
o.CleanOutput = o.CacheDir == ""
62+
// if a user hasn't specified a specific cache directory, generate a temporary one
63+
if o.CacheDir == "" {
64+
tmp, err := ioutil.TempDir("", "cache-")
65+
if err != nil {
66+
return err
67+
}
68+
o.CacheDir = tmp
5669

57-
// build a separate path for manifests and the built database, so that
58-
// building is idempotent
70+
// clean up temporary directories
71+
o.CleanOutput = true
72+
} else if err := os.MkdirAll(o.CacheDir, os.ModePerm); err != nil && !os.IsExist(err) {
73+
return err
74+
}
75+
76+
// build a separate path for manifests and the built database, so that building is idempotent
5977
if o.ManifestDir == "" {
60-
manifestDir, err := ioutil.TempDir("", "manifests-")
78+
manifestDir, err := ioutil.TempDir(o.CacheDir, "manifests-")
6179
if err != nil {
6280
return err
6381
}
@@ -75,6 +93,22 @@ func (o *AppregistryBuildOptions) Complete() error {
7593
if o.DatabasePath == "" {
7694
o.DatabasePath = path.Join(o.DatabaseDir, "bundles.db")
7795
}
96+
97+
// create the client
98+
if o.Client == nil {
99+
opts := apprclient.Options{Source: o.AppRegistryEndpoint}
100+
if o.AuthToken != "" {
101+
opts.AuthToken = o.AuthToken
102+
}
103+
104+
client, err := apprclient.New(opts)
105+
if err != nil {
106+
return err
107+
}
108+
109+
o.Client = client
110+
}
111+
78112
return nil
79113
}
80114

@@ -88,7 +122,7 @@ func (c *AppregistryBuildOptions) Apply(options []AppregistryBuildOption) {
88122
// ToOption converts an AppregistryBuildOptions object into a function that applies
89123
// its current configuration to another AppregistryBuildOptions instance
90124
func (c *AppregistryBuildOptions) ToOption() AppregistryBuildOption {
91-
return func(o *AppregistryBuildOptions) {
125+
return func(o *AppregistryBuildOptions) {
92126
if c.Appender != nil {
93127
o.Appender = c.Appender
94128
}
@@ -116,6 +150,9 @@ func (c *AppregistryBuildOptions) ToOption() AppregistryBuildOption {
116150
if c.DatabaseDir != "" {
117151
o.DatabaseDir = c.DatabaseDir
118152
}
153+
if c.Client != nil {
154+
o.Client = c.Client
155+
}
119156
}
120157
}
121158

@@ -174,3 +211,9 @@ func WithCacheDir(s string) AppregistryBuildOption {
174211
o.CacheDir = s
175212
}
176213
}
214+
215+
func WithClient(c apprclient.Client) AppregistryBuildOption {
216+
return func(o *AppregistryBuildOptions) {
217+
o.Client = c
218+
}
219+
}

‎pkg/appregistry/builder_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package appregistry
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
11+
"github.com/operator-framework/operator-registry/pkg/apprclient"
12+
"github.com/operator-framework/operator-registry/pkg/apprclient/apprclientfakes"
13+
)
14+
15+
var _ = Describe("Image building", func() {
16+
var (
17+
options []AppregistryBuildOption
18+
builder *AppregistryImageBuilder
19+
pkg *apprclient.RegistryMetadata
20+
operator *apprclient.OperatorMetadata
21+
)
22+
23+
JustBeforeEach(func() {
24+
var err error
25+
builder, err = NewAppregistryImageBuilder(options...)
26+
Expect(err).ToNot(HaveOccurred())
27+
Expect(builder).ToNot(BeNil())
28+
Expect(builder.Build()).To(Succeed())
29+
})
30+
31+
BeforeEach(func() {
32+
var noopAppender ImageAppendFunc = func(from, to, layer string) error {
33+
return nil
34+
}
35+
36+
client := &apprclientfakes.FakeClient{}
37+
pkg = &apprclient.RegistryMetadata{
38+
Namespace: "marsupials",
39+
Name: "koala",
40+
}
41+
client.ListPackagesReturns([]*apprclient.RegistryMetadata{pkg}, nil)
42+
43+
pkgBlob, err := ioutil.ReadFile("testdata/golden/marsupials_pkg.tar")
44+
Expect(err).ToNot(HaveOccurred())
45+
46+
operator = &apprclient.OperatorMetadata{
47+
RegistryMetadata: *pkg,
48+
Blob: pkgBlob,
49+
}
50+
client.RetrieveOneReturns(operator, nil)
51+
52+
options = append(options,
53+
WithAppender(noopAppender),
54+
WithAppRegistryOrg("metatheria"),
55+
WithClient(client),
56+
)
57+
})
58+
59+
Context("with a custom cache dir", func() {
60+
var (
61+
cacheDir string
62+
manifestsGlob string
63+
)
64+
65+
BeforeEach(func() {
66+
cacheDir = filepath.Join("testdata", "custom-cache")
67+
options = append(options, WithCacheDir(cacheDir))
68+
manifestsGlob = filepath.Join(cacheDir, "manifests-*/koala/marsupials")
69+
})
70+
71+
AfterEach(func() {
72+
Expect(os.RemoveAll(cacheDir)).To(Succeed())
73+
})
74+
75+
It("should retain unpacked operator manifests", func() {
76+
Expect(cacheDir).To(BeADirectory())
77+
Expect(filepath.Glob(filepath.Join(manifestsGlob, "package.yaml"))).To(HaveLen(1))
78+
Expect(filepath.Glob(filepath.Join(manifestsGlob, "v1.0.0", "koala.v1.0.0.clusterserviceversion.yaml"))).To(HaveLen(1))
79+
})
80+
81+
})
82+
})

‎pkg/appregistry/manifest_downloader.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ManifestDownloader
12
package appregistry
23

34
import (
Binary file not shown.

‎pkg/containertools/command.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate counterfeiter command.go CommandRunner
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . CommandRunner
22
package containertools
33

44
import (
@@ -25,8 +25,8 @@ type CommandRunner interface {
2525

2626
// ContainerCommandRunner is configured to select a container cli tool and execute commands with that
2727
// tooling.
28-
type ContainerCommandRunner struct{
29-
logger *logrus.Entry
28+
type ContainerCommandRunner struct {
29+
logger *logrus.Entry
3030
containerTool string
3131
}
3232

@@ -76,7 +76,7 @@ func (r *ContainerCommandRunner) Pull(image string) error {
7676
// Build takes a dockerfile and a tag and builds a container image
7777
func (r *ContainerCommandRunner) Build(dockerfile, tag string) error {
7878
args := []string{"build", "-f", dockerfile}
79-
79+
8080
if tag != "" {
8181
args = append(args, "-t", tag)
8282
}

‎pkg/containertools/dockerfilegenerator.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate counterfeiter dockerfilegenerator.go DockerfileGenerator
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . DockerfileGenerator
22
package containertools
33

44
import (
@@ -8,10 +8,10 @@ import (
88
)
99

1010
const (
11-
baseImage = "scratch"
11+
baseImage = "scratch"
1212
defaultBinarySourceImage = "quay.io/operator-framework/upstream-registry-builder"
13-
DefaultDbLocation = "./index.db"
14-
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
13+
DefaultDbLocation = "./index.db"
14+
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
1515
)
1616

1717
// DockerfileGenerator defines functions to generate index dockerfiles

‎pkg/containertools/imagereader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate counterfeiter imagereader.go ImageReader
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ImageReader
22
package containertools
33

44
import (

‎pkg/containertools/labelreader.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
//go:generate counterfeiter labelreader.go LabelReader
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . LabelReader
22
package containertools
33

44
import (
5-
"fmt"
65
"encoding/json"
6+
"fmt"
77

88
"github.com/sirupsen/logrus"
99
)
@@ -14,15 +14,15 @@ type LabelReader interface {
1414

1515
type ImageLabelReader struct {
1616
Logger *logrus.Entry
17-
Cmd CommandRunner
17+
Cmd CommandRunner
1818
}
1919

2020
func NewLabelReader(containerTool string, logger *logrus.Entry) LabelReader {
2121
cmd := NewCommandRunner(containerTool, logger)
22-
22+
2323
return ImageLabelReader{
2424
Logger: logger,
25-
Cmd: cmd,
25+
Cmd: cmd,
2626
}
2727
}
2828

@@ -39,7 +39,7 @@ type PodmanImageData struct {
3939
}
4040

4141
// GetLabelsFromImage takes a container image path as input, pulls that image
42-
// to the local environment and then inspects it for labels
42+
// to the local environment and then inspects it for labels
4343
func (r ImageLabelReader) GetLabelsFromImage(image string) (map[string]string, error) {
4444
err := r.Cmd.Pull(image)
4545
if err != nil {

‎pkg/lib/indexer/interfaces.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//go:generate counterfeiter indexer.go IndexAdder
2-
//go:generate counterfeiter indexer.go IndexDeleter
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
32
package indexer
43

54
import (
@@ -11,6 +10,7 @@ import (
1110

1211
// IndexAdder allows the creation of index container images from scratch or
1312
// based on previous index images
13+
//counterfeiter:generate . IndexAdder
1414
type IndexAdder interface {
1515
AddToIndex(AddToIndexRequest) error
1616
}
@@ -19,17 +19,18 @@ type IndexAdder interface {
1919
func NewIndexAdder(containerTool string, logger *logrus.Entry) IndexAdder {
2020
return ImageIndexer{
2121
DockerfileGenerator: containertools.NewDockerfileGenerator(containerTool, logger),
22-
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
23-
LabelReader: containertools.NewLabelReader(containerTool, logger),
24-
RegistryAdder: registry.NewRegistryAdder(logger),
25-
ImageReader: containertools.NewImageReader(containerTool, logger),
26-
ContainerTool: containerTool,
27-
Logger: logger,
22+
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
23+
LabelReader: containertools.NewLabelReader(containerTool, logger),
24+
RegistryAdder: registry.NewRegistryAdder(logger),
25+
ImageReader: containertools.NewImageReader(containerTool, logger),
26+
ContainerTool: containerTool,
27+
Logger: logger,
2828
}
2929
}
3030

3131
// IndexDeleter takes indexes and deletes all references to an operator
3232
// from them
33+
//counterfeiter:generate . IndexDeleter
3334
type IndexDeleter interface {
3435
DeleteFromIndex(DeleteFromIndexRequest) error
3536
}
@@ -38,11 +39,11 @@ type IndexDeleter interface {
3839
func NewIndexDeleter(containerTool string, logger *logrus.Entry) IndexDeleter {
3940
return ImageIndexer{
4041
DockerfileGenerator: containertools.NewDockerfileGenerator(containerTool, logger),
41-
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
42-
LabelReader: containertools.NewLabelReader(containerTool, logger),
43-
RegistryDeleter: registry.NewRegistryDeleter(logger),
44-
ImageReader: containertools.NewImageReader(containerTool, logger),
45-
ContainerTool: containerTool,
46-
Logger: logger,
42+
CommandRunner: containertools.NewCommandRunner(containerTool, logger),
43+
LabelReader: containertools.NewLabelReader(containerTool, logger),
44+
RegistryDeleter: registry.NewRegistryDeleter(logger),
45+
ImageReader: containertools.NewImageReader(containerTool, logger),
46+
ContainerTool: containerTool,
47+
Logger: logger,
4748
}
4849
}

‎pkg/lib/registry/interfaces.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//go:generate counterfeiter registry.go RegistryAdder
2-
//go:generate counterfeiter registry.go RegistryDeleter
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
32
package registry
43

54
import (
65
"github.com/sirupsen/logrus"
76
)
87

8+
//counterfeiter:generate . RegistryAdder
99
type RegistryAdder interface {
1010
AddToRegistry(AddToRegistryRequest) error
1111
}
@@ -16,6 +16,7 @@ func NewRegistryAdder(logger *logrus.Entry) RegistryAdder {
1616
}
1717
}
1818

19+
//counterfeiter:generate . RegistryDeleter
1920
type RegistryDeleter interface {
2021
DeleteFromRegistry(DeleteFromRegistryRequest) error
2122
}

0 commit comments

Comments
 (0)
Please sign in to comment.