大量匯入及匯出現有 Google Cloud 資源


本頁面說明 config-connector bulk-export 指令,以及如何使用該指令將 Google Cloud 資源匯出至 Config Connector YAML 檔案,以便日後匯入 Config Connector。

config-connector bulk-export 會使用 Cloud Asset Inventory匯出功能來探索現有的 Google Cloud 資源。您可以提供 Cloud Asset Inventory 匯出資料,也可以由 config-connector 代為執行匯出作業。

Cloud Asset Inventory 會匯出 JSON 結構體。每個結構都包含資源名稱、資產目錄類型,以及祖系資源:專案、資料夾和機構。如要瞭解資產目錄支援的類型,請參閱「支援的資產類型」。

限制

並非所有資源都支援 bulk-export 指令。如要取得支援的資源清單,請執行 config-connector print-resources

事前準備

  1. 安裝 config-connector tool

  2. 如果您想使用 config-connector 工具直接從 Cloud Asset Inventory 匯出資料,請使用 gcloud 在 Google CloudIdentity 專案中啟用 Cloud Asset Inventory API。

    gcloud services enable cloudasset.googleapis.com
    

大量匯出範例

在本例中,您將使用 Google Cloud CLI 建立 PubSubTopic,然後將其匯入 Config Connector。

  1. 使用 Google Cloud CLI 建立名為 sample-topic 的主題:

    gcloud pubsub topics create sample-topic
    

    您會收到主題建立成功的確認訊息。

    Created topic [projects/PROJECT_ID/topics/sample-topic].
    

    在輸出內容中,PROJECT_ID 會替換為您的Google Cloud 專案。

  2. 取得主題的 Google Cloud 資源名稱,並使用下列指令將其儲存至環境變數:

    TOPIC_RESOURCE_NAME=$(gcloud pubsub topics describe sample-topic --format "value(name)")
    
  3. config-connector 工具會使用 Cloud Asset Inventory JSON 結構定義來識別物件。將主題資產的 JSON 結構儲存至環境變數:

    TOPIC_ASSET='{"name":"//pubsub.googleapis.com/'"${TOPIC_RESOURCE_NAME}"'","asset_type":"pubsub.googleapis.com/Topic"}'
    
  4. 請執行下列指令,將資產傳遞至 config-connector bulk-export

    echo ${TOPIC_ASSET} | config-connector bulk-export
    

    輸出內容為 YAML 格式的 Config Connector 資源。

    ---
    apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
    kind: PubSubTopic
    metadata:
      annotations:
        cnrm.cloud.google.com/project-id: PROJECT_ID
      name: sample-topic
    ...
    

    在輸出內容中,PROJECT_ID 會替換為您的Google Cloud 專案。

  5. 您可以使用 kubectl apply -f - 將此資源傳遞至 Config Connector。如要直接傳遞資源,請執行下列指令:

    echo ${TOPIC_ASSET} | config-connector bulk-export | kubectl apply -f -  --namespace CC_NAMESPACE
    

    CC_NAMESPACE 替換為 Config Connector 管理資源的命名空間。

    Config Connector 取得資源

  6. 確認 Config Connector 是否使用 kubectl describe 管理資源:

    kubectl describe pubsubtopic sample-topic --namespace CC_NAMESPACE
    

    CC_NAMESPACE 替換為 Config Connector 管理資源的命名空間。

正在清除所用資源

您可以使用 config-connector bulk-exportkubectl delete 刪除 PubSubTopic。

echo ${TOPIC_ASSET} | config-connector bulk-export | kubectl delete -f - --namespace CC_NAMESPACE

CC_NAMESPACE 替換為 Config Connector 管理資源的命名空間。

探索要匯入的資源

匯入資源時,您可以執行 Cloud Asset Inventory 匯出作業,並將結果提供給 config-connector bulk-export,或是請 config-connector bulk-export 代為執行。

從 Cloud Asset Inventory 匯出內容匯入

您可以提供資產目錄匯出內容,方法是提供包含匯出內容的本機檔案路徑,或是將匯出結果管道傳送至 STDIN 上的 config-connector

從本機檔案匯入

您可以使用含有 --input 參數的本機檔案,為 config-connector bulk-export 提供資產目錄匯出內容

config-connector bulk-export --input ASSET_INVENTORY_EXPORT

ASSET_INVENTORY_EXPORT 替換為 Cloud Asset Inventory 匯出檔案的檔案名稱。

從 STDIN 匯入

如要在 STDIN 上提供資產目錄匯出資料,請將匯出結果管道傳送至 config-connector bulk-export。舉例來說,如果匯出內容位於名為 export.json 的本機檔案中,請將檔案內容管道傳送至 config-connector bulk-export,而不需要提供任何匯出參數。

cat export.json | config-connector bulk-export

在 STDIN 上篩選資產目錄匯出資料

如要篩選資產目錄匯出項目,您可以使用 jq 工具和管道,將結果輸入 config-connector bulk-export。舉例來說,如果您只想從檔案 EXPORT_FILE 匯入 PubSubTopic 資產,請執行下列指令:

cat EXPORT_FILE | jq '. | select( .asset_type == "pubsub.googleapis.com/Topic" )' | config-connector bulk-export

使用 config-connector 匯出廣告空間

config-connector bulk-export 工具可從 Google Cloud 資源階層匯出資源。

匯出專案

如要匯出專案中的所有資源,請使用 --project 參數。

config-connector bulk-export --project PROJECT_ID

PROJECT_ID 替換為您的 Google Cloud 專案。

匯出資料夾

如要匯出資料夾中的所有資源,請使用 --folder 參數。

config-connector bulk-export --folder FOLDER_NUMBER

FOLDER_NUMBER 替換為您的 Google Cloud 資料夾編號。

匯出貴機構

如要匯出機構的所有資源,請使用 --organization 參數。

config-connector bulk-export --organization ORGANIZATION_ID

ORGANIZATION_ID 替換為您的 Google Cloud 機構 ID。

Cloud Storage 位置

資產清單匯出作業的輸出位置是 Cloud Storage URI。config-connector bulk-export 執行匯出作業時,會使用 Cloud Storage 值區。根據預設,config-connector bulk-export 會建立臨時值區。您也可以指定資料夾名稱。

臨時 Cloud Storage 值區

如果您未提供 --storage-key 參數,config-connector bulk-export 會代您建立臨時 Cloud Storage 值區。系統會在儲存空間值區的預設位置建立值區,也就是 US 多地區。匯出作業完成後,系統會刪除值區。

指定臨時值區

如要指定值區,請使用 Cloud Storage URI 和 storage-key 參數。如果 URI 只是值區的名稱,系統會為匯出儲存空間物件產生名稱。如果 URI 是儲存空間物件的完整路徑,系統會使用完整路徑。

config-connector bulk-export --storage-key gs://BUCKET_NAME

輸出

config-connector bulk-export 指令的輸出內容是 YAML 格式的 Config Connector 資源。根據預設,YAML 檔案會寫入 STDOUT。您可以使用 output 選項,將資源輸出內容導向至檔案。

輸出至單一檔案

設定 --output 參數時,如果符合下列任一條件,config-connector bulk-export 就會將結果寫入單一檔案:

  • output 指定的檔案已存在,且為 regular 檔案。
  • output 指定的檔案不存在,但 output 代表的上層目錄確實存在。

輸出至目錄

如果 --output 參數是結尾為 / 的目錄,config-connector 就會將結果寫入多個檔案。config-connector bulk-export 會為每個資源建立一個檔案,且檔案名稱會與資源名稱相符。

config-connector bulk-export --project PROJECT_ID --on-error continue --output OUTPUT_DIRECTORY/

PROJECT_ID 替換為您的 Google Cloud 專案。

舉例來說,如要將資產從專案 my-project 輸出至 sample 目錄,請執行下列指令:

config-connector bulk-export --project my-project --on-error continue --output sample/

指令列選項

config-connector bulk-export 指令包含下列選項:

config-connector bulk-export
    --input FILENAME \
    --output FILENAME \
    --storage-key gs://BUCKET_NAME \
    --project PROJECT_ID \
    --folder FOLDER_NUMBER \
    --organization ORGANIZATION_ID \
    --oauth2-token TOKEN \
    --on-error [halt | continue | ignore] \
    --iam-format [policy | policymember | none] \
    --filter-deleted-iam-members [true | false] \
    --verbose
  • --input:Cloud Asset Inventory 輸入檔案。
  • --output:可選的輸出檔案路徑,可停用標準輸出內容。如果是檔案,結果會包含所有指令輸出內容;如果是目錄,則目錄會包含輸出內容中每項資源的新檔案。
  • --storage-key:指定要匯出的暫時性 Cloud Storage 值區。
  • --project: Google Cloud 要匯出的專案 ID
  • --folder:要匯出的 Google Cloud 資料夾 ID
  • --organization:要匯出的 Google Cloud 機構 ID。
  • --oauth2-tokenOAUTH2 權杖,做為 Google Cloud 身分。根據預設,config-connector 會使用 Google Cloud CLI 預設憑證
  • --on-error:控制發生可復原錯誤時的行為。選項包括「continue」、「halt」或「ignore」。
    • halt:發生任何錯誤時停止執行 (預設)
    • continue:繼續處理資源,並將錯誤訊息輸出至 STDERR
    • ignore:繼續處理資源,且不顯示錯誤
  • --iam-format:指定匯出作業產生的 IAM 資源類型。選項包括 policy (預設)、policymembernone
  • --filter-deleted-iam-members:指定是否要篩除已刪除的 IAM 主體。選項為 truefalse。預設值為 false
  • --verbose:啟用詳細記錄。

後續步驟