Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved detection of shell within containers using kubectl exec #949

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions modules/kubernetes/complete.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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 clusters = $k.data | get clusters | select name cluster.server | rename name server
let data = $k.data
| get contexts
| reduce -f {completion:[], mx_ns: 0, mx_cl: 0} {|x, a|
let ns = if ($x.context.namespace? | is-empty) { '' } else { $x.context.namespace }
let max_ns = $ns | str length
let cluster = $"($x.context.user)@($clusters | where name == $x.context.cluster | get cluster_server.0)"
let cluster = $"($x.context.user)@($clusters | where name == $x.context.cluster | first | get server)"
let max_cl = $cluster | str length
$a
| upsert mx_ns (if $max_ns > $a.mx_ns { $max_ns } else $a.mx_ns)
Expand All @@ -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
Loading