Skip to content

Commit

Permalink
Merge pull request #264 from mgoerens/get_ocp_range
Browse files Browse the repository at this point in the history
Get OCP range from external library
  • Loading branch information
komish authored Oct 17, 2023
2 parents f6b8d55 + 37e6b8e commit be673fd
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 192 deletions.
35 changes: 35 additions & 0 deletions .github/actions/get-ocp-range/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: 'Get OCP range'
description: 'Get the range of OCP versions corresponding to the provided range of Kubernetes versions'
inputs:
kube-version-range:
description: 'Range of Kubernetes versions'
required: true
outputs:
ocp-version-range:
description: "Corresponsing range of OCP versions"
value: ${{ steps.run-get-ocp-range.outputs.ocp-version-range }}
runs:
using: "composite"
steps:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '>=1.20'

- name: Install get-ocp-range
shell: bash
run: go install github.com/opdev/getocprange/cmd/get-ocp-range@latest

- name: Run get-ocp-range
id: run-get-ocp-range
shell: bash
run: |
echo "::debug::Received kubeVersionRange to translate '${{ inputs.kube-version-range }}'"
OCP_VERSION_RANGE=$(get-ocp-range '${{ inputs.kube-version-range }}')
echo "ocp-version-range=$OCP_VERSION_RANGE" >> $GITHUB_OUTPUT
echo "::debug::Successfully translated kubeVersionRange to OCPVersionRange $OCP_VERSION_RANGE"
- name: Display error message if get-ocp-range failed
if: ${{ failure() && steps.run-get-ocp-range.outcome == 'failure' }}
shell: bash
run: echo "::error file=.github/actions/get-ocp-range/action.yaml::Error running get-ocp-range"
42 changes: 40 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,35 @@ jobs:
error_message="The chart verifier returned an error when trying to obtain a verification report for the chart."
echo "verifier_error_message=$error_message" >> $GITHUB_OUTPUT
- name: Get profile version set in report provided by the user
id: get-profile-version
if: ${{ needs.setup.outputs.run_build == 'true' && steps.verify_requires.outputs.report_provided == 'true' }}
uses: mikefarah/yq@v4.35.1
with:
cmd: yq '.metadata.tool.profile.version' ${{ format('./pr-branch/{0}', steps.verify_requires.outputs.provided_report_relative_path) }}

- name: Get the range of Kubernetes versions set in the report provided by the user
id: get-kube-range
if: ${{ needs.setup.outputs.run_build == 'true' && steps.verify_requires.outputs.report_provided == 'true' }}
continue-on-error: true
uses: mikefarah/yq@v4.35.1
with:
cmd: yq '.metadata.chart.kubeversion' ${{ format('./pr-branch/{0}', steps.verify_requires.outputs.provided_report_relative_path) }}

- name: Get the corresponding range of OCP versions
id: get-ocp-range
if: ${{ needs.setup.outputs.run_build == 'true' && steps.verify_requires.outputs.report_provided == 'true' }}
continue-on-error: true
uses: ./.github/actions/get-ocp-range
with:
kube-version-range: ${{ steps.get-kube-range.outputs.result }}

- name: Only ignore errors in get-ocp-range for profile in version v1.0
if: ${{ (steps.get-kube-range.outcome == 'failure' || steps.get-ocp-range.outcome == 'failure') && steps.get-profile-version.outputs.result != 'v1.0' }}
run: |
echo "::error file=.github/workflows/build.yaml::Failure in get-ocp-range, mandatory for profile version ${{ steps.get-profile-version.outputs.result }}"
exit 1
- name: Check Report
id: check_report
if: ${{ needs.setup.outputs.run_build == 'true' }}
Expand All @@ -272,9 +301,13 @@ jobs:
GENERATED_REPORT_PATH: ${{ steps.run-verifier.outputs.report_file }}
REPORT_SUMMARY_PATH: ${{ steps.run-verifier.outputs.report_info_file }}
WORKFLOW_WORKING_DIRECTORY: "../pr"
OCP_VERSION_RANGE: ${{ steps.get-ocp-range.outputs.ocp-version-range }}
run: |
cd pr-branch
../ve1/bin/chart-pr-review --directory=../pr --verify-user=${{ github.event.pull_request.user.login }} --api-url=${{ github.event.pull_request._links.self.href }}
../ve1/bin/chart-pr-review \
--directory=../pr \
--verify-user=${{ github.event.pull_request.user.login }} \
--api-url=${{ github.event.pull_request._links.self.href }}
cd ..
- name: Delete Namespace
Expand Down Expand Up @@ -427,14 +460,19 @@ jobs:
CHART_ENTRY_NAME: ${{ needs.chart-verifier.outputs.chart_entry_name }}
REDHAT_TO_COMMUNITY: ${{ needs.chart-verifier.outputs.redhat_to_community }}
WEB_CATALOG_ONLY: ${{ needs.chart-verifier.outputs.web_catalog_only }}
OCP_VERSION_RANGE: ${{ steps.get-ocp-range.outputs.ocp-version-range }}
id: release-charts
run: |
tar zxvf ./scripts/dependencies/helm-chart-releaser/chart-releaser_1.2.0_linux_amd64.tar.gz
sudo cp -f cr /usr/local/bin/cr
INDEX_BRANCH=$(if [ "${GITHUB_REF}" = "refs/heads/main" ]; then echo "refs/heads/gh-pages"; else echo "${GITHUB_REF}-gh-pages"; fi)
CWD=`pwd`
cd pr-branch
../ve1/bin/chart-repo-manager --repository=${{ github.repository }} --index-branch=${INDEX_BRANCH} --api-url=${{ github.event.pull_request._links.self.href }} --pr-number=${{ github.event.number }}
../ve1/bin/chart-repo-manager \
--repository=${{ github.repository }} \
--index-branch=${INDEX_BRANCH} \
--api-url=${{ github.event.pull_request._links.self.href }} \
--pr-number=${{ github.event.number }}
cd ${CWD}
- name: Release
Expand Down
5 changes: 4 additions & 1 deletion scripts/src/chartprreview/chartprreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,10 @@ def main():
"charts", category, organization, chart, version, "report.yaml"
)
if os.path.exists(submitted_report_path):
report_valid, message = verifier_report.validate(submitted_report_path)
ocp_version_range = os.environ.get("OCP_VERSION_RANGE")
report_valid, message = verifier_report.validate(
submitted_report_path, ocp_version_range
)
if not report_valid:
msg = f"Submitted report is not valid: {message}"
print(f"[ERROR] {msg}")
Expand Down
29 changes: 20 additions & 9 deletions scripts/src/chartrepomanager/chartrepomanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,13 @@ def create_index_from_chart(
return crt


def create_index_from_report(category, report_path):
print("[INFO] create index from report. %s, %s" % (category, report_path))
def create_index_from_report(category, ocp_version_range, report_path):
print(
"[INFO] create index from report. %s, %s, %s"
% (category, ocp_version_range, report_path)
)

annotations = indexannotations.getIndexAnnotations(report_path)
annotations = indexannotations.getIndexAnnotations(ocp_version_range, report_path)

print("category:", category)
redhat_to_community = bool(os.environ.get("REDHAT_TO_COMMUNITY"))
Expand Down Expand Up @@ -454,15 +457,15 @@ def update_index_and_push(


def update_chart_annotation(
category, organization, chart_file_name, chart, report_path
category, organization, chart_file_name, chart, ocp_version_range, report_path
):
print(
"[INFO] Update chart annotation. %s, %s, %s, %s"
% (category, organization, chart_file_name, chart)
"[INFO] Update chart annotation. %s, %s, %s, %s, %s"
% (category, organization, chart_file_name, chart, ocp_version_range)
)
dr = tempfile.mkdtemp(prefix="annotations-")

annotations = indexannotations.getIndexAnnotations(report_path)
annotations = indexannotations.getIndexAnnotations(ocp_version_range, report_path)

print("category:", category)
redhat_to_community = bool(os.environ.get("REDHAT_TO_COMMUNITY"))
Expand Down Expand Up @@ -570,6 +573,7 @@ def main():

env = Env()
web_catalog_only = env.bool("WEB_CATALOG_ONLY", False)
ocp_version_range = os.environ.get("OCP_VERSION_RANGE", "N/A")

print(f"[INFO] webCatalogOnly/providerDelivery is {web_catalog_only}")

Expand Down Expand Up @@ -607,7 +611,12 @@ def main():

print("[INFO] Updating chart annotation")
update_chart_annotation(
category, organization, chart_file_name, chart, report_path
category,
organization,
chart_file_name,
chart,
ocp_version_range,
report_path,
)
chart_url = f"https://github.com/{args.repository}/releases/download/{organization}-{chart}-{version}/{chart_file_name}"
print("[INFO] Helm package was released at %s" % chart_url)
Expand All @@ -631,7 +640,9 @@ def main():
if signedchart.check_report_for_signed_chart(report_path):
public_key_file = get_key_file(category, organization, chart, version)
print("[INFO] Creating index from report")
chart_entry, chart_url = create_index_from_report(category, report_path)
chart_entry, chart_url = create_index_from_report(
category, ocp_version_range, report_path
)

if not web_catalog_only:
current_dir = os.getcwd()
Expand Down
117 changes: 12 additions & 105 deletions scripts/src/chartrepomanager/indexannotations.py
Original file line number Diff line number Diff line change
@@ -1,116 +1,26 @@
import sys
import semantic_version
import requests
import yaml

sys.path.append("../")
from report import report_info

# This dict maps Kubernetes versions with their corresponding OCP version
kubeOpenShiftVersionMap = {}

def getIndexAnnotations(ocp_version_range, report_path):
"""Get the annotations set in the report file.
def populateKubeVersionMap():
"""Populate the kubeOpenShiftVersionMap dictionary"""
if not kubeOpenShiftVersionMap:
content = requests.get(
"https://github.com/redhat-certification/chart-verifier/blob/main/internal/tool/kubeOpenShiftVersionMap.yaml?raw=true"
)
This function replaces the certifiedOpenShiftVersions annotation with the
testedOpenShiftVersion annotation. It also adds the
supportedOpenShiftVersions in the case it is not already set.
version_data = yaml.safe_load(content.text)
for kubeVersion in version_data["versions"]:
kubeOpenShiftVersionMap[kubeVersion["kube-version"]] = kubeVersion[
"ocp-version"
]


def getOCPVersions(kubeVersion):
"""Get the range of OCP versions corresponding to the provided range of Kubernetes
versions
It leaves all other annotations untouched.
Args:
kubeVersion (str): The range of supported Kubernetes version as reported in the
Chart
ocp_version_range (str): Range of supported OCP versions
report_path (str): Path to the report.yaml file
Returns:
str: The range of corresponding OCP version
dict: mapping of annotations names to their values
"""
if kubeVersion == "":
return "N/A"

checkKubeVersion = kubeVersion

try:
semantic_version.NpmSpec(kubeVersion)
except ValueError:
print(
f"Value error with kubeVersion - NpmSpec : {kubeVersion}, see if it fixable"
)

try:
# Kubversion is bad, see if we can fix it
separator = checkKubeVersion.find(" - ")
if separator != -1:
lowVersion = checkKubeVersion[:separator].strip()
highVersion = checkKubeVersion[separator + 3 :].strip()
checkKubeVersion = f"{semantic_version.Version.coerce(lowVersion)} - {semantic_version.Version.coerce(highVersion)}"
else:
firstDigit = -1
for i, c in enumerate(checkKubeVersion):
if c.isdigit():
firstDigit = i
break
if firstDigit != -1:
versionInRange = checkKubeVersion[firstDigit:].strip()
preVersion = checkKubeVersion[:firstDigit].strip()
checkKubeVersion = (
f"{preVersion}{semantic_version.Version.coerce(versionInRange)}"
)

# see if the updates have helped
semantic_version.NpmSpec(checkKubeVersion)
print(f"Fixed value error in kubeVersion : {checkKubeVersion}")

except ValueError:
print(f"Unable to fix value error in kubeVersion : {kubeVersion}")
return "N/A"

minOCP = ""
maxOCP = ""
populateKubeVersionMap()
for kubeVersionKey in kubeOpenShiftVersionMap:
coercedKubeVersionKey = semantic_version.Version.coerce(kubeVersionKey)
if coercedKubeVersionKey in semantic_version.NpmSpec(checkKubeVersion):
coercedOCPVersionValue = semantic_version.Version.coerce(
kubeOpenShiftVersionMap[kubeVersionKey]
)
if (
minOCP == ""
or semantic_version.Version.coerce(minOCP) > coercedOCPVersionValue
):
minOCP = kubeOpenShiftVersionMap[kubeVersionKey]
if (
maxOCP == ""
or semantic_version.Version.coerce(maxOCP) < coercedOCPVersionValue
):
maxOCP = kubeOpenShiftVersionMap[kubeVersionKey]

# check if minOCP is open ended
if minOCP != "" and semantic_version.Version(
"1.999.999"
) in semantic_version.NpmSpec(checkKubeVersion):
ocp_versions = f">={minOCP}"
elif minOCP == "":
ocp_versions = "N/A"
elif maxOCP == "" or maxOCP == minOCP:
ocp_versions = minOCP
else:
ocp_versions = f"{minOCP} - {maxOCP}"

return ocp_versions


def getIndexAnnotations(report_path):
annotations = report_info.get_report_annotations(report_path)

set_annotations = {}
Expand All @@ -133,11 +43,8 @@ def getIndexAnnotations(report_path):
set_annotations[annotation] = annotations[annotation]

if not OCPSupportedSet:
chart = report_info.get_report_chart(report_path)
OCPVersions = "N/A"
if "kubeVersion" in chart and chart["kubeVersion"]:
kubeVersion = chart["kubeVersion"]
OCPVersions = getOCPVersions(kubeVersion)
set_annotations["charts.openshift.io/supportedOpenShiftVersions"] = OCPVersions
set_annotations[
"charts.openshift.io/supportedOpenShiftVersions"
] = ocp_version_range

return set_annotations
37 changes: 0 additions & 37 deletions scripts/src/indexfile/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys

sys.path.append("../")
from chartrepomanager import indexannotations

INDEX_FILE = "https://charts.openshift.io/index.yaml"

Expand Down Expand Up @@ -110,39 +109,3 @@ def get_latest_charts():
latest_charts.append(chart_in_process)

return latest_charts


if __name__ == "__main__":
get_chart_info("redhat-dotnet-0.0.1")

chart_list = get_latest_charts()

for chart in chart_list:
print(f'[INFO] found latest chart : {chart["name"]} {chart["version"]}')

OCP_VERSION = semantic_version.Version.coerce("4.11")

for chart in chart_list:
if (
"supportedOCP" in chart
and chart["supportedOCP"] != "N/A"
and chart["supportedOCP"] != ""
):
if OCP_VERSION in semantic_version.NpmSpec(chart["supportedOCP"]):
print(
f'PASS: Chart supported OCP version {chart["supportedOCP"]} includes: {OCP_VERSION}'
)
else:
print(
f' ERROR: Chart supported OCP version {chart["supportedOCP"]} does not include {OCP_VERSION}'
)
elif "kubeVersion" in chart and chart["kubeVersion"] != "":
supportedOCPVersion = indexannotations.getOCPVersions(chart["kubeVersion"])
if OCP_VERSION in semantic_version.NpmSpec(supportedOCPVersion):
print(
f'PASS: Chart kubeVersion {chart["kubeVersion"]} (OCP: {supportedOCPVersion}) includes OCP version: {OCP_VERSION}'
)
else:
print(
f' ERROR: Chart kubeVersion {chart["kubeVersion"]} (OCP: {supportedOCPVersion}) does not include {OCP_VERSION}'
)
Loading

0 comments on commit be673fd

Please sign in to comment.