Use Kubectl Apply command using k8s.io package

kubectl commands
kubectl set env
kubectl delete pod
kubectl rollout restart
kubectl exec
kubectl run pod
kubectl stop pod
kubectl jsonpath

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.

Kubectl Reference Docs, use multiple kubeconfig files at the same time and view merged config apply manages applications through files defining Kubernetes resources. -o yaml # Get a pod's YAML # Describe commands with verbose output kubectl node --​selector='!node-role.kubernetes.io/master' # Get all running pods in  Edit This Page Install and Set Up kubectl. The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters.You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

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.

Managing Resources, 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  Run the installation command: brew install kubectl or. brew install kubernetes-cli Test to ensure the version you installed is up-to-date: kubectl version --client Install with Macports on macOS. If you are on macOS and using Macports package manager, you can install kubectl with Macports. Run the installation command:

kubectl Cheat Sheet, kubernetes/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go package apply for use in printing warnings / messages involving the base command name. # Create a service using the definition in example-service.yaml. kubectl apply -f example-service.yaml # Create a replication controller using the definition in example-controller.yaml. kubectl apply -f example-controller.yaml # Create the objects that are defined in any .yaml, .yml, or .json file within the <directory> directory. kubectl apply

Use Kubectl Apply command using k8s.io package, you may not use this file except in compliance with the License. You may obtain a copy of package cmd. import (. "flag". "fmt" "k8s.io/kubectl/pkg/cmd/apply". Using kubectl proxy. If you would like to query the API without an official client library, you can run kubectl proxy as the command of a new sidecar container in the Pod. This way, kubectl proxy will authenticate to the API and expose it on the localhost interface of the Pod, so that other containers in the Pod can use it directly. Without

kubernetes/apply.go at master · kubernetes/kubernetes · GitHub, This guide covers getting started with the kind command. If you are go get sigs.​k8s.io/kind@v0.8.1 or clone this repo and run make build from the repository. On Windows via Chocolatey (https://chocolatey.org/packages/kind) After creating a cluster, you can use kubectl to interact with it by using the  Update API Objects in Place Using kubectl patch. This task shows how to use kubectl patch to update an API object in place. The exercises in this task demonstrate a strategic merge patch and a JSON merge patch. Before you begin; Use a strategic merge patch to update a Deployment; Use a JSON merge patch to update a Deployment

kubernetes/cmd.go at master · kubernetes/kubernetes · GitHub, Command line tools reference This page describes common concepts in the Kubernetes API. (In Go this is called a Reflector and is located in the k8s.io/​client-go/cache package.) GET /api/v1/pods Accept: application/json;as=Table;​g=meta.k8s.io;v=v1beta1 --- 200 OK Content-Type: application/json { "kind": "​Table",  We worked out that whenever we did not use kubectl with the full path we hit errors when trying to create Deployment objects, either using apply with a pre-created yaml manifest or using kubectl create deployment, both times it failed. We were using kubectl by appending it's path to the main path variable on windows (as per usual).

Comments
  • 1. I am not supposed to use an external dependency. 2. I am using v1.10.3, my apply.go looks a bit different. Nevertheless, replicating the process isn't something I'm looking for... Other suggestions, idea?
  • Could you please clarify what you exactly mean with: "I am not supposed to use an external dependency". Does this mean no "kubectl" call in your program, or no "exec"?
  • If so, you could use your languages features and call the kubenetes-api directly (kubernetes.io/docs/reference/using-api/client-libraries) and then create the apply-logic yourself (github.com/kubernetes/client-go/tree/master/examples/…). It depends on your actual requirements. Something like createOrUpdate for specific resources could definitely be done with this client libraries. You can also get the current resources as JSON and compare them to only update differences ...
  • I am not supposed to use os.exec. About implementing the apply-logic using CreateOrUpdate is something I thought of too. Was looking for maybe something better, because I don't want the apply command to be restricted to a few chosen resources only.
  • I have mentioned, I'm familiar with the kubernetes go-client. I don't want the dirty approach as it violates the 12 Factor app
  • OffTopic: It would be nice if you could mention which part of the 12 Factor app manifesto the dirty approach would violate.
  • It gues all options are mentioned in this thread here. As there is no easy way to create a kubectl apply functionality in a client library, you could either use kubectl (via a plugin or directly) (see: github.com/box/kube-applier and github.com/box/kube-applier as existing references) or use a client-library and create the logic yourself. The client-libraries provide you all the methods that are necessary to do that, however it would be still quite some work. If you could for the latter it would be nice to share your solutions here and in some open source place.