diff --git a/roles/openshift_setup/defaults/main.yml b/roles/openshift_setup/defaults/main.yml index 0acb47393..e3d9d95a5 100644 --- a/roles/openshift_setup/defaults/main.yml +++ b/roles/openshift_setup/defaults/main.yml @@ -27,4 +27,11 @@ cifmw_openshift_setup_digest_mirrors: [] cifmw_openshift_setup_operator_override_catalog_name: "redhat-operators-4.17" cifmw_openshift_setup_operator_override_catalog_namespace: "openshift-marketplace" cifmw_openshift_setup_operator_override_catalog_image: "registry.redhat.io/redhat/redhat-operator-index:v4.17" -cifmw_openshift_setup_apply_marketplace_fix: false +# NOTE(dpawlik): To avoid potential errors, where: +# oc wait pod --all --for=condition=Ready -n openshift-marketplace --timeout=1m +# never pass or operatorhub does not show any items or +# other errors like: " installPlanRef not found - subscription resolution may have failed" +# let's clean the marketplace each time. +cifmw_openshift_setup_apply_marketplace_fix: true +cifmw_openshift_setup_marketplace_oneshot: false +cifmw_openshift_setup_marketplace_crontab: true diff --git a/roles/openshift_setup/files/marketplace-crontab.yml b/roles/openshift_setup/files/marketplace-crontab.yml new file mode 100644 index 000000000..f7b1eeb8e --- /dev/null +++ b/roles/openshift_setup/files/marketplace-crontab.yml @@ -0,0 +1,72 @@ +--- +apiVersion: v1 +kind: Namespace +metadata: + name: cifmw-marketplace-fix +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: cifmw-marketplace-fix-sa + namespace: cifmw-marketplace-fix +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: cifmw-marketplace-fix-role +rules: + - apiGroups: [""] + resources: ["pods", "pods/log"] + verbs: ["get", "list", "watch", "delete"] + - apiGroups: [""] + resources: ["pods"] + verbs: ["create"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: cifmw-marketplace-fix-binding +subjects: + - kind: ServiceAccount + name: cifmw-marketplace-fix-sa + namespace: cifmw-marketplace-fix +roleRef: + kind: ClusterRole + name: cifmw-marketplace-fix-role + apiGroup: rbac.authorization.k8s.io +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: cifmw-marketplace-fix-cron + namespace: cifmw-marketplace-fix +spec: + schedule: "*/5 * * * *" + concurrencyPolicy: Forbid + successfulJobsHistoryLimit: 3 + failedJobsHistoryLimit: 3 + jobTemplate: + spec: + backoffLimit: 0 + template: + spec: + serviceAccountName: cifmw-marketplace-fix-sa + restartPolicy: Never + containers: + - name: cifmw-fix-marketplace + image: registry.redhat.io/openshift4/ose-cli:latest + command: ["/bin/bash", "-c"] + args: + - | + set -euo pipefail + + echo "Checking if all pods in openshift-marketplace are Ready..." + if oc wait pod --all --for=condition=Ready -n openshift-marketplace --timeout=1m; then + echo "All pods are Ready - nothing to do." + exit 0 + else + echo "Timeout or not all pods Ready, restarting marketplace pods..." + oc delete pods --all -n openshift-marketplace --grace-period=30 || true + echo "Delete command executed." + exit 1 + fi diff --git a/roles/openshift_setup/tasks/fix_openshift_marketplace.yml b/roles/openshift_setup/tasks/fix_openshift_marketplace.yml index 2096cfd05..4c8eeb3eb 100644 --- a/roles/openshift_setup/tasks/fix_openshift_marketplace.yml +++ b/roles/openshift_setup/tasks/fix_openshift_marketplace.yml @@ -11,23 +11,43 @@ - not cifmw_openshift_setup_dry_run - cifmw_openshift_setup_apply_marketplace_fix block: - - name: Delete the pods from openshift-marketplace namespace - kubernetes.core.k8s: - kind: Pod - state: absent - delete_all: true - kubeconfig: "{{ cifmw_openshift_kubeconfig }}" - namespace: openshift-marketplace + - name: Execute marketplace fix once + when: cifmw_openshift_setup_marketplace_oneshot + block: + - name: Delete the pods from openshift-marketplace namespace + kubernetes.core.k8s: + kind: Pod + state: absent + delete_all: true + kubeconfig: "{{ cifmw_openshift_kubeconfig }}" + namespace: openshift-marketplace - - name: Wait for openshift-marketplace pods to be running - environment: - KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}" - PATH: "{{ cifmw_path }}" - ansible.builtin.command: - cmd: >- - oc wait pod --all --for=condition=Ready - -n openshift-marketplace --timeout=1m - register: _pod_status - retries: 4 - delay: 10 - until: _pod_status.rc == 0 + - name: Wait for openshift-marketplace pods to be running + environment: + KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}" + PATH: "{{ cifmw_path }}" + ansible.builtin.command: + cmd: >- + oc wait pod --all --for=condition=Ready + -n openshift-marketplace --timeout=1m + register: _pod_status + retries: 4 + delay: 10 + until: _pod_status.rc == 0 + + - name: Execute marketplace fix via crontab + when: cifmw_openshift_setup_marketplace_crontab + block: + - name: Copy marketplace-crontab.yml manifest + ansible.builtin.copy: + src: marketplace-crontab.yml + dest: /tmp/marketplace-crontab.yml + mode: "0644" + + - name: Apply marketplace-crontab.yml manifest + ansible.builtin.command: | + oc apply -f /tmp/marketplace-crontab.yml + changed_when: false + environment: + KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}" + PATH: "{{ cifmw_path }}"