Pages - Menu

2022年8月16日 星期二

[GCP] sh批次修改GCP的label

前言

發現有一整批的資源沒有加過 label ,
GCE的硬碟、負載平衡的前端轉導規則…
有些條件不一樣,一個一個加會瘋掉。
工程師就是懶…寫sh吧


正文

開始構思

  1. 先把關鍵值拉出來 (1)
gcloud compute forwarding-rules list




只有ip,要跑去每個叢集查資料,看這該歸屬哪個label,
太笨了,不行。

先看一下forwarding-rules 有哪些資訊(2),再把欄位顯示出來。

gcloud compute forwarding-rules describe a0589a367cdc1470e839b05803958e2b

裡面的description似乎蠻有用處的,可以幫忙釐清是哪個cluster



用 --format (3),顯示欄位

 gcloud compute forwarding-rules list  --format='table(name,IPAddress,description)'


  1. 開始寫shell script

將name丟進 文字檔,
然後執行bash,bash的流程,迴圈讀檔案內容(4),執行更新label指令(5)。這邊要注意,如果是ingress或是區域型的要額外加上 --global,
才能更新。

while read p; do
  echo "$p"; gcloud compute forwarding-rules update $p \
    --update-labels=product=abc,env=prod,dept=rd; echo "done";
done <lbfr.txt

ref.

  1. gcloud compute forwarding-rules list
  2. gcloud compute forwarding-rules describe
  3. gcloud topic formats
  4. http://www.compciv.org/topics/bash/loops/
  5. gcloud compute forwarding-rules update

沒有留言:

張貼留言