Using params with oc patch comand

1k Views Asked by At

Trying to write an sh script with several oc patch commands and need to parametrize several values. Now script looks like:

oc patch svc svc-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/ports/-", "value": {"name": "http-8080","port": 8080} }]'
oc patch vs vs-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/hosts/-", "value": "ingress-http-host.smth.com" }]'
oc patch gw gw-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/servers/-", "value": { "hosts": ["ingress-http-host.smth.com"], "port": {"name":"http-8080", "number":8080,"protocol":"HTTP"} } }]'

Trying to do smth like this:

HTTP_HOST='ingress-http-host.smth.com'
HTTP_PORT='8080'
oc patch svc svc-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/ports/-", "value": {"name": "http-${HTTP_PORT}","port": ${HTTP_PORT}} }]'
oc patch vs vs-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/hosts/-", "value": ${HTTP_HOST} }]'
oc patch gw gw-ingressgateway --type='json' -p='[{"op": "add", "path": "/spec/servers/-", "value": { "hosts": [${HTTP_HOST}], "port": {"name":"http-${HTTP_PORT}", "number":${HTTP_PORT},"protocol":"HTTP"} } }]'

but this is wrong, obviously, because the error appears:

Error from server: admission webhook "pilot.validation.istio.io" denied the request: configuration is invalid: domain name "$HTTP_HOST" invalid (label "$HTTP_HOST" invalid) Error from server: admission webhook "pilot.validation.istio.io" denied the request: configuration is invalid: 2 errors occurred: * short names (non FQDN) are not allowed * domain name "$HTTP_HOST" invalid (label "$HTTP_HOST" invalid)

Please, help, if you know how to do such things

1

There are 1 best solutions below

0
On

The error appears because the shell parameter expansion of ${HTTP_PORT} and ${HTTP_HOST} is not done within single quotes. A way to fix this is ending the quotation just before and beginning a new one just after the parameter expansion. Besides, the braces are unneeded in this case. So, you can effectively replace ${HTTP_....} with '$HTTP_....'