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

SetUpstream fail with error "bad argument #2 to 'encode' (table expected, got boolean)" #114

Closed
daverck opened this issue Jun 23, 2022 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@daverck
Copy link

daverck commented Jun 23, 2022

kong 2.8.1.1
go-pdk v0.8.0

I made a go plugin which use the SetUpstream method to select an existing upstream.

errSetUpstream := kong.Service.SetUpstream(upstream)

The SetUpstream method is called during the access phase.
The plugin is activated for a specific route.
The upstream has only one target (xxx.xxx.xxx.xxx:8080)
The api on that target run fine.

I get the following error:

2022/06/23 06:46:09 [debug] 381368#0: *841963 [kong] pb_rpc.lua:260 before set upstream
2022/06/23 06:46:09 [error] 381368#0: *841963 [kong] init.lua:317 [kong-ls-plugin] ...cal/share/lua/5.1/kong/runloop/plugin_se
rvers/pb_rpc.lua:266: bad argument #2 to 'encode' (table expected, got boolean), client: 172.16.35.41, server: kong, request:
"GET /api/v1/me HTTP/1.1", host: "xxxxxxxxxxxxxxxxxxxxxxxx", referrer: "xxxxxxxxxxxxxxxxxxxxxxx/home"
2022/06/23 06:46:09 [info] 381367#0: *48 [kong-ls-plugin:381397] 2022/06/23 06:46:09 write unix /usr/local/kong/kong-ls-plugin
.socket->@: write: broken pipe, context: ngx.timer
2022/06/23 06:46:14 [debug] 381367#0: *842031 [lua] init.lua:295: [cluster_events] polling events from: 1655965268.725
2022/06/23 06:46:14 [info] 381368#0: *841963 client 172.16.35.41 closed keepalive connection
2022/06/23 06:46:14 [debug] 381367#0: *842047 [lua] init.lua:223: flush_counters(): [counters] pid 381367 acquired lock
2022/06/23 06:46:14 [debug] 3/home/dserck/kong-ls-plugin/proof/proof.go81370#0: *842048 [lua] init.lua:256: flush_counters(): [counters] flush done

Is this a bug ?
The same plugin in python works fine.

I stripped down my plugin to only this SetUpstream function call, and it still gives me this error.

package main

import (
	"fmt"

	"github.com/Kong/go-pdk"
	"github.com/Kong/go-pdk/server"
)

func main() {
	server.StartServer(New, Version, Priority)
}

var Version = "0.1"
var Priority = 1

type Config struct {
	Message string
}

func New() interface{} {
	return &Config{}
}

func (conf Config) Access(kong *pdk.PDK) {
	kong.Log.Debug("----- proof -----")
	var upstream = "setupstream-test"
	kong.Log.Debug(fmt.Sprintf("upstream: %s", upstream))
	kong.Log.Debug("before set upstream")
	errSetUpstream := kong.Service.SetUpstream(upstream)
	kong.Log.Debug("after set upstream")
	if errSetUpstream != nil {
		kong.Log.Debug(fmt.Sprintf("kong set upstream err %s", errSetUpstream.Error()))
	}

}

@gszr gszr added the bug Something isn't working label Feb 22, 2024
@StarlightIbuki
Copy link
Contributor

StarlightIbuki commented Feb 26, 2024

The issue seems to be caused by the incorrect typing of the return value. The return value is marked to return Empty, while it can return a flag and a possible error.

Internally tracked: KAG-3814

@StarlightIbuki StarlightIbuki added the good first issue Good for newcomers label Feb 26, 2024
gszr added a commit that referenced this issue Mar 12, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue #114.

Sister PR:
gszr added a commit that referenced this issue Mar 12, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue #114.

Sister PR: Kong/kong#12727
@gszr
Copy link
Member

gszr commented Mar 12, 2024

@daverck,

Thank you for the report; I have opened the following PRs fixing the issue:

I will keep this issue open until those are merged and I release a new version of the Go PDK.

gszr added a commit to Kong/kong that referenced this issue Mar 12, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue Kong/go-pdk#114.

Sister PR: Kong/go-pdk#191
gszr added a commit to Kong/kong that referenced this issue Mar 13, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue Kong/go-pdk#114.

Sister PR: Kong/go-pdk#191
gszr added a commit that referenced this issue Mar 13, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue #114.

Sister PR: Kong/kong#12727
gszr added a commit that referenced this issue Mar 13, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue #114.

Sister PR: Kong/kong#12727
gszr added a commit that referenced this issue Mar 13, 2024
Fix the return type for the `.Service.SetUpstream` external plugin PDK
method.

Fixes issue #114.

Sister PR: Kong/kong#12727
@gszr
Copy link
Member

gszr commented Mar 15, 2024

The fix has been merged - and is part of the v0.11.0 release of the Go PDK.
If you'd like to test and leverage the fix, pick the proto file from Kong master and give it a try.

@gszr gszr closed this as completed Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants