简体   繁体   English

创建自定义kubernetes CLI

[英]Creating custom kubernetes cli

I have some workflows where I would like to automate kubectl with some custom golang code. 我有一些工作流程,我想使用一些自定义的golang代码自动执行kubectl。

Like Istio and others are using kubectl underneath istiocli. 像Istio一样,其他人也在istiocli下使用kubectl。

Do I need all the Kubernetes code and import some stuff or is it calling existig kubectl in a shell with os/exec? 我是否需要所有Kubernetes代码并导入一些东西,或者它是在具有os / exec的shell中调用existig kubectl的?

If you are writing golang code, I would recommend you using kubernetes client-go package to automate the workflow. 如果您正在编写golang代码,建议您使用kubernetes client-go软件包自动化工作流程。 The only thing you need to be careful is that the client version keeps changing and if it's not consistent with your kubernetes cluster api-server version, you will need to change your client package version. 您唯一需要注意的是客户端版本会不断变化,并且如果它与kubernetes集群api-server版本不一致,则需要更改客户端软件包的版本。

It looks like istio is using the k8s API client library: 看起来istio正在使用k8s API客户端库:

https://github.com/kubernetes/client-go https://github.com/kubernetes/client-go

I would expect that to perform better and allow you to provide a better experience to your users than shelling out through os/exec and exec.Command . 我希望它能比通过os/execexec.Command更好地执行,并为您的用户提供更好的体验。 It also won't require you reverse engineer or otherwise spend a bunch of cycles parsing the output from the commands. 它还不需要您进行逆向工程或以其他方式花费大量时间来解析命令的输出。

I believe the best way would be to create a kubectl plugin . 我相信最好的方法是创建一个kubectl插件

this feature allows you to extend the default set of commands available in kubectl by adding new subcommands to perform new tasks and extend the set of features available in the main distribution of kubectl. 此功能允许您通过添加新的子命令来执行新任务来扩展kubectl中可用的默认命令集,并扩展kubectl主发行版中可用的功能集。

[...] You can write a plugin in any programming language or script that allows you to write command-line commands. [...]您可以使用允许您编写命令行命令的任何编程语言或脚本来编写插件。 A plugin does not necessarily need to have a binary component. 插件不一定需要具有二进制组件。 It could rely entirely on operating system utilities like echo, sed, or grep. 它可能完全依赖于操作系统实用程序,例如echo,sed或grep。 Or it could rely on the kubectl binary. 或者它可能依赖于kubectl二进制文件。

This repository contains some examples. 该存储库包含一些示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM