Service
backend-openapi.yaml
(view on GitHub)
:
---
openapi: 3.0.2
info:
title: Order Service API
version: 0.1.0
description: Simple Order Service API
contact:
name: Laurent Broudoux
url: https://github.com/lbroudoux
email: laurent@microcks.io
license:
name: MIT License
url: https://opensource.org/licenses/MIT
paths:
/orders:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/OrderInfo'
examples:
valid_order:
value:
customerId: lbroudoux
productQuantities:
- productName: Millefeuille
quantity: 1
- productName: Eclair Cafe
quantity: 2
totalPrice: 9.4
invalid_order:
value:
customerId: lbroudoux
productQuantities:
- productName: Millefeuille
quantity: 1
- productName: Eclair Chocolat
quantity: 1
totalPrice: 4.8
responses:
"201":
content:
application/json:
schema:
$ref: '#/components/schemas/OrderInfo'
examples:
valid_order:
value:
id: 5455c8e8-087a-426e-8440-65c8c005d871
status: CREATED
customerId: lbroudoux
productQuantities:
- productName: Millefeuille
quantity: 1
- productName: Eclair Cafe
quantity: 2
totalPrice: 9.4
description: Order is correct and has been created
"422":
content:
application/json:
schema:
$ref: '#/components/schemas/UnavailableProduct'
examples:
invalid_order:
value:
productName: Eclair Chocolat
details: Eclair Chocolat are not available at the moment
description: "Order cannot be processed because of a validation error (ex:\
\ unavailable product)"
operationId: PlaceOrder
summary: Place a new Order
description: Place a new Order in the system. Will perform extra checks before
saving Order to detect invalid demand
components:
schemas:
OrderInfo:
description: Represents info needed for creating an Order
required:
- customerId
- productQuantities
- totalPrice
type: object
properties:
customerId:
description: Identifier of customer of this order
type: string
productQuantities:
description: Desired products and quantities for this order
type: array
items:
$ref: '#/components/schemas/ProductQuantity'
totalPrice:
format: double
description: Total price of the order
type: number
ProductQuantity:
description: Association of product name with quantity
required:
- productName
- quantity
type: object
properties:
productName:
description: Desired product name
type: string
quantity:
description: Desired quantity
type: integer
Order:
description: Full created Order with all informations
type: object
allOf:
- required:
- id
- status
type: object
properties:
id:
description: Unique identifier of order
type: string
status:
description: Status of Order
enum:
- CREATED
- VALIDATED
- CANCELED
- FAILED
type: string
- $ref: '#/components/schemas/OrderInfo'
UnavailableProduct:
description: ""
required:
- productName
type: object
properties:
productName:
description: ""
type: string
details:
description: Details of unavailability
type: string
score-backend.yaml
(view on GitHub)
:
apiVersion: score.dev/v1b1
metadata:
name: backend
containers:
my-container:
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo Hello Backend; sleep 5; done"]
score-frontend.yaml
(view on GitHub)
:
apiVersion: score.dev/v1b1
metadata:
name: frontend
containers:
my-container:
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo Hello $BACKEND_SVC!; sleep 5; done"]
variables:
BACKEND_SVC: ${resources.backend.url}/orders
resources:
backend:
type: service
params:
port: 8181
artifacts: backend-openapi.yaml:true
name: Order Service API
version: 0.1.0
10-service-with-microcks.provisioners.yaml
(view on GitHub)
:
- uri: template://custom-provisioners/service-with-microcks
type: service
description: Outputs a service URL for connecting to an other workload (a Microcks mock is generated if not found).
init: |
hostname: {{ splitList "." .Id | last }}
{{ if .Params.artifacts }}
{{ $artifacts := .Params.artifacts | splitList "," }}
{{ $parsedPath := $artifacts | first | splitList "/" }}
{{ if eq (len $parsedPath) 0 }}
resourcesPath: "."
{{ else }}
resourcesPath: {{ trimSuffix (last $parsedPath) (first $artifacts) | trimSuffix "/" }}
{{ end }}
{{ end }}
supported_params:
- port
- artifacts
- name
- version
outputs: |
{{ $w := (index .WorkloadServices .Init.hostname) }}
{{ if or (not $w) (not $w.ServiceName) }}
url: http://microcks:8080/rest/{{ .Params.name | replace " " "+" }}/{{ .Params.version }}/
{{ else }}
url: http://{{ .Init.hostname }}:{{ .Params.port }}/
{{ end }}
name: {{ .Init.hostname }}
expected_outputs:
- url
- name
services: |
{{ $w := (index .WorkloadServices .Init.hostname) }}
{{ if or (not $w) (not $w.ServiceName) }}
{{ .Init.hostname }}-mock:
image: quay.io/microcks/microcks-cli:latest
restart: on-failure
entrypoint:
- "microcks"
- "import"
- "{{ .Params.artifacts }}"
- "--microcksURL=http://microcks:8080/api"
- "--insecure-tls"
- "--keycloakClientId=foo"
- "--keycloakClientSecret=bar"
cap_drop:
- ALL
read_only: true
user: "65532"
volumes:
- type: bind
source: {{ .Init.resourcesPath }}
target: /{{ .Init.resourcesPath }}
read_only: true
depends_on:
microcks:
condition: service_started
required: true
{{ end }}
10-service.provisioners.yaml
(view on GitHub)
:
- uri: template://community-provisioners/static-service
type: service
description: Outputs the name of the Workload dependency if it exists in the list of Workloads.
init: |
name: {{ splitList "." .Id | last }}
outputs: |
{{ $w := (index .WorkloadServices .Init.name) }}
{{ if or (not $w) (not $w.ServiceName) }}{{ fail "unknown workload" }}{{ end }}
name: {{ $w.ServiceName | quote }}
url: http://{{ $w.ServiceName }}
expected_outputs:
- name
- url
Initialize your local workspace, by importing a specific community provisioner:
score-compose init --provisioners REPLACE-ME-WITH-ACTUAL-PROVISIONER-FILE-URL.yaml
Note: you need to replace REPLACE-ME-WITH-ACTUAL-PROVISIONER-FILE-URL.yaml by the actual provisioner file you want to use and import. More information here.
Get the provisioners definition:
score-compose provisioners list
Generate the platform specific manifests:
score-compose generate score.yaml
See the resource outputs:
score-compose resources list
You can run the following command on each resource listed with the previous command to get their outputs:
score-compose resources get-outputs
Deploy the generated manifests:
docker compose up -d
See the running containers:
docker ps
10-service-with-microcks-cli.provisioners.yaml
(view on GitHub)
:
- uri: cmd://bash#service-with-microcks-cli
type: service
description: Outputs a service URL for connecting to an other workload (a Microcks mock is generated if not found).
supported_params:
- port
- artifacts
- name
- version
expected_outputs:
- url
- name
args:
- -c
- |
STDIN=$(cat)
PARAM_PORT=$(echo $STDIN | yq eval -p json '.resource_params.port')
PARAM_NAME=$(echo $STDIN | yq eval -p json '.resource_params.name')
PARAM_VERSION=$(echo $STDIN | yq eval -p json '.resource_params.version')
PARAM_ARTIFACTS=$(echo $STDIN | yq eval -p json '.resource_params.artifacts')
WORKLOAD=$(echo $STDIN | yq eval -p json '.resource_id | split(".") | .[-1]')
WORKLOAD_EXISTS=$(echo $STDIN | WORKLOAD=${WORKLOAD} yq eval -p json '.workload_services | has(strenv(WORKLOAD))')
URL_HOSTNAME=${WORKLOAD}:${PARAM_PORT}
URL_SCHEME="http"
URL_PATH=""
if [ "$WORKLOAD_EXISTS" != "true" ]; then
URL_HOSTNAME="microcks.microcks.svc.cluster.local:8080"
URL_PATH=/rest/$(echo $PARAM_NAME | yq '. |= sub(" ", "+")')/$PARAM_VERSION
set -eu -o pipefail
microcks import ${PARAM_ARTIFACTS} --microcksURL=https://microcks.127.0.0.1.nip.io --insecure-tls --keycloakClientId=foo --keycloakClientSecret=bar >&2
fi
OUTPUTS='{"resource_outputs":{"url":"%s://%s%s/","name":"%s"},"manifests":[]}'
printf "$OUTPUTS" "$URL_SCHEME" "$URL_HOSTNAME" "$URL_PATH" "$WORKLOAD"
10-service-with-netpol.provisioners.yaml
(view on GitHub)
:
- uri: template://community-provisioners/static-service-with-netpol
type: service
description: Outputs the name of the Workload dependency if it exists in the list of Workloads, and generate NetworkPolicies between them.
init: |
name: {{ splitList "." .Id | last }}
outputs: |
{{ $w := (index .WorkloadServices .Init.name) }}
{{ if or (not $w) (not $w.ServiceName) }}{{ fail "unknown workload" }}{{ end }}
name: {{ .Init.name }}
url: http://{{ $w.ServiceName }}
expected_outputs:
- name
- url
manifests: |
- apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ .Init.name }}-from-{{ .SourceWorkload }}-ingress
{{ if ne .Namespace "" }}
namespace: {{ .Namespace }}
{{ end }}
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: {{ .Init.name }}
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app.kubernetes.io/name: {{ .SourceWorkload }}
namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: {{ .Namespace | default "default" }}
ports:
{{- range $k, $port := (index .WorkloadServices .Init.name).Ports }}
- protocol: TCP
port: {{ $port.TargetPort }}
{{- end }}
- apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ .SourceWorkload }}-to-{{ .Init.name }}-egress
{{ if ne .Namespace "" }}
namespace: {{ .Namespace }}
{{ end }}
spec:
podSelector:
matchLabels:
app.kubernetes.io/name: {{ .SourceWorkload }}
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app.kubernetes.io/name: {{ .Init.name }}
namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: {{ .Namespace | default "default" }}
ports:
{{- range $k, $port := (index .WorkloadServices .Init.name).Ports }}
- protocol: TCP
port: {{ $port.TargetPort }}
{{- end }}
10-service.provisioners.yaml
(view on GitHub)
:
- uri: template://community-provisioners/static-service
type: service
description: Outputs the name of the Workload dependency if it exists in the list of Workloads.
init: |
name: {{ splitList "." .Id | last }}
outputs: |
{{ $w := (index .WorkloadServices .Init.name) }}
{{ if or (not $w) (not $w.ServiceName) }}{{ fail "unknown workload" }}{{ end }}
name: {{ $w.ServiceName | quote }}
url: http://{{ $w.ServiceName }}
expected_outputs:
- name
- url
Initialize your local workspace, by importing a specific community provisioner:
score-k8s init --provisioners REPLACE-ME-WITH-ACTUAL-PROVISIONER-FILE-URL.yaml
Note: you need to replace REPLACE-ME-WITH-ACTUAL-PROVISIONER-FILE-URL.yaml by the actual provisioner file you want to use and import. More information here.
Get the provisioners definition:
score-k8s provisioners list
Generate the platform specific manifests:
score-k8s generate score.yaml
See the resource outputs:
score-k8s resources list
You can run the following command on each resource listed with the previous command to get their outputs:
score-k8s resources get-outputs
Deploy the generated manifests:
kubectl apply -f manifests.yaml
See the running containers:
kubectl get all