Skip to content

Commit

Permalink
Improved detection of shell within containers using kubectl exec
Browse files Browse the repository at this point in the history
New command: kadm update certs
Syntax updated
  • Loading branch information
nashvi committed Sep 15, 2024
1 parent d04eea6 commit 4ecb350
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
8 changes: 4 additions & 4 deletions modules/kubernetes/complete.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use utils.nu *

export def "nu-complete kube ctx" [] {
let k = (kube-config)
let cache = $'($env.HOME)/.cache/nu-complete/k8s/($k.path | path basename).json'
let cache = ([$nu.data-dir 'cache' 'k8s'] | path join $'($k.path | path basename).json')
let data = ensure-cache-by-lines $cache $k.path { ||
let clusters = $k.data | get clusters | select name cluster.server
let data = $k.data
Expand Down Expand Up @@ -38,7 +38,7 @@ export def "nu-complete kube ns" [] {

export def "nu-complete kube kind" [] {
let ctx = (kube-config)
let cache = $'($env.HOME)/.cache/nu-complete/k8s-api-resources/($ctx.data.current-context).json'
let cache = ([$nu.data-dir 'cache' 'k8s-api-resources'] | path join $'($ctx.data.current-context).json')
ensure-cache-by-lines $cache $ctx.path {||
kubectl api-resources | from ssv -a
| each {|x| {value: $x.NAME description: $x.SHORTNAMES} }
Expand Down Expand Up @@ -86,7 +86,7 @@ export def "nu-complete kube jsonpath" [context: string] {
let m = kubectl get ...$ns $kind $res $"--output=jsonpath={($p)}" | from json
let l = $row | last
let c = do -i {$m | get $l}
if ($c | is-not-empty) and ($c | describe | str substring 0..5) == 'table' {
if ($c | is-not-empty) and ($c | describe | str substring 0..<5) == 'table' {
$r = (0..(($c | length) - 1) | each {|x| $'($p).($l)[($x)]'})
} else {
$r = ($m | columns | each {|x| $'($p).($x)'})
Expand Down Expand Up @@ -150,7 +150,7 @@ export def "nu-complete kube port" [context: string, offset: int] {
}

export def "nu-complete kube cp" [cmd: string, offset: int] {
let ctx = $cmd | str substring ..$offset | argx parse
let ctx = $cmd | str substring ..<$offset | argx parse
let p = $ctx._args | get (($ctx._args | length) - 1)
let ns = $ctx.namespace? | with-flag -n
let c = $ctx.container? | with-flag -c
Expand Down
4 changes: 2 additions & 2 deletions modules/kubernetes/helm.nu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def "nu-complete helm list" [context: string, offset: int] {
def "nu-complete helm charts" [context: string, offset: int] {
let ctx = $context | argx parse
let path = $ctx | get _pos.chart?
let paths = do -i { ls ($"($path)*" | into glob) | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} }
let paths = do -i { ls ($"($path)*/**/Chart.yaml" | into glob) | each { $in.name | path dirname } }
helm repo list | from ssv -a | rename value description
| append $paths
}
Expand All @@ -36,7 +36,7 @@ export def kgh [
| from json
| update updated {|x|
$x.updated
| str substring ..-4
| str substring ..<-4
| into datetime -f '%Y-%m-%d %H:%M:%S.%f %z'
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions modules/kubernetes/kadm.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export def "kadm update certs" [] {
sudo kubeadm certs check-expiration
if ([n y] | input list 'renew all') == 'y' {
sudo kubeadm certs renew all
}
}
37 changes: 24 additions & 13 deletions modules/kubernetes/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export use helm.nu *
export use conf.nu *
export use resources.nu *
export use compose.nu *
export use kadm.nu *

def krefine [kind] {
let obj = $in
Expand Down Expand Up @@ -297,7 +298,20 @@ export def --wrapped ka [
} else {
[-c $container]
}
kubectl exec ...$n -it $pod ...$c -- ...(if ($args|is-empty) {['bash']} else { $args })
let args = if ($args | is-empty) {
let cmd = [
'/usr/local/bin/nu'
'/bin/nu'
'/bin/bash'
'/bin/sh'
]
| str join ' '
| $"for sh in ($in); do if [ -e $sh ]; then exec $sh; fi; done"
['/bin/sh' -c $cmd]
} else {
$args
}
kubectl exec ...$n -it $pod ...$c -- ...$args
}

# kubectl logs
Expand All @@ -314,7 +328,7 @@ export def kl [
let f = if $follow {[-f]} else {[]}
let p = if $previous {[-p]} else {[]}
let trg = if ($pod | str ends-with '-') {
$"deployment/($pod | str substring ..-1)"
$"deployment/($pod | str substring ..<-1)"
} else {
$pod
}
Expand Down Expand Up @@ -513,8 +527,8 @@ export def ktp [
{
namespace: $x.namespace
name: $x.name
cpu: ($x.cpu| str substring ..-1 | into float)
mem: ($x.mem | str substring ..-2 | into float)
cpu: ($x.cpu| str substring ..<-1 | into float)
mem: ($x.mem | str substring ..<-2 | into float)
}
}
} else {
Expand All @@ -523,8 +537,8 @@ export def ktp [
| each {|x|
{
name: $x.name
cpu: ($x.cpu| str substring ..-1 | into float)
mem: ($x.mem | str substring ..-2 | into float)
cpu: ($x.cpu| str substring ..<-1 | into float)
mem: ($x.mem | str substring ..<-2 | into float)
}
}
}
Expand All @@ -535,10 +549,10 @@ export def ktno [] {
kubectl top node | from ssv -a | rename name cpu pcpu mem pmem
| each {|x| {
name: $x.name
cpu: ($x.cpu| str substring ..-1 | into float)
cpu%: (($x.pcpu| str substring ..-1 | into float) / 100)
mem: ($x.mem | str substring ..-2 | into float)
mem%: (($x.pmem | str substring ..-1 | into float) / 100)
cpu: ($x.cpu| str substring ..<-1 | into float)
cpu%: (($x.pcpu| str substring ..<-1 | into float) / 100)
mem: ($x.mem | str substring ..<-2 | into float)
mem%: (($x.pmem | str substring ..<-1 | into float) / 100)
} }
}

Expand All @@ -560,9 +574,6 @@ export def "kclean stucked ns" [ns: string] {

export alias "kclean finalizer" = kubectl patch -p '{\"metadata\":{\"finalizers\":null}}'

export alias "kadm check" = kubeadm certs check-expiration
export alias "kadm renew" = kubeadm certs renew all

### cert-manager
export def kgcert [] {
kubectl get certificates -o wide | from ssv | rename certificates
Expand Down

0 comments on commit 4ecb350

Please sign in to comment.