-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_utils.sh
executable file
·59 lines (45 loc) · 1.29 KB
/
kubernetes_utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
#####
## This file contains utility functions to interact with kubernetes
## Some functions require the `ES_PORT` variable to be set
##
## NOTE: To avoid name clashes with other scripts
## * Functions used outside of this script are prefixed with `kubernetes_`
## * Helper functions for this script are prefixed with `kubernetes_utils_`
##
## Please stick to the guidelines
#####
##### dependiencies:
##### logger.sh
##### utils.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/logger.sh"
source "${SCRIPT_DIR}/utils.sh"
##### end of dependencies
kubernetes_get_contexts() {
kubectl config get-contexts --output name
}
kubernetes_get_current_context() {
kubectl config current-context
}
kubernetes_set_context() {
local context="$1"
_validate_is_set context || return 1
log INFO "Setting kubernetes context to: $context"
kubectl config use-context "$context" >/dev/null || {
log ERROR "Failed to set context to: $context"
return 1
}
}
kubernetes_port_forward() {
local namespace="$1"
local target="$2"
local port="$3"
kubectl port-forward \
--namespace "${namespace}" \
"${target}" \
"${port}:9200" >/dev/null &
local pid=$!
log INFO "Port-forward to ${target} started with PID: $pid"
echo "$pid"
}