Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(configmap): add configmap loader #3

Merged
merged 3 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
*.db
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Ignore binaries
bin

# Ignore sqlite
*.db
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
FROM golang:1.10-alpine
FROM golang:1.10-alpine as builder

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

COPY vendor vendor
COPY cmd cmd
COPY pkg pkg
RUN go build --tags json1 -o ./initializer ./cmd/init/...
COPY Makefile Makefile
RUN make build

COPY manifests manifests
RUN ./initializer
RUN ./bin/initializer -o ./bundles.db

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

9 changes: 4 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# go-tests = true
# unused-packages = true

[[constraint]]
name = "github.com/operator-framework/operator-lifecycle-manager"
branch = "master"

[[constraint]]
name = "github.com/mattn/go-sqlite3"
Expand Down
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: build test vendor

all: test build

test:
go test --tags json1 -v -race ./pkg/...

build:
go build --tags json1 -o ./bin/initializer ./cmd/init/...

image:
docker build .

vendor:
dep ensure -v
7,261 changes: 7,261 additions & 0 deletions configmap.example.yaml

Large diffs are not rendered by default.

Binary file removed initializer
Binary file not shown.
176 changes: 176 additions & 0 deletions pkg/registry/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package registry

import (
"fmt"
"strings"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
)

// Scheme is the default instance of runtime.Scheme to which types in the Kubernetes API are already registered.
var Scheme = runtime.NewScheme()

// Codecs provides access to encoding and decoding for the scheme
var Codecs = serializer.NewCodecFactory(Scheme)

func DefaultYAMLDecoder() runtime.Decoder {
return Codecs.UniversalDeserializer()
}

func init() {
if err := v1alpha1.AddToScheme(Scheme); err != nil {
panic(err)
}

if err := v1beta1.AddToScheme(Scheme); err != nil {
panic(err)
}
}

type Bundle struct {
objs []*unstructured.Unstructured
csv *v1alpha1.ClusterServiceVersion
crds []*apiextensions.CustomResourceDefinition
cacheStale bool
}

func NewBundle(objs ...*unstructured.Unstructured) *Bundle {
bundle := &Bundle{cacheStale:false}
for _, o := range objs {
bundle.Add(o)
}
return bundle
}

func (b *Bundle) Size() int {
return len(b.objs)
}

func (b *Bundle) Add(obj *unstructured.Unstructured) {
b.objs = append(b.objs, obj)
b.cacheStale = true
}

func (b *Bundle) ClusterServiceVersion() (*v1alpha1.ClusterServiceVersion, error) {
if err := b.cache(); err!=nil {
return nil, err
}
return b.csv, nil
}

func (b *Bundle) CustomResourceDefinitions() ([]*apiextensions.CustomResourceDefinition, error) {
if err := b.cache(); err!=nil {
return nil, err
}
return b.crds, nil
}

func (b *Bundle) ProvidedAPIs() (map[APIKey]struct{}, error) {
provided := map[APIKey]struct{}{}
crds, err := b.CustomResourceDefinitions()
if err != nil {
return nil, err
}
for _, crd := range crds {
for _, v := range crd.Spec.Versions {
provided[APIKey{Group: crd.Spec.Group, Version: v.Name, Kind: crd.Spec.Names.Kind}] = struct{}{}
}
if crd.Spec.Version != "" {
provided[APIKey{Group: crd.Spec.Group, Version: crd.Spec.Version, Kind: crd.Spec.Names.Kind}] = struct{}{}
}
}

csv, err := b.ClusterServiceVersion()
if err != nil {
return nil, err
}
for _, api := range csv.Spec.APIServiceDefinitions.Owned {
provided[APIKey{Group: api.Group, Version: api.Version, Kind: api.Kind}] = struct{}{}
}
return provided, nil
}

func (b *Bundle) AllProvidedAPIsInBundle() error {
csv, err := b.ClusterServiceVersion()
if err != nil {
return err
}
bundleAPIs, err := b.ProvidedAPIs()
if err != nil {
return err
}
shouldExist := make(map[APIKey]struct{}, len(csv.Spec.CustomResourceDefinitions.Owned))
for _, crdDef := range csv.Spec.CustomResourceDefinitions.Owned {
parts := strings.SplitAfterN(crdDef.Name, ".", 2)
shouldExist[APIKey{parts[1], crdDef.Version, crdDef.Kind}] = struct{}{}
}
for key := range shouldExist {
if _, ok := bundleAPIs[key]; !ok {
return fmt.Errorf("couldn't find %v in bundle. found: %v", key, bundleAPIs)
}
}
// note: don't need to check bundle for extension apiserver types, which don't require extra bundle entries
return nil
}

func (b *Bundle) Serialize() (csvName string, csvBytes []byte, bundleBytes []byte, err error) {
csvCount := 0
for _, obj := range b.objs {
objBytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return "", nil, nil, err
}
bundleBytes = append(bundleBytes, objBytes...)

if obj.GetObjectKind().GroupVersionKind().Kind == "ClusterServiceVersion" {
csvName = obj.GetName()
csvBytes, err = runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
if err != nil {
return "", nil, nil, err
}
csvCount += 1
if csvCount > 1 {
return "", nil, nil, fmt.Errorf("two csvs found in one bundle")
}
}
}

return csvName, csvBytes, bundleBytes, nil
}

func (b *Bundle) cache() error {
if !b.cacheStale {
return nil
}
for _, o := range b.objs {
if o.GetObjectKind().GroupVersionKind().Kind == "ClusterServiceVersion" {
csv := &v1alpha1.ClusterServiceVersion{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.UnstructuredContent(), csv); err != nil {
return err
}
b.csv = csv
break
}
}

if b.crds == nil {
b.crds = []*apiextensions.CustomResourceDefinition{}
}
for _, o := range b.objs {
if o.GetObjectKind().GroupVersionKind().Kind == "CustomResourceDefinition" {
crd := &apiextensions.CustomResourceDefinition{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(o.UnstructuredContent(), crd); err != nil {
return err
}
b.crds = append(b.crds, crd)
}
}

b.cacheStale = false
return nil
}
4 changes: 1 addition & 3 deletions pkg/registry/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package registry

import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type Load interface {
AddOperatorBundle(bundleObjs []*unstructured.Unstructured) error
AddOperatorBundle(bundle *Bundle) error
AddPackageChannels(manifest PackageManifest) error
AddProvidedApis() error
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/registry/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package registry

// APIKey stores GroupVersionKind for use as map keys
type APIKey struct {
Group string
Version string
Kind string
}

// PackageManifest holds information about a package, which is a reference to one (or more)
// channels under a single package.
type PackageManifest struct {
Expand Down Expand Up @@ -47,6 +54,6 @@ func (pc PackageChannel) IsDefaultChannel(pm PackageManifest) bool {
type ChannelEntry struct {
PackageName string
ChannelName string
BundleName string
Replaces string
BundleName string
Replaces string
}
Loading