u123
u123

Reputation: 16329

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 

Upvotes: 1

Views: 2882

Answers (2)

Here_2_learn
Here_2_learn

Reputation: 5451

Instead of OC patch would prefer OC apply on your templates. Templates are the best way to configure Openshift/Kubernetes objects which can be stored in git for version control to follow Infrastructure as code.

I am not the admin for my Openshift cluster, so can't access the resource quotas hence suggesting a way in Kubernetes but same can be applied in Openshift too, except the CLI change from kubectl to oc

Let's take a simple resource quota template:

$ cat resourcequota.yaml 
apiVersion: v1
kind: ResourceQuota
metadata:
  name: demo-quota
spec:
  hard:
    cpu: "1"
    memory: 2Gi
    pods: "10"
  scopeSelector:
    matchExpressions:
    - operator : In
      scopeName: PriorityClass
      values: ["high"]

Now configure your quota using kubectl apply on your template. This creates resource which is configured in the template, in our case its resourcequota

$ kubectl apply -f resourcequota.yaml 
resourcequota/demo-quota created

$ kubectl get quota
NAME         CREATED AT
demo-quota   2019-11-19T12:23:37Z

$ kubectl describe quota demo-quota
Name:       demo-quota
Namespace:  default
Resource    Used  Hard
--------    ----  ----
cpu         0     1
memory      0     2Gi
pods        0     10

As your looking for an update in resource quota using patch, I would suggest here to edit the template and execute kubectl apply again to update the object.

$ kubectl apply -f resourcequota.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
resourcequota/demo-quota configured


$ kubectl describe quota demo-quota
Name:       demo-quota
Namespace:  default
Resource    Used  Hard
--------    ----  ----
cpu         0     2
memory      0     4Gi
pods        0     20

Similarly, you can execute oc apply for your operations as oc patch is not so user-friendly to configure.

Upvotes: 1

Rinor
Rinor

Reputation: 1999

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\"}}}"

Upvotes: 2

Related Questions