使用 Cloud Functions 和 Cloud Scheduler 來識別和清理浪費的雲端資源。
unused_ip_function
)檢查並刪除沒有被使用的ip資源gcloud services enable cloudscheduler.googleapis.com
git clone <https://github.com/GoogleCloudPlatform/gcf-automated-resource-cleanup.git> && cd gcf-automated-resource-cleanup/
export PROJECT_ID=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
export region=us-east1
export WORKDIR=$(pwd)
cd $WORKDIR/unused-ip
export USED_IP=used-ip-address
export UNUSED_IP=unused-ip-address
gcloud compute addresses create $USED_IP --project=$PROJECT_ID --region=us-east1
gcloud compute addresses create $UNUSED_IP --project=$PROJECT_ID --region=us-east1
gcloud compute addresses list --filter="region:(us-east1)"
export USED_IP_ADDRESS=$(gcloud compute addresses describe $USED_IP --region=us-east1 --format=json | jq -r '.address')
gcloud compute instances create static-ip-instance \\
--zone=us-east1-d \\
--machine-type=e2-medium \\
--subnet=default \\
--address=$USED_IP_ADDRESS
gcloud compute addresses list --filter="region:(us-east1)"
cat $WORKDIR/unused-ip/function.js | grep "const compute" -A 31
output:
const compute = new Compute();
compute.getAddresses(function(err, addresses){ // gets all addresses across regions
if(err){
console.log("there was an error: " + err);
}
if (addresses == null) {
console.log("no addresses found");
return;
}
console.log("there are " + addresses.length + " addresses");
// iterate through addresses
for (let item of addresses){
// get metadata for each address
item.getMetadata(function(err, metadata, apiResponse) {
// if the address is not used:
if (metadata.status=='RESERVED'){
// compute age by convering ISO 8601 timestamps to Date
var creationDate = new Date(metadata.creationTimestamp);
var currDate = new Date();
var addressAge = Math.floor((currDate - creationDate)/86400e3);;
// delete address
item.delete(function(err, operation, apiResponse2){
if (err) {
console.log("could not delete address: " + err);
}
})
}
compute.getAddresses(function(err, addresses)
uses the getAddresses method to retrieve IP addresses across all regions in the project.item.getMetadata(function(err, metadata, apiResponse)
gets the metadata for each IP address and checks its STATUS field.if ((metadata.status=='RESERVED') & (calculateAge(metadata.creationTimestamp) >= ageToDelete)){
checks whether the IP address is in use, calculates its age by using a helper function, and compares its age against a constant (set to 0 for the purposes of the lab).item.delete(function(err, operation, apiResponse2){
deletes the IP address.gcloud functions deploy unused_ip_function --trigger-http --runtime=nodejs12 --region=us-east1
export FUNCTION_URL=$(gcloud functions describe unused_ip_function --region=us-east1 --format=json | jq -r '.httpsTrigger.url')
gcloud app create --region us-east1
gcloud scheduler jobs create http unused-ip-job \\
--schedule="* 2 * * *" \\
--uri=$FUNCTION_URL \\
--location=us-east1
gcloud scheduler jobs run unused-ip-job \\
--location=us-east1
gcloud compute addresses list --filter="region:(us-east1)"
如果你喜歡這篇文章歡迎幫我按愛心鼓勵一下喔!~閱讀愉快!~