grpc - replaced fortune-builder app with official greeter app (#7360)

This commit is contained in:
Long 2021-07-23 02:21:18 +05:30 committed by GitHub
parent 9e274dd41c
commit 91a4bba026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 220 additions and 617 deletions

View file

@ -1,93 +1,152 @@
# gRPC
This example demonstrates how to route traffic to a gRPC service through the
nginx controller.
This example demonstrates how to route traffic to a gRPC service through the nginx controller.
## Prerequisites
1. You have a kubernetes cluster running.
2. You have a domain name such as `example.com` that is configured to route
traffic to the ingress controller. Replace references to
`fortune-teller.stack.build` (the domain name used in this example) to your
own domain name (you're also responsible for provisioning an SSL certificate
for the ingress).
3. You have the nginx-ingress controller installed in typical fashion (must be
at least
[quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0](https://quay.io/kubernetes-ingress-controller/nginx-ingress-controller)
for grpc support.
4. You have a backend application running a gRPC server and listening for TCP
traffic. If you prefer, you can use the
[fortune-teller](https://github.com/kubernetes/ingress-nginx/tree/master/images/grpc-fortune-teller)
application provided here as an example.
2. You have a domain name such as `example.com` that is configured to route traffic to the ingress controller.
3. You have the nginx-ingress controller installed as per docs, with gRPC support.
4. You have a backend application running a gRPC server and listening for TCP traffic. If you want, you can use <https://github.com/grpc/grpc-go/blob/91e0aeb192456225adf27966d04ada4cf8599915/examples/features/reflection/server/main.go> as an example.
5. You're also responsible for provisioning an SSL certificate for the ingress. So you need to have a valid SSL certificate, deployed as a Kubernetes secret of type tls, in the same namespace as the gRPC application.
### Step 1: kubernetes `Deployment`
### Step 1: Create a Kubernetes `Deployment` for gRPC app
```sh
$ kubectl create -f app.yaml
```
- Make sure your gRPC application pod is running and listening for connections. For example you can try a kubectl command like this below:
```
$ kubectl get po -A -o wide | grep go-grpc-greeter-server
```
- If you have a gRPC app deployed in your cluster, then skip further notes in this Step 1, and continue from Step 2 below.
This is a standard kubernetes deployment object. It is running a grpc service
listening on port `50051`.
- As an example gRPC application, we can use this app <https://github.com/grpc/grpc-go/blob/91e0aeb192456225adf27966d04ada4cf8599915/examples/features/reflection/server/main.go> .
The sample application
[fortune-teller-app](https://github.com/kubernetes/ingress-nginx/tree/master/images/grpc-fortune-teller)
is a grpc server implemented in go. Here's the stripped-down implementation:
- To create a container image for this app, you can use [this Dockerfile](../../../images/go-grpc-greeter-server/rootfs/Dockerfile).
```go
func main() {
grpcServer := grpc.NewServer()
fortune.RegisterFortuneTellerServer(grpcServer, &FortuneTeller{})
lis, _ := net.Listen("tcp", ":50051")
grpcServer.Serve(lis)
}
```
- If you use the Dockerfile mentioned above, to create a image, then given below is an example of a Kubernetes manifest, to create a deployment resource, that uses that image. If needed, then edit this manifest to suit your needs. Assuming the name of this yaml file is `deployment.go-grpc-greeter-server.yaml` ;
The takeaway is that we are not doing any TLS configuration on the server (as we
are terminating TLS at the ingress level, grpc traffic will travel unencrypted
inside the cluster and arrive "insecure").
```
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: go-grpc-greeter-server
name: go-grpc-greeter-server
spec:
replicas: 1
selector:
matchLabels:
app: go-grpc-greeter-server
template:
metadata:
labels:
app: go-grpc-greeter-server
spec:
containers:
- image: <reponame>/go-grpc-greeter-server # Edit this for your reponame
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 50m
memory: 50Mi
name: go-grpc-greeter-server
ports:
- containerPort: 50051
EOF
```
For your own application you may or may not want to do this. If you prefer to
forward encrypted traffic to your POD and terminate TLS at the gRPC server
itself, add the ingress annotation `nginx.ingress.kubernetes.io/backend-protocol: "GRPCS"`.
### Step 2: Create the Kubernetes `Service` for the gRPC app
### Step 2: the kubernetes `Service`
- You can use the following example manifest to create a service of type ClusterIP. Edit the name/namespace/label/port to match your deployment/pod ;
```
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
labels:
app: go-grpc-greeter-server
name: go-grpc-greeter-server
spec:
ports:
- port: 80
protocol: TCP
targetPort: 50051
selector:
app: go-grpc-greeter-server
type: ClusterIP
EOF
```
- You can save the above example manifest to a file with name `service.go-grpc-greeter-server.yaml` and edit it to match your deployment/pod, if required. You can create the service resource with a kubectl command like this ;
```sh
$ kubectl create -f svc.yaml
```
```
$ kubectl create -f service.go-grpc-greeter-server.yaml
```
Here we have a typical service. Nothing special, just routing traffic to the
backend application on port `50051`.
### Step 3: Create the Kubernetes `Ingress` resource for the gRPC app
### Step 3: the kubernetes `Ingress`
- Use the following example manifest of a ingress resource to create a ingress for your grpc app. If required, edit it to match your app's details like name, namespace, service, secret etc. Make sure you have the required SSL-Certificate, existing in your Kubernetes cluster, in the same namespace where the gRPC app is. The certificate must be available as a kubernetes secret resource, of type "kubernete.io/tls" https://kubernetes.io/docs/concepts/configuration/secret/#tls-secrets. This is because we are terminating TLS on the ingress;
```sh
$ kubectl create -f ingress.yaml
```
```
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/backend-protocol: "GRPC"
name: fortune-ingress
namespace: default
spec:
rules:
- host: grpctest.dev.mydomain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: go-grpc-greeter-server
port:
number: 80
tls:
# This secret must exist beforehand
# The cert must also contain the subj-name grpctest.dev.mydomain.com
# https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/PREREQUISITES.md#tls-certificates
- secretName: wildcard.dev.mydomain.com
hosts:
- grpctest.dev.mydomain.com
EOF
```
A few things to note:
- If you save the above example manifest as a file named `ingress.go-grpc-greeter-server.yaml` and edit it to match your deployment and service, you can create the ingress like this ;
1. We've tagged the ingress with the annotation
`nginx.ingress.kubernetes.io/backend-protocol: "GRPC"`. This is the magic
ingredient that sets up the appropriate nginx configuration to route http/2
traffic to our service.
1. We're terminating TLS at the ingress and have configured an SSL certificate
`fortune-teller.stack.build`. The ingress matches traffic arriving as
`https://fortune-teller.stack.build:443` and routes unencrypted messages to
our kubernetes service.
```
$ kubectl create -f ingress.go-grpc-greeter-server.yaml
```
- The takeaway is that we are not doing any TLS configuration on the server (as we are terminating TLS at the ingress level, gRPC traffic will travel unencrypted inside the cluster and arrive "insecure").
- For your own application you may or may not want to do this. If you prefer to forward encrypted traffic to your POD and terminate TLS at the gRPC server itself, add the ingress annotation `nginx.ingress.kubernetes.io/backend-protocol: "GRPCS"`.
- A few more things to note:
- We've tagged the ingress with the annotation `nginx.ingress.kubernetes.io/backend-protocol: "GRPC"`. This is the magic ingredient that sets up the appropriate nginx configuration to route http/2 traffic to our service.
- We're terminating TLS at the ingress and have configured an SSL certificate `wildcard.dev.mydomain.com`. The ingress matches traffic arriving as `https://grpctest.dev.mydomain.com:443` and routes unencrypted messages to the backend Kubernetes service.
### Step 4: test the connection
Once we've applied our configuration to kubernetes, it's time to test that we
can actually talk to the backend. To do this, we'll use the
[grpcurl](https://github.com/fullstorydev/grpcurl) utility:
- Once we've applied our configuration to Kubernetes, it's time to test that we can actually talk to the backend. To do this, we'll use the [grpcurl](https://github.com/fullstorydev/grpcurl) utility:
```sh
$ grpcurl fortune-teller.stack.build:443 build.stack.fortune.FortuneTeller/Predict
{
"message": "Let us endeavor so to live that when we come to die even the undertaker will be sorry.\n\t\t-- Mark Twain, \"Pudd'nhead Wilson's Calendar\""
}
```
```
$ grpcurl grpctest.dev.mydomain.com:443 helloworld.Greeter/SayHello
{
"message": "
}
```
### Debugging Hints

View file

@ -1,23 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: fortune-teller-app
labels:
k8s-app: fortune-teller-app
namespace: default
spec:
replicas: 1
selector:
matchLabels:
k8s-app: fortune-teller-app
template:
metadata:
labels:
k8s-app: fortune-teller-app
spec:
containers:
- name: fortune-teller-app
image: quay.io/kubernetes-ingress-controller/grpc-fortune-teller:0.1
ports:
- containerPort: 50051
name: grpc

View file

@ -1,7 +0,0 @@
apiVersion: "stable.k8s.psg.io/v1"
kind: "Certificate"
metadata:
name: fortune-teller.stack.build
namespace: default
spec:
domain: "fortune-teller.stack.build"

View file

@ -1,24 +0,0 @@
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/backend-protocol: "GRPC"
name: fortune-ingress
namespace: default
spec:
rules:
- host: fortune-teller.stack.build
http:
paths:
- backend:
serviceName: fortune-teller-service
servicePort: grpc
tls:
# This secret must exist beforehand
# The cert must also contain the subj-name fortune-teller.stack.build
# https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/PREREQUISITES.md#tls-certificates
- secretName: fortune-teller.stack.build
hosts:
- fortune-teller.stack.build

View file

@ -1,12 +0,0 @@
apiVersion: v1
kind: Service
metadata:
name: fortune-teller-service
namespace: default
spec:
selector:
k8s-app: fortune-teller-app
ports:
- port: 50051
targetPort: 50051
name: grpc

View file

@ -0,0 +1,55 @@
# Copyright 2021 The Kubernetes Authors. All rights reserved.
#
# 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.
# set default shell
SHELL=/bin/bash -o pipefail -o errexit
DIR:=$(strip $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
INIT_BUILDX=$(DIR)/../../hack/init-buildx.sh
TAG ?=v$(shell date +%m%d%Y)-$(shell git rev-parse --short HEAD)
REGISTRY ?= local
IMAGE = $(REGISTRY)/go-grpc-greeter-server
# required to enable buildx
export DOCKER_CLI_EXPERIMENTAL=enabled
# build with buildx
PLATFORMS?=linux/amd64,linux/arm,linux/arm64,linux/s390x
OUTPUT=
PROGRESS=plain
build: ensure-buildx
docker buildx build \
--platform=${PLATFORMS} $(OUTPUT) \
--progress=$(PROGRESS) \
--pull \
-t $(IMAGE):$(TAG) rootfs
# push the cross built image
push: OUTPUT=--push
push: build
# enable buildx
ensure-buildx:
# this is required for cloudbuild
ifeq ("$(wildcard $(INIT_BUILDX))","")
@curl -sSL https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/hack/init-buildx.sh | bash
else
@exec $(INIT_BUILDX)
endif
@echo "done"
.PHONY: build push ensure-buildx

View file

@ -0,0 +1,24 @@
timeout: 1200s
options:
substitution_option: ALLOW_LOOSE
# job builds a multi-arch docker image for amd64,arm,arm64 and s390x.
machineType: N1_HIGHCPU_8
steps:
- name: gcr.io/k8s-testimages/gcb-docker-gcloud:v20210622-762366a
entrypoint: bash
env:
- DOCKER_CLI_EXPERIMENTAL=enabled
- TAG=$_GIT_TAG
- BASE_REF=$_PULL_BASE_REF
- REGISTRY=gcr.io/k8s-staging-ingress-nginx
# default cloudbuild has HOME=/builder/home and docker buildx is in /root/.docker/cli-plugins/docker-buildx
# set the home to /root explicitly to if using docker buildx
- HOME=/root
args:
- -c
- |
gcloud auth configure-docker \
&& make push
substitutions:
_GIT_TAG: "12345"
_PULL_BASE_REF: "master"

View file

@ -0,0 +1,16 @@
FROM golang:buster as build
WORKDIR /go/src/greeter-server
RUN curl -o main.go https://github.com/grpc/grpc-go/blob/91e0aeb192456225adf27966d04ada4cf8599915/examples/features/reflection/server/main.go && \
go mod init greeter-server && \
go mod tidy && \
go build -o /greeter-server main.go
FROM gcr.io/distroless/base-debian10
COPY --from=build /greeter-server /
EXPOSE 50051
CMD ["/greeter-server"]

View file

@ -1 +0,0 @@
bazel-*

View file

@ -1,15 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_prefix")
gazelle(
name = "gazelle",
args = [
"-build_file_name",
"BUILD,BUILD.bazel",
],
command = "fix",
prefix = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller",
)
go_prefix(
prefix = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller",
)

View file

@ -1,104 +0,0 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/golang/protobuf"
packages = [
"proto",
"protoc-gen-go/descriptor",
"ptypes",
"ptypes/any",
"ptypes/duration",
"ptypes/timestamp"
]
revision = "925541529c1fa6821df4e44ce2723319eb2be768"
version = "v1.0.0"
[[projects]]
name = "github.com/vromero/gofortune"
packages = [
"lib",
"lib/fortune"
]
revision = "8a3c485287c0d3d3e4f8f0d9e0058f6cdd29740d"
version = "v0.0.1"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = [
"context",
"http2",
"http2/hpack",
"idna",
"internal/timeseries",
"lex/httplex",
"trace"
]
revision = "b3c676e531a6dc479fa1b35ac961c13f5e2b4d2e"
[[projects]]
name = "golang.org/x/text"
packages = [
"collate",
"collate/build",
"internal/colltab",
"internal/gen",
"internal/tag",
"internal/triegen",
"internal/ucd",
"language",
"secure/bidirule",
"transform",
"unicode/bidi",
"unicode/cldr",
"unicode/norm",
"unicode/rangetable"
]
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
branch = "master"
name = "google.golang.org/genproto"
packages = ["googleapis/rpc/status"]
revision = "35de2414665fc36f56b72d982c5af480d86de5ab"
[[projects]]
name = "google.golang.org/grpc"
packages = [
".",
"balancer",
"balancer/base",
"balancer/roundrobin",
"codes",
"connectivity",
"credentials",
"encoding",
"encoding/proto",
"grpclb/grpc_lb_v1/messages",
"grpclog",
"internal",
"keepalive",
"metadata",
"naming",
"peer",
"reflection",
"reflection/grpc_reflection_v1alpha",
"resolver",
"resolver/dns",
"resolver/passthrough",
"stats",
"status",
"tap",
"transport"
]
revision = "d89cded64628466c4ab532d1f0ba5c220459ebe8"
version = "v1.11.2"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "fcd36504ba120cfbbe161eac2ddd439c884fcef61ce318b5c0a544d27bc47043"
solver-name = "gps-cdcl"
solver-version = 1

View file

@ -1,59 +0,0 @@
# grpc-fortune-teller
This is the grpc server application for the nginx-ingress grpc example. Bazel
0.12 is used for the building and container management.
## Build
Builds a statically compiled go binary cross-compiled for linux:
```
$ bazel build //app:fortune
Target //app:fortune up-to-date:
bazel-bin/app/linux_amd64_static_pure_stripped/fortune
```
> To build for your host system, comment out the `goos` and `goarch` attributes
> in the `go_binary` rule.
## Run
Builds a minimal docker image that wraps the go_binary, loads it into your local
docker image repository, and runs it:
```sh
$ bazel run //app:image
Loaded image ID: sha256:aa597c897c873116fcbfccafecf5ab0f6f4178a05e4a00c8e79de91ac0d2e9e7
Tagging aa597c897c873116fcbfccafecf5ab0f6f4178a05e4a00c8e79de91ac0d2e9e7 as bazel/app:image
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/fortunes.dat
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/literature
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/literature.dat
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/riddles
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/riddles.dat
2018/05/01 02:13:43 Restored /tmp/fortune-teller/usr/share/games/fortunes/fortunes
2018/05/01 02:13:43 Assets restored to /tmp/fortune-teller
2018/05/01 02:13:43 Listening for gRPC requests at 50051
```
Or run it via docker:
```sh
$ docker run bazel/app:image
```
Build image and push to the container registry specified in the `container_push`
rule:
```sh
$ bazel run //app:push
```
## Invoke
```sh
$ grpcurl -plaintext localhost:50051 build.stack.fortune.FortuneTeller/Predict
{
"message": "Whenever the literary German dives into a sentence, that is the last\nyou are going to see of him until he emerges on the other side of his\nAtlantic with his verb in his mouth.\n\t\t-- Mark Twain \"A Connecticut Yankee in King Arthur's Court\""
}
```

View file

@ -1,62 +0,0 @@
workspace(name = "com_github_kubernetes_ingress_nginx_images_grpc_fortune_teller")
#####################################################################
# RULES_GO
#####################################################################
git_repository(
name = "io_bazel_rules_go",
remote = "https://github.com/bazelbuild/rules_go.git",
commit = "161c91485b007c6bf51c0e81808cf4ee2ded299d",
)
http_archive(
name = "com_github_scele_rules_go_dep",
urls = ["https://github.com/scele/rules_go_dep/archive/49a5e4ca9f6a16c9b4c930a51ce3a537498bb4e1.tar.gz"],
strip_prefix = "rules_go_dep-49a5e4ca9f6a16c9b4c930a51ce3a537498bb4e1",
sha256 = "f170d3d6f55e216f1493f975cde6c489d7070da2a8a41fd4de9812d96f4fb38b",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
load("@com_github_scele_rules_go_dep//dep:dep.bzl", "dep_import")
go_register_toolchains(go_version = "1.10.1")
go_rules_dependencies()
dep_import(
name = "godeps",
prefix = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller",
gopkg_lock = "//:Gopkg.lock",
)
load("@godeps//:Gopkg.bzl", "go_deps")
go_deps()
#############################################################
# RULES_DOCKER
#############################################################
RULES_DOCKER_VERSION = "553d5506bb7325185950f91533b967da8f5bc536"
http_archive(
name = "io_bazel_rules_docker",
url = "https://github.com/bazelbuild/rules_docker/archive/%s.zip" % RULES_DOCKER_VERSION,
strip_prefix = "rules_docker-" + RULES_DOCKER_VERSION,
sha256 = "e0b3d966f2a5c0fe921b6294df7c823afa63b4c439f0a7f3b9da3ed6534bab83",
)
load(
"@io_bazel_rules_docker//container:container.bzl",
container_repositories = "repositories",
)
container_repositories()
load(
"@io_bazel_rules_docker//go:image.bzl",
go_image_repositories = "repositories",
)
go_image_repositories()

View file

@ -1,69 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_embed_data")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//container:push.bzl", "container_push")
# Concatenates the fortune databases to a single bundle.
# May need to adjust paths for your system (built on ubuntu 16.04).
# $ apt-get install fortune
genrule(
name = "tar",
outs = ["fortune.tar"],
cmd = " && ".join([
"OUT=$$(pwd)/$@",
"tar -cvf $$OUT /usr/share/games/fortunes",
]),
)
# Generates a .go source file with the tarball content in
# the fortuneFiles variable.
go_embed_data(
name = "fortune_assets",
srcs = [
":tar",
],
package = "main",
unpack = True,
var = "fortuneFiles",
)
go_library(
name = "go_default_library",
srcs = [
"main.go",
":fortune_assets", # keep
],
importpath = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller/app",
visibility = ["//visibility:private"],
deps = [
"//proto/fortune:go_default_library",
"@com_github_vromero_gofortune//lib/fortune:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//reflection:go_default_library",
],
)
go_binary(
name = "fortune",
embed = [":go_default_library"],
goarch = "amd64",
goos = "linux",
pure = "on",
static = "on",
visibility = ["//visibility:public"],
)
go_image(
name = "image",
binary = ":fortune",
visibility = ["//visibility:public"],
)
container_push(
name = "push",
format = "Docker",
image = ":image",
registry = "quay.io",
repository = "kubernetes-ingress-controller/grpc-fortune-teller",
tag = "0.1",
)

View file

@ -1,137 +0,0 @@
/*
Copyright 2018 The Kubernetes Authors.
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.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"path"
proto "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller/proto/fortune"
"github.com/vromero/gofortune/lib/fortune"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
)
const (
grpcPort = 50051
)
func main() {
baseDir := "/tmp/fortune-teller"
mustMkdirAll(baseDir)
opts := []grpc.ServerOption{
grpc.MaxConcurrentStreams(200),
}
grpcServer := grpc.NewServer(opts...)
fortuneTeller := &FortuneTeller{
fs: createFortuneFilesystemNodeDescriptor(baseDir),
}
proto.RegisterFortuneTellerServer(grpcServer, fortuneTeller)
reflection.Register(grpcServer)
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort))
if err != nil {
log.Fatalf("Error while starting grpc server: %v\n", err)
}
log.Printf("Listening for gRPC requests at %d\n", grpcPort)
grpcServer.Serve(lis)
}
// FortuneTeller - struct that will implement the grpc service interface.
type FortuneTeller struct {
fs *fortune.FileSystemNodeDescriptor
}
// Predict - implementation for the grpc unary request method.
func (f *FortuneTeller) Predict(ctx context.Context, r *proto.PredictionRequest) (*proto.PredictionResponse, error) {
_, data, err := fortune.GetRandomFortune(*f.fs)
if err != nil {
return nil, grpc.Errorf(codes.Internal, "Unable to render fortune: %v", err)
}
return &proto.PredictionResponse{
Message: data,
}, nil
}
func createFortuneFilesystemNodeDescriptor(baseDir string) *fortune.FileSystemNodeDescriptor {
// Restore the packed fortune data
fortuneDir := path.Join(baseDir, "usr/share/games/fortunes")
mustRestore(baseDir, fortuneFiles, nil)
// init gofortune fs
fs, err := fortune.LoadPaths([]fortune.ProbabilityPath{
{Path: fortuneDir},
})
if err != nil {
log.Fatalf("Unable to load fortune paths: %v", err)
}
fortune.SetProbabilities(&fs, true) // consider all equal probabilities
return &fs
}
// mustRestore - Restore assets.
func mustRestore(baseDir string, assets map[string][]byte, mappings map[string]string) {
// unpack variable is provided by the go_embed data and is a
// map[string][]byte such as {"/usr/share/games/fortune/literature.dat":
// bytes... }
for basename, bytes := range assets {
if mappings != nil {
replacement := mappings[basename]
if replacement != "" {
basename = replacement
}
}
filename := path.Join(baseDir, basename)
dirname := path.Dir(filename)
//log.Printf("file %s, dir %s, rel %d, abs %s, absdir: %s", file, dir, rel, abs, absdir)
if err := os.MkdirAll(dirname, os.ModePerm); err != nil {
log.Fatalf("Failed to create asset dir %s: %v", dirname, err)
}
if err := ioutil.WriteFile(filename, bytes, os.ModePerm); err != nil {
log.Fatalf("Failed to write asset %s: %v", filename, err)
}
log.Printf("Restored %s", filename)
}
log.Printf("Assets restored to %s", baseDir)
}
// mustMkdirAll - make all dirs and panic if fail
func mustMkdirAll(dirs ...string) {
for _, dir := range dirs {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
panic(fmt.Sprintf("Failed mkdir %s: %v", dir, err))
}
}
}

View file

@ -1,24 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
go_library(
name = "go_default_library",
srcs = ["doc.go"],
embed = [":build_stack_fortune_go_proto"], # keep
importpath = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller/proto/fortune",
visibility = ["//visibility:public"],
)
proto_library(
name = "build_stack_fortune_proto",
srcs = ["fortune.proto"],
visibility = ["//visibility:public"],
)
go_proto_library(
name = "build_stack_fortune_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_grpc"],
importpath = "github.com/kubernetes/ingress-nginx/images/grpc-fortune-teller/proto/fortune",
proto = ":build_stack_fortune_proto",
visibility = ["//visibility:public"],
)

View file

@ -1,14 +0,0 @@
syntax = "proto3";
package build.stack.fortune;
message PredictionRequest {
}
message PredictionResponse {
string message = 1;
}
service FortuneTeller {
rpc Predict(PredictionRequest) returns (PredictionResponse);
}