Skip to content

Commit 892d924

Browse files
grego952abbi-gaurav
authored andcommitted
Copy orders-service and create a workflow
1 parent 8a52014 commit 892d924

25 files changed

+1547
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build docker for the Orders Service Sample
2+
on:
3+
push:
4+
branches: [ main ]
5+
paths:
6+
- "orders-service/**"
7+
workflow_dispatch:
8+
env:
9+
REGISTRY: ghcr.io
10+
SUBDIRECTORY: orders-service
11+
IMAGE_NAME: orders-service
12+
LABEL: 1.0.0
13+
jobs:
14+
build_and_push:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v3
22+
- name: Log in to the Container registry (GH Packages)
23+
uses: docker/login-action@v2
24+
with:
25+
registry: ${{ env.REGISTRY }}
26+
username: ${{ github.actor }}
27+
password: ${{ secrets.GITHUB_TOKEN }}
28+
- name: Extract metadata (tags, labels) for Docker
29+
id: meta
30+
uses: docker/metadata-action@v4
31+
with:
32+
images: ${{env.REGISTRY}}/${{ github.repository }}/${{env.IMAGE_NAME }}
33+
tags: |
34+
type=semver,pattern={{version}},value=${{ env.LABEL }}
35+
- name: Build and push Docker image
36+
uses: docker/build-push-action@v3.1.0
37+
with:
38+
context: ./${{ env.SUBDIRECTORY}}
39+
push: true
40+
tags: ${{ steps.meta.outputs.tags }}
41+
labels: ${{ steps.meta.outputs.labels }}

orders-service/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all
2+
**
3+
# Allow
4+
!internal
5+
!pkg
6+
!cmd
7+
!go.mod
8+
!go.sum

orders-service/Dockerfile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM golang:1.14-alpine as builder
2+
3+
ENV BASE_APP_DIR=/go/src/github.com/kyma-project/examples/orders-service \
4+
CGO_ENABLED=0 \
5+
GOOS=linux \
6+
GOARCH=amd64
7+
8+
WORKDIR ${BASE_APP_DIR}
9+
10+
COPY ./go.mod .
11+
COPY ./go.sum .
12+
13+
# cache deps before building and copying source so that we don't need to re-download as much
14+
# and so that source changes don't invalidate our downloaded layer
15+
RUN go mod download
16+
17+
#
18+
# copy files allowed in .dockerignore
19+
#
20+
COPY . ${BASE_APP_DIR}/
21+
22+
RUN go build -ldflags "-s -w" -a -o main cmd/main.go \
23+
&& mkdir /app \
24+
&& mv ./main /app/main
25+
26+
# get latest CA certs
27+
FROM alpine:latest as certs
28+
RUN apk --update add ca-certificates
29+
30+
# result container
31+
FROM alpine:latest
32+
33+
LABEL source = git@github.com:kyma-project/examples.git
34+
35+
COPY --from=builder /app /app
36+
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
37+
38+
ENTRYPOINT ["/app/main"]

orders-service/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
APP_NAME = orders-service
2+
IMG = $(DOCKER_PUSH_REPOSITORY)$(DOCKER_PUSH_DIRECTORY)/$(APP_NAME)
3+
TAG = $(DOCKER_TAG)
4+
5+
.PHONY: docker-compose
6+
test:
7+
docker-compose -f ./docker-compose.yaml
8+
9+
.PHONY: test
10+
test:
11+
go test ./... -count=1
12+
13+
.PHONY: build-image
14+
build-image:
15+
docker build -t $(APP_NAME):latest .
16+
17+
.PHONY: push-image
18+
push-image:
19+
docker tag $(APP_NAME) $(IMG):$(TAG)
20+
docker push $(IMG):$(TAG)
21+
22+
.PHONY: ci-pr
23+
ci-pr: test build-image push-image
24+
25+
.PHONY: ci-main
26+
ci-main: test build-image push-image

orders-service/README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Orders Service
2+
3+
## Overview
4+
5+
This example demonstrates how you can use Kyma to expose microservices and Functions on HTTP endpoints and bind them to an external database.
6+
7+
This example contains:
8+
9+
- A sample application (microservice) written in [Go](http://golang.org). It can expose HTTP endpoints used to create, read, and delete basic order JSON entities, as described in the [service's OpenAPI specification](docs/openapi.yaml). This service can run with either a default in-memory database or the external Redis database.
10+
11+
- A [serverless](https://kyma-project.io/#/01-overview/serverless/README) Function with the ability to expose HTTP endpoints used to read all order records or post single orders. Just like the microservice, the Function can run with either the default in-memory database or the external Redis instance. See the source code of this Function in the [`function.yaml`](./deployment/orders-function.yaml)) file under the **spec.source** field.
12+
13+
To see this microservice and Function in action, see the [getting started guides](https://github.com/kyma-project/kyma/blob/release-1.24/docs/getting-started/01-overview.md) and learn more about exposing services and Functions through API Rule CRs. You will also learn how to bind them to an external application like Redis and subscribe them to events from a sample mock application.
14+
15+
## Prerequisites
16+
17+
- Kyma 1.14 or higher. To deploy the Function, [Serverless](https://github.com/kyma-project/kyma/tree/release-1.24/docs/serverless) must be installed on the cluster
18+
- [Kubectl](https://kubernetes.io/docs/reference/kubectl/kubectl/) 1.16 or higher
19+
- [Helm](https://helm.sh/) 3.0 or higher (optional)
20+
21+
## Installation
22+
23+
You can install Orders Service (microservice or Function) either through kubectl or Helm.
24+
25+
### Use kubectl
26+
27+
To install the microservice on a Kyma cluster, run:
28+
29+
```bash
30+
kubectl create ns orders-service
31+
kubectl apply -f ./deployment/orders-service-deployment.yaml
32+
kubectl apply -f ./deployment/orders-service-service.yaml
33+
```
34+
35+
To install the Function on a Kyma cluster, run:
36+
37+
```bash
38+
kubectl create ns orders-service
39+
kubectl apply -f ./deployment/orders-function.yaml
40+
```
41+
42+
### Use Helm
43+
44+
To install the microservice on a Kyma cluster, run:
45+
46+
```bash
47+
helm install orders-service --namespace orders-service --create-namespace --timeout 60s --wait ./chart
48+
```
49+
50+
See the [`values.yaml`](./chart/values.yaml) file for the configuration of the Helm release.
51+
52+
## Cleanup
53+
54+
See how to remove the example from the cluster through kubectl and Helm.
55+
56+
### Use kubectl
57+
58+
Run this command to completely remove the microservice and all its resources from the cluster:
59+
60+
```bash
61+
kubectl delete all -l app=orders-service -n orders-service
62+
kubectl delete ns orders-service
63+
```
64+
65+
Run this command to completely remove the Function and all its resources from the cluster:
66+
67+
```bash
68+
kubectl delete all -l app=orders-function -n orders-service
69+
kubectl delete ns orders-service
70+
```
71+
72+
### Use Helm
73+
74+
Run this command to completely remove the Helm release with the example and all its resources from the cluster:
75+
76+
```bash
77+
helm delete orders-service -n orders-service
78+
kubectl delete ns orders-service
79+
```
80+
81+
## Configuration
82+
83+
To configure the microservice or the Function, override the default values of these environment variables:
84+
85+
| Environment variable | Description | Required | Default value |
86+
| ---------------------- | ----------------------------------------------------------------------------- | ------ | ------------- |
87+
| **APP_PORT** | Specifies the port of the running service. The function doesn't use this variable. | No | `8080` |
88+
| **APP_REDIS_PREFIX** | Specifies the prefix for all Redis environment variables. See the variables in other rows. | No | `REDIS_` |
89+
| **{APP_REDIS_PREFIX}HOST** | Specifies the host of the Redis instance. | No | `nil` |
90+
| **{APP_REDIS_PREFIX}PORT** | Specifies the port of the Redis instance. | No | `nil` |
91+
| **{APP_REDIS_PREFIX}REDIS_PASSWORD** | Specifies the password to authorize access to the Redis instance. | No | `nil` |
92+
93+
See the example:
94+
95+
```bash
96+
export APP_REDIS_PREFIX="R_"
97+
export R_HOST="abc.com"
98+
export R_PORT="8080"
99+
export R_REDIS_PASSWORD="xyz"
100+
```
101+
102+
> **NOTE:** To allow the microservice and the Function to communicate with the Redis instance, you must provide the **{APP_REDIS_PREFIX}HOST**, **{APP_REDIS_PREFIX}PORT**, **{APP_REDIS_PREFIX}REDIS_PASSWORD** environments. Otherwise, the microservice and the Function will always use in-memory storage.
103+
104+
## Testing
105+
106+
Learn how to test both the microservice and the Function.
107+
108+
### Microservice
109+
110+
To send a sample order to the microservice, run:
111+
112+
```bash
113+
curl -X POST ${APP_URL}/orders -k \
114+
-H "Content-Type: application/json" -d \
115+
'{
116+
"consignmentCode": "76272727",
117+
"orderCode": "76272725",
118+
"consignmentStatus": "PICKUP_COMPLETE"
119+
}'
120+
```
121+
122+
To retrieve all orders saved in storage, run:
123+
124+
```bash
125+
curl -X GET ${APP_URL}/orders -k
126+
```
127+
128+
**APP_URL** is the URL of the running microservice. See the [tutorial on exposing an application with an API Rule](https://github.com/kyma-project/kyma/blob/release-1.24/docs/api-gateway/08-02-exposesecure.md) for reference.
129+
130+
131+
> **TIP:** See the [service's OpenAPI specification](docs/openapi.yaml) for details of all endpoints.
132+
133+
### Function
134+
135+
To send a sample order to the Function, run:
136+
137+
```bash
138+
curl -X POST ${FUNCTION_URL} -k \
139+
-H "Content-Type: application/json" -d \
140+
'{
141+
"consignmentCode": "76272727",
142+
"orderCode": "76272725",
143+
"consignmentStatus": "PICKUP_COMPLETE"
144+
}'
145+
```
146+
147+
To retrieve all orders saved in storage, run:
148+
149+
```bash
150+
curl -X GET ${FUNCTION_URL} -k
151+
```
152+
153+
**FUNCTION_URL** is the URL of the running Function. See the [tutorial on exposing a Function with an API Rule](https://github.com/kyma-project/kyma/blob/release-1.24/docs/serverless/08-02-expose-function.md) for reference.

orders-service/chart/.helmignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
22+
.vscode/

orders-service/chart/Chart.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
description: Kyma example 'orders-service'
3+
name: kyma-example-orders-service
4+
version: 0.1.0
5+
home: https://kyma-project.io
6+
icon: https://github.com/kyma-project/kyma/blob/main/logo.png?raw=true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ template "fullname" . }}
5+
namespace: {{ .Release.Namespace }}
6+
labels:
7+
app: {{ template "fullname" . }}
8+
example: {{ template "fullname" . }}
9+
release: {{ .Release.Name }}
10+
spec:
11+
replicas: {{ .Values.deployment.replicas }}
12+
selector:
13+
matchLabels:
14+
app: {{ template "fullname" . }}
15+
example: {{ template "fullname" . }}
16+
release: {{ .Release.Name }}
17+
template:
18+
metadata:
19+
labels:
20+
app: {{ template "fullname" . }}
21+
example: {{ template "fullname" . }}
22+
release: {{ .Release.Name }}
23+
spec:
24+
containers:
25+
- name: {{ template "fullname" . }}
26+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
27+
imagePullPolicy: {{ .Values.image.pullPolicy }}
28+
resources:
29+
limits:
30+
cpu: {{ .Values.pod.resources.limits.cpu }}
31+
memory: {{ .Values.pod.resources.limits.memory }}
32+
requests:
33+
cpu: {{ .Values.pod.resources.requests.cpu }}
34+
memory: {{ .Values.pod.resources.requests.memory }}
35+
env:
36+
- name: APP_PORT
37+
value: "{{ .Values.pod.env.appPort }}"
38+
- name: APP_REDIS_PREFIX
39+
value: "{{ .Values.pod.env.redisPrefix }}"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ template "fullname" . }}
5+
namespace: {{ .Release.Namespace }}
6+
labels:
7+
app: {{ template "fullname" . }}
8+
example: {{ template "fullname" . }}
9+
release: {{ .Release.Name }}
10+
spec:
11+
type: {{ .Values.service.type }}
12+
ports:
13+
- name: {{ .Values.service.name }}
14+
port: {{ .Values.service.port }}
15+
protocol: {{ .Values.service.protocol }}
16+
targetPort: {{ .Values.service.targetPort }}
17+
selector:
18+
app: {{ template "fullname" . }}
19+
example: {{ template "fullname" . }}
20+
release: {{ .Release.Name }}

0 commit comments

Comments
 (0)