大量匯入及匯出現有 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 在您 Identity 的專案中啟用 Cloud Asset Inventory API。 Google Cloud

    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 bucket。根據預設,config-connector bulk-export 會建立暫時值區。您也可以指定 bucket 名稱。

臨時 Cloud Storage bucket

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

指定臨時 bucket

如要指定 bucket,請使用 storage-key 參數的 Cloud Storage URI。如果 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 bucket。
  • --project: Google Cloud 要匯出的專案 ID
  • --folder: Google Cloud 要匯出的資料夾 ID
  • --organization:要匯出的 Google Cloud 機構 ID。
  • --oauth2-token:做為 Google Cloud 身分的 OAUTH2 權杖。根據預設,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:啟用詳細記錄。

後續步驟