Let’s build a custom kubectl plugin in Golang

  1. kubectl → Kubernetes command-line tool
  2. A kind cluster (or build any other clusters using tools like minikube, kubespray, microk8s, k3s and so on)
  1. GoLang Client for kubernetes
  2. GoLang SSH Terminal library
  3. Kubernetes Core V1 API Golang Library
func readLine(reader *bufio.Reader) (strLine string, err error) {
buffer := new(bytes.Buffer)
for {
var line []byte
var isPrefix bool
line, isPrefix, err = reader.ReadLine()
if err != nil && err != io.EOF {
return "", err
}
buffer.Write(line)
if !isPrefix {
break
}
}
return buffer.String(), err}
  1. Build the program: go build
  2. Make the golang binary an executable: sudo chmod +x ./kubectl-portScanner
  3. Place it in the PATH : sudo mv ./kubectl-portScanner /usr/local/bin
  4. You can use the plugin as: kubectl portScanner "podName" "port number" "pod namespace"
100 port is not listening for any process
9100 port is listening for a particular process

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store