Skip to content

Commit 7a09b7c

Browse files
authored
Merge pull request #3 from ecordell/configmap
feat(configmap): add configmap loader
2 parents 06f1c06 + 2272df9 commit 7a09b7c

File tree

26 files changed

+7954
-369
lines changed

26 files changed

+7954
-369
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
*.db

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# Ignore binaries
15+
bin
16+
17+
# Ignore sqlite
18+
*.db

Dockerfile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
FROM golang:1.10-alpine
1+
FROM golang:1.10-alpine as builder
22

33
RUN apk update && apk add sqlite build-base
44
WORKDIR /go/src/github.com/operator-framework/operator-registry
55

66
COPY vendor vendor
77
COPY cmd cmd
88
COPY pkg pkg
9-
RUN go build --tags json1 -o ./initializer ./cmd/init/...
9+
COPY Makefile Makefile
10+
RUN make build
1011

1112
COPY manifests manifests
12-
RUN ./initializer
13+
RUN ./bin/initializer -o ./bundles.db
1314

15+
FROM scratch
16+
COPY --from=builder /go/src/github.com/operator-framework/operator-registry/bundles.db /bundles.db
1417

Gopkg.lock

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

Gopkg.toml

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
# go-tests = true
2525
# unused-packages = true
2626

27+
[[constraint]]
28+
name = "github.com/operator-framework/operator-lifecycle-manager"
29+
branch = "master"
2730

2831
[[constraint]]
2932
name = "github.com/mattn/go-sqlite3"

Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.PHONY: build test vendor
2+
3+
all: test build
4+
5+
test:
6+
go test --tags json1 -v -race ./pkg/...
7+
8+
build:
9+
go build --tags json1 -o ./bin/initializer ./cmd/init/...
10+
11+
image:
12+
docker build .
13+
14+
vendor:
15+
dep ensure -v

configmap.example.yaml

+7,261
Large diffs are not rendered by default.

initializer

-35.6 MB
Binary file not shown.

pkg/registry/bundle.go

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package registry
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
8+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
9+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
10+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11+
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/runtime/serializer"
13+
)
14+
15+
// Scheme is the default instance of runtime.Scheme to which types in the Kubernetes API are already registered.
16+
var Scheme = runtime.NewScheme()
17+
18+
// Codecs provides access to encoding and decoding for the scheme
19+
var Codecs = serializer.NewCodecFactory(Scheme)
20+
21+
func DefaultYAMLDecoder() runtime.Decoder {
22+
return Codecs.UniversalDeserializer()
23+
}
24+
25+
func init() {
26+
if err := v1alpha1.AddToScheme(Scheme); err != nil {
27+
panic(err)
28+
}
29+
30+
if err := v1beta1.AddToScheme(Scheme); err != nil {
31+
panic(err)
32+
}
33+
}
34+
35+
type Bundle struct {
36+
objs []*unstructured.Unstructured
37+
csv *v1alpha1.ClusterServiceVersion
38+
crds []*apiextensions.CustomResourceDefinition
39+
cacheStale bool
40+
}
41+
42+
func NewBundle(objs ...*unstructured.Unstructured) *Bundle {
43+
bundle := &Bundle{cacheStale:false}
44+
for _, o := range objs {
45+
bundle.Add(o)
46+
}
47+
return bundle
48+
}
49+
50+
func (b *Bundle) Size() int {
51+
return len(b.objs)
52+
}
53+
54+
func (b *Bundle) Add(obj *unstructured.Unstructured) {
55+
b.objs = append(b.objs, obj)
56+
b.cacheStale = true
57+
}
58+
59+
func (b *Bundle) ClusterServiceVersion() (*v1alpha1.ClusterServiceVersion, error) {
60+
if err := b.cache(); err!=nil {
61+
return nil, err
62+
}
63+
return b.csv, nil
64+
}
65+
66+
func (b *Bundle) CustomResourceDefinitions() ([]*apiextensions.CustomResourceDefinition, error) {
67+
if err := b.cache(); err!=nil {
68+
return nil, err
69+
}
70+
return b.crds, nil
71+
}
72+
73+
func (b *Bundle) ProvidedAPIs() (map[APIKey]struct{}, error) {
74+
provided := map[APIKey]struct{}{}
75+
crds, err := b.CustomResourceDefinitions()
76+
if err != nil {
77+
return nil, err
78+
}
79+
for _, crd := range crds {
80+
for _, v := range crd.Spec.Versions {
81+
provided[APIKey{Group: crd.Spec.Group, Version: v.Name, Kind: crd.Spec.Names.Kind}] = struct{}{}
82+
}
83+
if crd.Spec.Version != "" {
84+
provided[APIKey{Group: crd.Spec.Group, Version: crd.Spec.Version, Kind: crd.Spec.Names.Kind}] = struct{}{}
85+
}
86+
}
87+
88+
csv, err := b.ClusterServiceVersion()
89+
if err != nil {
90+
return nil, err
91+
}
92+
for _, api := range csv.Spec.APIServiceDefinitions.Owned {
93+
provided[APIKey{Group: api.Group, Version: api.Version, Kind: api.Kind}] = struct{}{}
94+
}
95+
return provided, nil
96+
}
97+
98+
func (b *Bundle) AllProvidedAPIsInBundle() error {
99+
csv, err := b.ClusterServiceVersion()
100+
if err != nil {
101+
return err
102+
}
103+
bundleAPIs, err := b.ProvidedAPIs()
104+
if err != nil {
105+
return err
106+
}
107+
shouldExist := make(map[APIKey]struct{}, len(csv.Spec.CustomResourceDefinitions.Owned))
108+
for _, crdDef := range csv.Spec.CustomResourceDefinitions.Owned {
109+
parts := strings.SplitAfterN(crdDef.Name, ".", 2)
110+
shouldExist[APIKey{parts[1], crdDef.Version, crdDef.Kind}] = struct{}{}
111+
}
112+
for key := range shouldExist {
113+
if _, ok := bundleAPIs[key]; !ok {
114+
return fmt.Errorf("couldn't find %v in bundle. found: %v", key, bundleAPIs)
115+
}
116+
}
117+
// note: don't need to check bundle for extension apiserver types, which don't require extra bundle entries
118+
return nil
119+
}
120+
121+
func (b *Bundle) Serialize() (csvName string, csvBytes []byte, bundleBytes []byte, err error) {
122+
csvCount := 0
123+
for _, obj := range b.objs {
124+
objBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
125+
if err != nil {
126+
return "", nil, nil, err
127+
}
128+
bundleBytes = append(bundleBytes, objBytes...)
129+
130+
if obj.GetObjectKind().GroupVersionKind().Kind == "ClusterServiceVersion" {
131+
csvName = obj.GetName()
132+
csvBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
133+
if err != nil {
134+
return "", nil, nil, err
135+
}
136+
csvCount += 1
137+
if csvCount > 1 {
138+
return "", nil, nil, fmt.Errorf("two csvs found in one bundle")
139+
}
140+
}
141+
}
142+
143+
return csvName, csvBytes, bundleBytes, nil
144+
}
145+
146+
func (b *Bundle) cache() error {
147+
if !b.cacheStale {
148+
return nil
149+
}
150+
for _, o := range b.objs {
151+
if o.GetObjectKind().GroupVersionKind().Kind == "ClusterServiceVersion" {
152+
csv := &v1alpha1.ClusterServiceVersion{}
153+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.UnstructuredContent(), csv); err != nil {
154+
return err
155+
}
156+
b.csv = csv
157+
break
158+
}
159+
}
160+
161+
if b.crds == nil {
162+
b.crds = []*apiextensions.CustomResourceDefinition{}
163+
}
164+
for _, o := range b.objs {
165+
if o.GetObjectKind().GroupVersionKind().Kind == "CustomResourceDefinition" {
166+
crd := &apiextensions.CustomResourceDefinition{}
167+
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.UnstructuredContent(), crd); err != nil {
168+
return err
169+
}
170+
b.crds = append(b.crds, crd)
171+
}
172+
}
173+
174+
b.cacheStale = false
175+
return nil
176+
}

pkg/registry/interface.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package registry
22

33
import (
44
"context"
5-
6-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
75
)
86

97
type Load interface {
10-
AddOperatorBundle(bundleObjs []*unstructured.Unstructured) error
8+
AddOperatorBundle(bundle *Bundle) error
119
AddPackageChannels(manifest PackageManifest) error
1210
AddProvidedApis() error
1311
}

pkg/registry/types.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package registry
22

3+
// APIKey stores GroupVersionKind for use as map keys
4+
type APIKey struct {
5+
Group string
6+
Version string
7+
Kind string
8+
}
9+
310
// PackageManifest holds information about a package, which is a reference to one (or more)
411
// channels under a single package.
512
type PackageManifest struct {
@@ -47,6 +54,6 @@ func (pc PackageChannel) IsDefaultChannel(pm PackageManifest) bool {
4754
type ChannelEntry struct {
4855
PackageName string
4956
ChannelName string
50-
BundleName string
51-
Replaces string
57+
BundleName string
58+
Replaces string
5259
}

0 commit comments

Comments
 (0)