Skip to content

Commit

Permalink
fix: query params in runtime request
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Aug 15, 2024
1 parent 8376c64 commit c2da1fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 8 additions & 4 deletions runtime/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var tests = []testcase{
},
Token: "my fake token",
Method: "GET",
URL: "/api/v1/namespaces/default/services/",
URL: "/api/v1/namespaces/another/services/",
},
{
NewReqWrapper: func(namespace string, options *runtime.RuntimeOptions) *request {
Expand All @@ -40,7 +40,7 @@ var tests = []testcase{
},
Token: "my fake token",
Method: "GET",
URL: "/apis/apps/v1/namespaces/default/deployments/?labelSelector=foo%3Dbar",
URL: "/apis/apps/v1/namespaces/another/deployments/?labelSelector=foo%3Dbar",
},
}

Expand All @@ -55,9 +55,13 @@ var wrappedHandler = func(test *testcase, t *testing.T) http.HandlerFunc {
}

func TestKubernetes(t *testing.T) {
for _, test := range tests {
for i, test := range tests {
ts := httptest.NewServer(wrappedHandler(&test, t))
req := test.NewReqWrapper("default", &runtime.RuntimeOptions{
namespace := "default"
if i%2 == 0 {
namespace = "another"
}
req := test.NewReqWrapper(namespace, &runtime.RuntimeOptions{
Host: ts.URL,
BearerToken: test.Token,
Client: &http.Client{},
Expand Down
13 changes: 8 additions & 5 deletions runtime/kubernetes/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"

"github.com/w-h-a/pkg/runtime"
"github.com/w-h-a/pkg/telemetry/log"
)

type request struct {
Expand Down Expand Up @@ -43,10 +42,16 @@ func (r *request) setResource(n string) *request {
func (r *request) setParams(p *params) *request {
for k, v := range p.labelSelector {
// create new key=value
pair := fmt.Sprintf("%s=%s", k, v)
value := fmt.Sprintf("%s=%s", k, v)

// check if there is already a value
label := r.params.Get("labelSelector")
if len(label) > 0 {
value = fmt.Sprintf("%s,%s", label, value)
}

// set it
r.params.Set("labelSelector", pair)
r.params.Set("labelSelector", value)
}

return r
Expand Down Expand Up @@ -81,8 +86,6 @@ func (r *request) request() (*http.Request, error) {
url += "?" + r.params.Encode()
}

log.Infof("HERE IS MY URL: %+v", url)

// build request
req, err := http.NewRequest(r.method, url, r.body)
if err != nil {
Expand Down

0 comments on commit c2da1fc

Please sign in to comment.