nu-modules/kube.nu

110 lines
3.4 KiB
Plaintext

export def list-pods [] {
kubectl --context $env.KUBE_CONTEXT get pods --output json | from json | get items
}
export def pods [--running(-r)] {
if $running {
containers | where ready | select pod | uniq
} else {
containers | get pod | uniq
}
}
export def apply [] {
kubectl --context $env.KUBE_CONTEXT apply -f -
}
export def containers [] {
list-pods | each {|item|
$item.status.containerStatuses | flatten | select image name restartCount ready
| insert pod ($item.metadata.name)
| insert creation ($item.metadata.creationTimestamp)
| insert nodeName ($item.spec.nodeName)
} | flatten
}
def list-images [] {
containers | get image | uniq
}
export def set-image [deployment:string@apps, image:string@list-images] {
kubectl --context $env.KUBE_CONTEXT set image $"deployment/($deployment)" $"($deployment)=($image)"
}
export def "del app-pods" [app:string@apps] {
kubectl --context $env.KUBE_CONTEXT delete pods -l $"app=($app)"
}
# list all app names
export def apps [] {
containers | get name | uniq
}
# list all availble deployments
export def deployments [] {
call_kube ["get", "deployments"] | lines | parse "{name} {rest}" | skip | get name
}
def when [condition:bool, and_block] {
let stream = $in
if ($condition) {
$stream | do $and_block
} else {
$stream
}
}
def call_kube [args,--quiet] {
run-external --redirect-stdout "kubectl" "--context" $env.KUBE_CONTEXT $args
}
export def "pod logs" [...pods:string@pods] {
let args = [logs]
}
export def "deployment restart" [deployment:string@deployments] {
call_kube [rollout, restart, deployment, $deployment]
}
export def "deployment restart status" [deployment:string@deployments] {
call_kube [rollout, restart, deployment, $deployment]
}
# show the logs of all containers with a certain app label
export def "app logs" [
...app:string@apps, # the name of app
--json (-j), # if set parse the log as jsongn
--since:duration # how old the logs should be
--timestamps (-t),
--previous (-p),
--prefix,
] {
let args = [logs]
let args = ($args | append $"-l app in \(($app | str join ',')\)")
let args = if $since != null { $args | append $"--since=($since / 1min)m"} else { $args }
let args = if $timestamps { $args | append "--timestamps" } else { $args }
let args = if $previous { $args | append "--previous" } else { $args }
let args = if $prefix { $args | append "--prefix" } else { $args }
call_kube $args
| when $json { from json --objects }
}
# List the logs for a deployment
export def "deployment logs" [
deployment:string@deployments
--json (-j), # if set parse the log as json
--since:duration # how old the logs should be
--timestamps (-t),
--previous (-p),
--prefix,
] {
let args = [logs]
let args = ($args | append $"deployment/($deployment)")
let args = if $since != null { $args | append $"--since=($since / 1min)m"} else { $args }
let args = if $timestamps { $args | append "--timestamps" } else { $args }
let args = if $previous { $args | append "--previous" } else { $args }
let args = if $prefix { $args | append "--prefix" } else { $args }
call_kube $args
| when $json { lines | filter { |line| $line | str starts-with '{' } | each { |line| $line | from json } }
}