diff --git a/modules/kubernetes/complete.nu b/modules/kubernetes/complete.nu index 3c09cb374..cdce4b510 100644 --- a/modules/kubernetes/complete.nu +++ b/modules/kubernetes/complete.nu @@ -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) @@ -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} } @@ -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)'}) @@ -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 diff --git a/modules/kubernetes/helm.nu b/modules/kubernetes/helm.nu index e81d444ce..a3cd486bb 100644 --- a/modules/kubernetes/helm.nu +++ b/modules/kubernetes/helm.nu @@ -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 } @@ -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 { diff --git a/modules/kubernetes/kadm.nu b/modules/kubernetes/kadm.nu new file mode 100644 index 000000000..87b9d30e7 --- /dev/null +++ b/modules/kubernetes/kadm.nu @@ -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 + } +} diff --git a/modules/kubernetes/mod.nu b/modules/kubernetes/mod.nu index 6c0e3ed75..c18f2add8 100644 --- a/modules/kubernetes/mod.nu +++ b/modules/kubernetes/mod.nu @@ -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 @@ -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 @@ -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 } @@ -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 { @@ -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) } } } @@ -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) } } } @@ -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