Skip to content

Commit

Permalink
Ensure old CSVs are included in redhat bundle manifest or else upgrad…
Browse files Browse the repository at this point in the history
…ing is not possible
  • Loading branch information
Will James committed Sep 9, 2020
1 parent 50425ed commit ff45890
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 10 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7.0
2 changes: 1 addition & 1 deletion olm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apk add py3-pip

RUN pip3 install --upgrade pip

RUN pip3 install pyyaml operator-courier
RUN pip3 install pyyaml operator-courier requests semver

COPY olm/ olm/

Expand Down
2 changes: 1 addition & 1 deletion olm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ To create the templates, run

./create-artifacts.sh

This requires a couple of prerequisites (`jsonnet`, `python3`, `pyyaml`, and `operator-courier`), which are packaged in the Dockerfile for running in CI.
This requires a couple of prerequisites (`jsonnet`, `python3`, `pyyaml`, `operator-courier`, `semver` and `requests`), which are packaged in the Dockerfile for running in CI.

### TODO

Expand Down
8 changes: 5 additions & 3 deletions olm/create-artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
CRD_DESCRIPTORS=$(${SCRIPTPATH}/yaml_to_json < "$SCRIPTPATH/CRD.descriptors.yaml")
EXAMPLES=$(${SCRIPTPATH}/yaml_to_json < "$SCRIPTPATH/../deploy/instana-agent.customresource.yaml")

mkdir -p ${MANIFEST_DIR}

REPLACES=$(${SCRIPTPATH}/versioning/collect_csvs.py --outdir $MANIFEST_DIR --source $MANIFEST_NAME)

# Generate versioned operator artifacts if they do not exist
if [[ ! -f "$OPERATOR_RESOURCES_DIR/$VERSION/instana-agent-operator.yaml" ]] ; then
${SCRIPTPATH}/operator-resources/create-operator-artifacts.sh ${VERSION}
fi

RESOURCES=$(${SCRIPTPATH}/yaml_to_json < "$OPERATOR_RESOURCES_DIR/$VERSION/instana-agent-operator.yaml")

mkdir -p ${MANIFEST_DIR}

jsonnet \
--ext-str crd_descriptors="$CRD_DESCRIPTORS" \
--ext-str-file description=${SCRIPTPATH}/description.md \
--ext-str examples="$EXAMPLES" \
--ext-str-file image=${SCRIPTPATH}/image.svg \
--ext-str isoDate=${DATE} \
--ext-str registry=${REGISTRY} \
--ext-str prevPackage="$PREV_PACKAGE" \
--ext-str replaces="$REPLACES" \
--ext-str redhat="$REDHAT" \
--ext-str resources="$RESOURCES" \
--ext-str version=${VERSION} \
Expand Down
5 changes: 2 additions & 3 deletions olm/template.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ local description = std.extVar('description');
local examples = std.extVar('examples');
local image = std.extVar('image');
local isoDate = std.extVar('isoDate');
local prevPackage = std.parseJson(std.extVar('prevPackage'))[0];
local redhat = std.parseJson(std.extVar('redhat'));
local registry = std.extVar('registry');
local replaces = std.extVar('replaces');
local resources = std.parseJson(std.extVar('resources'));
local version = std.extVar('version');

local crdVersion = "v1beta1";
local imagePrefix = if std.length(registry) > 0 then registry + "/" else "";
local isClusterRole(res) = res.kind == "ClusterRole";
local rules = std.filter(isClusterRole, resources)[0].rules;
local prevVersion = std.split(prevPackage.channels[0].currentCSV, 'v')[1];
local isDeployment(res) = res.kind == "Deployment";
local mapDeployment(dep) = {
name: dep.metadata.name,
Expand Down Expand Up @@ -81,7 +80,7 @@ local crdWithDescriptors = {
}
],
"version": version,
"replaces": "instana-agent-operator.v" + prevVersion,
"replaces": replaces,
"minKubeVersion": "1.11.0",
"provider": {
"name": "Instana"
Expand Down
57 changes: 57 additions & 0 deletions olm/versioning/collect_csvs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

import requests, io, tarfile, yaml, semver, argparse

parser = argparse.ArgumentParser(description='Fetch old CSVs for the operator, dump csvs to outdir and print latest version')
parser.add_argument('--outdir', default='target', help='target directory for the old CSVs')
parser.add_argument('--source', default='redhat', help='if "olm", get the latest upstream community operators version and print it')
args = parser.parse_args()

if args.source == 'olm':
package_raw = requests.get('https://raw.githubusercontent.com/operator-framework/community-operators/master/upstream-community-operators/instana-agent/instana-agent.package.yaml')
package = yaml.load(package_raw.content, Loader=yaml.SafeLoader)
print(package['channels'][0]['currentCSV'])
exit(0)

if args.source != 'redhat':
print('unrecognized source "%s"' % args.source)
exit(1)

bundles = requests.get('https://quay.io/cnr/api/v1/packages/certified-operators/instana-agent/').json()

digests = [bundle['content']['digest'] for bundle in bundles]

csvs_by_version = {}

for digest in digests:
response = requests.get('https://quay.io/cnr/api/v1/packages/certified-operators/instana-agent/blobs/sha256/%s' % digest, stream=True)

tar = tarfile.open(fileobj=io.BytesIO(response.content), mode='r')

bundle_member = [member for member in tar.getmembers() if 'bundle.yaml' in member.name][0]

bundle = tar.extractfile(bundle_member)

data = yaml.load(bundle, Loader=yaml.SafeLoader)

csv_bundles = yaml.load_all(data['data']['clusterServiceVersions'], Loader=yaml.SafeLoader)

for csv_bundle in csv_bundles:
for csv in csv_bundle:
csvs_by_version[csv['spec']['version']] = csv


ordered_csvs = sorted(csvs_by_version.values(), key=lambda csv: semver.VersionInfo.parse(csv['spec']['version']))

prior = ''
for csv in ordered_csvs:
if (csv['spec']['maturity'] == 'alpha'):
continue
name = csv['metadata']['name']
if prior:
csv['spec']['replaces'] = prior
prior = name
with open('%s/%s.yaml' % (args.outdir, name), 'w') as f:
yaml.safe_dump(csv, f, default_flow_style=False)

print(ordered_csvs[len(ordered_csvs) - 1]['metadata']['name'])
8 changes: 6 additions & 2 deletions olm/yaml_to_json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3

import sys, yaml, json
import sys, yaml, json, datetime

json.dump(list(yaml.load_all(sys.stdin, Loader=yaml.SafeLoader)), sys.stdout, indent=4)
def DateEncoder(obj):
if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()

json.dump(list(yaml.load_all(sys.stdin, Loader=yaml.SafeLoader)), sys.stdout, indent=4, default=DateEncoder)

0 comments on commit ff45890

Please sign in to comment.