Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

自動化神器 nushell ,kubernetes 好幫手

有時候我們會臨時想要修改一下 kubernetes 的相關設定,不過不需要變更到檔案。所以就想說如果可以直接修改 kubernetes 資源就好。過去可能是靠 kubernetes edit 還編輯。但是編輯久了,有時候會覺得沒效率。這時候可以靠 nushell 幫你自度化修改。

下面範例為修改 deployment 的一些設定

def "complete deployment" [ ] {
  kubectl get deployments -o name|lines
}

export def use-host-namespace [ name: string@"complete deployment" ] {
  mut yaml =  (kubectl get $name -o yaml |from yaml)
  $yaml = ($yaml | upsert spec.template.spec.hostNetwork true)
  $yaml = ($yaml | upsert spec.template.spec.hostPID true)
  $yaml = ($yaml | upsert spec.template.spec.hostIPC true)  
  $yaml | to yaml | kubectl apply -f -
}

export def set-admin [ name: string@"complete deployment" ] {
  mut yaml =  (kubectl get $name -o yaml |from yaml)
  $yaml.spec.template.spec.containers = ($yaml.spec.template.spec.containers | each { $in | upsert securityContext.privileged true })
  $yaml.spec.template.spec.containers = ($yaml.spec.template.spec.containers | each { $in | upsert securityContext.allowPrivilegeEscalation true })
  $yaml | to yaml | kubectl apply -f -
}
export def set-hosts [ name: string@"complete deployment", ip: string, domain: list ] {
  mut yaml =  (kubectl get $name -o yaml |from yaml)
  mut hosts = ($yaml| get spec.template.spec.hostAliases? | default [])
  $hosts = ($hosts | filter {|it| $it.ip != $ip})
  $hosts = ($hosts | append {ip: $ip, hostnames: $domain})
  $yaml | to yaml | kubectl apply -f -
}



You'll only receive email when they publish something new.

More from kjelly
All posts