Skip to content

Commit f666f9d

Browse files
ecordellnjhale
authored andcommitted
feat(deamonless): add buildah-based deamonless image puller
this still requires some refactoring and does not implement Unpack
1 parent d05586a commit f666f9d

File tree

41 files changed

+1242
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1242
-51
lines changed

cmd/opm/registry/add.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ func newRegistryAddCmd() *cobra.Command {
3838
}
3939

4040
func addFunc(cmd *cobra.Command, args []string) error {
41-
bundleImages, err := cmd.Flags().GetStringSlice("bundle-images")
41+
permissive, err := cmd.Flags().GetBool("permissive")
4242
if err != nil {
4343
return err
4444
}
45-
fromFilename, err := cmd.Flags().GetString("database")
45+
skipTLS, err := cmd.Flags().GetBool("skip-tls")
4646
if err != nil {
4747
return err
4848
}
49-
permissive, err := cmd.Flags().GetBool("permissive")
49+
fromFilename, err := cmd.Flags().GetString("database")
5050
if err != nil {
5151
return err
5252
}
53-
skipTLS, err := cmd.Flags().GetBool("skip-tls")
53+
bundleImages, err := cmd.Flags().GetStringSlice("bundle-images")
5454
if err != nil {
5555
return err
5656
}
5757

5858
request := registry.AddToRegistryRequest{
59-
Bundles: bundleImages,
60-
InputDatabase: fromFilename,
6159
Permissive: permissive,
6260
SkipTLS: skipTLS,
61+
InputDatabase: fromFilename,
62+
Bundles: bundleImages,
6363
}
6464

6565
logger := logrus.WithFields(logrus.Fields{"bundles": bundleImages})

go.mod

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,40 @@ module github.com/operator-framework/operator-registry
33
go 1.13
44

55
require (
6-
github.com/Microsoft/hcsshim v0.8.7 // indirect
76
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6
87
github.com/blang/semver v3.5.0+incompatible
98
github.com/containerd/containerd v1.3.2
109
github.com/containerd/continuity v0.0.0-20200228182428-0f16d7a0959c // indirect
10+
github.com/containers/buildah v1.14.3
1111
github.com/docker/cli v0.0.0-20200130152716-5d0cf8839492
1212
github.com/docker/distribution v2.7.1+incompatible
1313
github.com/docker/docker v1.4.2-0.20200203170920-46ec8731fbce
14-
github.com/docker/docker-credential-helpers v0.6.3 // indirect
1514
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
1615
github.com/ghodss/yaml v1.0.0
1716
github.com/gogo/protobuf v1.3.1 // indirect
1817
github.com/golang-migrate/migrate/v4 v4.6.2
1918
github.com/golang/mock v1.3.1
2019
github.com/golang/protobuf v1.3.2
21-
github.com/google/go-cmp v0.4.0 // indirect
2220
github.com/grpc-ecosystem/grpc-health-probe v0.2.1-0.20181220223928-2bf0a5b182db
2321
github.com/mattn/go-sqlite3 v1.10.0
2422
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2
2523
github.com/morikuni/aec v1.0.0 // indirect
26-
github.com/onsi/ginkgo v1.10.1
27-
github.com/onsi/gomega v1.7.0
28-
github.com/opencontainers/image-spec v1.0.1
29-
github.com/opencontainers/runc v0.1.1 // indirect
24+
github.com/onsi/ginkgo v1.12.0
25+
github.com/onsi/gomega v1.9.0
26+
github.com/opencontainers/image-spec v1.0.2-0.20190823105129-775207bd45b6
3027
github.com/operator-framework/api v0.1.1
3128
github.com/otiai10/copy v1.0.2
3229
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
3330
github.com/pkg/errors v0.9.1
3431
github.com/sirupsen/logrus v1.4.2
35-
github.com/spf13/cobra v0.0.5
36-
github.com/stretchr/testify v1.4.0
32+
github.com/spf13/cobra v0.0.6
33+
github.com/stretchr/testify v1.5.1
3734
go.etcd.io/bbolt v1.3.3
38-
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect
3935
golang.org/x/mod v0.2.0
4036
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271
4137
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
4238
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
43-
google.golang.org/grpc v1.23.1
39+
google.golang.org/grpc v1.24.0
4440
gopkg.in/yaml.v2 v2.2.8
4541
k8s.io/api v0.17.3
4642
k8s.io/apiextensions-apiserver v0.17.3

go.sum

Lines changed: 128 additions & 23 deletions
Large diffs are not rendered by default.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package buildahregistry
2+
3+
import (
4+
"github.com/containers/storage"
5+
"github.com/containers/storage/pkg/idtools"
6+
"io/ioutil"
7+
"os"
8+
"path"
9+
"path/filepath"
10+
"sync"
11+
12+
//"sync"
13+
//
14+
//contentlocal "github.com/containerd/containerd/content/local"
15+
//"github.com/containerd/containerd/metadata"
16+
//"github.com/containerd/containerd/platforms"
17+
"github.com/sirupsen/logrus"
18+
//bolt "go.etcd.io/bbolt"
19+
)
20+
21+
type RegistryConfig struct {
22+
Log *logrus.Entry
23+
ResolverConfigDir string
24+
DBPath string
25+
CacheDir string
26+
PreserveCache bool
27+
SkipTLS bool
28+
}
29+
30+
func (r *RegistryConfig) apply(options []RegistryOption) {
31+
for _, option := range options {
32+
option(r)
33+
}
34+
}
35+
36+
func (r *RegistryConfig) complete() error {
37+
if err := os.Mkdir(r.CacheDir, os.ModePerm); err != nil && !os.IsExist(err) {
38+
return err
39+
}
40+
41+
if r.DBPath == "" {
42+
r.DBPath = filepath.Join(r.CacheDir, "metadata.db")
43+
}
44+
45+
return nil
46+
}
47+
48+
func defaultConfig() *RegistryConfig {
49+
config := &RegistryConfig{
50+
Log: logrus.NewEntry(logrus.New()),
51+
ResolverConfigDir: "",
52+
CacheDir: "cache",
53+
}
54+
55+
return config
56+
}
57+
58+
func NewRegistry(options ...RegistryOption) (*Registry, error) {
59+
config := defaultConfig()
60+
config.apply(options)
61+
if err := config.complete(); err != nil {
62+
return nil, err
63+
}
64+
65+
var (
66+
once sync.Once
67+
closed bool
68+
)
69+
closeFunc := func() error {
70+
defer func() {
71+
once.Do(func() {
72+
closed = true
73+
})
74+
}()
75+
if closed {
76+
// Already closed, no-op
77+
return nil
78+
}
79+
80+
if config.PreserveCache {
81+
return nil
82+
}
83+
return os.RemoveAll(config.CacheDir)
84+
}
85+
86+
// TODO: at this point we've overwritten all the defaults, may as well not use this
87+
storeOpts, err := storage.DefaultStoreOptionsAutoDetectUID()
88+
if err != nil {
89+
return nil, err
90+
}
91+
storeOpts.RootlessStoragePath = config.CacheDir
92+
storeOpts.RunRoot = config.CacheDir
93+
storeOpts.GraphRoot = config.CacheDir
94+
storeOpts.GraphDriverName = "vfs"
95+
storeOpts.UIDMap = []idtools.IDMap{
96+
{ContainerID: 0, HostID: os.Getuid()},
97+
}
98+
storeOpts.GIDMap = []idtools.IDMap{
99+
{ContainerID: 0, HostID: os.Getgid()},
100+
}
101+
102+
store, err := storage.GetStore(storeOpts)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
// TODO: probably don't want the signature policy to be here
108+
ioutil.WriteFile(path.Join(config.CacheDir, "policy.json"), []byte(`
109+
{
110+
"default": [
111+
{
112+
"type": "insecureAcceptAnything"
113+
}
114+
],
115+
"transports":
116+
{
117+
"docker-daemon":
118+
{
119+
"": [{"type":"insecureAcceptAnything"}]
120+
}
121+
}
122+
}
123+
`), os.ModePerm)
124+
125+
r := &Registry{
126+
Store: store,
127+
CacheDir: config.CacheDir,
128+
log: config.Log,
129+
close: closeFunc,
130+
}
131+
return r, nil
132+
}
133+
134+
type RegistryOption func(config *RegistryConfig)
135+
136+
func WithLog(log *logrus.Entry) RegistryOption {
137+
return func(config *RegistryConfig) {
138+
config.Log = log
139+
}
140+
}
141+
142+
func WithResolverConfigDir(path string) RegistryOption {
143+
return func(config *RegistryConfig) {
144+
config.ResolverConfigDir = path
145+
}
146+
}
147+
148+
func WithCacheDir(dir string) RegistryOption {
149+
return func(config *RegistryConfig) {
150+
config.CacheDir = dir
151+
}
152+
}
153+
154+
func PreserveCache() RegistryOption {
155+
return func(config *RegistryConfig) {
156+
config.PreserveCache = true
157+
}
158+
}
159+
160+
func SkipTLS() RegistryOption {
161+
return func(config *RegistryConfig) {
162+
config.SkipTLS = true
163+
}
164+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package buildahregistry
2+
3+
import (
4+
"context"
5+
"github.com/containers/storage"
6+
"path"
7+
8+
"github.com/containers/buildah"
9+
"github.com/containers/image/v5/types"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
type Registry struct {
14+
storage.Store
15+
16+
CacheDir string
17+
18+
log *logrus.Entry
19+
20+
close func() error
21+
}
22+
23+
// Pull fetches and stores an image by reference.
24+
func (r *Registry) Pull(ctx context.Context, ref string) error {
25+
img, err := buildah.Pull(ctx, ref, buildah.PullOptions{
26+
SignaturePolicyPath: path.Join(r.CacheDir, "policy.json"),
27+
ReportWriter: r.log.Writer(),
28+
Store: r.Store,
29+
SystemContext: &types.SystemContext{
30+
// TODO: auth stuff goes here too
31+
// TODO: if we're okay with buildah's cobra args, there's a function to build this from the standard args
32+
SignaturePolicyPath: path.Join(r.CacheDir, "policy.json"),
33+
OCIInsecureSkipTLSVerify: true,
34+
DockerInsecureSkipTLSVerify: types.OptionalBoolTrue,
35+
36+
},
37+
BlobDirectory: r.CacheDir,
38+
AllTags: false,
39+
RemoveSignatures: false,
40+
MaxRetries: 0,
41+
RetryDelay: 0,
42+
})
43+
44+
r.log.Info(img)
45+
return err
46+
}
47+
48+
// Unpack writes the unpackaged content of an image to a directory.
49+
// If the referenced image does not exist in the registry, an error is returned.
50+
func (r *Registry) Unpack(ctx context.Context, ref, dir string) error {
51+
return nil
52+
}
53+
54+
func (r *Registry) Close() error {
55+
return r.close()
56+
}
57+

pkg/image/unprivileged/registry_test.go renamed to pkg/image/buildahregistry/registry_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package unprivileged
1+
package buildahregistry
22

33
import (
44
"context"
@@ -33,15 +33,15 @@ func setupRegistry(t *testing.T, ctx context.Context, rootDir string) string {
3333
}
3434
config.HTTP.DrainTimeout = time.Duration(2) * time.Second
3535

36-
dockerRegistry, err := registry.NewRegistry(context.Background(), config)
36+
dockerRegistry, err := registry.NewRegistry(ctx, config)
3737
require.NoError(t, err)
3838

3939
go func() {
4040
require.NoError(t, dockerRegistry.ListenAndServe())
4141
}()
4242

4343
// Return the registry host string
44-
return fmt.Sprintf("127.0.0.1:%d", dockerPort)
44+
return fmt.Sprintf("localhost:%d", dockerPort)
4545
}
4646

4747
func dirChecksum(t *testing.T, dir string) string {
@@ -86,6 +86,7 @@ func TestPullAndUnpack(t *testing.T) {
8686
}
8787
for _, tt := range tests {
8888
t.Run(tt.description, func(t *testing.T) {
89+
logrus.SetLevel(logrus.DebugLevel)
8990
ctx, close := context.WithCancel(context.Background())
9091
defer close()
9192

pkg/image/unprivileged/resolver.go renamed to pkg/image/buildahregistry/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package unprivileged
1+
package buildahregistry
22

33
import (
44
"crypto/tls"

pkg/image/unprivileged/store.go renamed to pkg/image/buildahregistry/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package unprivileged
1+
package buildahregistry
22

33
import (
44
"github.com/containerd/containerd/content"

0 commit comments

Comments
 (0)