Changing label of existing GCP Cloud functions

Viewed 2101

I am using below command to update the label of a GCP Cloud function which is already deployed.

   $ gcloud functions deploy GCFunction --update-labels env=dev,app=myapp
     Deploying function (may take a while - up to 2 minutes)...failed.

It looks it does a deploy when we try to change the label for existing functions . Can we do a label change without doing any deployment like any other API or Cloud function to do the same task.

1 Answers

It works.

PROJECT=[[YOUR-PROJECT]]
REGION=[[YOUR-REGION]]
FUNCTION=[[YOUR-FUNCTION]]
ENDPOINT="https://cloudfunctions.googleapis.com/v1"
NAME="projects/${PROJECT}/locations/${REGION}/functions/${FUNCTION}"
URL="${ENDPOINT}/${NAME}"

gcloud functions describe ${FUNCTION} \
--project=${PROJECT} \
--region=${REGION} \
--format="yaml(labels)"
labels:
  app: myapp
  deployment-tool: cli-gcloud
  env: dev

curl \
--request PATCH \
--header "Authorization: Bearer $(gcloud auth print-access-token)"  \
--header "content-type: application/json" \
--data "{\"labels\":{\"env\":\"testing\"}}" \
${URL}?updateMask=labels


gcloud functions describe ${FUNCTION} \
--project=${PROJECT} \
--region=${REGION} \
--format="yaml(labels)"
labels:
  env: testing

NOTE You need to duplicate labels that you wish to preserve. In my example, I did not duplicate app and it is deleted by the PATCH.

NOTE The response body is an async Operation so you'll need to check on its completion.

Update: Operations

If you have the most excellent jq installed (or similar JSON parser), then you can poll the operation's status until it completes (better yet, set a timeout too... for the reader).

ENDPOINT="https://cloudfunctions.googleapis.com/v1"
NAME="projects/${PROJECT}/locations/${REGION}/functions/${FUNCTION}"
URL="${ENDPOINT}/${NAME}"

TOKEN=$(gcloud auth print-access-token)

VALUE="full-testing"
DATA="{\"labels\":{\"env\":\"${VALUE}\"}}"

NAME=$(curl \
--silent \
--request PATCH \
--header "Authorization: Bearer ${TOKEN}"  \
--header "content-type: application/json" \
--data "${DATA}" \
${URL}?updateMask=labels |\
jq -r .name) && echo ${NAME}

URL="${ENDPOINT}/${NAME}"

while [ $(curl --silent --request GET --header "Authorization: Bearer ${TOKEN}" ${URL} | jq -r .done) != "true" ]
do
  printf "."
  sleep 15s
done

gcloud functions describe ${FUNCTION} \
--project=${PROJECT} \
--region=${REGION} \
--format="yaml(labels)"

I was unable to find gcloud functions operations implemented.

Related