4

Currently I have an OKD/openshift template which exposes port 1883 on a specific container:

ports:
 - name: 1883-tcp
   port: 1883
   containerPort: 1883
   protocol: TCP
   hostPort: ${MQTT-PORT}

Is it possible to have an if/else clause depending on parameters. For example:

ports:
 - name: 1883-tcp
   port: 1883
   containerPort: 1883
   protocol: TCP
   {{ if ${MQTT-PORT} != 0 }}
   hostPort: ${MQTT-PORT}
   {{ /if }}

By doing this, I can have the same template in all my environments (e.g.: development/testing/production) but based on the parameters given by creation, some ports are available for debugging or testing without having to forward them each time using the oc command.

Bob Claerhout
  • 781
  • 5
  • 24
  • OpenShift templates can't do that. I believe helm charts have the capability. Have you tried just using default parameters instead of if/else? I've just used multiple templates in the past. – Will Gordon Dec 04 '18 at 17:40
  • How will the default parameters help me with this? The hostport defines whether or not this port is forwarded to and opened on the host. I don't think any default parameter does this? – Bob Claerhout Dec 05 '18 at 07:55

1 Answers1

3

You can't do this kind of conditional processing at the template level.

But, to achieve your desired outcome, you can do one of 2 things.

Option 1 Pass all the parameters required for the condition to process at the template level, like MQTT-PORTand map the correct port number when building your service. This might be the correct approach as templates are designed to be as logic-less as possible, you do all the decision making at a much lower level.

Option 2 If you can relax the "same template" constraint, We could have 2 flavors of the same template, one with the specific port and another with the parameterized port. The only issue with this option is to change 2 templates every time you change your app/service specs, which violates the DRY principle.

Update

Using Helm with OpenShift might be the best option here. You can templatize your artifacts using Helm's conditionals and deploy a Helm app to OpenShift. Here's a repository which has a Helm chart tailored for OpenShift. Also, you need to point to the right namespace for Tiller to use Helm with OpenShift. You can find more details about it here.

Badri
  • 2,212
  • 3
  • 25
  • 26
  • I agree option 2 is not what I want. About option 1, what do you mean by "processing at the template level"? Does this mean I can change my code/implementation to listen on a different port number? If so, this is not possible since I'm trying to change the hostPort, which is an openshift/OKD functionality for exposing the port externally. – Bob Claerhout Dec 05 '18 at 07:58
  • Yes, I've worded the first option rather vaguely. I think your best bet is to go with Helm. I've updated the answer to reflect the same. – Badri Dec 05 '18 at 15:01
  • Thanks, I'll have a look into Helm. – Bob Claerhout Dec 07 '18 at 09:11