Skip to content

Commit

Permalink
Merge pull request #1 from CIS-Projects-in-CS-S21/grpc-api-def
Browse files Browse the repository at this point in the history
Grpc api def
  • Loading branch information
ryan-mcgregor authored Apr 2, 2021
2 parents 8f2c759 + 8bac9c9 commit 18692e3
Show file tree
Hide file tree
Showing 30 changed files with 7,526 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/gke.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Build and Deploy to GKE

on:
push:
branches:
- master

env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: kic-cluster
GKE_ZONE: us-east1-d
DEPLOYMENT_NAME: kic-feed
IMAGE: kic-users
NAMESPACE: kic

jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@master
with:
version: '290.0.1'
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
project_id: ${{ secrets.GKE_PROJECT }}

# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker
# Get the GKE credentials so we can deploy to the cluster
- run: |-
gcloud container clusters get-credentials "$GKE_CLUSTER" --zone "$GKE_ZONE"
# Build the Docker image
- name: Build
run: |-
docker build \
--tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" \
.
# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA"
# Set up kustomize
- name: Set up Kustomize
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name: Deploy
run: |-
./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$DEPLOYMENT_NAME -n $NAMESPACE
kubectl get services -o wide -n $NAMESPACE
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.15.6-alpine as builder

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build ./cmd/server/server.go

FROM alpine as production

WORKDIR /app
COPY --from=builder /app/server .

ENTRYPOINT [ "./server" ]
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build:
go build -o ./bin/server ./cmd/server/server.go

push:
docker build -t gcr.io/keeping-it-casual/kic-feed:dev .
docker push gcr.io/keeping-it-casual/kic-feed:dev
Binary file added bin/server
Binary file not shown.
73 changes: 73 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"net"
"os"
"os/signal"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"

"github.com/kic/feed/internal/server"
"github.com/kic/feed/pkg/logging"
pbfeed "github.com/kic/feed/pkg/proto/feed"
)

func main() {
IsProduction := os.Getenv("PRODUCTION") != ""
var logger *zap.SugaredLogger
var connectionURL string
if IsProduction {
logger = logging.CreateLogger(zapcore.InfoLevel)
connectionURL = "keeping-it-casual.com:50051"
} else {
logger = logging.CreateLogger(zapcore.DebugLevel)
connectionURL = "test.keeping-it-casual.com:50051"
}

ListenAddress := ":" + os.Getenv("PORT")

listener, err := net.Listen("tcp", ListenAddress)

if err != nil {
logger.Fatalf("Unable to listen on %v: %v", ListenAddress, err)
}

grpcServer := grpc.NewServer()

if err != nil {
logger.Fatalf("Unable connect to db %v", err)
}

conn, err := grpc.Dial(connectionURL, grpc.WithInsecure())

if err != nil {
logger.Fatalf("fail to dial: %v", err)
}

feedGen := server.NewFeedGenerator(
logger,
server.NewFriendClientWrapper(conn),
server.NewUserClientWrapper(conn),
server.NewMediaClientWrapper(conn),
)

serv := server.NewFeedService(feedGen, logger)

pbfeed.RegisterFeedServer(grpcServer, serv)

go func() {
defer listener.Close()
if err := grpcServer.Serve(listener); err != nil {
logger.Fatalf("Failed to serve: %v", err)
}
}()

defer grpcServer.Stop()

// the server is listening in a goroutine so hang until we get an interrupt signal
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
<-c
}
18 changes: 18 additions & 0 deletions deployment/auth-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
namespace: kic
name: kic-feed
spec:
selector:
matchLabels:
app: kic-feed
action: CUSTOM
provider:
name: ext-authz-grpc
rules:
- to:
- operation:
paths: [
"/kic.feed.Feed/GenerateFeedForUserRequest"
]
51 changes: 51 additions & 0 deletions deployment/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: v1
kind: Service
metadata:
namespace: kic
name: kic-feed-service
spec:
ports:
- port: 50051
targetPort: 50051
name: grpc-web
selector:
app: kic-feed
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: kic
name: kic-feed
spec:
selector:
matchLabels:
app: kic-feed
strategy:
type: Recreate
template:
metadata:
namespace: kic
labels:
app: kic-feed
spec:
containers:
- image: gcr.io/PROJECT_ID/IMAGE:TAG
name: kic-feed
imagePullPolicy: Always
ports:
- containerPort: 50051
env:
- name: PORT
value: "50051"
- name: PRODUCTION
value: "true"
- name: DB_PASS
valueFrom:
secretKeyRef:
name: db-pass
key: db-pass
- name: SECRET_KEY
valueFrom:
secretKeyRef:
name: secret-key
key: secret-key
51 changes: 51 additions & 0 deletions deployment/istio-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
namespace: kic
name: feed-virtual-service
spec:
hosts:
- "api.keeping-it-casual.com"
gateways:
- kic-gateway
http:
- match:
- uri:
prefix: /kic.feed.Feed
route:
- destination:
host: kic-feed-service
port:
number: 50051
corsPolicy:
allowOrigin:
- "*"
allowMethods:
- POST
- GET
- OPTIONS
- PUT
- DELETE
allowHeaders:
- grpc-timeout
- content-type
- keep-alive
- user-agent
- cache-control
- content-type
- content-transfer-encoding
- custom-header-1
- x-accept-content-transfer-encoding
- x-accept-response-streaming
- x-user-agent
- x-grpc-web
- Authorization
- authorization
maxAge: 1728s
exposeHeaders:
- custom-header-1
- grpc-status
- grpc-message
- Authorization
- authorization

7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/kic/feed

go 1.14

require (
github.com/golang/protobuf v1.5.2
go.uber.org/zap v1.16.0
google.golang.org/grpc v1.36.1
google.golang.org/protobuf v1.26.0
)
Loading

0 comments on commit 18692e3

Please sign in to comment.