mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
improve kconf import (#481)
* improve kconf import The order of fields loaded via `from yaml` is unstable, so use yq to read it as json, and then load it by `from json`. Aims to be `diff` friendly. Remove the square brackets from the week field and use color to distinguish it, taking up less space. * kube_stat shows $ctx.NAME instead of complex combination of $ctx.AUTHINFO and $ctx.CLUSTER - Improve the default theme of kube_stat. - Clean up duplicate comments in `kubernetes.nu`. * when power's DECORATOR mode is `power`, allows to dynamically change the background color - components now needs to return two values in an array, the first value is the background color, and the second value is the original value. - as an example, `atuin_stat` reveal its status with two different colors instead of showing or hiding * allowing pass `config` when register or inject a component The main difference between `config` and `theme` is: theme assumes you pass a color and will apply with `ansi -e {fg : $arg}` And in most cases, you don't need to pass `config` * added new function `power set` time_segment allows format control via option `short` * rename `nu-complete kube def` to `nu-complete kube kind` - get all resources via `kubectl api-resources` - enable cache * use `from yaml` instead of `yq -o=json | from json` (for next version) * nu-complete kube kind: add SHORTNAMES as description * Fix the caching mechanism of `kube ctx` and `kube kind` based on the number of file lines instead of modification time. Because switching the context will change the content of the file, the time will be updated every time * Added new command `kcache flush` to rebuild the cache. Sometimes, such as manually editing the cluster address in the configuration file, the number of lines has not changed, and the cache does need to be refreshed * use `path dirname` instead of `dirname` * applying `ensure-cache-by-lines` in `kube_stat` --------- Co-authored-by: agent <agent@nuc>
This commit is contained in:
parent
ee996ec169
commit
2d6f32657a
7 changed files with 164 additions and 93 deletions
|
@ -22,15 +22,24 @@ export def "parse cmd" [] {
|
|||
| reject sw
|
||||
}
|
||||
|
||||
export def ensure-cache [cache path action] {
|
||||
let ts = (do -i { ls $path | sort-by modified | reverse | get 0.modified })
|
||||
if ($ts | is-empty) { return false }
|
||||
let tc = (do -i { ls $cache | get 0.modified })
|
||||
if not (($cache | path exists) and ($ts < $tc)) {
|
||||
mkdir (dirname $cache)
|
||||
do $action | save -f $cache
|
||||
export def ensure-cache-by-lines [cache path action] {
|
||||
let ls = (do -i { open $path | lines | length })
|
||||
if ($ls | is-empty) { return false }
|
||||
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)
|
||||
{
|
||||
lines: $ls
|
||||
payload: (do $action)
|
||||
} | save -f $cache
|
||||
}
|
||||
open $cache
|
||||
(open $cache).payload
|
||||
}
|
||||
|
||||
export def `kcache flush` [] {
|
||||
rm -rf ~/.cache/nu-complete/k8s/
|
||||
nu-complete kube ctx
|
||||
rm -rf ~/.cache/nu-complete/k8s-api-resources/
|
||||
}
|
||||
|
||||
export-env {
|
||||
|
@ -70,13 +79,13 @@ export def kk [p: path] {
|
|||
### ctx
|
||||
export def "kube-config" [] {
|
||||
let file = if ($env.KUBECONFIG? | is-empty) { $"($env.HOME)/.kube/config" } else { $env.KUBECONFIG }
|
||||
{ path: $file, data: (cat $file | from yaml)}
|
||||
{ path: $file, data: (cat $file | from yaml) }
|
||||
}
|
||||
|
||||
def "nu-complete kube ctx" [] {
|
||||
let k = (kube-config)
|
||||
let cache = $'($env.HOME)/.cache/nu-complete/k8s/(basename $k.path).json'
|
||||
let data = (ensure-cache $cache $k.path { ||
|
||||
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
|
||||
| get contexts
|
||||
|
@ -120,18 +129,18 @@ export def 'kconf import' [name: string, path: string] {
|
|||
let k = (kube-config)
|
||||
let d = $k.data
|
||||
let i = (cat $path | from yaml)
|
||||
let c = [{
|
||||
let c = {
|
||||
name: $name,
|
||||
context: {
|
||||
cluster: $name,
|
||||
namespace: default,
|
||||
user: $name
|
||||
}
|
||||
}]
|
||||
}
|
||||
$d
|
||||
| upsert contexts ($d.contexts | append $c)
|
||||
| upsert clusters ($d.clusters | append ($i.clusters.0 | upsert name $name))
|
||||
| upsert users ($d.users | append ($i.users.0 | upsert name $name))
|
||||
| upsert contexts ($d.contexts | append $c)
|
||||
| to yaml
|
||||
}
|
||||
|
||||
|
@ -159,14 +168,19 @@ export def-env kcconf [name: string@"nu-complete kube ctx"] {
|
|||
}
|
||||
|
||||
### common
|
||||
def "nu-complete kube def" [] {
|
||||
[
|
||||
pod deployment svc endpoints
|
||||
configmap secret event
|
||||
namespace node pv pvc ingress
|
||||
job cronjob daemonset statefulset
|
||||
clusterrole clusterrolebinding role serviceaccount rolebinding
|
||||
] | append (kubectl get crd | from ssv -a | get NAME)
|
||||
export def "nu-complete kube kind without cache" [] {
|
||||
kubectl api-resources | from ssv -a | get NAME
|
||||
| append (kubectl get crd | from ssv -a | get NAME)
|
||||
}
|
||||
|
||||
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'
|
||||
ensure-cache-by-lines $cache $ctx.path {||
|
||||
kubectl api-resources | from ssv -a
|
||||
| each {|x| {value: $x.NAME description: $x.SHORTNAMES} }
|
||||
| append (kubectl get crd | from ssv -a | each {|x| {$x.NAME} })
|
||||
}
|
||||
}
|
||||
|
||||
def "nu-complete kube res" [context: string, offset: int] {
|
||||
|
@ -178,7 +192,7 @@ def "nu-complete kube res" [context: string, offset: int] {
|
|||
}
|
||||
|
||||
export def kg [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
-n: string@"nu-complete kube ns"
|
||||
--all (-A):bool
|
||||
] {
|
||||
|
@ -195,7 +209,7 @@ export def kg [
|
|||
}
|
||||
|
||||
export def kc [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
-n: string@"nu-complete kube ns"
|
||||
name:string
|
||||
] {
|
||||
|
@ -204,7 +218,7 @@ export def kc [
|
|||
}
|
||||
|
||||
export def ky [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
|
@ -213,7 +227,7 @@ export def ky [
|
|||
}
|
||||
|
||||
export def kd [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
|
@ -222,7 +236,7 @@ export def kd [
|
|||
}
|
||||
|
||||
export def ke [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
] {
|
||||
|
@ -231,7 +245,7 @@ export def ke [
|
|||
}
|
||||
|
||||
export def kdel [
|
||||
r: string@"nu-complete kube def"
|
||||
r: string@"nu-complete kube kind"
|
||||
i: string@"nu-complete kube res"
|
||||
-n: string@"nu-complete kube ns"
|
||||
--force(-f): bool
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue