{"id":19236098,"url":"https://github.com/kedacore/sample-go-gcppubsub","last_synced_at":"2025-02-23T13:41:56.804Z","repository":{"id":44780839,"uuid":"197727517","full_name":"kedacore/sample-go-gcppubsub","owner":"kedacore","description":"Sample KEDA example for GCP PubSub","archived":false,"fork":false,"pushed_at":"2022-01-25T07:52:17.000Z","size":3749,"stargazers_count":17,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-01-05T01:26:13.220Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kedacore.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-19T07:48:40.000Z","updated_at":"2024-07-13T09:07:26.000Z","dependencies_parsed_at":"2022-09-05T02:20:54.049Z","dependency_job_id":null,"html_url":"https://github.com/kedacore/sample-go-gcppubsub","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kedacore%2Fsample-go-gcppubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kedacore%2Fsample-go-gcppubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kedacore%2Fsample-go-gcppubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kedacore%2Fsample-go-gcppubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kedacore","download_url":"https://codeload.github.com/kedacore/sample-go-gcppubsub/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240324057,"owners_count":19783453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T16:19:08.971Z","updated_at":"2025-02-23T13:41:56.769Z","avatar_url":"https://github.com/kedacore.png","language":"Makefile","readme":"# KEDA Sample for GCP PubSub\n\nThis is an example of using [KEDA](https://github.com/kedacore/keda) with GCP PubSub.\n\nKEDA (Kubernetes-based Event Driven Autoscaling) allows you to auto scale your kubernetes pods based on external metrics derived from systems such as RabbitMQ, Azure Storage Queues, GCP PubSub, Azure ServiceBus, etc. It also lets your scale the number of pods to zero so that you're not consuming resources when there is no processing to be done.\n\n# Prerequisites\nYou need a Kubernetes cluster with KEDA installed. The [KEDA git hub repository](https://github.com/kedacore/keda) explains how this can be done using Helm.  This sample uses KEDA 2.0.\n\nAdditionally, you must be comfortable with the gcloud command line tool and should have setup a GCP account and project.\n\n# Tutorial\n\nWe shall start by creating a service account in GCP. This service account will be used by KEDA to figure out how many messages are present in the PubSub subscription as well as by our sample program that will receive messages from the subscription. We will give the service account access to view Stackdriver metrics. The metric called \"pubsub.googleapis.com/subscription/num_undelivered_messages\" is updated by PubSub when new messages are added to the subscription.\n\n```sh\nSERVICE_ACCOUNT_NAME=gcppubsubtest\nPROJECT_ID=$(gcloud config get-value project)\nSERVICE_ACCOUNT_FULL_NAME=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com\n\ngcloud beta iam service-accounts create $SERVICE_ACCOUNT_NAME \\\n    --display-name \"PubSub Sample\"\n\ngcloud iam service-accounts keys create $(pwd)/sa.json \\\n  --iam-account $SERVICE_ACCOUNT_FULL_NAME\n\ngcloud projects add-iam-policy-binding $PROJECT_ID \\\n  --member serviceAccount:$SERVICE_ACCOUNT_FULL_NAME \\\n  --role roles/monitoring.viewer\n```\n\nNext we will create the topic and the subscription\n\n```sh\nSUBSCRIPTION_NAME=mysubscription\nTOPIC_NAME=mytopic\n\n# Create Topic\ngcloud beta pubsub topics create $TOPIC_NAME\n\n# Create Subscription\ngcloud beta pubsub subscriptions create $SUBSCRIPTION_NAME \\\n  --topic $TOPIC_NAME\n\n# We need to give the service account access to the subscriber to\n# receive messages\ngcloud beta pubsub subscriptions add-iam-policy-binding $SUBSCRIPTION_NAME \\\n  --member=serviceAccount:$SERVICE_ACCOUNT_FULL_NAME \\\n  --role=roles/pubsub.subscriber\n```\n\nNow we will create the namespace and deploy the application. We will create a secret containing the service account keys and the project ID and then use those as environment variables in the Deployment yaml\n\n```sh\nkubectl create ns keda-pubsub-test\n\nkubectl create secret generic pubsub-secret \\\n  --from-file=GOOGLE_APPLICATION_CREDENTIALS_JSON=./sa.json \\\n  --from-literal=PROJECT_ID=$PROJECT_ID \\\n  -n keda-pubsub-test\n\n\nkubectl apply -f manifests/\n```\n\nThis is the YAML for the Deployment\n```yaml\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: keda-pubsub-go\n  namespace: keda-pubsub-test\nspec:\n  selector:\n    matchLabels:\n      service: keda-pubsub-go\n  replicas: 1\n  template:\n    metadata:\n      labels:\n        service: keda-pubsub-go\n    spec:\n      containers:\n      - image: patnaikshekhar/keda-pubsub-sample:1\n        name: consumer\n        env:\n        - name: SUBSCRIPTION_NAME\n          value: \"mysubscription\"\n        - name: GOOGLE_APPLICATION_CREDENTIALS_JSON\n          valueFrom:\n            secretKeyRef:\n              name: pubsub-secret\n              key: GOOGLE_APPLICATION_CREDENTIALS_JSON\n        - name: PROJECT_ID\n          valueFrom:\n            secretKeyRef:\n              name: pubsub-secret\n              key: PROJECT_ID\n```\nAnd this is the YAML for the scaled object (KEDA 2.0)\n\n```yaml\napiVersion: keda.sh/v1alpha1\nkind: ScaledObject\nmetadata:\n  name: pubsub-scaledobject\n  namespace: keda-pubsub-test\nspec:\n  scaleTargetRef:\n    name: keda-pubsub-go\n  triggers:\n  - type: gcp-pubsub\n    metadata:\n      subscriptionSize: \"5\"\n      subscriptionName: \"mysubscription\" # Required \n      credentialsFromEnv: GOOGLE_APPLICATION_CREDENTIALS_JSON # Required\n\n```\n\nFinally, we're able to test the scaler by adding messages to the topic.\n\n```sh\nfor x in {1..20}\ndo\ngcloud beta pubsub topics publish $TOPIC_NAME \\\n  --message \"Test Message ${x}\"\ndone\n\n```\n\nYou should now see the number of replicas increasing.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkedacore%2Fsample-go-gcppubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkedacore%2Fsample-go-gcppubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkedacore%2Fsample-go-gcppubsub/lists"}