GithubHelp home page GithubHelp logo

googlecloudplatform / cloud-run-samples Goto Github PK

View Code? Open in Web Editor NEW
210.0 23.0 97.0 17.1 MB

Samples for Cloud Run

Home Page: https://cloud.run

License: Apache License 2.0

Dockerfile 26.42% Python 10.14% Shell 42.86% Go 20.58%
google-cloud sample samples golang nodejs containers python cloud-run serverless google-cloud-run

cloud-run-samples's Introduction

Cloud Run Samples

This repository contains sample applications used in Cloud Run documentation.

Sample Index

For all Cloud Run code samples, see the Cloud Run sample browser.

Sample Description Languages
Hello World service Hello World! A quickstart sample collection Go, Node.js, Python, Java, PHP, Ruby, Shell, C++
Hello World job Job quickstart samples Go, Node.js, Python, Java, Shell
Manual Logging Structured logging without client library Go, Node.js, Python, Java
System Package Use system-installed binaries. Go, Node.js, Java, Python
Pub/Sub Push Handle messages from a push subscription Go, Node.js, Python, Java
Image Processing Event-driven image analysis & transformation Go, Node.js, Python, Java
Cloud SQL (MySQL) Use MySQL with Cloud Run Node.js, Python, Java
Cloud SQL (Postgres) Use Postgres with Cloud Run Node.js, Python, Java
Global State Snippet: Using global state for in-memory caching Go, Nodejs, Python, Java
Lazy Initialization Snippet: Lazy loading faster cold start Go, Nodejs, Python, Java
Hello Broken "Broken" service for troubleshooting. Go, Node.js, Java, Python
End user authentication Integrate with Identity Platform to restrict access Node.js, Java, Python
Invoking with gRPC Demonstrates service-to-service gRPC requests Go
Service to Service Request Snippet: Authenticated requests between services Go, Node.js, Python, Java
gcloud as a service Use gcloud and gsutil in a service Go
VPC Testing Egress and ingress settings with VPC Python
Multi-container Hello Nginx Multi-containers with nginx YAML
Markdown Preview 2 tier secure microservices for Markdown rendering Go, Nodejs, Python, Java

Samples by Language: nodejs, golang, python, java, php, ruby

Deploy a sample with a button click!

The Cloud Run Button makes your Cloud Run service deployable with the push of a button. (It will open a Cloud Shell window.)

Sample Cloud Run Button Details from the Cloud Run Documentation
Hello World - Python Run on Google Cloud Quickstart: build and deploy
Hello World - Java Run on Google Cloud Quickstart: build and deploy
Hello World - Node.js Run on Google Cloud Quickstart: build and deploy
Hello World - Go Run on Google Cloud Quickstart: build and deploy

Find more samples to deploy with the Cloud Run Button by using the Sample Index above.

Contributing changes

Entirely new samples are not accepted. Bug fixes are welcome, either as pull requests or as GitHub issues.

See CONTRIBUTING.md for details on how to contribute.

Licensing

Code in this repository is licensed under the Apache 2.0. See LICENSE.

cloud-run-samples's People

Contributors

averikitsch avatar dependabot[bot] avatar dinagraves avatar google-cloud-policy-bot[bot] avatar grayside avatar halvards avatar hennikatsoci avatar jamesward avatar jping0220 avatar justinbeckwith avatar pattishin avatar renovate-bot avatar rogerthatdev avatar rsamborski avatar wietsevenema avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cloud-run-samples's Issues

[Policy Bot] found one or more issues with this repository.

Policy Bot found one or more issues with this repository.

  • Default branch is 'main'
  • Branch protection is enabled
  • Renovate bot is enabled
  • Merge commits disabled
  • There is a CODEOWNERS file
  • There is a valid LICENSE.md
  • There is a CODE_OF_CONDUCT.md
  • There is a CONTRIBUTING.md
  • There is a SECURITY.md

Make the default branch "main"

Switch the default branch to main. This requires:

  • Tweaking a Github setting
  • Editing in progress PRs to point to the new branch
  • Providing an update instruction for existing collaborators
  • Editing existing Cloud Build triggers.
  • Editing any templates or scripts in the repo referring to master.

Multi-container runtime issue

Hello,

I followed the tutorial exactly the same as mentioned in https://cloud.google.com/run/docs/internet-proxy-nginx-sidecar
It deployed succesfully, but when I try using cloud run generated link, it gave me this error

"upstream connect error or disconnect/reset before headers. reset reason: protocol error"

Edit: Basically I want to achieve this
https://cloud.google.com/run/docs/internet-proxy-nginx-sidecar#:~:text=translate%20HTTP/2%20to%20HTTP/1%20if%20your%20application%20containers%20supports%20only%20HTTP/1
which translates http/2 to http/1.
When I checked "Use HTTP/2 end-to-end" option, it gave above error. But when I turned it off, it can run normally.

How does this happen and how to solve it? Thank you

testing(buildcop): add handling for TIMEOUT and INTERNAL_ERROR

#45 does not address these in it's handling, but these are also error conditions:

  • TIMEOUT: Something has had a performance regression. We need to know if this happens as more than a flake so we can take steps: 1) Optimize our test, 2) Upgrade the Cloud Build infra, or 3) File a product bug
  • INTERNAL_ERROR: Cloud Build infra has a problem, not sure what cases surface this kind error but if it happens consistently we need alerting to escalate.

cc @dinagraves

Add retries to gcloud operations used in sample testing

To minimize the frequency of flaky tests, we prefer to retry gcloud on failures. This is best done on a per gcloud step to avoid needless rerun of entire builds.

In discussion with @averikitsch, we might incorporate gist.github.com/averikitsch/961ff4e99395adcf1bcb1849fc1a4020 into or alongside https://github.com/GoogleCloudPlatform/cloud-run-samples/blob/main/testing/cloudbuild-templates/common.sh so it can be easily referenced for use.

This should be incorporated into the cloudbuild-templates and the existing sample test pipelines.

Unable to install ko

$ GO111MODULE=on go get github.com/google/ko/cmd/ko
go: module github.com/google/ko@upgrade found (v0.14.1), but does not contain package github.com/google/ko/cmd/ko
go version
go version go1.20.2 darwin/amd64

Adjust included file rules for dockerfile-lint-pr rule

The current configuration for dockerfile-lint-pr does not cover the template files and has what seems like redundant configuration for Dockerfile matching. I did some verification on this via my local BASH 4 shell with shopt -s globstar but did not check it in a build trigger config.

  • Included Files: **/*Dockerfile, *Dockerfile
  • Excluded Files: testing/test-cases/**

Cloud Run does not load .env variables

I spend the last couple of days trying to find what I have done wrong but I am still not able to figure out because I am able to run the app locally using flask run and also using Docker using docker-compose up --build.

My issue is my Cloud Run deployment is successful but Service Unavailable when I am clicking on the URL. I checked the logs and seems my environment variables are not correctly loaded:

line 7, in <module> from web_messaging.blueprints.user import user File 
"/web_messaging/web_messaging/blueprints/user/__init__.py", line 1, in <module> from 
web_messaging.blueprints.user.views import user File 
"/web_messaging/web_messaging/blueprints/user/views.py", line 3, in <module> from 
web_messaging.extensions import mongo, login_manager, c, bc File 
"/web_messaging/web_messaging/extensions.py", line 18, in <module> twilio_client = Client(TWILIO_SID,
TWILIO_TOKEN) File "/usr/local/lib/python3.9/site-packages/twilio/rest/__init__.py", line 54, in __init__
raise TwilioException("Credentials are required to create a TwilioClient") 
twilio.base.exceptions.TwilioException: Credentials are required to create a TwilioClient    

I have a config/.env file and a config/settings.py. I am loading the env variables from .env using load_dotenv() on my config/settings.py. I decided to add some print and try/expect statements in my config/settings.py to see the value of variables.

settings.py

import os
from dotenv import load_dotenv
BASEDIR = os.path.abspath(os.path.dirname(__file__))

try:
    load_dotenv(os.path.join(BASEDIR, '.env'))
    print("OK")
    print(BASEDIR)
except Exception as e:
    print(str(e))

# Mongo Database
MONGO_URI = os.getenv('MONGO_URI')
TWILIO_SID = os.getenv('TWILIO_SID')
TWILIO_TOKEN = os.getenv('TWILIO_TOKEN')
print(MONGO_URI)
print(TWILIO_SID)

When I am running with flask run, docker-compose or on cloud-run:

  • The BASEDIR is /web_messaging/config
  • There is no exception during the load_dotenv() call

However, there is one major difference, the value of MONGO_URI, TWILIO_SID and all of my environment are correct on flask run and docker-compose but not on the Cloud Run logs. On Cloud Run, those variables are equal to None.

When I don't use a .env and directly put the value of my variables inside /web_messaging/config, there is no issues. I also tried to moved .env outside of the config file and in few other locations but I still got the same issue.

.
├── requirements.txt
├── Dockerfile
├── Docker-compose.yml
├── config    
│   ├── .env                           
│   ├── settings.py            
│   ├── gunicorn.py 
│   └── __init__.py 
├── web_messaging                   
│   ├── app.py      # where I am calling create_app() - factory pattern         
│   ├── blueprints              
│   ├── static                     
│   └── ...                 
└── ...

Dockerfile

FROM python:3.9-slim

ENV INSTALL_PATH /web_messaging
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD gunicorn -b 0.0.0.0:8080 --access-logfile - "web_messaging.app:create_app()"

docker-compose.yml

version: '2'

services:
  website:
    build: .
    command: >
      gunicorn -b 0.0.0.0:8080
        --access-logfile -
        --reload
        "web_messaging.app:create_app()"
    environment:
      PYTHONUNBUFFERED: 'true'
    volumes:
      - '.:/web_messaging'
    ports:
      - '8080:8080'

config/.env

COMPOSE_PROJECT_NAME=web_messaging
FLASK_SECRET='xxx'
MONGO_URI="mongodb+srv://xxx"
MONGO_DB='xxx'
TWILIO_SID='xxx'
TWILIO_TOKEN='xxx' 

config/settings.py

import os
from dotenv import load_dotenv
BASEDIR = os.path.abspath(os.path.dirname(__file__))


load_dotenv(os.path.join(BASEDIR, '.env'))

DEBUG = True
PYTHONDONTWRITEBYTECODE=1
#SERVER_NAME = '127.0.0.1:5000'


# Mongo Database
MONGO_DBNAME = os.getenv('MONGO_DB')
MONGO_URI = os.getenv('MONGO_URI')


# Twilio API 
FLASK_SECRET = os.getenv('FLASK_SECRET')
TWILIO_SID = os.getenv('TWILIO_SID')
TWILIO_TOKEN = os.getenv('TWILIO_TOKEN')

config/gunicorn.py

bind = '0.0.0.0:8080'
accesslog = '-'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" in %(D)sµs'

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
gcloud-report/Dockerfile
  • golang 1.20-buster
helloworld-shell/Dockerfile
  • golang 1.20-buster
jobs-shell/Dockerfile
  • ubuntu 24.04
testing/cloudbuild-templates/Dockerfile
  • alpine 20240329
testing/test-cases/Dockerfile
  • node 21-slim
testing/test-cases/module.Dockerfile
  • golang 1.22
  • alpine 20240329
testing/test-cases/multistage.Dockerfile
  • maven 3.6-jdk-8-alpine
testing/test-cases/shell.Dockerfile
  • node 21-slim
testing/test-cases/syntax.Dockerfile
volume-checker/Dockerfile
  • golang 1.20-buster
vpc-sample/Dockerfile
  • python 3.12-slim
gomod
gcloud-report/go.mod
  • go 1.19
helloworld-shell/go.mod
  • go 1.19
testing/flakybot-function/go.mod
  • go 1.19
volume-checker/go.mod
  • go 1.20
pip_requirements
testing/flakybot-function/requirements.txt
  • junit-xml ==1.9
vpc-sample/requirements.txt
  • google-auth ==2.29.0
  • requests ==2.32.2
  • functions-framework ==3.7.0

  • Check this box to trigger a request for Renovate to run again on this repository

Working mTLS gRPC sample for Cloud Run?

My suspicion is that you cannot get mTLS gRPC working in Cloud Run, but if there's a clever way to achieve this, I would be really grateful in seeing it demonstrated?

In our situation, we have our own CA and chain of trust and would like to have mTLS verify both client and server. Most GCP services now allow you to provide your own certificates, but Cloud Run seems to enforce it's own Let's Encrypt cert, even if you configure a hosted gRPC service to use mTLS itself.

Ideally, we'd like to have both server and client using our own certificates (and chain of trust bundles), but a compromise would be to have Cloud Run allow us to validate the client from the server using ClientCAs and ClientAuth fields of grpc.Creds(credentials.NewTLS(&tls.Config{...})).

Example server set-up:

lis, _ := net.Listen("tcp", addr) // addr uses env:PORT to bind port

// configure TLS client verification
var creds = grpc.Creds(credentials.NewTLS(&tls.Config{
	ClientAuth:    tls.RequireAndVerifyClientCert,
	ClientCAs:     agentBundle,
	Certificates:  []tls.Certificate{serverCert},
	RootCAs:       serverBundle,
}))

// start gRPC service
grpcServer := grpc.NewServer(creds)
RegisterOurServer(grpcServer, impl)
log.Printf("Serving on %s", addr)
return grpcServer.Serve(lis)

Can anyone provide a sample for this?

Cloud Run Sidecar Won't Update

I was having trouble updating a service I was using to deploy a sidecar container configuration. The image being built was the same as the image I updated. I needed to run

gcloud run services add-iam-policy-binding [SERVICE_NAME] \
    --member="allUsers" \
    --role="roles/run.invoker"

To update the container policy on every

gcloud run services replace service.yaml

It'd be cool if that was managed. Keep up the good work!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.