-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathhelm.nu
125 lines (114 loc) · 3.93 KB
/
helm.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use complete.nu *
use utils.nu *
use argx
export def record-to-set-json [value] {
$value | transpose k v
| each {|x| $"($x.k)=($x.v | to json -r)"}
| str join ','
}
def "nu-complete helm list" [context: string, offset: int] {
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 paths = do -i { ls ($"($path)*" | into glob) | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} }
helm repo list | from ssv -a | rename value description
| append $paths
}
# helm list and get
export def kgh [
name?: string@"nu-complete helm list"
--namespace (-n): string@"nu-complete kube ns"
--manifest (-m)
--values(-v)
--all (-a)
] {
if ($name | is-empty) {
let ns = if $all { [--all] } else { $namespace | with-flag -n }
helm list ...$ns --output json
| from json
| update updated {|x|
$x.updated
| str substring ..-4
| into datetime -f '%Y-%m-%d %H:%M:%S.%f %z'
}
} else {
if $manifest {
helm get manifest $name ...($namespace | with-flag -n)
} else if $values {
helm get values $name ...($namespace | with-flag -n)
} else {
helm get notes $name ...($namespace | with-flag -n)
}
}
}
# helm install or upgrade via values file
export def kah [
name: string@"nu-complete helm list"
chart: string@"nu-complete helm charts"
valuefile: path
--values (-v): any
--namespace (-n): string@"nu-complete kube ns"
--ignore-image (-i) # for kdh
] {
let update = $name in (
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 ...($namespace | with-flag -n)
}
# helm diff
export def kdh [
name: string@"nu-complete helm list"
chart: string@"nu-complete helm charts"
valuefile: path
--values (-v): any
--namespace (-n): string@"nu-complete kube ns"
--ignore-image (-i)
--has-plugin (-h)
] {
if $has_plugin {
helm diff $name $chart -f $valuefile ...($namespace | with-flag -n)
} else {
let update = $name in (
helm list ...($namespace | with-flag -n) --output json
| from json | get name
)
if not $update {
echo "new installation"
return
}
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 ...($namespace | with-flag -n) | save -f $target
if $ignore_image {
do -i { yq -i ea 'del(.spec.template.spec.containers.[].image)' $target }
}
kubectl diff -f $target
}
}
# helm delete
export def kdelh [
name: string@"nu-complete helm list"
--namespace (-n): string@"nu-complete kube ns"
] {
helm uninstall $name ...($namespace | with-flag -n)
}
# helm template
export def kh [
chart: string@"nu-complete helm charts"
valuefile: path
--values (-v): any
--namespace (-n): string@"nu-complete kube ns"='test'
--app (-a): string='test'
] {
let values = if ($values | is-empty) { [] } else { [--set-json (record-to-set-json $values)] }
let target = $valuefile | split row '.' | slice ..-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 ...($namespace | with-flag -n)
| save -f $target
}