# kubectl-sre-plugin
This plugin is written in python to provide addtional functionality to kubectl. Additional Function can be addedd as per needs.
# Pre-requisites:
1. Python Interpreter should be installed on the machine you are using this plugin with your kubectl.
2. Create a virtual env and install python packages
mkdir /YourDirectory
cd /YourDirectory
python -m venv venv
python -m venv venv
source venv/bin/activate
pip install kubernetes
pip install click
3. Setup kubectl appropriately before running the plugin. Ensure that the config is put in .kube/config file and appropriate context is in use
# Deployment Steps:
1. Copy the attached code to the location which has been created per pre-requisites
2. Copy the file to the same directory which has kubectl binary. You can find this out by running "which kubectl". Typically it would be in one of the bin folders like /usr/local/bin, etc
3. Rename the sre.py file to kubectl-sre in kubectl location. Note that the file should not have any extension.
4. Update the file in a text editor to include the path to your python interpreter. You can be found by running "which python" after sourcing the venv/bin/activate script as mentioned in the pre-requisite steps.
5. Update the permission of the file to 755 by running "chmod 755 kubectl-sre".
# Usage:
One the deployment steps are compete you can use the sre plugin. Currently it has three sub commands:
1. listall: This option lists out details like deployment name, namespace, Replicas per specs and currently available replicas of all the deployments in the current context. It list deployments for the namespaces by default. If--namespace switch is used witht he command it will only list deployment for the namespace. I have used listall as the function name as the list is a reserved keyword in python
usage: kubectl sre listall
Sample Output:
$kubectl sre listall
Deployments:
Namespace: default, Name: nginx-deployment, Replicas: 4, Available : 4
Namespace: kube-system, Name: coredns, Replicas: 2, Available : 2
Namespace: kube-system, Name: metrics-server, Replicas: 1, Available : 1
Namespace: prometheus, Name: prometheus-server, Replicas: 1, Available : 1```
$kubectl sre listall --namespace kube-system
Deployments:
Namespace: kube-system, Name: coredns, Replicas: 2, Available : 2
Namespace: kube-system, Name: metrics-server, Replicas: 1, Available : 1
2. info: This option proved infor about the deployment. You can provide the namespace with this option using --namespace with default value of default and --deployment to provide deployment name,
usage: kubectl sre info --deployment <deployment-name> --namespace <namespace_name>
Sample Output:
% kubectl sre info --deployment nginx-deployment --namespace nginx
Namespace: nginx, Name: nginx-deployment, Replicas-Spec: 4, Replicas-Available : None, Replicas-Unavailable : 4 Labels: None
3. scale: This option can be used to scale a deployment replicas. You need to provide --deployment switch with deployment name, namespace with --namespace switch and --replica replica count.
usage: kubectl sre scale --deployment <deplyment-name> --namespace <namespacename> --replica <replica count in integer>
Sample Output:
% kubectl sre scale --deplyment nginx-deployment --replica 4 --namespace nginx
Deployment 'nginx-deployment' scaled to 4 replicas in namespace 'nginx'.
# Alternate usage
This python code can be run directly as well.
For directly running the code, you should have kubectl configured appropriately. Also, the python interpreter path needs to be updated appropriately
Follow the steps to create a virtual env and install python packages click and kubernetes.
The commands to run the python code will be:
$python sre.py info --deployment nginx-deployment --namespace nginx
$python sre.py scale --deployment nginx-deployment --replica 4