Creating custom kubernetes cli

8/30/2018

I have some workflows where I would like to automate kubectl with some custom golang code.

Like Istio and others are using kubectl underneath istiocli.

Do I need all the Kubernetes code and import some stuff or is it calling existig kubectl in a shell with os/exec?

-- Chris G.
go
kubernetes

3 Answers

8/30/2018

If you are writing golang code, I would recommend you using kubernetes client-go package to automate the workflow. 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.

-- Cindy
Source: StackOverflow

8/30/2018

It looks like istio is using the k8s API client library:

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. It also won't require you reverse engineer or otherwise spend a bunch of cycles parsing the output from the commands.

-- stderr
Source: StackOverflow

8/30/2018

I believe the best way would be to create a kubectl plugin.

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.

[...] 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. Or it could rely on the kubectl binary.

This repository contains some examples.

-- Jose Armesto
Source: StackOverflow