Use Kubectl Apply command using k8s.io package

10/25/2018

I need to add kubectl apply functionality to my application.

I've looked through kubectl go-client, it has no provisions for the apply command.

  1. Can I create an instance of kubectl in my go-application?
  2. If not 1, can I use the k8s.io/kubernetes package to emulate an kubectl apply command?

Questions and clarifications if needed, will be given.

-- Sufiyan Parkar
go
kubernetes

3 Answers

10/25/2018
  • kubectl is supposed to be used from command line only
  • but you can wraped it around inside the code using some form of exec such os.system in python , similar will exist in golang import "os/exec", but this approach is dirty
  • you need to use the client libary in your code to carry out the operations
  • list of client libraries is here
-- Ijaz Ahmad Khan
Source: StackOverflow

10/25/2018

This can be done by created and adding a plugin to kubectl.

You can write a plugin in any programming language or script that allows you to write command-line commands.

There is no plugin installation or pre-loading required. Plugin executables receive the inherited environment from the kubectl binary. A plugin determines which command path it wishes to implement based on its name. For example, a plugin wanting to provide a new command kubectl foo, would simply be named kubectl-foo, and live somewhere in the user’s PATH.

Example plugin can look as follows:

#!/bin/bash
# optional argument handling
if [[ "$1" == "version" ]]
then
  echo "1.0.0"
  exit 0
fi

# optional argument handling
if [[ "$1" == "config" ]]
then
  echo $KUBECONFIG
  exit 0
fi
echo "I am a plugin named kubectl-foo"

After that you just make it executable chmod +x ./kubectl-foo and move it for in your path mv ./kubectl-foo /usr/local/bin.

Now you should be able to call it by kubectl foo:

$ kubectl foo
I am a plugin named kubectl-foo

All args and flags are passed as-is to the executable:

$ kubectl foo version
1.0.0

You can read more about the kubectl plugins inside Kubernetes Extend kubectl with plugins documentation.

-- Crou
Source: StackOverflow

10/25/2018
  1. Can I create an instance of kubectl in my application?

You can wrap the kubectl command in your application and start it in a new child-process, like you would do via a shell-script. See the exec package in go for more information: https://golang.org/pkg/os/exec/

This works pretty good for us and kubectl usually has the -o-Parameter that lets you control the output format, so you get machine readable text back.

There are already some open-source projects that use this approach:

  1. If not 1, can I use the k8s.io/kubernetes package to emulate an kubectl apply command?

Have you found https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/apply/apply.go while searching in kubectl-source code? Take a look at the run-function:

func (o *ApplyOptions) Run() error { ... r := o.Builder. Unstructured(). Schema(o.Validator). ContinueOnError(). NamespaceParam(o.Namespace).DefaultNamespace(). FilenameParam(o.EnforceNamespace, &o.DeleteOptions.FilenameOptions). LabelSelectorParam(o.Selector). IncludeUninitialized(o.ShouldIncludeUninitialized). Flatten(). Do() ... err = r.Visit(func(info *resource.Info, err error) error { ...

It is not very good readable it guess but this is what kubectl apply does. Maybe one possible way of whould be to debug the code and see what is does further more.

-- Peter Ittner
Source: StackOverflow