Using OpenShift template to conditionally include/exclude certain resources during apply

16 Views Asked by At

I am using Ansible to automate the oc process -f allresources.yaml | oc apply -f - command. Inside of allresources.yaml template, I have my routes, services, etc... I want to add an additional but optional route to the template. The template will deploy both HTTPS and (optionally) HTTP routes during the oc apply phase. Not always will the developers require an HTTP route, so I want this to be configurable in the repository Jenkinsfile.

The question comes down to this: Is there a way to add a selector/conditional to oc process that will only evaluate resources that match the selector/conditional. Ultimately, Ansible will evaluate Jenkins configuration and determine how to process the oc process command.

Something along these lines:

oc process -f allresources.yaml APP=someapp ENV=dev --ignore="HTTPRoute" | oc apply -f -"
1

There are 1 best solutions below

0
larsks On

There is no support for conditionals in openshift templates. However, since you're using Ansible, you have lots of options:

  • You can have multiple templates and apply them conditionally.

    - name: apply main templates
      shell: oc process -f allresources.yaml | oc apply -f-
    
    - name: apply route templates
      when: route_required
      shell: oc process -f route.yaml | oc apply -f-
    
  • You could use Jinja templates via the Ansible template module instead of openshift templates.

    - name: render templates
      template:
        src: allresources.yaml.j2
        dest: allresources.yaml
    
    - name: apply generated manifests
      command: oc apply -f allresources.yaml
    
  • If you don't like jinja2 templates, you could do something similar using an external templating tool like jsonnet, for example:

    - name: apply generated manifests
      shell: jsonnet --tla-code route_required={{ route_required }} allresources.json | oc apply -f-