Created
November 1, 2023 17:13
-
-
Save itsvinayak/5423995de96d802e58e22041a18036eb to your computer and use it in GitHub Desktop.
π Elevate your Kubernetes game with our powerful Bash script! Introducing a seamless solution designed for efficiency and productivity. Say goodbye to manual alias setup β our script automates the process, configuring essential Kubernetes aliases in your .bashrc file effortlessly. Supercharge your workflow and boost productivity with just a few β¦
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to add or update an alias in .bashrc | |
add_alias_to_bashrc() { | |
local alias_name="$1" | |
local alias_command="$2" | |
# Check if the alias already exists in .bashrc | |
if grep -q "^alias $alias_name=" ~/.bashrc; then | |
# Update the existing alias | |
sed -i "s/^alias $alias_name=.*/alias $alias_name='$alias_command'/" ~/.bashrc | |
else | |
# Add a new alias to .bashrc | |
echo "alias $alias_name='$alias_command'" >> ~/.bashrc | |
fi | |
} | |
# Add or update aliases | |
# General: | |
add_alias_to_bashrc "k" "kubectl" | |
add_alias_to_bashrc "kg" "kubectl get" | |
add_alias_to_bashrc "kgp" "kubectl get pods" | |
add_alias_to_bashrc "kgs" "kubectl get services" | |
add_alias_to_bashrc "kgc" "kubectl get deployments" | |
add_alias_to_bashrc "kga" "kubectl get all" | |
add_alias_to_bashrc "kd" "kubectl describe" | |
add_alias_to_bashrc "kdp" "kubectl describe pods" | |
add_alias_to_bashrc "kds" "kubectl describe services" | |
add_alias_to_bashrc "kdc" "kubectl describe deployments" | |
add_alias_to_bashrc "kda" "kubectl describe all" | |
add_alias_to_bashrc "krm" "kubectl delete" | |
# Editing Kubernetes Resources with Editor: | |
add_alias_to_bashrc "ke" "kubectl edit" | |
# Applying Kubernetes Configurations: | |
add_alias_to_bashrc "ka" "kubectl apply -f" | |
# Rollout Commands for Deployments: | |
add_alias_to_bashrc "krl" "kubectl rollout" | |
add_alias_to_bashrc "krlr" "kubectl rollout restart" | |
add_alias_to_bashrc "krlh" "kubectl rollout history" | |
# Debugging Pods: | |
add_alias_to_bashrc "kl" "kubectl logs" | |
add_alias_to_bashrc "klf" "kubectl logs -f" | |
# Context and Configuration: | |
add_alias_to_bashrc "kctx" "kubectl config use-context" | |
add_alias_to_bashrc "kconfig" "kubectl config get-contexts" | |
# Kubectl Autocomplete: | |
source <(kubectl completion bash) | |
# Add more aliases as needed | |
# Apply the changes to the current shell session | |
source ~/.bashrc | |
echo "Aliases added/updated in ~/.bashrc." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When creating an alias for
kubectl logs
command with a specific pod name, you can't dynamically provide the pod name as an argument to the alias. However, you can create a function instead of an alias to achieve dynamic pod name input. Here's how you can do it:In this example, replace
<pod-name>
with the actual name of the pod you want to view logs for. When you runprevious_logs <pod-name>
, it will substitute<pod-name>
with the provided pod name and execute thekubectl logs
command accordingly.Please note that you cannot achieve this level of dynamic substitution with a simple alias; you'll need to use a function or a script for this kind of parameterized behavior.