Kong Custom Golang插件在Kubernetes/Helm设置中不起作用

发布于 2025-02-06 10:31:14 字数 2565 浏览 2 评论 0原文

我已经编写了自定义Golang Kong插件,称为go-wait之后的示例,来自github repo https://github.com/redhwannacef/youtube-tutorials/tree/main/main/kong-gong-gateway-custom-plugin

我唯一创建了一个唯一的区别Docker Image因此,Kong默认情况下将在其/usr/local/bin 目录中使用上述插件 这是Dockerfile,

FROM golang:1.18.3-alpine as pluginbuild
COPY ./charts/custom-plugins/ /app/custom-plugins
RUN cd /app/custom-plugins && \
    for d in ./*/ ; do (cd "$d" && go mod tidy && GOOS=linux GOARCH=amd64 go build .); done

RUN mkdir /app/all-plugin-execs && cd /app/custom-plugins && \
    find . -type f -not -name "*.*" | xargs -i cp {} /app/all-plugin-execs/

FROM kong:2.8

COPY --from=pluginbuild /app/all-plugin-execs/ /usr/local/bin/
COPY --from=pluginbuild /app/all-plugin-execs/ /usr/local/bin/plugin-ref/

# Loop through the plugin-ref directory and create an entry for all of them in 
# both KONG_PLUGINS and KONG_PLUGINSERVER_NAMES env vars respectively
# Additionally append `bundled` to KONG_PLUGINS list as without it any unused plugin will case Kong to error out

#### Example Env vars for a plugin named `go-wait`
# ENV KONG_PLUGINS=go-wait
# ENV KONG_PLUGINSERVER_NAMES=go-wait
# ENV KONG_PLUGINSERVER_GO_WAIT_QUERY_CMD="/usr/local/bin/go-wait -dump"
####
RUN cd /usr/local/bin/plugin-ref/ && \
    PLUGINS=$(ls | tr '\n' ',') && PLUGINS=${PLUGINS::-1} && \
    echo -e "KONG_PLUGINS=bundled,$PLUGINS\nKONG_PLUGINSERVER_NAMES=$PLUGINS" >> ~/.bashrc

# Loop through the plugin-ref directory and create an entry for QUERY_CMD entries needed to load the plugin
# format KONG_PLUGINSERVER_EG_PLUGIN_QUERY_CMD if the plugin name is `eg-plugin` and it should point to the 
# plugin followed by `-dump` argument
RUN cd /usr/local/bin/plugin-ref/ && \
    for f in *; do echo "$f" | tr "[:lower:]" "[:upper:]" | tr '-' '_' | \
    xargs -I {} sh -c "echo 'KONG_PLUGINSERVER_{}_QUERY_CMD=' && echo '\"/usr/local/bin/{} -dump\"' | tr [:upper:] [:lower:] | tr '_' '-'" | \
    sed -e '$!N;s/\n//' | xargs -i echo "{}" >> ~/.bashrc; done

这在Docker-Compose文件和Docker容器中正常工作。但是,当我尝试在Kubernetes环境中使用相同的图像以及Kong-ingress-controller时,我开始遇到错误“无法填写插件的默认值:go-wait”和/或日志中的许多其他错误,包括“启用插件” go-wait'但未安装”我最终无法启用它。

是否有人尝试过在Kubernetes/Helm Kong设置中进行包括GO插件。如果是这样,请为此提供一些启示

I have written custom golang kong plugin called go-wait following the example from the github repo https://github.com/redhwannacef/youtube-tutorials/tree/main/kong-gateway-custom-plugin

The only difference is I created a custom docker image so kong would have the mentioned plugin by default in it's /usr/local/bin directory
Here's the dockerfile

FROM golang:1.18.3-alpine as pluginbuild
COPY ./charts/custom-plugins/ /app/custom-plugins
RUN cd /app/custom-plugins && \
    for d in ./*/ ; do (cd "$d" && go mod tidy && GOOS=linux GOARCH=amd64 go build .); done

RUN mkdir /app/all-plugin-execs && cd /app/custom-plugins && \
    find . -type f -not -name "*.*" | xargs -i cp {} /app/all-plugin-execs/

FROM kong:2.8

COPY --from=pluginbuild /app/all-plugin-execs/ /usr/local/bin/
COPY --from=pluginbuild /app/all-plugin-execs/ /usr/local/bin/plugin-ref/

# Loop through the plugin-ref directory and create an entry for all of them in 
# both KONG_PLUGINS and KONG_PLUGINSERVER_NAMES env vars respectively
# Additionally append `bundled` to KONG_PLUGINS list as without it any unused plugin will case Kong to error out

#### Example Env vars for a plugin named `go-wait`
# ENV KONG_PLUGINS=go-wait
# ENV KONG_PLUGINSERVER_NAMES=go-wait
# ENV KONG_PLUGINSERVER_GO_WAIT_QUERY_CMD="/usr/local/bin/go-wait -dump"
####
RUN cd /usr/local/bin/plugin-ref/ && \
    PLUGINS=$(ls | tr '\n' ',') && PLUGINS=${PLUGINS::-1} && \
    echo -e "KONG_PLUGINS=bundled,$PLUGINS\nKONG_PLUGINSERVER_NAMES=$PLUGINS" >> ~/.bashrc

# Loop through the plugin-ref directory and create an entry for QUERY_CMD entries needed to load the plugin
# format KONG_PLUGINSERVER_EG_PLUGIN_QUERY_CMD if the plugin name is `eg-plugin` and it should point to the 
# plugin followed by `-dump` argument
RUN cd /usr/local/bin/plugin-ref/ && \
    for f in *; do echo "$f" | tr "[:lower:]" "[:upper:]" | tr '-' '_' | \
    xargs -I {} sh -c "echo 'KONG_PLUGINSERVER_{}_QUERY_CMD=' && echo '\"/usr/local/bin/{} -dump\"' | tr [:upper:] [:lower:] | tr '_' '-'" | \
    sed -e '$!N;s/\n//' | xargs -i echo "{}" >> ~/.bashrc; done

This works fine in the docker-compose file and docker container. But when I tried to use the same image in the kubernetes environment along with kong-ingress-controller, I started running into errors "failed to fill-in defaults for plugin: go-wait" and/or a bunch of other errors including "plugin 'go-wait' enabled but not installed" in the logs and I ended up not being able to enable it.

Has anyone tried including go plugins in their kubernetes/helm kong setup. If so please shed some light on this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2025-02-13 10:31:14

更新:找到了我要寻找的答案,以及设置图像生成的环境变量,在_helpers.tpl kong Helm图表本身的文件中进行了修改。
原因是在部署图中,配置期望将插件配置为values-custom.yml用于覆盖默认设置。

但是,Helm图似乎特定于通过configmaps加载的值和插件,该插件原来是一个巨大的瓶颈,因为您将在Golang中生成的任何二进制插件都将超过允许的最大值kubernetes中configmaps的限制。
这就是我在这项工作中提出的全部原因,以使插件的一部分成为图像的一部分。

tl; dr
我能够将存储库克隆到本地系统,对以下补丁进行更改,以从值中加载插件,而无需使用LUA插件来俱乐部。 (信用:的答案theBenguy从讨论中 https://discuss.konghq.com/t/how-to-to-to-load-load-go-plugins-using-kong-helm-chart/5717/10

--- a/charts/kong/templates/_helpers.tpl
+++ b/charts/kong/templates/_helpers.tpl
@@ -530,6 +530,9 @@ The name of the service used for the ingress controller's validation webhook

 {{- define "kong.plugins" -}}
 {{ $myList := list "bundled" }}
+{{- range .Values.plugins.goPlugins -}}
+{{- $myList = append $myList .pluginName -}}
+{{- end -}}
 {{- range .Values.plugins.configMaps -}}
 {{- $myList = append $myList .pluginName -}}
 {{- end -}}

将以下块添加到我的值custom.yml,我很高兴。

希望这对其他任何人也都试图在Golang中为Helm图表使用自定义插件。

env:
  database: "off"
  plugins: bundled,go-wait
  pluginserver_names: go-wait
  pluginserver_go_wait_query_cmd: "/usr/local/bin/go-wait -dump"
  

plugins:
  goPlugins:
    - pluginName: "go-wait"

注意:请记住,所有这些仍然取决于您在图像中使用预制的自定义孔插件,就我而言,我已经从上面的Dockerfile内容(有问题)构建了图像,并将其推向了我自己Docker Hub Repo并使用以下块PS替换了values-custom.yml的图像

image:
  repository: chalukyaj/kong-custom-image
  tag: "1.0.1"

:正如你们可能已经注意到的那样,我对此唯一的失望是,环境变量不能只是从docker image的〜1中选择。 /.bashrc,这会让它变得很棒。但是,尽管如此,这也起作用了,我找不到一个帖子,该帖子显示了如何使用新的Go-PDK(而不是较旧的Go-Pluginserver)来构建GO插件并将其用于掌舵。

Update: Found the answer I was looking for, along with setting the environment variables generated by the image, there's modifications in the _helpers.tpl file of the kong helm chart itself.
The reason is that in the deployment charts, the configuration expects plugins to be configured in values-custom.yml used to override the default settings.

But the helm chart seems to be specific to values and plugins being loaded via configMaps which turned out to be a huge bottleneck, as any binary plugin you will generate in golang for kong is going to exceed the maximum allowed limit of the configMaps in kubernetes.
That's the whole reason I had set out on this endeavor to make the plugins part of my image.

TL;dr
I was able to clone the repo to my local system, make the changes for the following patch for loading the plugins from values without having to club them with the lua plugins. (Credits : Answer of thatbenguy from the discussion https://discuss.konghq.com/t/how-to-load-go-plugins-using-kong-helm-chart/5717/10)

--- a/charts/kong/templates/_helpers.tpl
+++ b/charts/kong/templates/_helpers.tpl
@@ -530,6 +530,9 @@ The name of the service used for the ingress controller's validation webhook

 {{- define "kong.plugins" -}}
 {{ $myList := list "bundled" }}
+{{- range .Values.plugins.goPlugins -}}
+{{- $myList = append $myList .pluginName -}}
+{{- end -}}
 {{- range .Values.plugins.configMaps -}}
 {{- $myList = append $myList .pluginName -}}
 {{- end -}}

Add the following block to my values-custom.yml and I was good to go.

Hopefully this helps anyone else also trying to write custom plugins for kong in golang for use in helm charts.

env:
  database: "off"
  plugins: bundled,go-wait
  pluginserver_names: go-wait
  pluginserver_go_wait_query_cmd: "/usr/local/bin/go-wait -dump"
  

plugins:
  goPlugins:
    - pluginName: "go-wait"

NOTE : Please remember all this still depends on you having the prebuilt custom kong plugins in your image, in my case I had built an image from the above dockerfile contents (in question) and pushed that to my own docker hub repo and replaced the image in the values-custom.yml using the following block

image:
  repository: chalukyaj/kong-custom-image
  tag: "1.0.1"

PS: As you guys might have noticed, the only disappointment I have with this is that the environment variables couldn't just be picked from the docker image's ~/.bashrc, which would have made this awesome. But nonetheless, this works, and I couldn't find a single post which showed how to use the new go-pdk (instead of the older go-pluginserver) to build the go plugins and use them in helm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文