登录
首页 >  Golang >  Go问答

Kubernetes/Helm 中设置的 Kong 自定义 Golang 插件无效化

来源:stackoverflow

时间:2024-02-14 11:57:24 466浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《Kubernetes/Helm 中设置的 Kong 自定义 Golang 插件无效化》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

我按照 github 存储库中的示例编写了名为 go-wait 的自定义 golang kong 插件 https://github.com/redhwannacef/youtube-tutorials/tree/main/kong-gateway-custom-plugin

唯一的区别是我创建了一个自定义 docker 映像,因此 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 一起使用相同的映像时,我开始遇到错误 "failed to fill-in defaults for plugin: go-wait" 和/或一堆其他错误日志中包括 “插件‘go-wait’已启用但未安装”,但我最终无法启用它。

有没有人尝试过在他们的 kubernetes/helm kong 设置中包含 go 插件。如果是这样,请对此进行一些说明


正确答案


更新:找到了我一直在寻找的答案,除了设置图像生成的环境变量之外,kong helm 图表本身的 _helpers.tpl 文件也有修改。 原因是在部署图表中,配置期望在 value-custom.yml 中配置插件,用于覆盖默认设置。

但是 helm 图表似乎特定于通过 configmaps 加载的值和插件,这被证明是一个巨大的瓶颈,因为您在 golang 中为 kong 生成的任何二进制插件都将超过 configmaps 允许的最大限制在 kubernetes 中。 这就是我开始努力让插件成为我形象的一部分的全部原因。

tl;博士 我能够将存储库克隆到本地系统,对以下补丁进行更改,以便从值加载插件,而无需将它们与 lua 插件结合在一起。 (来源:讨论 https://discuss.konghq.com/t/how-to-load-go-plugins-using-kong-helm-chart/5717/10thatbenguy 的回答)

--- 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 -}}

将以下块添加到我的 value-custom.yml 中,我就可以开始了。

希望这可以帮助其他尝试在 golang 中为 kong 编写自定义插件以在 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"

注意:请记住,这一切仍然取决于您的镜像中是否有预构建的自定义 kong 插件,在我的例子中,我已经从上面的 dockerfile 内容(有问题)构建了一个镜像并推送了该镜像到我自己的 docker hub 存储库,并使用以下块替换 value-custom.yml 中的图像

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

ps:正如你们可能已经注意到的,我对此唯一的失望是环境变量不能仅仅从 docker 镜像的 ~/.bashrc 中选取,这会让这变得很棒。但尽管如此,这仍然有效,而且我找不到一篇文章来展示如何使用新的 go-pdk(而不是旧的 go-pluginserver)来构建 go 插件并在 helm 中使用它们。

以上就是《Kubernetes/Helm 中设置的 Kong 自定义 Golang 插件无效化》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>