Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mirror from dir role #65

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions roles/mirror_from_directory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Mirror from file role

A role that mirror operators from a local directory into a container images registry using the `oc-mirror` plugin.

## Parameters

Name | Required | Default | Description
---------------------- | -------- | --------------------------------------------- | ------------
mfd_local_registry | Yes | | Target registry
mfd_operators_dir | Yes | | Directory containing the tarball files generated by oc-mirror
mfd_delete_workspace | No | True | Deletes the `oc-mirror` workspace generated during the mirroring

## Outputs

The role copies a generated Image Sources manifest and CatalogSources files into a temporary directory. The files can bee access those files via following variables:
betoredhat marked this conversation as resolved.
Show resolved Hide resolved

- `mdf_image_source_file`: The path to the Image Source file produced.
- `mdf_catalog_source_file`: The catalog manifest for the mirrored operators.
- `mdf_manifests_dir`: The directory with the mirroring artifacts

## Example of usage

```yaml
- name: "Mirror operators from directory"
include_role:
name: redhatci.ocp.mirror_from_directory
vars:
mfd_local_registry: "<registry>:<port>/<optional_path>"
mfd_operators_dir: "<tarball_dir>"
```

## Authentication

If registry authentication is required, please pass `DOCKER_CONFIG` as an environment variable to the role pointing to the directory that contains the `config.json` file with the proper authentication strings. See [docker-config](https://www.systutorials.com/docs/linux/man/5-docker-config-json/).
3 changes: 3 additions & 0 deletions roles/mirror_from_directory/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
mfd_delete_workspace: true
...
101 changes: 101 additions & 0 deletions roles/mirror_from_directory/tasks/load-operators.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
- name: "Create working directory"
ansible.builtin.tempfile:
state: directory
register: image_dir

- name: "Set workdir path"
ansible.builtin.set_fact:
image_dir: "{{ image_dir.path }}"

- name: "Working directory path"
ansible.builtin.debug:
msg: "Manifest directory {{ image_dir }}"

- name: "Download stable oc-mirror plugin"
vars:
ocp_mirror_url: "https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/stable/oc-mirror.tar.gz"
ansible.builtin.unarchive:
src: "{{ ocp_mirror_url }}"
dest: "{{ image_dir }}"
remote_src: true
mode: "0755"
register: result
retries: 3
delay: 10
until: result is not failed

- name: "Load Images to {{ mfd_local_registry }}"
ansible.builtin.shell:
chdir: "{{ image_dir }}"
cmd: >
set -x;
{{ image_dir }}/oc-mirror
--from={{ mfd_operators_dir }}
docker://{{ mfd_local_registry }}
--skip-metadata-check
--dest-skip-tls
retries: 3
delay: 10
register: result
changed_when: result.rc != 0
until: result.rc == 0

- name: "Find the generated ICSP manifest"
ansible.builtin.find:
paths: "{{ image_dir }}"
recurse: true
patterns: "imageContentSourcePolicy.yaml"
register: icsp_manifest
failed_when:
- icsp_manifest.matched == 0

- name: "Read Image Source file"
ansible.builtin.slurp:
src: "{{ icsp_manifest.files[0].path }}"
register: file_content

- name: "Replace references to Red Hat internal registries in the ICSP"
ansible.builtin.replace:
path: "{{ icsp_manifest.files[0].path }}"
regexp: 'brew.registry.redhat.io'
replace: 'registry.redhat.io'
backup: true
when: file_content.content | b64decode | regex_search('brew.registry.redhat.io')

- name: "Find the generated Catalogsource manifests"
ansible.builtin.find:
paths: "{{ image_dir }}"
recurse: true
patterns: "catalogSource-*.yaml"
register: catalog_manifest
failed_when:
- catalog_manifest.matched == 0

- name: "Copy ICSP to working dir"
ansible.builtin.copy:
src: "{{ icsp_manifest.files[0].path }}"
dest: "{{ image_dir }}/imageContentSourcePolicy.yaml"
mode: "0644"

- name: "Copy CatalogSource to working dir"
ansible.builtin.copy:
src: "{{ catalog_manifest.files[0].path }}"
dest: "{{ image_dir }}/catalogSource.yaml"
mode: "0644"

- name: "Delete workspaces directory"
ansible.builtin.file:
state: absent
path: "{{ item }}"
with_items:
- "{{ image_dir }}/oc-mirror-workspace"
when:
- mfd_delete_workspace | bool

- name: "Set image source file fact"
ansible.builtin.set_fact:
mdf_image_source_file: "{{ image_dir }}/imageContentSourcePolicy.yaml"
mdf_catalog_source_file: "{{ image_dir }}/catalogSource.yaml"
mdf_manifests_dir: "{{ image_dir }}"
...
7 changes: 7 additions & 0 deletions roles/mirror_from_directory/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: "Validate requirements"
include_tasks: validation.yml

- name: "Mirror operators"
include_tasks: load-operators.yml
...
17 changes: 17 additions & 0 deletions roles/mirror_from_directory/tasks/validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: "Validate requirements"
ansible.builtin.assert:
that:
- mfd_local_registry is defined
- mfd_local_registry | length

- name: "Assert that the source directory exists and has tar files"
ansible.builtin.find:
path: "{{ mfd_operators_dir }}"
recurse: true
patterns: "*.tar"
register: catalog_path
failed_when:
- catalog_path is undefined or
catalog_path.matched == 0
...