Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use RunKubectlAndGetOutputE to CURL a Pod with a JSON Body #1347

Open
rstoermer opened this issue Sep 22, 2023 · 1 comment
Open

Can't use RunKubectlAndGetOutputE to CURL a Pod with a JSON Body #1347

rstoermer opened this issue Sep 22, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@rstoermer
Copy link

rstoermer commented Sep 22, 2023

Describe the bug
I want to run HTTP Requests inside a KIND cluster using Terratest without creating a Port-Forward first. With a simple GET request this works easily using something like

	frontendResponse, err := k8s.RunKubectlAndGetOutputE(
		t,
		kubectlOptionsArgocd, "run", "curl-frontend", "--image", "alpine/curl", "--rm", "-it", "--restart=Never", "--",
		"curl", "-iLk", SomeFrontendUrl)
	if err != nil {
		t.Fatal(err)
	}

However when doing a POST Request with some JSON data this fails, although the request works when I do this manually in kubectl.

To Reproduce
I am using ArgoCD here, but I guess this should be the same for all services that unmarshal some JSON they receive from curl. I reckon this has to do with the way the JSON string is passed through Go until it reaches kubectl...

While this works

k run curl-argocd-session-token --image alpine/curl --rm -it --restart=Never -- curl -X POST -H "Content-Type: application/json", -iLk argocd-server.argocd.svc.cluster.local:443/api/v1/session -d "{\"username\":\"test-user\",\"password\":\"somePassword\"}"

This doesn't

	argoSessionToken, err := k8s.RunKubectlAndGetOutputE(
		t,
		kubectlOptionsArgocd,
		"run", "curl-argocd-session-token", "--image", "alpine/curl", "--rm", "-it", "--restart=Never", "--",
		"curl", "-X", "POST", "-H", "Content-Type: application/json", "-iLk", ArgoCdApiSessionUrl, "-d", argoCurlRequestData)
	if err != nil {
		t.Fatal(err)
	}

The following happens depending on what I format in Go

1. Input

	argoCurlRequestData := fmt.Sprintf(`{\"username\":\"test-user\",\"password\":\"%s\"}`, argoCdPassword)

Output:
{\"username\":\"test-user\",\"password\":\"somePassword\"}
Terratest Output in Console:
Running command kubectl with args [--namespace argocd run curl-argocd-session-token --image alpine/curl --rm -it --restart=Never -- curl -X POST -H Content-Type: application/json -iLk argocd-server.argocd.svc.cluster.local:443/api/v1/session -d {\"username\":\"test-user\",\"password\":\"somePassword\"}]
Error Message:
{"error":"invalid character '\\\\' looking for beginning of object key string","code":3,"message":"invalid character '\\\\' looking for beginning of object key string"}

2. Input

	argoCurlRequestData := fmt.Sprintf(`"{\"username\":\"test-user\",\"password\":\"%s\"}"`, argoCdPassword)

Output:
"{\"username\":\"test-user\",\"password\":\"somePassword\"}"
Terratest Output in Console:
Running command kubectl with args [--namespace argocd run curl-argocd-session-token --image alpine/curl --rm -it --restart=Never -- curl -X POST -H Content-Type: application/json -iLk argocd-server.argocd.svc.cluster.local:443/api/v1/session -d "{\"username\":\"test-user\",\"password\":\"somePassword\"}"]
Error Message:
{"error":"json: cannot unmarshal string into Go value of type session.SessionCreateRequest","code":3,"message":"json: cannot unmarshal string into Go value of type session.SessionCreateRequest"}

Expected behavior
The POST Request using a Curl POD works the same as if I would execute the command from a local shell using Kubectl

Versions

  • Terratest version: v0.43.13
  • Environment details (Ubuntu 20.04, Windows 10, etc.): OS X newest version

Additional context
I also tried other ways to format the String, e.g. by using backticks or even more obscure ways. No luck. I only see the option to create a Port-Forwarding or start the tests in a Docker Container inside the K8s Cluster

@rstoermer rstoermer added the bug Something isn't working label Sep 22, 2023
@rstoermer
Copy link
Author

rstoermer commented Dec 8, 2023

Also tried this with an override:

frontendResponse, err := k8s.RunKubectlAndGetOutputE(t, kubectlOptionsMongoDb, "run", "curl", "--image", "curlimages/curl", "--rm", "-it", "--restart=Never",
		"--overrides", `'{"apiVersion": "v1", "metadata": {"annotations": { "sidecar.istio.io/inject":"false" } } }'`,
		"--", "curl", "argo-argo-workflows-server.argowf.svc.cluster.local:2746")

Tried all kinds of formatting styles apart from the above one, always getting a error: Invalid JSON Patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant