Skip to content

Instantly share code, notes, and snippets.

View caruccio's full-sized avatar
😐
state = not in mood

Mateus Caruccio caruccio

😐
state = not in mood
View GitHub Profile
@caruccio
caruccio / kpfz
Created May 10, 2024 20:30
Kubectl port-forward with fzf service selector
#!/bin/bash
##
## kubectl port-forward on selected services, combined by all service ports.
##
function kpfz()
{
local svc_ports_command="jq -r '.items[] |
{
name: .metadata.name,
@caruccio
caruccio / kubectl-top_node_pod
Last active May 10, 2024 14:30
Kubectl "top pods-" on a given node
#!/bin/bash
function usage()
{
echo Usage: $0 '[--cpu/-c|--memory/-m]' nodes...
echo Sort by memory is the default
exit ${1:-0}
}
node_names=()
@caruccio
caruccio / kubecfg
Created May 10, 2024 14:27
Use local kubeconfig on dir change
#!/bin/bash
COLOR_LIGHT_GREEN="$(tput setaf 10)"
COLOR_RESET="$(tput sgr0)"
function kubecfg()
{
if [ $# -eq 0 ]; then
echo $KUBECONFIG
return
@caruccio
caruccio / pause.sh
Last active April 20, 2024 15:41
Pause processes during night
#!/bin/bash
#
# This script will stop a list of running proccesses (var PROCS), disable all monitors and CPUs (except cpu-0).
#
# Usage:
# pause [duration]
#
# With no parameters it will pause right alway.
# If [duration] is specified, it will detect for inactivity and pause, in an infinite loop. Use CTRL+C to exit.
# [duration] accepts the same as the command sleep.
@caruccio
caruccio / migrate-pv-to-zone.sh
Last active April 17, 2024 20:13
Migrate EBS Volume based PVs across AWS availability zones
#!/bin/bash
if [ $# -lt 6 ]; then
echo "Usage: $0 [source-namespace] [source-pvc-name] [target-namespace] [target-pvc-name] [target-aws-zone] [target-pv-name] [kind/workload=None]"
echo "Clone EBS, PV and PVC from source to target. Will stop kind/workload if defined."
exit
fi
set -eu
@caruccio
caruccio / latest-version.md
Last active March 18, 2024 16:22
Print all latest patch-level version

How to get the latest patch-level version from a list of distinct versions using bash and awk. This idea was taken from https://stackoverflow.com/a/21103864/1006369

Supose you have the following list of versions:

v1.22.8
v1.22.9
v1.22.10   <-- latest v1.22 series
v1.23.1
@caruccio
caruccio / kpfz.md
Created February 6, 2024 12:13
Kubectl port-forward with fzf

If you have fuzzy finder, use the following functions to filter for multiple kubernetes services and port-foward using a stable and unique local port.

Put the code below into your ~/.bashrc (or any place you source from)

kpfz ()
{ 
    local svcs=$(FZF_DEFAULT_COMMAND="command kubectl get svc -o json $_NS | svc-ports" fzf -m --ansi --no-preview -1 --exact --select-1 --query="$@");
    if [ -z "$svcs" ]; then
 return;
@caruccio
caruccio / termseq.py
Created January 18, 2024 01:03
Simple Python terminal sequence
class TermSequence:
'''Reference: https://stackoverflow.com/a/33206814/1006369
'''
sequence = []
# standard 4-bit color codes
black = 30
red = 31
green = 32
yellow = 33
@caruccio
caruccio / multi-value-array.sh
Last active January 6, 2024 22:30
Bash multi-value arrays
#
# Given an array of multi-value items, transform each value into a new array replacing its
# separator for a space, then access each individual inner-value by index:
#
ITEMS=(
cup:blue:3
phone:black:15
pen:red:1
)
@caruccio
caruccio / bash-path-vars
Last active December 28, 2023 22:47
Path manipulation with bash vars
$ FILE=/some/path/to/file.txt
###################################
### Remove matching suffix pattern
###################################
$ echo ${FILE%.*} # remove ext
/some/path/to/file
$ FILE=/some/path/to/file.txt.jpg.gpg # note various file exts