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/10 中 thatbenguy
的回答)
--- 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学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习