Skip to content

Commit

Permalink
[olm] Include script to generate olm bindata
Browse files Browse the repository at this point in the history
  • Loading branch information
varshaprasad96 committed Sep 22, 2020
1 parent ad071c1 commit d59940c
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 312 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ GIT_VERSION = $(shell git describe --dirty --tags --always)
GIT_COMMIT = $(shell git rev-parse HEAD)
K8S_VERSION = v1.18.2
GOLANGCI_LINT_VER = "1.30.0"
OLM_VERSION = "0.15.1"
REPO = github.com/operator-framework/operator-sdk
PKGS = $(shell go list ./... | grep -v /vendor/)
TEST_PKGS = $(shell go list ./... | grep -v -E 'github.com/operator-framework/operator-sdk/test/')
Expand Down Expand Up @@ -111,6 +112,9 @@ gen-cli-doc: ## Generate CLI documentation
gen-changelog: ## Generate CHANGELOG.md and migration guide updates
./hack/generate/changelog/gen-changelog.sh

gen-olm-bindata:
./hack/generate/olm_bindata.sh $(OLM_VERSION)

##############################
# Release #
##############################
Expand Down Expand Up @@ -303,3 +307,4 @@ test-e2e-helm: image-build-helm

test-integration: ## Run integration tests
./hack/tests/integration.sh

8 changes: 8 additions & 0 deletions changelog/fragments/store-olm-bindata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
entries:
- description: >
Added bindata for specific version of olm manifests. If the olm manifests for version specified in install
command are available as bindata locally, we not longer fetch them from github.
kind: "addition"
breaking: false
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.5.1
golang.org/x/tools v0.0.0-20200916150407-587cf2330ce8
golang.org/x/tools v0.0.0-20200922173257-82fe25c37531
gomodules.xyz/jsonpatch/v3 v3.0.1
helm.sh/helm/v3 v3.2.4
k8s.io/api v0.18.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b h1:AFZdJUT7jJYXQEC29hYH/WZkoV7+KhwxQGmdZ19yYoY=
golang.org/x/tools v0.0.0-20200403190813-44a64ad78b9b/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200916150407-587cf2330ce8 h1:lIJ1QeajWjHg0sYO9manyujReNonmQQ4OpYx+BtgawI=
golang.org/x/tools v0.0.0-20200916150407-587cf2330ce8/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20200922173257-82fe25c37531 h1:FS7ZiladzQ5yC5TWXke5sO9bHgSg37DItOho2WWf43U=
golang.org/x/tools v0.0.0-20200922173257-82fe25c37531/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
2 changes: 1 addition & 1 deletion hack/check-error-log-msg-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o pipefail
source "hack/lib/test_lib.sh"

echo "Checking format of error and log messages..."
allfiles=$(listFiles)
allfiles=$(listValidFilesForSanityCheck)
log_case_output=$(grep -PRn '(Error\((.*[Ee]rr|nil), |^(?!.*(fmt|errors)).+\.Error(f)?\(|Fatal(f)?\(|Info(f)?\(|Warn(f)?\()"[[:lower:]]' $allfiles | sort -u)
if [ -n "${log_case_output}" ]; then
echo -e "Log messages do not begin with upper case:\n${log_case_output}"
Expand Down
2 changes: 1 addition & 1 deletion hack/check-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -o pipefail
source "hack/lib/test_lib.sh"

echo "Checking for license header..."
allfiles=$(listFiles)
allfiles=$(listValidFilesForSanityCheck)
licRes=""
for file in $allfiles; do
if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED|Licensed)" ; then
Expand Down
35 changes: 35 additions & 0 deletions hack/generate/olm_bindata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

function get_olm_manifests() {
mkdir olm-manifests
echo "downloading olm manifests for version ${1}"
wget -O olm-manifests/olm.yaml "https://github.com/operator-framework/operator-lifecycle-manager/releases/download/${1}/olm.yaml"
wget -O olm-manifests/crds.yaml "https://github.com/operator-framework/operator-lifecycle-manager/releases/download/${1}/crds.yaml"
}

function remove_olm_manifests {
rm -rf olm-manifests
}

# check for files starting with the name "olm-bindata" inside internal/olm folder
function delete_old_olmbindata {
echo "Deleting previous versions of olm-bindata files if they exist"
find internal/bindata/olmbindata -maxdepth 1 -type f -name olm_bindata-* -exec rm {} \;
}

# TODO:
# 1. Modify this to accept multiple versions and download bindata.
# 2. Discuss on the number of olm versions of will be supported.
FILE=internal/bindata/olmbindata/"olm_bindata-"$1.go
if [ -f "$FILE" ]; then
delete_old_olmbindata
get_olm_manifests $1

go get -u github.com/go-bindata/go-bindata/...
$(go env GOPATH)/bin/go-bindata -o olm_bindata-$1.go -pkg olmbindata olm-manifests/
mv olm_bindata-$1.go internal/bindata/olmbindata

remove_olm_manifests
fi

go mod tidy
8 changes: 4 additions & 4 deletions hack/lib/test_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

source hack/lib/common.sh

function listPkgDirs() {
go list -f '{{.Dir}}' ./cmd/... ./test/... ./internal/... | grep -v generated
function listPkgDirsForSanityCheck() {
go list -f '{{.Dir}}' ./cmd/... ./test/... ./internal/... | grep -v generated | grep -v olmbindata
}

function listFiles() {
function listValidFilesForSanityCheck() {
# pipeline is much faster than for loop
listPkgDirs | xargs -I {} find {} -name '*.go' | grep -v generated
listPkgDirsForSanityCheck | xargs -I {} find {} -name '*.go' | grep -v generated | grep -v olmbindata
}

#===================================================================
Expand Down
270 changes: 270 additions & 0 deletions internal/bindata/olmbindata/olm_bindata-0.15.1.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

package olmbindata

func GetAvailableOLMVersions() map[string]struct{} {
return map[string]struct{}{
"0.15.1": struct{}{},
}
var availableVersions = map[string]struct{}{
"0.15.1": struct{}{},
}

// GetAvailableVersions returns a map of available olm versions stored as bindata.
func HasVersion(version string) bool {
_, ok := availableVersions[version]
return ok
}
42 changes: 15 additions & 27 deletions internal/olm/installer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"time"

olmapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
olmmanifests "github.com/operator-framework/operator-sdk/internal/olmbindata"
olmmanifests "github.com/operator-framework/operator-sdk/internal/bindata/olmbindata"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -42,11 +42,13 @@ import (
)

const (
olmOperatorName = "olm-operator"
catalogOperatorName = "catalog-operator"
packageServerName = "packageserver"
olmManifestBindataPath = "internal/olmbindata/olm.yaml"
crdManifestBindataPath = "internal/olmbindata/crds.yaml"
olmOperatorName = "olm-operator"
catalogOperatorName = "catalog-operator"
packageServerName = "packageserver"

// These paths are keys to look up internal OLM bindata.
olmManifestBindataPath = "olm-manifests/olm.yaml"
crdManifestBindataPath = "olm-manifests/crds.yaml"
)

type Client struct {
Expand Down Expand Up @@ -175,15 +177,16 @@ func (c Client) getResources(ctx context.Context, version string) ([]unstructure
var crdResources, olmResources []unstructured.Unstructured
var err error

if isAvailableLocally(version) {
log.Infof("Fetching crd.yaml from locally stored bindata in SDK")
crdResources, err = getPackagedManifests("crd")
// If the manifests for the requested version are saved as bindata in SDK, use
// them instead of fetching them from
if olmmanifests.HasVersion(version) {
log.Infof("Fetching crd.yaml and olm.yaml from locally stored bindata in SDK")
crdResources, err = getPackagedManifests(crdManifestBindataPath)
if err != nil {
return nil, err
}

log.Infof("Fetching olm.yaml from locally stored bindata in SDK")
olmResources, err = getPackagedManifests("olm")
olmResources, err = getPackagedManifests(olmManifestBindataPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -222,22 +225,7 @@ func (c Client) getOLM(ctx context.Context, version string) ([]unstructured.Unst
return decodeResources(resp.Body)
}

func isAvailableLocally(version string) bool {
availableManifests := olmmanifests.GetAvailableOLMVersions()
_, ok := availableManifests[version]
return ok
}

func getPackagedManifests(manifestName string) ([]unstructured.Unstructured, error) {
var manifestPath string
if manifestName == "olm" {
manifestPath = olmManifestBindataPath
} else if manifestName == "crd" {
manifestPath = crdManifestBindataPath
} else {
return nil, fmt.Errorf("unexpected manifest name %s", manifestName)
}

func getPackagedManifests(manifestPath string) ([]unstructured.Unstructured, error) {
data, err := olmmanifests.Asset(manifestPath)
if err != nil {
return nil, fmt.Errorf("error retrieving bindata asset %v", err)
Expand Down
272 changes: 0 additions & 272 deletions internal/olmbindata/olm_bindata-0.15.1.go

This file was deleted.

0 comments on commit d59940c

Please sign in to comment.