Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created June 29, 2023 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckylittle/019c22d72bd3654eeaf7d65125ad8c3f to your computer and use it in GitHub Desktop.
Save luckylittle/019c22d72bd3654eeaf7d65125ad8c3f to your computer and use it in GitHub Desktop.
Python transformation of Openshift's ImageContentSourcePolicy. It changes the mirrors based on the source. Useful for cases when you do not need to mirror images, but you just proxy them.

Example of the ImageContentSourcePolicy:

spec:
  repositoryDigestMirrors:
  - mirrors: artifactory-proxy.domain.com.au/redhat-registry-openshift-remote/redhat-openshift-logging-kibana6-rhel8
    source: registry.redhat.io/openshift-logging/kibana6-rhel8
  - mirrors: artifactory-proxy.domain.com.au/redhat-registry-openshift-remote/redhat-openshift-gitops-1-gitops-operator-bundle
    source: registry.connect.redhat.io/openshift-gitops-1/gitops-operator-bundle
  - mirrors: artifactory-proxy.domain.com.au/redhat-registry-openshift-remote/redhat-openshift-gitops-1-dex-rhel8
    source: quay.io/openshift-gitops-1/dex-rhel8
  - mirrors: artifactory-proxy.domain.com.au/redhat-registry-openshift-remote/redhat-rhel8-redis-5
    source: registry.access.redhat.io/rhel8/redis-5
import yaml
def modify_yaml(yaml_file):
"""
Transforms the mirror namespace based on the source in the ImageContentSourcePolicy.
"""
with open(yaml_file, 'r') as file:
yaml_data = yaml.safe_load(file)
if 'spec' in yaml_data and 'repositoryDigestMirrors' in yaml_data['spec']:
mirrors = yaml_data['spec']['repositoryDigestMirrors']
artifactory = 'artifactory-proxy.domain.com.au'
for mirror in mirrors:
if 'source' in mirror and mirror['source'].startswith('registry.access.redhat.io'):
source_parts = mirror['source'].split('/')
path = '/'.join(source_parts[1:])
url = artifactory, 'redhat-registry-access-remote', path
mirror['mirrors'] = '/'.join(url)
if 'source' in mirror and mirror['source'].startswith('registry.connect.redhat.io'):
source_parts = mirror['source'].split('/')
path = '/'.join(source_parts[1:])
url = artifactory, 'redhat-registry-connect-remote', path
mirror['mirrors'] = '/'.join(url)
if 'source' in mirror and mirror['source'].startswith('quay.io'):
source_parts = mirror['source'].split('/')
path = '/'.join(source_parts[1:])
url = artifactory, 'redhat-quayio-remote', path
mirror['mirrors'] = '/'.join(url)
if 'source' in mirror and mirror['source'].startswith('registry.redhat.io'):
source_parts = mirror['source'].split('/')
path = '/'.join(source_parts[1:])
url = artifactory, 'redhat-registry-openshift-remote', path
mirror['mirrors'] = '/'.join(url)
with open(yaml_file, 'w') as file:
yaml.dump(yaml_data, file)
yaml_file_path = 'imageContentSourcePolicy.yaml'
modify_yaml(yaml_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment