Skip to content

Instantly share code, notes, and snippets.

@noizo
Last active March 8, 2024 14:51
Show Gist options
  • Save noizo/f9b9fb4ce420885f74308193ab70405b to your computer and use it in GitHub Desktop.
Save noizo/f9b9fb4ce420885f74308193ab70405b to your computer and use it in GitHub Desktop.
awsume wrapper with console-plugin and multicontainer firefox profiles + eks function to select a cluster and a namespace
as() {
# awsume wrapper function
# pip install awsume
# pip install awsume-console-plugin
# https://addons.mozilla.org/en-US/firefox/addon/multi-account-containers/
# https://addons.mozilla.org/en-US/firefox/addon/open-url-in-container/
# curl -sL https://raw.githubusercontent.com/honsiorovskyi/open-url-in-container/master/bin/launcher.sh | sudo tee ~/.local/bin/firefox-container > /dev/null
# chmod +x ~/.local/bin/firefox-container
#
# .awsume/config.yaml
# colors: true
# console:
# browser_command: ~/.local/bin/firefox-container --orange --fingerprint --name={profile} "{url}"
# fuzzy-match: true
# role-duration: 0
local profile="$1"
export FIREFOX="/Applications/Firefox.app/Contents/MacOS/firefox"
# If no arguments were passed, use fzf to select a profile and exit early
if [[ -z "${profile}" ]]; then
profile=$(awsume -l | awk 'NR > 4 {print $1}' | fzf)
. awsume "${profile}"
return
fi
shift # Shift the arguments now that we know at least one was provided
# If profile is non-empty, use 'awsume' with the profile and any provided args
if [[ -n "${profile}" ]]; then
. awsume "${profile}" "$@"
fi
# EKS_PROFILE for eks function
export EKS_PROFILE="${profile}"
}
eks() {
# The eks function is a utility for managing Kubernetes configurations in Amazon EKS.
# It uses awsume and fzf tools to provide interactive prompts for AWS profile,
# EKS cluster, and Kubernetes namespace selection. The function is responsible
# for creating or updating a custom Kubeconfig file for the selected EKS cluster
# and setting the KUBECONFIG environment variable accordingly.
# Check if EKS_PROFILE is set; if not, prompt the user to select a profile
if [[ -z "${EKS_PROFILE}" ]]; then
echo "EKS_PROFILE is not set. Selecting the current AWS profile..."
local eks_profile=$(awsume -l | awk 'NR > 4 {print $1}' | fzf)
if [[ -z "${eks_profile}" ]]; then
echo "No AWS profile selected. Exiting."
return 1
fi
. awsume "${eks_profile}"
else
local eks_profile=${EKS_PROFILE}
fi
# Use the AWS profile to list clusters and select one
local cluster=$(EKS_PROFILE=${eks_profile} aws eks list-clusters | jq '.clusters[]' -r | fzf)
if [[ -z "${cluster}" ]]; then
echo "No cluster selected. Exiting."
return 1
fi
# Define a custom config file path based on the AWS profile
local kubeconfig_path="${HOME}/.kube/config-${eks_profile}"
# Update or create the kubeconfig file for the selected cluster
if ! EKS_PROFILE=${eks_profile} aws eks update-kubeconfig --name "${cluster}" --kubeconfig "${kubeconfig_path}"; then
echo "Failed to update kubeconfig"
return 1
fi
# Optionally, set KUBECONFIG environment variable to use the new config file
export KUBECONFIG=${kubeconfig_path}
echo "KUBECONFIG set to ${kubeconfig_path}"
# Execute namespace selection. fzf required
echo "Select a namespace now:"
kubens
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment