Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventarc pubsub example #37

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# cloud-run-express-api

ExpressJS API running on GCP Cloud Run

## Massdriver `gcp-cloud-run` Template

The `gcp-cloud-run` template will run your application on GCP's Cloud Run.

**Files**:

- a [terraform module](./src) is included that configurings IAM permissions and sets up you environment variables. You likely _do not_ need to modify these files. This module is simply rigging code to integrate with Massdriver Cloud.
- the [`massdriver.yaml`](./massdriver.yaml) controls the UI to expose for configuring your application and its dependencies. By default there are a lot of fields in your [`params`](https://docs.massdriver.cloud/bundles/configuration#bundle-params) section, feel free to remove fields that you do not want exposed in your configuration form in Massdriver Cloud. Values that you do not want to change (e.g.: your image repository) can be hard coded in the [main.tf](./src/main.tf) file.
4 changes: 4 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dockerfile
.dockerignore
node_modules
*debug.log
8 changes: 8 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18

COPY package.json .
RUN yarn install

COPY . .

CMD ["yarn", "start"]
34 changes: 34 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// [START eventarc_pubsub_handler]
const express = require('express');
const {
toMessagePublishedData,
} = require('@google/events/cloud/pubsub/v1/MessagePublishedData');
const app = express();

app.use(express.json());
app.post('/', (req, res) => {
if (!req.body) {
const errorMessage = 'no Pub/Sub message received';
res.status(400).send(`Bad Request: ${errorMessage}`);
console.log(`Bad Request: ${errorMessage}`);
return;
}
if (!req.body.message) {
const errorMessage = 'invalid Pub/Sub message format';
res.status(400).send(`Bad Request: ${errorMessage}`);
console.log(`Bad Request: ${errorMessage}`);
return;
}
// Cast to MessagePublishedEvent for IDE autocompletion
const pubSubMessage = toMessagePublishedData(req.body);
const name =
pubSubMessage.message && pubSubMessage.message.data
? Buffer.from(pubSubMessage.message.data, 'base64').toString().trim()
: 'World';

const result = `Hello, ${name}! ID: ${req.get('ce-id') || ''}`;
console.log(result);
res.send(result);
});

module.exports = app;
9 changes: 9 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3"

services:
app:
platform: linux/amd64
build:
context: .
dockerfile: ./Dockerfile

22 changes: 22 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020 Google, LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START eventarc_pubsub_server]
const app = require('./app.js');
const PORT = parseInt(process.env.PORT) || 8080;

app.listen(PORT, () =>
console.log(`nodejs-eventarc-pubsub listening on port ${PORT}`)
);
// [END eventarc_pubsub_server]
25 changes: 25 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cloud-eventarc-pubsub",
"version": "1.0.0",
"private": true,
"description": "Simple Events for Cloud Run – Pub/Sub sample",
"main": "index.js",
"author": "Google LLC",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">= 12.0.0"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@google/events": "^5.0.0",
"express": "^4.16.4"
},
"devDependencies": {
}
}
105 changes: 105 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/massdriver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
schema: draft-07
name: cloud-run-eventarc-pubsub
description: CloudRun EventArc handler for PubSub messages
source_url: github.com/YOUR_ORG/cloud-run-eventarc-pubsub
access: private
type: application

app:
envs: {}
# MONGO_DSN: .connections.mongo_authentication.data.authentication.username + ":" + .connections.mongo_authentication.data.authentication.password + "@" + .connections.mongo_authentication.data.authentication.hostname + ":" + (.connections.mongo_authentication.data.authentication.port|tostring)
policies: []
# Use jq expressions to select policies from artifact security blocks
# - .connections.sqs.data.security.iam.subscribe

params:
required:
- container
- scaling
- endpoint
properties:
container:
type: object
title: Container
required:
- tag
properties:
tag:
title: Image Tag
$ref: https://raw.githubusercontent.com/massdriver-cloud/artifact-definitions/main/definitions/types/docker-image-tag.json
default: latest
scaling:
type: object
title: Scaling
properties:
max_instances:
type: integer
title: Max Instances
description: The maximum number of instances to run at once.
default: 5
endpoint:
type: object
title: Endpoint
description: Configure a public endpoint with DNS and a SSL certificate.
required:
- enabled
properties:
enabled:
type: boolean
title: Enabled
default: false
dependencies:
enabled:
oneOf:
- required:
- zone
- subdomain
properties:
enabled:
const: true
title: Enabled
zone:
$ref: https://raw.githubusercontent.com/massdriver-cloud/artifact-definitions/main/definitions/types/gcp-cloud-dns-managed-zone.json
title: DNS Zone
subdomain:
type: string
title: Subdomain
description: The subdomain will be prefixed to the DNS zone, creating a public endpoint for your application.
- properties:
enabled:
const: false
title: Enabled

connections:
required:
- gcp_authentication
- gcp_subnetwork
# - firebase
properties:
gcp_authentication:
$ref: massdriver/gcp-service-account
gcp_subnetwork:
$ref: massdriver/gcp-subnetwork
firebase:
$ref: massdriver/gcp-firebase-authentication

ui:
ui:order:
- container
- scaling
- endpoint
- "*"
container:
ui:order:
- repository
- image
- tag
endpoint:
ui:order:
- enabled
- zone
- subdomain
zone:
name:
ui:field: dnsZonesDropdown
cloud: gcp
49 changes: 49 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/src/_providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
terraform {
required_version = ">= 1.0"
required_providers {
mdxc = {
source = "massdriver-cloud/mdxc"
}
massdriver = {
source = "massdriver-cloud/massdriver"
version = "~> 1.1"
}
google = {
source = "hashicorp/google"
version = "~> 4.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 4.0"
}
null = {
source = "hashicorp/null"
version = "~> 3.0"
}
}
}

locals {
gcp_project_id = var.gcp_authentication.data.project_id
gcp_credentials = jsonencode(var.gcp_authentication.data)
gcp_region = var.gcp_subnetwork.specs.gcp.region
}

provider "google" {
project = local.gcp_project_id
credentials = local.gcp_credentials
region = local.gcp_region
}

provider "google-beta" {
project = local.gcp_project_id
credentials = local.gcp_credentials
region = local.gcp_region
}

provider "mdxc" {
gcp = {
project = local.gcp_project_id
credentials = local.gcp_credentials
}
}
43 changes: 43 additions & 0 deletions gcp/cloud-run/eventarc-pubsub/src/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
locals {
repository = "us-west2-docker.pkg.dev/mdc-trellis-staging-trellis/trellis"
image = "eventarc"
}

module "application_cloud_run" {
source = "github.com/massdriver-cloud/terraform-modules//massdriver-application-gcp-cloud-run?ref=3ee9d5f"
md_metadata = var.md_metadata
location = local.gcp_region
container_image = "${local.repository}/${local.image}:${var.container.tag}"
container_port = 8080
endpoint = var.endpoint
max_instances = var.scaling.max_instances
vpc_connector = var.gcp_subnetwork.data.infrastructure.vpc_access_connector
}

resource "google_eventarc_trigger" "main" {
name = var.md_metadata.name_prefix
location = local.gcp_region
matching_criteria {
attribute = "type"
value = "google.cloud.pubsub.topic.v1.messagePublished"
}
destination {
cloud_run_service {
service = var.md_metadata.name_prefix
region = local.gcp_region
}
}
labels = var.md_metadata.default_tags

depends_on = [
module.application_cloud_run,
google_project_service.main
]
}

resource "google_project_service" "main" {
service = "eventarc.googleapis.com"

disable_dependent_services = false
disable_on_destroy = false
}
4 changes: 4 additions & 0 deletions gcp/pubsub/k8s-pubsub-worker/app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dockerfile
.dockerignore
node_modules
*debug.log
1 change: 1 addition & 0 deletions gcp/pubsub/k8s-pubsub-worker/app/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: "3"

services:
app:
platform: linux/amd64
build:
context: .
dockerfile: ./Dockerfile
Expand Down
4 changes: 2 additions & 2 deletions gcp/pubsub/k8s-pubsub-worker/massdriver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type: application

app:
envs:
SUBSCRIPTION_ID: .connections.subscription.data.infastructure.grn
SUBSCRIPTION_ID: .connections.subscription.data.infrastructure.grn
policies:
- .connections.subscription.data.security.iam.subscribe
- .connections.subscription.data.security.iam.subscriber

params:
required:
Expand Down
4 changes: 2 additions & 2 deletions gcp/pubsub/k8s-pubsub-worker/src/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
image:
repository:
tag:
repository: us-west2-docker.pkg.dev/mdc-trellis-staging-trellis/trellis/k8s-pubsub-worker
tag: 1

command: []
args: []
Expand Down
3 changes: 1 addition & 2 deletions gcp/pubsub/k8s-pubsub-worker/src/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module "helm" {
source = "github.com/massdriver-cloud/terraform-modules//massdriver-application-helm?ref=8892201"
name = var.md_metadata.name_prefix
namespace = var.namespace
namespace = "default"
chart = "${path.module}/chart"
kubernetes_cluster = var.kubernetes_cluster
additional_envs = var.additionalEnvs
}