Skip to content

Commit

Permalink
* covert olm bundle structure to operator-registyr-structure
Browse files Browse the repository at this point in the history
* This commit supports case when more than one version of crd is presented in olm bundle
  • Loading branch information
annastopel committed Jun 27, 2019
1 parent f5b9ee1 commit 927f678
Show file tree
Hide file tree
Showing 206 changed files with 10,820 additions and 11,282 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#See the License for the specific language governing permissions and
#limitations under the License.

.PHONY: build build-controller build-importer build-cloner build-apiserver build-uploadproxy build-uploadserver build-operator build-functest-file-image-init build-functest-registry-image-init build-functest \
.PHONY: build build-controller build-importer build-cloner build-apiserver build-uploadproxy build-uploadserver build-operator build-functest-file-image-init build-functest-registry-image-init build-cdi-olm-catalog build-functest \
docker docker-controller docker-cloner docker-importer docker-apiserver docker-uploadproxy docker-uploadserver docker-operator docker-functest-image-init docker-functest-image-http docker-functest-registry-populate docker-functest-registry docker-functest-registry-init docker-functest-block-device \
cluster-up cluster-down cluster-sync cluster-sync-controller cluster-sync-cloner cluster-sync-importer cluster-sync-apiserver cluster-sync-uploadproxy cluster-sync-uploadserver \
olm-verify olm-push \
Expand Down Expand Up @@ -51,7 +51,7 @@ apidocs:
${DO} "./hack/update-codegen.sh && ./hack/gen-swagger-doc/gen-swagger-docs.sh v1alpha1 html"

build:
${DO} "DOCKER_REPO=${DOCKER_REPO} DOCKER_TAG=${DOCKER_TAG} VERBOSITY=${VERBOSITY} PULL_POLICY=${PULL_POLICY} QUAY_NAMESPACE=${QUAY_NAMESPACE} QUAY_REPOSITORY=${QUAY_REPOSITORY} CSV_VERSION=${CSV_VERSION} ./hack/build/build-go.sh clean && ./hack/build/build-go.sh build ${WHAT} && ./hack/build/build-cdi-func-test-file-host.sh && ./hack/build/build-cdi-func-test-registry-host.sh && ./hack/build/build-cdi-func-test-block-device.sh && ./hack/build/build-cdi-olm-catalog.sh && ./hack/build/build-copy-artifacts.sh ${WHAT}"
${DO} "DOCKER_REPO=${DOCKER_REPO} DOCKER_TAG=${DOCKER_TAG} VERBOSITY=${VERBOSITY} PULL_POLICY=${PULL_POLICY} QUAY_NAMESPACE=${QUAY_NAMESPACE} QUAY_REPOSITORY=${QUAY_REPOSITORY} CSV_VERSION=${CSV_VERSION} ./hack/build/build-go.sh clean && ./hack/build/build-go.sh build ${WHAT} && ./hack/build/build-cdi-func-test-file-host.sh && ./hack/build/build-cdi-func-test-registry-host.sh && ./hack/build/build-cdi-func-test-block-device.sh && ./hack/build/build-copy-artifacts.sh ${WHAT}"

build-controller: WHAT = cmd/cdi-controller
build-controller: build
Expand All @@ -67,6 +67,8 @@ build-cloner: WHAT = cmd/cdi-cloner
build-cloner: build
build-operator: WHAT = cmd/cdi-operator
build-operator: build
build-cdi-olm-catalog: WHAT = tools/cdi-olm-catalog
build-cdi-olm-catalog: build
build-functest-file-image-init: WHAT = tools/cdi-func-test-file-host-init
build-functest-file-image-init:
build-functest-registry-image-init: WHAT= tools/cdi-func-test-registry-init
Expand Down
14 changes: 11 additions & 3 deletions glide.lock

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

4 changes: 4 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import:
subpackages:
- pkg/appregistry
version: 1cbd326243493ff9ec589a542fa335fdc84dc3e7
- package: github.com/operator-framework/operator-lifecycle-manager
subpackages:
- pkg/api/apis/operators/v1alpha1
version: 73c00f855607f246bfb413566bff78e404eb8302
- package: github.com/gorilla/mux
version: ^1.6.1
- package: github.com/minio/minio-go
Expand Down
66 changes: 63 additions & 3 deletions hack/build/build-cdi-olm-catalog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,94 @@ OLM_CATALOG_OUT_PATH=${OUT_PATH}/${OLM_CATALOG_INIT_PATH}
OLM_MANIFESTS_SRC_PATH=${OUT_PATH}/manifests/release/olm/bundle
OLM_MANIFESTS_DIR=olm-catalog
OLM_PACKAGE=${QUAY_REPOSITORY}
OLM_TMP_CRDS=${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE}/crds

#create directory for olm catalog container
mkdir -p "${OLM_CATALOG_OUT_PATH}"


#extract CRD version supported by given CSV
function getCSVCRDVersion {
csv=$1
local crdVersion
crdVersion=$(${OLM_CATALOG_OUT_PATH}/cdi-olm-catalog --cmd get-csv-crd-version --csv-file $csv --crd-kind "CDI")
echo $crdVersion
}

#locate CRD file with the provided version
function getCRD {
crdversion=$1
crdslocation=$2

local crdfilename
crdfilename="none"
for crd in $(ls $crdslocation); do
if [[ $crd =~ "crd" ]]; then
if [[ $(${OLM_CATALOG_OUT_PATH}/cdi-olm-catalog --cmd get-crd-version --crd-file $crdslocation/$crd) == "$crdVersion" ]]; then
crdfilename=$crd
break
fi
fi
done

if [ "$crdfilename" = "none" ]; then
echo "Error: No matching CRD for version "$crdversion
exit -1
fi
echo $crdfilename
}

#copy OLM manifests of a provided csv version to a dedicated directory
function packBundle {
csv=$1
mkdir -p ${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE=}/${csv}
crds=$2

mkdir -p ${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE}/${csv}
cp ${OLM_MANIFESTS_SRC_PATH}/${csv} ${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE}/${csv}
cp ${OLM_MANIFESTS_SRC_PATH}/*crd* ${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE}/${csv}

crdVersion=$(getCSVCRDVersion "${OLM_MANIFESTS_SRC_PATH}/${csv}")
crdFile=$(getCRD $crdVersion $crds)

cp $crds/$crdFile ${OLM_CATALOG_OUT_PATH}/${OLM_MANIFESTS_DIR}/${OLM_PACKAGE}/${csv}/cdi-crds.yaml
}

#copy crds unified manifiest to temp location and split it into manifests per crd
function prepareCRDFiles {
crds=$1
cur=$(pwd)
cp ${OLM_MANIFESTS_SRC_PATH}/*crds* $crds
cd $crds
csplit --digits=3 --quiet --elide-empty-files --prefix=cdi-crd --suffix-format=%03d.yaml $crds/cdi-crds.yaml "/--/" "{*}"
rm -rf $crds/cdi-crds.yaml
cd $cur

}

#iterate over all OLM bundles and for each one build a dedicated directory
function packBundles {

csvs=$1
crds=${OLM_TMP_CRDS}

mkdir -p $crds
prepareCRDFiles $crds

for csv in $(ls -1 $csvs); do
if [[ $csv =~ "csv" ]]; then
echo "pack bundle for CSV "${csv}
packBundle $csv
packBundle $csv $crds
fi
done

#cleanup temp crds directory
rm -rf $crds
}

${BUILD_DIR}/build-copy-artifacts.sh "${OLM_CATALOG_INIT_PATH}"

#copy Dockerfile
cp ${BUILD_DIR}/docker/${CDI_OLM_CATALOG}/* ${OLM_CATALOG_OUT_PATH}/
cp "${OLM_CATALOG_INIT_PATH}"/* ${OLM_CATALOG_OUT_PATH}/

#Build directory structure expected by olm-catalog-registry
# olm-catalog---
Expand Down
222 changes: 222 additions & 0 deletions tools/cdi-olm-catalog/olm-csv-tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"strings"

"gopkg.in/yaml.v2"

olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
extv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/klog"
)

//CSVAnnotations - CSVAnnotations of OLM CSV manifest
type CSVAnnotations struct {
// AlmExamples is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
AlmExamples map[string]string `json:"alm-examples,omitempty" protobuf:"bytes,12,rep,name=annotations"`
//Capabilities
Capabilites string `json:"capabilities,omitempty" protobuf:"bytes,1,opt,name=capabilities"`
//Categories
Categories string `json:"categories,omitempty" protobuf:"bytes,1,opt,name=categories"`
//Description
Description string `json:"description,omitempty" protobuf:"bytes,1,opt,name=description"`
}

//CSVMetadata - CSVMetadata of OLM CSV manifest
type CSVMetadata struct {
// CSVAnnnotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
CSVAnnnotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`

// Name must be unique within a namespace. Is required when creating resources, although
// some resources may allow a client to request the generation of an appropriate name
// automatically. Name is primarily intended for creation idempotence and configuration
// definition.
// Cannot be updated.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
// +optional
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`

// Namespace defines the space within each name must be unique. An empty namespace is
// equivalent to the "default" namespace, but "default" is the canonical representation.
// Not all objects are required to be scoped to a namespace - the value of this field for
// those objects will be empty.
//
// Must be a DNS_LABEL.
// Cannot be updated.
// More info: http://kubernetes.io/docs/user-guide/namespaces
// +optional

Namespace string `json:"namespace,omitempty" protobuf:"bytes,3,opt,name=namespace"`
}

// ClusterServiceVersionSpec declarations tell OLM how to install an operator
// that can manage apps for a given version.
type ClusterServiceVersionSpec struct {
InstallStrategy olm.NamedInstallStrategy `json:"install"`
Version string `json:"version,omitempty"`
Maturity string `json:"maturity,omitempty"`
CustomResourceDefinitions olm.CustomResourceDefinitions `json:"customresourcedefinitions,omitempty"`
APIServiceDefinitions olm.APIServiceDefinitions `json:"apiservicedefinitions,omitempty"`
NativeAPIs []metav1.GroupVersionKind `json:"nativeAPIs,omitempty"`
MinKubeVersion string `json:"minKubeVersion,omitempty"`
DisplayName string `json:"displayName"`
Description string `json:"description,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Maintainers []olm.Maintainer `json:"maintainers,omitempty"`
Provider olm.AppLink `json:"provider,omitempty"`
Links []olm.AppLink `json:"links,omitempty"`
Icon []olm.Icon `json:"icon,omitempty"`

// InstallModes specify supported installation types
// +optional
InstallModes []olm.InstallMode `json:"installModes,omitempty"`

// The name of a CSV this one replaces. Should match the `metadata.Name` field of the old CSV.
// +optional
Replaces string `json:"replaces,omitempty"`

// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects.
// +optional
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`

// Annotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata.
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`

// Label selector for related resources.
// +optional
Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,2,opt,name=selector"`
}

//CSVManifest - struct that represents CSV manifest
type CSVManifest struct {
APIVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty"`
CSVMetadat CSVMetadata `json:"metadata,omitempty"`
Spec ClusterServiceVersionSpec `json:"spec,omitempty"`
}

//CustomResourceDefinition - custome resouirce definition
type CustomResourceDefinition struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

// Spec describes how the user wants the resources to appear
Spec extv1beta1.CustomResourceDefinitionSpec `json:"spec" protobuf:"bytes,2,opt,name=spec"`
}

func getCSVCRDVersion(csvFile, crdKind string) (string, error) {
var crdVersion string
var csvStruct CSVManifest

yamlFile, err := ioutil.ReadFile(csvFile)
if err != nil {
return "", errors.New("Failed to read csv struct " + csvFile)
}

err = yaml.Unmarshal(yamlFile, &csvStruct)
if err != nil {
return "", err
}

crds := csvStruct.Spec.CustomResourceDefinitions.Owned
for _, crd := range crds {
if strings.Compare(crd.Kind, crdKind) == 0 {
return crd.Version, nil
} //find crd
} //for iterate over crds of csv

return crdVersion, errors.New("No crd " + crdKind + " defined in csv " + csvFile)
}

func getCRDVersion(crdFile string) (string, error) {
var crdStruct CustomResourceDefinition

yamlFile, err := ioutil.ReadFile(crdFile)
if err != nil {
return "", errors.New("Failed to read crd manifest " + crdFile)
}

err = yaml.Unmarshal(yamlFile, &crdStruct)
if err != nil {
return "", err
}

return crdStruct.Spec.Version, nil
}

const (
help string = "h"
getCrdVersionCmd string = "get-crd-version"
getCsvVersionCmd string = "get-csv-crd-version"
getCsvVersionCmdHelp string = "olm-csv-tool --cmd get-csv-crd-version --csv-file --crd-kind"
getCrdVersionCmdHelp string = "olm-csv-tool --cmd get-crd-version --crd-file "
helpInfo string = "\nUsage:\n" + getCrdVersionCmdHelp + "\n" + getCsvVersionCmdHelp
)

func main() {

cmd := flag.String("cmd", "h", "command")
csvFile := flag.String("csv-file", "", "csv manifest")
crdFile := flag.String("crd-file", "", "crd manifest")
crdKind := flag.String("crd-kind", "CDI", "name of crd")
flag.Parse()

switch *cmd {
case getCrdVersionCmd:
if *crdFile == "" {
klog.Errorf(getCrdVersionCmdHelp)
klog.Error("No csv manifest provided on command %v", getCsvVersionCmd)
panic(nil)
}

crdVersion, err := getCRDVersion(*crdFile)
if err != nil {
panic(err)
}
fmt.Println(crdVersion)

break
case getCsvVersionCmd:
if *csvFile == "" {
klog.Errorf(getCsvVersionCmdHelp)
klog.Error("No csv manifest provided on command %v", getCsvVersionCmd)
panic(nil)
}
if *crdKind == "" {
klog.Errorf(getCsvVersionCmdHelp)
klog.Error("No crd-kind provided on command %v", getCsvVersionCmd)
panic(nil)
}

crdVersion, err := getCSVCRDVersion(*csvFile, *crdKind)
if err != nil {
panic(err)
}

fmt.Println(crdVersion)

break
case help:
fmt.Println(helpInfo)
break
default:
panic("Invalid command " + *cmd + helpInfo)
}
}
3 changes: 2 additions & 1 deletion vendor/github.com/appscode/jsonpatch/.gitignore

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

0 comments on commit 927f678

Please sign in to comment.