Search code examples
openshift-origin

How to parameterize ports in OpenShift JSON Project Template


I'm trying to create a custom project template in OpenShift Origin. The Service configuration specifically, looks like below:

{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "${NAME}",
    "annotations": {
      "description": "Exposes and load balances the node.js application pods"
    }
  },
  "spec": {
    "ports": [
      {
        "name": "web",
        "port": "${APPLICATION_PORT}",
        "targetPort": "${APPLICATION_PORT}",
        "protocol": "TCP"
      }
    ],
    "selector": {
      "name": "${NAME}"
    }
  }
},

where, APPLICATION_PORT is supplied as a user parameter:

"parameters": [
  {
    "name": "APPLICATION_PORT",
    "displayName": "Application Port",
    "description": "The exposed port that will route to the node.js application",
    "value": "8000"
  },

When I try to use this template to create a project, I get the following error:

spec.ports[0].targetPort: Invalid value: "8000": must be an IANA_SVC_NAME (at most 15 characters, matching regex [a-z0-9]([a-z0-9-]*[a-z0-9])*...

I get a similar error in my DeploymentConfig as well, for the http ports in the liveness and readiness probes:

"readinessProbe": {
  "timeoutSeconds": 3,
  "initialDelaySeconds": 3,
  "httpGet": {
    "path": "/Info",
    "port": "${APPLICATION_ADMIN_PORT}"
  }
},
"livenessProbe": {
  "timeoutSeconds": 3,
  "initialDelaySeconds": 30,
  "httpGet": {
    "path": "/Info",
    "port": "${APPLICATION_ADMIN_PORT}"
  }
},

where, APPLICATION_ADMIN_PORT, again, is user-supplied.

Error:

spec.template.spec.containers[0].livenessProbe.httpGet.port: Invalid value: "8001": must be an IANA_SVC_NAME...

spec.template.spec.containers[0].readinessProbe.httpGet.port: Invalid value: "8001": must be an IANA_SVC_NAME...

I've been following https://blog.openshift.com/part-2-creating-a-template-a-technical-walkthrough/ to understand templates, and it, unfortunately, does not have any examples of ports being parameterized anywhere.

It almost seems as if strings are not allowed as the values of these ports. Is that the case? What's the right way to parameterize these values? Should I switch to YAML?

Versions:

OpenShift Master: v1.1.6-3-g9c5694f

Kubernetes Master: v1.2.0-36-g4a3f9c5

Edit 1: I tried the same configuration in YAML format, and got the same error. So, JSON vs YAML is not the issue.


Solution

  • Unfortunately it is not currently possible to parameterize non-string field values: https://docs.openshift.org/latest/dev_guide/templates.html#writing-parameters

    " Parameters can be referenced by placing values in the form "${PARAMETER_NAME}" in place of any string field in the template."

    Templates are in the process of being upstreamed to Kubernetes and this limitation is being addressed there: https://github.com/kubernetes/kubernetes/blob/master/docs/proposals/templates.md

    The proposal is being implemented in PRs 25622 and 25293 in the kubernetes repo.

    edit: Templates now support non-string parameters as documented here: https://docs.openshift.org/latest/dev_guide/templates.html#writing-parameters