1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

nu-complete kube deploys and pods (#682)

- update `kadm check` and `kadm renew`
- refactor `nu-complete kube pods or deploys` to `nu-complete kube
deploys and pods`

in #680, `nu-complete kube pods` changed to `pods or deploys`, This has
caused some problems:
- double the number of candidates, and candidates with prefix `pods/`
and `deployment.apps/`, looks a little confused
- two requests in one completion, slower

i wrote `nu-complete kube deploys and pods`, it's completed in two
stages
- first it will complete the `deployment` and add a suffix `-` when
token not ends-with `-`
- when `ka` or `kl` accepts an argument with suffix `-`, it'll be
rewritten as `$"deployment/($pod | str substring ..-1)"`
- if token ends-with `-`, it's considered unfinished `pod` and continues

---------

Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
fj0r 2023-12-02 09:00:07 +08:00 committed by GitHub
parent c65ae517d0
commit 82abb8944c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 205 additions and 258 deletions

View file

@ -1,9 +1,9 @@
use argx.nu
export def ensure-cache-by-lines [cache path action] {
let ls = (do -i { open $path | lines | length })
let ls = do -i { open $path | lines | length }
if ($ls | is-empty) { return false }
let lc = (do -i { open $cache | get lines})
let lc = do -i { open $cache | get lines}
if not (($cache | path exists) and (not ($lc | is-empty)) and ($ls == $lc)) {
mkdir ($cache | path dirname)
{
@ -16,7 +16,7 @@ export def ensure-cache-by-lines [cache path action] {
export def normalize-column-names [ ] {
let i = $in
let cols = ($i | columns)
let cols = $i | columns
mut t = $i
for c in $cols {
$t = ($t | rename -c {$c: ($c | str downcase | str replace ' ' '_')})
@ -24,25 +24,9 @@ export def normalize-column-names [ ] {
$t
}
def sprb [flag, args] {
if $flag {
$args
} else {
[]
}
}
def spr [args] {
let lst = ($args | last)
if ($lst | is-empty) {
[]
} else {
let init = ($args | range ..-2)
if ($init | is-empty) {
[ $lst ]
} else {
$init | append $lst
}
def --wrapped with-flag [...ns] {
if ($in | is-empty) { [] } else {
[$ns $in] | flatten
}
}
@ -106,7 +90,7 @@ export def kgh [
--all (-a)
] {
if ($name | is-empty) {
let ns = if $all { [--all] } else { (spr [-n $namespace]) }
let ns = if $all { [--all] } else { $namespace | with-flag -n }
helm list $ns --output json
| from json
| update updated {|x|
@ -116,25 +100,25 @@ export def kgh [
}
} else {
if $manifest {
helm get manifest $name (spr [-n $namespace])
helm get manifest $name ($namespace | with-flag -n)
} else if $values {
helm get values $name (spr [-n $namespace])
helm get values $name ($namespace | with-flag -n)
} else {
helm get notes $name (spr [-n $namespace])
helm get notes $name ($namespace | with-flag -n)
}
}
}
def "nu-complete helm list" [context: string, offset: int] {
let ctx = ($context | argx parse)
let ctx = $context | argx parse
kgh -n $ctx.namespace? | each {|x| {value: $x.name description: $x.updated} }
}
def "nu-complete helm charts" [context: string, offset: int] {
let ctx = ($context | argx parse)
let path = ($ctx | get _pos.chart)
let ctx = $context | argx parse
let path = $ctx | get _pos.chart
let path = if ($path | is-empty) { '.' } else { $path }
let paths = (do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} })
let paths = do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} }
helm repo list | from ssv -a | rename value description
| append $paths
}
@ -154,12 +138,12 @@ export def kah [
--namespace (-n): string@"nu-complete kube ns"
] {
let update = $name in (
helm list (spr [-n $namespace]) --output json
helm list ($namespace | with-flag -n) --output json
| from json | get name
)
let act = if $update { [upgrade] } else { [install] }
let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] }
helm $act $name $chart -f $valuefile $values (spr [-n $namespace])
helm $act $name $chart -f $valuefile $values ($namespace | with-flag -n)
}
# helm diff
@ -173,10 +157,10 @@ export def kdh [
--has-plugin (-h)
] {
if $has_plugin {
helm diff $name $chart -f $valuefile (spr [-n $namespace])
helm diff $name $chart -f $valuefile ($namespace | with-flag -n)
} else {
let update = $name in (
helm list (spr [-n $namespace]) --output json
helm list ($namespace | with-flag -n) --output json
| from json | get name
)
if not $update {
@ -186,7 +170,7 @@ export def kdh [
let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] }
let target = $'/tmp/($chart | path basename).($name).out.yaml'
helm template --debug $name $chart -f $valuefile $values (spr [-n $namespace]) | save -f $target
helm template --debug $name $chart -f $valuefile $values ($namespace | with-flag -n) | save -f $target
if $ignore_image {
do -i { yq -i ea 'del(.spec.template.spec.containers.[].image)' $target }
}
@ -199,7 +183,7 @@ export def kdelh [
name: string@"nu-complete helm list"
--namespace (-n): string@"nu-complete kube ns"
] {
helm uninstall $name (spr [-n $namespace])
helm uninstall $name ($namespace | with-flag -n)
}
# helm template
@ -211,9 +195,9 @@ export def kh [
--app (-a): string='test'
] {
let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] }
let target = ($valuefile | split row '.' | range ..-2 | append [out yaml] | str join '.')
let target = $valuefile | split row '.' | range ..-2 | append [out yaml] | str join '.'
if (not ($target | path exists)) and (([yes no] | input list $'create ($target)?') in [no]) { return }
helm template --debug $app $chart -f $valuefile $values (spr [-n $namespace])
helm template --debug $app $chart -f $valuefile $values ($namespace | with-flag -n)
| save -f $target
}
@ -226,26 +210,26 @@ export def "kube-config" [] {
def "nu-complete kube ctx" [] {
let k = (kube-config)
let cache = $'($env.HOME)/.cache/nu-complete/k8s/($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
let data = ensure-cache-by-lines $cache $k.path { ||
let clusters = $k.data | get clusters | select name cluster.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 max_cl = ($cluster | str length)
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 max_cl = $cluster | str length
$a
| upsert mx_ns (if $max_ns > $a.mx_ns { $max_ns } else $a.mx_ns)
| upsert mx_cl (if $max_cl > $a.mx_cl { $max_cl } else $a.mx_cl)
| upsert completion ($a.completion | append {value: $x.name, ns: $ns, cluster: $cluster})
})
}
{completion: $data.completion, max: {ns: $data.mx_ns, cluster: $data.mx_cl}}
})
}
$data.completion | each {|x|
let ns = ($x.ns | fill -a l -w $data.max.ns -c ' ')
let cl = ($x.cluster | fill -a l -w $data.max.cluster -c ' ')
let ns = $x.ns | fill -a l -w $data.max.ns -c ' '
let cl = $x.cluster | fill -a l -w $data.max.cluster -c ' '
{value: $x.value, description: $"\t($ns) ($cl)"}
}
}
@ -320,7 +304,7 @@ def upsert_row [table col mask id value] {
export def 'kconf import' [name: string, path: string] {
let k = (kube-config)
mut d = $k.data
let i = (cat $path | from yaml)
let i = cat $path | from yaml
let c = {
context: {
cluster: $name,
@ -340,9 +324,9 @@ export def 'kconf delete' [name: string@"nu-complete kube ctx"] {
export def 'kconf export' [name: string@"nu-complete kube ctx"] {
let d = (kube-config).data
let ctx = ($d | get contexts | where name == $name | get 0)
let user = ($d | get users | where name == $ctx.context.user)
let cluster = ($d | get clusters | where name == $ctx.context.cluster)
let ctx = $d | get contexts | where name == $name | get 0
let user = $d | get users | where name == $ctx.context.user
let cluster = $d | get clusters | where name == $ctx.context.cluster
{
apiVersion: 'v1',
current-context: $ctx.name,
@ -378,23 +362,23 @@ def "nu-complete kube kind" [] {
}
def "nu-complete kube res" [context: string, offset: int] {
let ctx = ($context | argx parse)
let kind = ($ctx | get _args.1)
let ctx = $context | argx parse
let kind = $ctx | get _args.1
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
kubectl get $ns $kind | from ssv -a | get NAME
}
def "nu-complete kube res via name" [context: string, offset: int] {
let ctx = ($context | argx parse)
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..)))
let ctx = $context | argx parse
let kind = $env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..))
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
kubectl get $ns $kind | from ssv -a | get NAME
}
def "nu-complete kube jsonpath" [context: string] {
let ctx = ($context | argx parse)
let kind = ($ctx | get _args.1)
let res = ($ctx | get _args.2)
let ctx = $context | argx parse
let kind = $ctx | get _args.1
let res = $ctx | get _args.2
let path = $ctx.jsonpath?
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
mut r = []
@ -405,8 +389,8 @@ def "nu-complete kube jsonpath" [context: string] {
$r = ['']
}
} else if ($path | str starts-with '.') {
let row = ($path | split row '.')
let p = ($row | range ..-2 | str join '.')
let row = $path | split row '.'
let p = $row | range ..-2 | str join '.'
if ($p | is-empty) {
$r = ( kubectl get $ns -o json $kind $res
| from json
@ -414,9 +398,9 @@ def "nu-complete kube jsonpath" [context: string] {
| each {|x| $'($p).($x)'}
)
} else {
let m = (kubectl get $ns $kind $res $"--output=jsonpath={($p)}" | from json)
let l = ($row | last)
let c = (do -i {$m | get $l})
let m = kubectl get $ns $kind $res $"--output=jsonpath={($p)}" | from json
let l = $row | last
let c = do -i {$m | get $l}
if (not ($c | is-empty)) and ($c | describe | str substring 0..5) == 'table' {
$r = (0..(($c | length) - 1) | each {|x| $'($p).($l)[($x)]'})
} else {
@ -448,10 +432,10 @@ export def kg [
} else {
[-n $namespace]
}
let r = (spr [$r])
let l = (spr [-l $selector])
let r = $r | with-flag
let l = $selector | with-flag -l
if ($jsonpath | is-empty) {
let wide = (sprb $wide [-o wide])
let wide = if $wide {[-o wide]} else {[]}
if ($verbose) {
kubectl get -o json $n $k $r $l | from json | get items
| each {|x|
@ -482,7 +466,7 @@ export def kd [
i: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns"
] {
kubectl describe (spr [-n $namespace]) $r $i
kubectl describe ($namespace | with-flag -n) $r $i
}
# kubectl create
@ -491,7 +475,7 @@ export def kc [
--namespace (-n): string@"nu-complete kube ns"
name:string
] {
kubectl create (spr [-n $namespace]) $r $name
kubectl create ($namespace | with-flag -n) $r $name
}
# kubectl get -o yaml
@ -500,7 +484,7 @@ export def ky [
i: string@"nu-complete kube res"
--namespace (-n): string@"nu-complete kube ns"
] {
kubectl get (spr [-n $namespace]) -o yaml $r $i
kubectl get ($namespace | with-flag -n) -o yaml $r $i
}
# kubectl edit
@ -510,9 +494,9 @@ export def ke [
--namespace (-n): string@"nu-complete kube ns"
--selector(-l): string
] {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
let r = if ($selector | is-empty) { $r } else {
let res = (kubectl get $k $n -l $selector | from ssv -a | each {|x| $x.NAME})
let res = kubectl get $k $n -l $selector | from ssv -a | each {|x| $x.NAME}
if ($res | length) == 1 {
$res.0
} else if ($res | length) == 0 {
@ -531,7 +515,7 @@ export def kdel [
--namespace (-n): string@"nu-complete kube ns"
--force(-f)
] {
kubectl delete (spr [-n $namespace]) (sprb $force [--grace-period=0 --force]) $r $i
kubectl delete ($namespace | with-flag -n) (if $force {[--grace-period=0 --force]} else {[]}) $r $i
}
@ -541,29 +525,21 @@ export def kgno [] {
| rename name status roles age version internal-ip external-ip os kernel runtime
}
def "nu-complete kube pods" [context: string, offset: int] {
let ctx = ($context | argx parse)
let ns = (do -i { $ctx | get namespace })
let ns = (spr [-n $ns])
kubectl get $ns pods | from ssv -a | get NAME
}
def "nu-complete kube pods or deploys" [context: string, offset: int] {
let ctx = ($context | argx parse)
let ns = (do -i { $ctx | get namespace })
let ns = (spr [-n $ns])
let pods = (kubectl $ns get pods -o name|lines)
let deploys = (kubectl $ns get deploy -o name|lines)
$pods | append $deploys
def "nu-complete kube deploys and pods" [context: string, offset: int] {
let ctx = $context | argx parse
let ns = $ctx.namespace? | with-flag -n
if ($ctx._pos.pod? | default '' | str ends-with '-') {
kubectl get $ns pods | from ssv -a | get NAME
} else {
kubectl get $ns deployments | from ssv -a | get NAME | each {|x| $"($x)-"}
}
}
def "nu-complete kube ctns" [context: string, offset: int] {
let ctx = ($context | argx parse)
let ns = (do -i { $ctx | get namespace })
let ns = (spr [-n $ns])
let ctn = (do -i { $ctx | get container })
let ctn = (spr [-c $ctn])
let pod = ($ctx | get _args.1)
let ctx = $context | argx parse
let ns = $ctx.namespace? | with-flag -n
let ctn = $ctx.container? | with-flag -c
let pod = $ctx | get _args.1
kubectl get $ns pod $pod -o jsonpath={.spec.containers[*].name} | split row ' '
}
@ -610,21 +586,25 @@ export def kdp [
# kubectl attach (exec -it)
export def ka [
pod?: string@"nu-complete kube pods or deploys"
pod?: string@"nu-complete kube deploys and pods"
--namespace (-n): string@"nu-complete kube ns"
--container(-c): string@"nu-complete kube ctns"
--selector(-l): string
...args
] {
let n = (spr [-n $namespace])
let pod = if ($selector | is-empty) { $pod } else {
let pods = (
kubectl get pods $n -o wide -l $selector
let n = $namespace | with-flag -n
let pod = if ($selector | is-empty) {
if ($pod | str ends-with '-') {
$"deployment/($pod | str substring ..-1)"
} else {
$pod
}
} else {
let pods = kubectl get pods $n -o wide -l $selector
| from ssv -a
| where STATUS == Running
| select NAME IP NODE
| rename name ip node
)
if ($pods | length) == 1 {
($pods.0).name
} else if ($pods | length) == 0 {
@ -635,7 +615,7 @@ export def ka [
}
let c = if ($container | is-empty) {
if ($selector | is-empty) { [] } else {
let cs = (kgp -n $n $pod -p '.spec.containers[*].name' | split row ' ')
let cs = kgp -n $n $pod -p '.spec.containers[*].name' | split row ' '
let ctn = if ($cs | length) == 1 {
$cs.0
} else {
@ -651,17 +631,22 @@ export def ka [
# kubectl logs
export def kl [
pod: string@"nu-complete kube pods or deploys"
pod: string@"nu-complete kube deploys and pods"
--namespace(-n): string@"nu-complete kube ns"
--container(-c): string@"nu-complete kube ctns"
--follow(-f)
--previous(-p)
] {
let n = (spr [-n $namespace])
let c = (spr [-c $container])
let f = (sprb $follow [-f])
let p = (sprb $previous [-p])
kubectl logs $n $f $p $pod $c
let n = $namespace | with-flag -n
let c = $container | with-flag -c
let f = if $follow {[-f]} else {[]}
let p = if $previous {[-p]} else {[]}
let trg = if ($pod | str ends-with '-') {
$"deployment/($pod | str substring ..-1)"
} else {
$pod
}
kubectl logs $n $f $p $trg $c
}
def "nu-complete port forward type" [] {
@ -669,10 +654,10 @@ def "nu-complete port forward type" [] {
}
def "nu-complete kube port" [context: string, offset: int] {
let ctx = ($context | argx parse)
let kind = ($ctx | get _args.1)
let ctx = $context | argx parse
let kind = $ctx | get _args.1
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
let res = ($ctx | get _args.2)
let res = $ctx | get _args.2
if ($kind | str starts-with 's') {
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
| from json
@ -692,28 +677,26 @@ export def kpf [
--local (-l): string
--namespace (-n): string@"nu-complete kube ns"
] {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
let port = if ($local | is-empty) { $port } else { $"($local):($port)" }
kubectl port-forward $n $"($res)/($target)" $port
}
def "nu-complete kube cp" [cmd: string, offset: int] {
let ctx = ($cmd | str substring ..$offset | argx parse)
let p = ($ctx._args | get (($ctx._args | length) - 1))
let ns = (do -i { $ctx | get namespace })
let ns = (spr [-n $ns])
let c = (do -i { $ctx | get container })
let c = (spr [-c $c])
let ctn = (kubectl get pod $ns | from ssv -a | each {|x| {description: $x.READY value: $"($x.NAME):" }})
let n = ($p | split row ':')
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
let ctn = kubectl get pod $ns | from ssv -a | each {|x| {description: $x.READY value: $"($x.NAME):" }}
let n = $p | split row ':'
if $"($n | get 0):" in ($ctn | get value) {
kubectl exec $ns ($n | get 0) $c -- sh -c $"ls -dp ($n | get 1)*"
| lines
| each {|x| $"($n | get 0):($x)"}
} else {
let files = (do -i { ls -a $"($p)*"
let files = do -i { ls -a $"($p)*"
| each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }}
})
}
$files | append $ctn
}
}
@ -724,7 +707,7 @@ export def kcp [
--container (-c): string@"nu-complete kube ctns"
--namespace (-n): string@"nu-complete kube ns"
] {
kubectl cp (spr [-n $namespace]) $lhs (spr [-c $container]) $rhs
kubectl cp ($namespace | with-flag -n) $lhs ($container | with-flag -c) $rhs
}
# kubectl get services
@ -783,7 +766,7 @@ export def ksd [
if ($num | into int) > 9 {
"too large"
} else {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
kubectl scale $n deployments $d --replicas $num
}
}
@ -798,7 +781,7 @@ export def ksdr [
} else if $num <= 0 {
"too small"
} else {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
kubectl scale $n deployments $d --replicas 0
kubectl scale $n deployments $d --replicas $num
}
@ -815,7 +798,7 @@ export def krhd [
--revision (-v): int
dpl: string@"nu-complete kube res via name"
] {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
let v = if ($revision|is-empty) { [] } else { [ $"--revision=($revision)" ] }
kubectl $n rollout history $"deployment/($dpl)" $v
}
@ -826,7 +809,7 @@ export def krud [
--revision (-v): int
dpl: string@"nu-complete kube res via name"
] {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
let v = if ($revision|is-empty) { [] } else { [ $"--to-revision=($revision)" ] }
kubectl $n rollout undo $"deployment/($dpl)" $v
}
@ -849,7 +832,7 @@ export def ktp [
}
}
} else {
let n = (spr [-n $namespace])
let n = $namespace | with-flag -n
kubectl top pod $n | from ssv -a | rename name cpu mem
| each {|x|
{
@ -891,7 +874,8 @@ export def "kclean stucked ns" [ns: string] {
export alias "kclean finalizer" = kubectl patch -p '{\"metadata\":{\"finalizers\":null}}'
export alias "kadm renew" = kubeadm alpha certs renew all
export alias "kadm check" = kubeadm certs check-expiration
export alias "kadm renew" = kubeadm certs renew all
### cert-manager
export def kgcert [] {