Skip to content

Commit d7480d6

Browse files
committedOct 9, 2018
chore(pkg): make more like a kube-style go package
1 parent 6ee1739 commit d7480d6

File tree

9 files changed

+125
-89
lines changed

9 files changed

+125
-89
lines changed
 

‎Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ RUN apk update && apk add sqlite build-base
44
WORKDIR /go/src/github.com/operator-framework/operator-registry
55

66
COPY vendor vendor
7-
COPY init init
8-
COPY store store
9-
RUN go build --tags json1 -o ./initializer ./init/...
7+
COPY cmd cmd
8+
COPY pkg pkg
9+
RUN go build --tags json1 -o ./initializer ./cmd/init/...
1010

1111
COPY manifests manifests
1212
RUN ./initializer

‎init/main.go ‎cmd/init/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"github.com/operator-framework/operator-registry/store"
4+
"github.com/operator-framework/operator-registry/pkg/sqlite"
55
"github.com/sirupsen/logrus"
66
"github.com/spf13/cobra"
77
)
@@ -43,13 +43,13 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
4343
return err
4444
}
4545

46-
dbLoader, err := store.NewSQLLiteLoader(outFilename)
46+
dbLoader, err := sqlite.NewSQLLiteLoader(outFilename)
4747
if err != nil {
4848
logrus.Fatal(err)
4949
}
5050
defer dbLoader.Close()
5151

52-
loader := store.NewSQLLoaderForDirectory(dbLoader, manifestDir)
52+
loader := sqlite.NewSQLLoaderForDirectory(dbLoader, manifestDir)
5353
if err := loader.Populate(); err != nil {
5454
logrus.Fatal(err)
5555
}

‎init.sql

-17
This file was deleted.

‎pkg/registry/interface.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package registry
2+
3+
import (
4+
"context"
5+
6+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7+
)
8+
9+
type Load interface {
10+
AddOperatorBundle(bundleObjs []*unstructured.Unstructured) error
11+
AddPackageChannels(manifest PackageManifest) error
12+
AddProvidedApis() error
13+
}
14+
15+
type Query interface {
16+
ListPackages(context context.Context) ([]string, error)
17+
GetPackage(context context.Context, name string) (*PackageManifest, error)
18+
GetBundleForChannel(context context.Context, pkgName string, channelName string) (string, error)
19+
GetBundleForName(context context.Context, name string) (string, error)
20+
// Get all channel entries that say they replace this one
21+
GetChannelEntriesThatReplace(context context.Context, name string) (entries []ChannelEntry, err error)
22+
// Get the bundle in a package/channel that replace this one
23+
GetBundleThatReplaces(context context.Context, name, pkgName, channelName string) (string, error)
24+
// Get all channel entries that provide an api
25+
GetChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error)
26+
// Get latest channel entries that provide an api
27+
GetLatestChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error)
28+
// Get the the latest bundle that provides the API in a default channel
29+
GetBundleThatProvides(context context.Context, groupOrName, version, kind string) (string, error)
30+
}

‎pkg/registry/types.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package registry
2+
3+
// PackageManifest holds information about a package, which is a reference to one (or more)
4+
// channels under a single package.
5+
type PackageManifest struct {
6+
// PackageName is the name of the overall package, ala `etcd`.
7+
PackageName string `json:"packageName"`
8+
9+
// Channels are the declared channels for the package, ala `stable` or `alpha`.
10+
Channels []PackageChannel `json:"channels"`
11+
12+
// DefaultChannelName is, if specified, the name of the default channel for the package. The
13+
// default channel will be installed if no other channel is explicitly given. If the package
14+
// has a single channel, then that channel is implicitly the default.
15+
DefaultChannelName string `json:"defaultChannel"`
16+
}
17+
18+
// GetDefaultChannel gets the default channel or returns the only one if there's only one. returns empty string if it
19+
// can't determine the default
20+
func (m PackageManifest) GetDefaultChannel() string {
21+
if m.DefaultChannelName != "" {
22+
return m.DefaultChannelName
23+
}
24+
if len(m.Channels) == 1 {
25+
return m.Channels[0].Name
26+
}
27+
return ""
28+
}
29+
30+
// PackageChannel defines a single channel under a package, pointing to a version of that
31+
// package.
32+
type PackageChannel struct {
33+
// Name is the name of the channel, e.g. `alpha` or `stable`
34+
Name string `json:"name"`
35+
36+
// CurrentCSVName defines a reference to the CSV holding the version of this package currently
37+
// for the channel.
38+
CurrentCSVName string `json:"currentCSV"`
39+
}
40+
41+
// IsDefaultChannel returns true if the PackageChennel is the default for the PackageManifest
42+
func (pc PackageChannel) IsDefaultChannel(pm PackageManifest) bool {
43+
return pc.Name == pm.DefaultChannelName || len(pm.Channels) == 1
44+
}
45+
46+
// ChannelEntry is a denormalized node in a channel graph
47+
type ChannelEntry struct {
48+
PackageName string
49+
ChannelName string
50+
BundleName string
51+
Replaces string
52+
}

‎store/directory.go ‎pkg/sqlite/directory.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package store
1+
package sqlite
22

33
import (
44
"fmt"
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
11-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
1211
"github.com/sirupsen/logrus"
1312
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
1413
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
@@ -17,6 +16,7 @@ import (
1716
"k8s.io/apimachinery/pkg/runtime/serializer"
1817
"k8s.io/apimachinery/pkg/util/yaml"
1918
"k8s.io/client-go/kubernetes/scheme"
19+
"github.com/operator-framework/operator-registry/pkg/registry"
2020
)
2121

2222
// Scheme is the default instance of runtime.Scheme to which types in the Kubernetes API are already registered.
@@ -54,13 +54,13 @@ type APIKey struct {
5454
// files ending in `.clusterserviceversion.yaml` will be parsed as CSVs
5555
// files ending in `.package.yaml` will be parsed as Packages
5656
type DirectoryLoader struct {
57-
store Load
57+
store registry.Load
5858
directory string
5959
}
6060

6161
var _ SQLPopulator = &DirectoryLoader{}
6262

63-
func NewSQLLoaderForDirectory(store Load, directory string) *DirectoryLoader {
63+
func NewSQLLoaderForDirectory(store registry.Load, directory string) *DirectoryLoader {
6464
return &DirectoryLoader{
6565
store: store,
6666
directory: directory,

‎store/load.go ‎pkg/sqlite/load.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
package store
1+
package sqlite
22

33
import (
44
"database/sql"
55
"encoding/json"
66
"fmt"
77

88
_ "github.com/mattn/go-sqlite3"
9-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
9+
"github.com/operator-framework/operator-registry/pkg/registry"
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1111
"k8s.io/apimachinery/pkg/runtime"
1212
)
1313

14-
type Load interface {
15-
AddOperatorBundle(bundleObjs []*unstructured.Unstructured) error
16-
AddPackageChannels(manifest registry.PackageManifest) error
17-
AddProvidedApis() error
18-
}
1914

2015
type SQLLoader struct {
2116
db *sql.DB
2217
}
2318

24-
var _ Load = &SQLLoader{}
19+
var _ registry.Load = &SQLLoader{}
2520

2621
func NewSQLLiteLoader(outFilename string) (*SQLLoader, error) {
2722
db, err := sql.Open("sqlite3", outFilename) // TODO: ?immutable=true

‎store/query.go ‎pkg/sqlite/query.go

+24-48
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,20 @@
1-
package store
1+
package sqlite
22

33
import (
44
"context"
55
"database/sql"
66
"fmt"
77

88
_ "github.com/mattn/go-sqlite3"
9-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
9+
"github.com/operator-framework/operator-registry/pkg/registry"
1010
)
1111

12-
type ChannelEntry struct {
13-
packageName string
14-
channelName string
15-
bundleName string
16-
replaces string
17-
}
18-
19-
type Query interface {
20-
ListPackages(context context.Context) ([]string, error)
21-
GetPackage(context context.Context, name string) (*registry.PackageManifest, error)
22-
GetBundleForChannel(context context.Context, pkgName string, channelName string) (string, error)
23-
GetBundleForName(context context.Context, name string) (string, error)
24-
// Get all channel entries that say they replace this one
25-
GetChannelEntriesThatReplace(context context.Context, name string) (entries []ChannelEntry, err error)
26-
// Get the bundle in a package/channel that replace this one
27-
GetBundleThatReplaces(context context.Context, name, pkgName, channelName string) (string, error)
28-
// Get all channel entries that provide an api
29-
GetChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error)
30-
// Get latest channel entries that provide an api
31-
GetLatestChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error)
32-
// Get the the latest bundle that provides the API in a default channel
33-
GetBundleThatProvides(context context.Context, groupOrName, version, kind string) (string, error)
34-
}
3512

3613
type SQLQuerier struct {
3714
db *sql.DB
3815
}
3916

40-
var _ Query = &SQLQuerier{}
17+
var _ registry.Query = &SQLQuerier{}
4118

4219
func NewSQLLiteQuerier(dbFilename string) (*SQLQuerier, error) {
4320
db, err := sql.Open("sqlite3", "file:" + dbFilename + "?immutable=true")
@@ -145,7 +122,7 @@ func (s *SQLQuerier) GetBundleForName(context context.Context, name string) (str
145122
return bundle.String, nil
146123
}
147124

148-
func (s *SQLQuerier) GetChannelEntriesThatReplace(context context.Context, name string) (entries []ChannelEntry, err error) {
125+
func (s *SQLQuerier) GetChannelEntriesThatReplace(context context.Context, name string) (entries []registry.ChannelEntry, err error) {
149126
query := `SELECT DISTINCT channel_entry.package_name, channel_entry.channel_name, channel_entry.operatorbundle_name
150127
FROM channel_entry
151128
LEFT OUTER JOIN channel_entry replaces ON channel_entry.replaces = replaces.entry_id
@@ -155,7 +132,7 @@ func (s *SQLQuerier) GetChannelEntriesThatReplace(context context.Context, name
155132
return
156133
}
157134

158-
entries = []ChannelEntry{}
135+
entries = []registry.ChannelEntry{}
159136

160137
for rows.Next() {
161138
var pkgNameSQL sql.NullString
@@ -165,12 +142,11 @@ func (s *SQLQuerier) GetChannelEntriesThatReplace(context context.Context, name
165142
if err = rows.Scan(&pkgNameSQL, &channelNameSQL, &bundleNameSQL); err != nil {
166143
return
167144
}
168-
169-
entries = append(entries, ChannelEntry{
170-
packageName: pkgNameSQL.String,
171-
channelName: channelNameSQL.String,
172-
bundleName: bundleNameSQL.String,
173-
replaces: name,
145+
entries = append(entries, registry.ChannelEntry{
146+
PackageName: pkgNameSQL.String,
147+
ChannelName: channelNameSQL.String,
148+
BundleName: bundleNameSQL.String,
149+
Replaces: name,
174150
})
175151
}
176152
if len(entries) == 0 {
@@ -201,7 +177,7 @@ func (s *SQLQuerier) GetBundleThatReplaces(context context.Context, name, pkgNam
201177
return bundle.String, nil
202178
}
203179

204-
func (s *SQLQuerier) GetChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error) {
180+
func (s *SQLQuerier) GetChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []registry.ChannelEntry, err error) {
205181
query := `SELECT DISTINCT channel_entry.package_name, channel_entry.channel_name, channel_entry.operatorbundle_name, replaces.operatorbundle_name
206182
FROM channel_entry
207183
INNER JOIN api_provider ON channel_entry.entry_id = api_provider.channel_entry_id
@@ -213,7 +189,7 @@ func (s *SQLQuerier) GetChannelEntriesThatProvide(context context.Context, group
213189
return
214190
}
215191

216-
entries = []ChannelEntry{}
192+
entries = []registry.ChannelEntry{}
217193

218194
for rows.Next() {
219195
var pkgNameSQL sql.NullString
@@ -224,11 +200,11 @@ func (s *SQLQuerier) GetChannelEntriesThatProvide(context context.Context, group
224200
return
225201
}
226202

227-
entries = append(entries, ChannelEntry{
228-
packageName: pkgNameSQL.String,
229-
channelName: channelNameSQL.String,
230-
bundleName: bundleNameSQL.String,
231-
replaces: replacesSQL.String,
203+
entries = append(entries, registry.ChannelEntry{
204+
PackageName: pkgNameSQL.String,
205+
ChannelName: channelNameSQL.String,
206+
BundleName: bundleNameSQL.String,
207+
Replaces: replacesSQL.String,
232208
})
233209
}
234210
if len(entries) == 0 {
@@ -239,7 +215,7 @@ func (s *SQLQuerier) GetChannelEntriesThatProvide(context context.Context, group
239215
}
240216

241217
// Get latest channel entries that provide an api
242-
func (s *SQLQuerier) GetLatestChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []ChannelEntry, err error) {
218+
func (s *SQLQuerier) GetLatestChannelEntriesThatProvide(context context.Context, groupOrName, version, kind string) (entries []registry.ChannelEntry, err error) {
243219
query := `SELECT DISTINCT channel_entry.package_name, channel_entry.channel_name, channel_entry.operatorbundle_name, replaces.operatorbundle_name, MIN(channel_entry.depth)
244220
FROM channel_entry
245221
INNER JOIN api_provider ON channel_entry.entry_id = api_provider.channel_entry_id
@@ -251,7 +227,7 @@ func (s *SQLQuerier) GetLatestChannelEntriesThatProvide(context context.Context,
251227
return nil, err
252228
}
253229

254-
entries = []ChannelEntry{}
230+
entries = []registry.ChannelEntry{}
255231

256232
for rows.Next() {
257233
var pkgNameSQL sql.NullString
@@ -263,11 +239,11 @@ func (s *SQLQuerier) GetLatestChannelEntriesThatProvide(context context.Context,
263239
return nil, err
264240
}
265241

266-
entries = append(entries, ChannelEntry{
267-
packageName: pkgNameSQL.String,
268-
channelName: channelNameSQL.String,
269-
bundleName: bundleNameSQL.String,
270-
replaces: replacesSQL.String,
242+
entries = append(entries, registry.ChannelEntry{
243+
PackageName: pkgNameSQL.String,
244+
ChannelName: channelNameSQL.String,
245+
BundleName: bundleNameSQL.String,
246+
Replaces: replacesSQL.String,
271247
})
272248
}
273249
if len(entries) == 0 {

‎store/store_test.go ‎pkg/sqlite/sqlite_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package store
1+
package sqlite
22

33
import (
44
"context"
55
"os"
66
"testing"
77

88
"github.com/stretchr/testify/require"
9-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
9+
"github.com/operator-framework/operator-registry/pkg/registry"
1010
)
1111

1212
func TestLoader(t *testing.T) {
1313
store, err := NewSQLLiteLoader("test.db")
1414
require.NoError(t, err)
1515

16-
loader := NewSQLLoaderForDirectory(store, "../manifests")
16+
loader := NewSQLLoaderForDirectory(store, "../../manifests")
1717
require.NoError(t, loader.Populate())
1818
}
1919

@@ -50,21 +50,21 @@ func TestQuerier(t *testing.T) {
5050

5151
etcdChannelEntries, err := store.GetChannelEntriesThatReplace(context.TODO(), "etcdoperator.v0.9.0")
5252
require.NoError(t, err)
53-
require.ElementsMatch(t, []ChannelEntry{{"etcd", "alpha", "etcdoperator.v0.9.2", "etcdoperator.v0.9.0"}}, etcdChannelEntries)
53+
require.ElementsMatch(t, []registry.ChannelEntry{{"etcd", "alpha", "etcdoperator.v0.9.2", "etcdoperator.v0.9.0"}}, etcdChannelEntries)
5454

5555
etcdBundleByReplaces, err := store.GetBundleThatReplaces(context.TODO(), "etcdoperator.v0.9.0", "etcd", "alpha")
5656
require.NoError(t, err)
5757
require.EqualValues(t, expectedBundle, etcdBundleByReplaces)
5858

5959
etcdChannelEntriesThatProvide, err := store.GetChannelEntriesThatProvide(context.TODO(), "etcdclusters.etcd.database.coreos.com", "v1beta2", "EtcdCluster")
60-
require.ElementsMatch(t, []ChannelEntry{
60+
require.ElementsMatch(t, []registry.ChannelEntry{
6161
{"etcd", "alpha", "etcdoperator.v0.6.1", ""},
6262
{"etcd", "alpha", "etcdoperator.v0.9.0", "etcdoperator.v0.6.1"},
6363
{"etcd", "alpha", "etcdoperator.v0.9.2", "etcdoperator.v0.9.0"}}, etcdChannelEntriesThatProvide)
6464

6565
etcdLatestChannelEntriesThatProvide, err := store.GetLatestChannelEntriesThatProvide(context.TODO(), "etcdclusters.etcd.database.coreos.com", "v1beta2", "EtcdCluster")
6666
require.NoError(t, err)
67-
require.ElementsMatch(t, []ChannelEntry{{"etcd", "alpha", "etcdoperator.v0.9.2", "etcdoperator.v0.9.0"}}, etcdLatestChannelEntriesThatProvide)
67+
require.ElementsMatch(t, []registry.ChannelEntry{{"etcd", "alpha", "etcdoperator.v0.9.2", "etcdoperator.v0.9.0"}}, etcdLatestChannelEntriesThatProvide)
6868

6969
etcdBundleByProvides, err := store.GetBundleThatProvides(context.TODO(), "etcdclusters.etcd.database.coreos.com", "v1beta2", "EtcdCluster")
7070
require.NoError(t, err)

0 commit comments

Comments
 (0)
Please sign in to comment.