Configurable Replica Number in Template using ValueFrom

7/20/2017

I have a template.yml file that is used when deploying to any OpenShift project. Each project has specific project-props configMap to be used, this is part of our CICD pipeline, so each project has a unique project.props available to it

I would like to be able to control the number of replicas and CPU/Memory limits based on what project I am deploying to. For example a branch testing OpenShift project vs Performance testing OpenShift project would have a different CPU request and limit than an ephemeral OpenShift project.

My template.yml file looks something like this:

     // <snip>
     spec:
        replicas: "${OS_REPLICAS}"
     // <snip>
     resources:
        limits:
          cpu: "${OS_CPU_LIMIT}"
          memory: "${OS_MEMORY_LIMIT}"
        requests:
          cpu: "${OS_CPU_REQUEST}"
          memory: "${OS_MEMORY_REQUEST}"
     // <snip>
     parameters:
        - name: OS_REPLICAS
          displayName: OS Number of Replicas
          valueFrom:
             configMapKeyRef:
               name: project-props
               key: os.replicas
    // rest of params are similar

My relevant project-props section is:

os.replicas=2
os.cpu.limit=2
os.cpu.request=250m
os.memory.limit=1Gi
os.memory.request=1Gi

When I try to deploy this I get the following error: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)

#x27;

If I change template.yml to have a parameter defined it works fine

       parameters:
        - name: OS_REPLICAS
          displayName: OS Number of Replicas
          value: 2

It seems that valueFrom vs value has a different behavior. Is this impossible to do using valueFrom? Is there another way I can dynamically change spec and resources using a configMap?

The alternative is to deploy and then use oc scale dc <deploy_config_name> --replicas=<number> but it's not very elegant.

-- wiredniko
continuous-integration
kubernetes
openshift

2 Answers

7/21/2017

I figured out the answer, it's does not read the values from the file but at least they can be dynamic.

OpenShift has an oc process command that you can be run when using a template.

So this works by doing:

oc process -f <template_name>.yaml -v <param_name>=<param_value>

This will over write the parameter value with the one being inserted by -v.

An actual example would be

oc process -f ./src/main/openshift/service.template.yaml -v OS_REPLICAS=2

You can read more about it OpenShift template documentation

It seems that the OS Origin team does not want to support using files for parameter insertion. You can read more about it here:

https://github.com/openshift/origin/pull/10952

https://github.com/openshift/origin/issues/10687

-- wiredniko
Source: StackOverflow

7/21/2017

Where you have:

 spec:
    replicas: "${OS_REPLICAS}"

you should have:

 spec:
    replicas: "${{OS_REPLICAS}}"

With template parameter of:

   parameters:
    - name: OS_REPLICAS
      displayName: OS Number of Replicas
      value: 2

See:

for use of "${{}}".

What it does is interpret the contents of the parameter as JSON/YAML, rather than a string value. This allows you to supply an integer, which replicas requires.

So you don't need valueFrom, which wouldn't work anyway as that is only usable for environment variables and not arbitrary fields like replicas.

As to trying to set a default for memory and CPU for pods deployed in a project, you should look at having a LimitRange resource defined against the project and set a default.

-- Graham Dumpleton
Source: StackOverflow