From 2360e8414a4455cfc466e53454f76f59ec51fa80 Mon Sep 17 00:00:00 2001
From: Leonardo Rossetti <lrossett@redhat.com>
Date: Dec 08 2021 23:39:33 +0000
Subject: kojira updates + annotation usage


---

diff --git a/operator/config/samples/buildsys_v1alpha1_kojira.yaml b/operator/config/samples/buildsys_v1alpha1_kojira.yaml
index 545b2f8..1817c70 100644
--- a/operator/config/samples/buildsys_v1alpha1_kojira.yaml
+++ b/operator/config/samples/buildsys_v1alpha1_kojira.yaml
@@ -2,16 +2,10 @@ apiVersion: buildsys.apps.fedoraproject.org/v1alpha1
 kind: Kojira
 metadata:
   name: sample
+   annotations:
+      buildsys.apps.fedoraproject.org/hub: sample/default # $NAME/$NAMESPACE
 spec:
-  replicas: 1
   image: quay.io/fedora/kojira:f34
-  configmap: kojira-config
-  hub_username: kojira
-  hub_host: koji-hub:8443
   src: 'no'
   max_repo_tasks: 15
-  repo_tasks_limit: 15
-  cacert_secret: koji-hub-ca-cert
-  client_cert_secret: kojira-client-cert
-  shared_pvc: koji-hub-mnt-pvc
-  admin_secret: koji-hub-admin-cert
+  repo_tasks_limit: 15
\ No newline at end of file
diff --git a/operator/molecule/default/tasks/kojira_test.yml b/operator/molecule/default/tasks/kojira_test.yml
index aec0509..61d80d2 100644
--- a/operator/molecule/default/tasks/kojira_test.yml
+++ b/operator/molecule/default/tasks/kojira_test.yml
@@ -3,15 +3,25 @@
   k8s:
     state: present
     namespace: '{{ namespace }}'
-    definition: "{{ lookup('template', '/'.join([samples_dir, cr_file])) | from_yaml }}"
+    definition:
+      apiVersion: buildsys.apps.fedoraproject.org/v1alpha1
+      kind: Kojira
+      metadata:
+        name: sample
+        annotations:
+          buildsys.apps.fedoraproject.org/hub: sample/osdk-test
+      spec:
+        replicas: 1
+        image: quay.io/fedora/kojira:f34
+        src: 'no'
+        max_repo_tasks: 15
+        repo_tasks_limit: 15
     wait: yes
     wait_timeout: 300
     wait_condition:
       type: Running
       reason: Successful
       status: "True"
-  vars:
-    cr_file: 'buildsys_v1alpha1_kojira.yaml'
 
 - block:
     - name: 'TEST: kojira.configmap'
@@ -19,12 +29,12 @@
         api_version: v1
         kind: ConfigMap
         namespace: "{{ namespace }}"
-        name: kojira-config
+        name: kojira-sample-conf
       register: kojira_configmap
     - assert:
         that:
           - kojira_configmap.resources|length == 1
-          - kojira_configmap.resources[0].metadata.labels['app'] == 'kojira'
+          - "kojira_configmap.resources[0].metadata.labels['app'] == 'kojira'"
           - "'kojira.conf' in kojira_configmap.resources[0].data"
 
 - block:
@@ -33,7 +43,7 @@
         api_version: v1
         kind: Secret
         namespace: "{{ namespace }}"
-        name: kojira-client-cert
+        name: kojira-sample-client-cert
       register: kojira_client_secrets
     - assert:
         that:
diff --git a/operator/roles/kojira/defaults/main.yml b/operator/roles/kojira/defaults/main.yml
index 1c730c7..acdca6e 100644
--- a/operator/roles/kojira/defaults/main.yml
+++ b/operator/roles/kojira/defaults/main.yml
@@ -1,18 +1,6 @@
 ---
 # defaults file for kojira
-kojira_replicas: "{{ replicas | default(1) }}"
 kojira_image: "{{ image | default('quay.io/fedora/kojira:latest') }}"
-kojira_configmap: "{{ configmap | default('kojira-config') }}"
-kojira_hub_username: "{{ hub_username | default('kojira') }}"
-kojira_hub_host: "{{ hub_host | default('koji-hub:8443') }}"
 kojira_src: "{{ src | default('no') }}"
 kojira_max_repo_tasks: "{{ max_repo_tasks | default(15) }}"
-kojira_repo_tasks_limit: "{{ repo_tasks_limit | default(15) }}"
-
-kojira_admin_secret: "{{ admin_secret | default('kojira-admin-cert') }}"
-kojira_cacert_secret: "{{ cacert_secret|default('kojira-ca-cert') }}"
-kojira_client_cert_secret: "{{ client_cert_secret|default('kojira-client-cert') }}"
-
-kojira_shared_pvc: "{{ shared_pvc | default('koji-hub-mnt-pvc') }}"
-
-kojira_builder_mbox: "{{ mbox | default('') }}"
+kojira_repo_tasks_limit: "{{ repo_tasks_limit | default(15) }}"
\ No newline at end of file
diff --git a/operator/roles/kojira/tasks/main.yml b/operator/roles/kojira/tasks/main.yml
index a1258cf..c49ebcf 100644
--- a/operator/roles/kojira/tasks/main.yml
+++ b/operator/roles/kojira/tasks/main.yml
@@ -6,6 +6,40 @@
     suffix: deploy
   register: kojira_dir
 
+- name: Retrieve the full Kojira object
+  k8s_info:
+    api_version: buildsys.apps.fedoraproject.org/v1alpha1
+    kind: Kojira
+    namespace: "{{ meta.namespace }}"
+    name: "{{ meta.name }}"
+  register: kojira_k8s
+
+- name: Fail if Kojira resource couldn't be found
+  fail:
+    msg: "Failed to fetch kojira {{ meta.name }} "
+  when: kojira_k8s.resources | length == 0
+
+- name: Retrieve koji-hub data
+  koji_hub_info:
+    hub: "{{ kojira_k8s.resources.0.metadata.annotations['buildsys.apps.fedoraproject.org/hub'] }}"
+  register: kojira_hub_info
+
+- name: Get the annotation value from the retrieved KojiBuilder resource
+  set_fact:
+    kojira_hub: "{{ kojira_k8s.resources.0.metadata.annotations['buildsys.apps.fedoraproject.org/hub'].split('/')  }}"
+
+- name: Set play variables
+  set_fact:
+    kojira_replicas: 1
+    kojira_configmap: "kojira-{{ meta.name }}-conf"
+    kojira_hub_username: "{{ meta.name }}"
+    kojira_hub_host: "{{ kojira_hub_info['result']['svc'] }}"
+    kojira_cacert_secret: "{{ kojira_hub_info['result']['hub']['spec']['ca_cert_secret'] }}"
+    kojira_client_cert_secret: "kojira-{{ meta.name }}-client-cert"
+    kojira_shared_pvc: "{{ kojira_hub_info['result']['hub']['spec']['mnt_pvc_name'] }}"
+    kojira_hub_name: "{{ kojira_hub.0 }}"
+    kojira_hub_namespace: "{{ kojira_hub.1 }}"
+
 - include_tasks: cert.yml
 
 - block:
@@ -20,21 +54,6 @@
         src: "{{ kojira_dir.path }}/kojira.configmap.yaml"
 
 - block:
-    - name: retrieve and set shared pvc name var
-      k8s_info:
-        api_version: apps.fedoraproject.org/v1alpha1
-        kind: Mbox
-        namespace: "{{ meta.namespace }}"
-        name: "{{ kojira_builder_mbox }}"
-      register: k8s_mboxes
-    - fail:
-        msg: "Failed to find mbox {{ kojira_builder_mbox }} in namespace {{ meta.namespace }}"
-      when: k8s_mboxes.resources|length == 0
-    - set_fact:
-        kojira_shared_pvc: "{{ k8s_mboxes.resources[0].spec.koji_pvc_name }}"
-  when: kojira_builder_mbox|length > 0
-
-- block:
     - name: ensure kojira deployment
       template:
         src: kojira.deployment.yaml.j2
@@ -46,21 +65,6 @@
         src: "{{ kojira_dir.path }}/kojira.deployment.yaml"
 
 - block:
-    - name: setup kojira user
-      k8s_info:
-        api_version: v1
-        kind: Secret
-        namespace: "{{ meta.namespace }}"
-        name: "{{ kojira_admin_secret }}"
-      register: k8s_res
-    - fail:
-        msg: "Secret not found: {{ kojira_admin_secret }}"
-      when: k8s_res.resources|length == 0
-    - set_fact:
-        koji_admin_secret: "{{ k8s_res.resources[0] | from_yaml }}"
-    - copy:
-        content: "{{ koji_admin_secret.data['client.pem'] | b64decode }}"
-        dest: "{{ kojira_dir.path }}/admin.pem"
     - k8s_info:
         api_version: v1
         kind: Secret
@@ -76,7 +80,7 @@
         content: "{{ koji_ca_secret.data['cert'] | b64decode }}"
         dest: "{{ kojira_dir.path }}/ca.pem"
     - ktdreyer.koji_ansible.koji_user:
-        koji: "{{ meta.name  }}-{{ meta.namespace }}"
+        koji: "{{ kojira_hub_name }}-{{ kojira_hub_namespace }}"
         name: "{{ kojira_hub_username }}"
         permissions:
           - repo
@@ -85,7 +89,6 @@
         state: absent
         path: "{{ koji_dir.path }}"
 
-
 - name: cleanup
   file:
     path: "{{ kojira_dir.path }}"

