From 658b589df3c2c507b3783720b2671d3be71fac3f Mon Sep 17 00:00:00 2001 From: Doctor Vince Date: Wed, 14 Feb 2024 12:59:12 -0500 Subject: [PATCH] incorporate some feedback (#14) --- Makefile | 9 ++++-- README.md | 18 +++++++++++ helm/Chart.yaml | 8 +++++ helm/templates/configmap.yaml | 9 ++++++ helm/templates/deployment.yaml | 55 ++++++++++++++++++++++++++++++++++ helm/templates/service.yaml | 14 +++++++++ helm/values.yaml | 11 +++++++ 7 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 README.md create mode 100644 helm/Chart.yaml create mode 100644 helm/templates/configmap.yaml create mode 100644 helm/templates/deployment.yaml create mode 100644 helm/templates/service.yaml create mode 100644 helm/values.yaml diff --git a/Makefile b/Makefile index 5f6531db..b772cfa6 100644 --- a/Makefile +++ b/Makefile @@ -43,10 +43,13 @@ push-sandbox-image: image push-image: image docker push ${DOCKER_IMAGE}:latest +# if you want to use the sandbox repo, this target puts the files from this service in place. You will +# need to update the sandbox Makefile to get helm to load everything properly. load-sandbox: - @cp sandbox/service.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-service.yaml" - @cp sandbox/deployment.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-deployment.yaml" - @cp sandbox/configmap.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-configmap.yaml" + @cp helm/values.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-values.yaml" + @cp helm/service.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-service.yaml" + @cp helm/deployment.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-deployment.yaml" + @cp helm/configmap.yaml "${SANDBOX_TEMPLATE_DIR}/skeleton-configmap.yaml" @echo "Be sure to do a helm (re)load to get the service started" # https://gist.github.com/prwhite/8168133 diff --git a/README.md b/README.md new file mode 100644 index 00000000..863ad460 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +### What is this? +This is a small service that exposes a toy RESTful API. It is intended to be a template or example for how we write this sort of code in FleetServices. +It also contains example configuration for other useful things such as: +1. Creating a Docker image +2. Launching the service image into our [sandbox](https://github.com/metal-toolbox/sandbox) environment. See the [helm](./helm) directory. +3. Typical pull-request workflow (linting, building, code-analysis) +4. Creating a release image of the software for deployment to Kubernetes. + +### How do I use it? +That depends on what you want to do. +- You can clone this repo as a template service, make your modifications, and push it to a new repo on Github.com. +- You can get a sense of how we handle common tasks (like using [gin](https://gin-gonic.com) or [zap](https://pkg.go.dev/go.uber.org/zap)) without being overwhelmed by details of a non-trivial service. +- You can propose new conventions (such as adding a client for [NATS](https://nats.io) or [FleetDB](https://github.com/metal-toolbox/fleetdb)) +- You can launch this service into our [kind](https://kind.sigs.k8s.io) sandbox by doing `helm install skeleton-test helm` from the root of this repo. Port-forward to your local environment to test the API by hand, or configure service-to-service tests with other services in kind. + +Much of the functionality is encapsulated into `Makefile` targets. On the one hand this is a pretty clear abuse of `make`, but on the other we do it in many other repositories. `make` will tab-prompt the user with potential targets (e.g. `build`, `image`, `push-sandbox-image` et al.) + +I hope it serves to reduce the friction of getting a service into production for you. diff --git a/helm/Chart.yaml b/helm/Chart.yaml new file mode 100644 index 00000000..a9a8be97 --- /dev/null +++ b/helm/Chart.yaml @@ -0,0 +1,8 @@ +--- +apiVersion: v2 +name: fleet-skeleton-chart +description: "template service helm chart" +type: application +version: 0.1.0 +appVersion: 0.1.0 + diff --git a/helm/templates/configmap.yaml b/helm/templates/configmap.yaml new file mode 100644 index 00000000..48a1458d --- /dev/null +++ b/helm/templates/configmap.yaml @@ -0,0 +1,9 @@ +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Values.app.name }}-config +data: + config.yaml: | + listen_address: 0.0.0.0:{{ .Values.app.containerPort }} + developer_mode: true diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml new file mode 100644 index 00000000..49945c33 --- /dev/null +++ b/helm/templates/deployment.yaml @@ -0,0 +1,55 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + k8s-service: {{ .Values.app.serviceName }} + name: {{ .Values.app.name }} +spec: + replicas: 1 + selector: + matchLabels: + k8s-service: {{ .Values.app.serviceName }} + template: + metadata: + labels: + k8s-service: {{ .Values.app.serviceName }} + spec: + containers: + - image: "{{ .Values.image.repo }}/{{ .Values.app.name }}:{{ .Values.image.tag }}" + name: {{ .Values.app.name }} + args: + - server + - "--config={{ .Values.app.configPath }}/config.yaml" + ports: + - name: api-port + containerPort: {{ .Values.app.containerPort }} + volumeMounts: + - name: config-volume + mountPath: {{ .Values.app.configPath }} + env: + securityContext: + capabilities: + drop: + - NET_RAW + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 1000 + resources: + limits: + cpu: 100m + memory: 100M + requests: + cpu: 100m + memory: 100M + livenessProbe: + httpGet: + path: {{ .Values.app.livenessURI }} + port: api-port + initialDelaySeconds: 30 + periodSeconds: 30 + volumes: + - name: config-volume + configMap: + name: {{ .Values.app.name}}-config + restartPolicy: Always diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml new file mode 100644 index 00000000..ab7a59fc --- /dev/null +++ b/helm/templates/service.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: v1 +kind: Service +metadata: + labels: + k8s-service: {{ .Values.app.serviceName }} + name: {{ .Values.app.name }} +spec: + ports: + - protocol: {{ .Values.app.protocol | default "TCP" }} + port: {{ .Values.app.containerPort }} + targetPort: {{ .Values.app.containerPort }} + selector: + k8s-service: {{ .Values.app.serviceName }} diff --git a/helm/values.yaml b/helm/values.yaml new file mode 100644 index 00000000..301205a0 --- /dev/null +++ b/helm/values.yaml @@ -0,0 +1,11 @@ +--- +image: + repo: localhost:5001 + tag: latest + +app: + name: fleet-rest-skeleton + serviceName: skeleton-api + configPath: /etc/skeleton + livenessURI: /_health/liveness + containerPort: 7500