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

gRPC service #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ typings/

# next.js build output
.next

# gontador
.vscode
vendor
gontador
21 changes: 13 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
FROM docker.tools.pnet.ch/r-base:latest
FROM golang:latest AS builder

RUN yum install -y nodejs && yum clean all
USER baseuser
# Download and install the latest release of dep
ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep

WORKDIR /app
ADD node_modules node_modules

CMD [ "node", "index.js"]
# Copy the code from the host and compile it
WORKDIR $GOPATH/src/github.com/philipsahli/gontador/
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure --vendor-only -v
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /app github.com/philipsahli/gontador/src

EXPOSE 3000

ADD index.js .
FROM scratch
COPY --from=builder /app ./
ENTRYPOINT ["./app"]
169 changes: 169 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#

# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/golang/protobuf"

[[constraint]]
branch = "master"
name = "github.com/almariah/go-graphite-client"

[[constraint]]
name = "github.com/go-redis/redis"
version = "6.14.2"

[[constraint]]
name = "github.com/gorilla/mux"
version = "1.6.2"

[prune]
go-tests = true
unused-packages = true
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,62 @@
# contador

A 12-factor counter microservice

Exposes an HTTP-API and a gRPC Interface.

## Dependant services

**Telegraf**

Run somewhere a simple tcp listener to mock a telegraf daemon.

nc -l 2003

**Redis**

Run somewhere a docker container with an unsecured redis daemon.

docker run --name gontador-redis -p 6379:6379 -d redis


## Test local

go build -o gontador src/*.go
REDIS_URL=192.168.1.2:6379 METRIC_HOST=192.168.1.2 METRIC_PORT=2003 ./gontador

### HTTP Interface

A HTTP/1.1 GET request to `/counter` increments the counter by 1 and returns the new counter.

curl http://localhost:3000/counter

### gRPC Service

Gonsumidor increments the counter by 1 and prints out the counter every 2 seconds. Communication over gRPC to the service on port `3001`.

git clone https://github.com/philipsahli/gonsumidor.git
cd gonsumidor && go build -o gonsumidor main.go
go build -o gonsumidor main.go

See https://github.com/philipsahli/gonsumidor/blob/master/README.md

## Test on Openshift

bash -xe deploy/deploy.sh

URL=`oc get route.route.openshift.io/gontador -o go-template --template={{.spec.host}}`

curl -v $URL/counter

## Undeploy

oc delete all -l app=gontador
oc delete template gontador-template

## Generate grpc service

PROTOC_ZIP=protoc-3.3.0-osx-x86_64.zip\ncurl -OL https://github.com/google/protobuf/releases/download/v3.3.0/$PROTOC_ZIP\nsudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc\nrm -f $PROTOC_ZIP
go get -u github.com/golang/protobuf/protoc-gen-go

protoc -I service/ service/gontador.proto --go_out=plugins=grpc:service

12 changes: 6 additions & 6 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/sh
docker build -t docker.pnet.ch/misc/contador .
docker push docker.pnet.ch/misc/contador:latest
if oc get dc | grep contador -q; then
oc import-image contador
docker build -t docker.pnet.ch/misc/gontador .
docker push docker.pnet.ch/misc/gontador:latest
if oc get dc | grep gontador -q; then
oc import-image gontador
else
oc create -f template.yaml
oc new-app --template=contador-template
oc expose service/contador
oc new-app --template=gontador-template
oc expose service/gontador
fi
50 changes: 50 additions & 0 deletions deploy/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
# annotations:
# description: Defines how to build the application
# template.alpha.openshift.io/wait-for-ready: 'true'
# labels:
# app: gontador
# template: gontador
name: gontador-build
# namespace: gontador
spec:
# failedBuildsHistoryLimit: 5
# nodeSelector: null
output:
to:
kind: ImageStreamTag
name: 'gontador:latest'
# postCommit:
# script: ./manage.py test
# resources: {}
# runPolicy: Serial
source:
git:
uri: 'https://github.com/philipsahli/gontador.git'
ref: 'grpc-service'
type: Git
strategy:
type: Docker
dockerStrategy:
dockerfilePath: Dockerfile

# env:
# - name: PIP_INDEX_URL
# from:
# kind: ImageStreamTag
# name: 'python:3.6'
# namespace: openshift

# successfulBuildsHistoryLimit: 5
triggers:
- imageChange:
type: ImageChange
- type: ConfigChange
- github:
secret: 4tqgB6gCvDQBoWBiKtNcm3NsT37MM602KhLGKrCR
type: GitHub
# status:
# lastVersion: 1

Loading