Converting milliCPU to string using ParseFloat

Hi there,

I want to do computation on CPU limits in the preflights to make sure that the cluster has enough capabilities, so I want to convert strings in milliCPU like 250m into floats (0.25). Does the ParseFloat function support units like this? If not, how can I do the conversion?

Hi - Are you simply trying to convert the type ? As in make a string value be treated like a float? Or do you need an actual conversion (take one number, like ‘25’ and then covert it to something like 0.25?

Here is an example where we used templating to test the CPU capacity How Can I Use License Custom Fields Value in a Pre-Flight Check? While this article uses License Field Values, it shows how to use templating which would be needed to use the ParseFloat function?

I’m really trying to convert the type. The CPU limit is an optional configuration field, allowing users to enter their own limit. If the config value is 250m I need the float 0.25

FWIW code is the following, reading cpu_limits as a config option.

- name: cpu_requested
  type: text
  value: '{{repl (Mult (ConfigOption "nb_replicas" | ParseInt) (ConfigOption "cpu_limits" | ParseFloat)) }}'
  readonly: true
  hidden: true

The value is then used in the following preflight check:

- nodeResources:
    outcomes:
      - fail:
          when: 'sum(cpuCapacity) <= {{repl ConfigOption "cpu_requested"}}'

I’m trying to convert the type, not a conversion. The CPU limit is an optional configuration field, allowing users to enter their own limit. If the config value is 250m I need a function that returns the float 0.25

FWIW code is the following, reading cpu_limits as a config option.

- name: cpu_requested
  type: text
  value: '{{repl (Mult (ConfigOption "nb_replicas" | ParseInt) (ConfigOption "cpu_limits" | ParseFloat)) }}'
  readonly: true
  hidden: true

The value is then used in the following preflight check:

- nodeResources:
    outcomes:
      - fail:
          when: 'sum(cpuCapacity) <= {{repl ConfigOption "cpu_requested"}}'

I could force the cpu_limits configuration field to be float, but people are used to specify these values in milliCPU so I’d rather let them use this unit.

Could you use the Div template function?
{{repl Div (ConfigOption"cpu_limits" | ParseInt) 1000.00}}

In the end I don’t need to do such a thing anymore so I didn’t dig further more. Thanks for your help anyway :slight_smile: