Search code examples
openshiftjsonpath

Include variable in jsonpath for oc patch (openshift CLI operations)


In bash I am trying to use a variable in a jsonpath for an openshift patch cli command:

  OS_OBJECT='sample.k8s.io/element'
  VALUE='5'
  oc patch quota "my-object" -p '{"spec":{"hard":{"$OS_OBJECT":"$VALUE"}}}'

But that gives the error:

Error from server: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$'

indicating that the variable is not substituted/expanded.

If I write it explicitly it works:

oc patch quota "my-object" -p '{"spec":{"hard":{"sample.k8s.io/element":"5"}}}'

Any suggestions on how to include a variable in the jsonstring?

EDIT: Based on below answer I have also tried:

oc patch quota "my-object" -p "{'spec':{'hard':{'$OS_OBJECT':'$VALUE'}}}"

but that gives the error:

 Error from server (BadRequest): invalid character '\'' looking for beginning of object key string 

Solution

  • In single quotes everything is preserved by bash, you have to use double quotes for string interpolation to work (and use the escape sequence \" for the other double quotes).

    Try this out:

    oc patch quota "my-object" -p "{\"spec\":{\"hard\":{\"$OS_OBJECT\":\"$VALUE\"}}}"