gvdm
gvdm

Reputation: 3166

Malformed command in kubectl run

I have a Kubernetes cluster on which is deployed a replica of this image (but it's not important for the sake of the question). Now, in Powershell I need to perform one REST request against the pod. In particular I have to send a PUT request to the endpoint http://<POD_IP>:18681/PCCIS/V1/Service/Properties/Servers passing this JSON payload:

{"servers":[{"port":"18682","address":"10.244.1.29"}]}

What I was trying to do is perform this action from within a curlimages/curl pod, just to be able to reach the pod's network. This is my attempt:

$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{"servers":[{"port":"18682","address":"10.244.1.29"}]}'
$cmd = "curl -i --header 'Content-Type: application/json' --request PUT --data '$JsonPostBody' http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers"
kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never "$cmd"

Unexpectedly, the command output this error:

curl: (3) nested brace in URL position 82:
curl -i --header 'Content-Type: application/json' --request PUT --data '{servers:[{port:18682,address:10.244.1.29}]}' http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers

As you can see, the problem I have is that the string command I pass is not interpreted the right way and moreover the payload loses all the doublequotes of the JSON, that are important for the curl command to propertly work.

One another test I made is with this command (the echo is used to show what the sh command really would execute)

kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- sh -c "echo $cmd"

And the output is

curl -i --header Content-Type: application/json --request PUT --data {servers:[{port:18682,address:10.244.1.29}]} http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers

Showing that the cmd Powershell variable loses its doublequotes when passed to kubectl run

What should be the right syntax to pass the $cmd command as is (preserving all its quotes and chars)?

Upvotes: 0

Views: 441

Answers (1)

gvdm
gvdm

Reputation: 3166

I found the solution after some searching and trials. First, I have to escape the double quotes in the $JsonPostBody variable.

$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{\"servers\":[{\"port\":\"18682\",\"address\":\"10.244.1.29\"}]}'

The other error is that the curlimages/curl Docker image executes the kubectl run arguments as the curl's arguments, so I don't have to use the curl command:

kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- -i --header 'Content-Type: application/json' --request PUT --data $JsonPostBody http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers

This outputs

HTTP/1.1 200 OK
[1mDate[0m: Wed, 08 Jul 2020 13:23:30 GMT
[1mConnection[0m: keep-alive
1mContent-Length[0m: 0

Upvotes: 1

Related Questions