diff options
18 files changed, 178 insertions, 211 deletions
diff --git a/README.md b/README.md index 8f24a31..89b3a2a 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,6 @@ helm install rabbitmq oci://registry-1.docker.io/bitnamicharts/rabbitmq bazel run //src/rest-api/cmd:push -- --tag "rest-api-$(git rev-parse --short HEAD)" helm upgrade rest-api --set image.tag="rest-api-$(git rev-parse --short HEAD)" k8s/rest-api + +bazel run //src/payload-processor/cmd:push -- --tag "payload-processor-$(git rev-parse --short HEAD)" +helm upgrade payload-processor --set image.tag="payload-processor-$(git rev-parse --short HEAD)" k8s/payload-processor diff --git a/k8s/payloads-processor/.helmignore b/k8s/payload-processor/.helmignore index 0e8a0eb..0e8a0eb 100644 --- a/k8s/payloads-processor/.helmignore +++ b/k8s/payload-processor/.helmignore diff --git a/k8s/payloads-processor/Chart.yaml b/k8s/payload-processor/Chart.yaml index f07f67a..a845876 100644 --- a/k8s/payloads-processor/Chart.yaml +++ b/k8s/payload-processor/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 -name: payloads-processor +name: payload-processor description: A Helm chart for Kubernetes # A chart can be either an 'application' or a 'library' chart. diff --git a/k8s/payloads-processor/templates/_helpers.tpl b/k8s/payload-processor/templates/_helpers.tpl index 5afe85a..0f89fcc 100644 --- a/k8s/payloads-processor/templates/_helpers.tpl +++ b/k8s/payload-processor/templates/_helpers.tpl @@ -1,7 +1,7 @@ {{/* Expand the name of the chart. */}} -{{- define "payloads-processor.name" -}} +{{- define "payload-processor.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} {{- end }} @@ -10,7 +10,7 @@ Create a default fully qualified app name. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). If release name contains chart name it will be used as a full name. */}} -{{- define "payloads-processor.fullname" -}} +{{- define "payload-processor.fullname" -}} {{- if .Values.fullnameOverride }} {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} {{- else }} @@ -26,16 +26,16 @@ If release name contains chart name it will be used as a full name. {{/* Create chart name and version as used by the chart label. */}} -{{- define "payloads-processor.chart" -}} +{{- define "payload-processor.chart" -}} {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} {{- end }} {{/* Common labels */}} -{{- define "payloads-processor.labels" -}} -helm.sh/chart: {{ include "payloads-processor.chart" . }} -{{ include "payloads-processor.selectorLabels" . }} +{{- define "payload-processor.labels" -}} +helm.sh/chart: {{ include "payload-processor.chart" . }} +{{ include "payload-processor.selectorLabels" . }} {{- if .Chart.AppVersion }} app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} {{- end }} @@ -45,17 +45,17 @@ app.kubernetes.io/managed-by: {{ .Release.Service }} {{/* Selector labels */}} -{{- define "payloads-processor.selectorLabels" -}} -app.kubernetes.io/name: {{ include "payloads-processor.name" . }} +{{- define "payload-processor.selectorLabels" -}} +app.kubernetes.io/name: {{ include "payload-processor.name" . }} app.kubernetes.io/instance: {{ .Release.Name }} {{- end }} {{/* Create the name of the service account to use */}} -{{- define "payloads-processor.serviceAccountName" -}} +{{- define "payload-processor.serviceAccountName" -}} {{- if .Values.serviceAccount.create }} -{{- default (include "payloads-processor.fullname" .) .Values.serviceAccount.name }} +{{- default (include "payload-processor.fullname" .) .Values.serviceAccount.name }} {{- else }} {{- default "default" .Values.serviceAccount.name }} {{- end }} diff --git a/k8s/payloads-processor/templates/deployment.yaml b/k8s/payload-processor/templates/deployment.yaml index 585c443..c86398d 100644 --- a/k8s/payloads-processor/templates/deployment.yaml +++ b/k8s/payload-processor/templates/deployment.yaml @@ -1,16 +1,16 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ include "payloads-processor.fullname" . }} + name: {{ include "payload-processor.fullname" . }} labels: - {{- include "payloads-processor.labels" . | nindent 4 }} + {{- include "payload-processor.labels" . | nindent 4 }} spec: {{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} {{- end }} selector: matchLabels: - {{- include "payloads-processor.selectorLabels" . | nindent 6 }} + {{- include "payload-processor.selectorLabels" . | nindent 6 }} template: metadata: {{- with .Values.podAnnotations }} @@ -18,35 +18,27 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} labels: - {{- include "payloads-processor.selectorLabels" . | nindent 8 }} + {{- include "payload-processor.selectorLabels" . | nindent 8 }} spec: {{- with .Values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} - serviceAccountName: {{ include "payloads-processor.serviceAccountName" . }} + serviceAccountName: {{ include "payload-processor.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} containers: - name: {{ .Chart.Name }} - securityContext: - {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} - ports: - - name: http - containerPort: {{ .Values.service.port }} - protocol: TCP - livenessProbe: - httpGet: - path: / - port: http - readinessProbe: - httpGet: - path: / - port: http resources: {{- toYaml .Values.resources | nindent 12 }} + env: + - name: RABBITMQ_PASSWORD + valueFrom: + secretKeyRef: + name: rabbitmq + key: rabbitmq-password {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} diff --git a/k8s/payloads-processor/templates/serviceaccount.yaml b/k8s/payload-processor/templates/serviceaccount.yaml index 5bc090b..55e048e 100644 --- a/k8s/payloads-processor/templates/serviceaccount.yaml +++ b/k8s/payload-processor/templates/serviceaccount.yaml @@ -2,9 +2,9 @@ apiVersion: v1 kind: ServiceAccount metadata: - name: {{ include "payloads-processor.serviceAccountName" . }} + name: {{ include "payload-processor.serviceAccountName" . }} labels: - {{- include "payloads-processor.labels" . | nindent 4 }} + {{- include "payload-processor.labels" . | nindent 4 }} {{- with .Values.serviceAccount.annotations }} annotations: {{- toYaml . | nindent 4 }} diff --git a/k8s/payloads-processor/values.yaml b/k8s/payload-processor/values.yaml index 9ea2cd9..b6c065b 100644 --- a/k8s/payloads-processor/values.yaml +++ b/k8s/payload-processor/values.yaml @@ -1,11 +1,11 @@ -# Default values for payloads-processor. +# Default values for payload-processor. # This is a YAML-formatted file. # Declare variables to be passed into your templates. replicaCount: 1 image: - repository: nginx + repository: docker.io/baitinq/fs-tracer pullPolicy: IfNotPresent # Overrides the image tag whose default is the chart appVersion. tag: "" @@ -25,37 +25,6 @@ serviceAccount: podAnnotations: {} -podSecurityContext: {} - # fsGroup: 2000 - -securityContext: {} - # capabilities: - # drop: - # - ALL - # readOnlyRootFilesystem: true - # runAsNonRoot: true - # runAsUser: 1000 - -service: - type: ClusterIP - port: 80 - -ingress: - enabled: false - className: "" - annotations: {} - # kubernetes.io/ingress.class: nginx - # kubernetes.io/tls-acme: "true" - hosts: - - host: chart-example.local - paths: - - path: / - pathType: ImplementationSpecific - tls: [] - # - secretName: chart-example-tls - # hosts: - # - chart-example.local - resources: {} # We usually recommend not to specify default resources and to leave this as a conscious # choice for the user. This also increases chances charts run on environments with little diff --git a/k8s/payloads-processor/templates/NOTES.txt b/k8s/payloads-processor/templates/NOTES.txt deleted file mode 100644 index 75b1008..0000000 --- a/k8s/payloads-processor/templates/NOTES.txt +++ /dev/null @@ -1,22 +0,0 @@ -1. Get the application URL by running these commands: -{{- if .Values.ingress.enabled }} -{{- range $host := .Values.ingress.hosts }} - {{- range .paths }} - http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} - {{- end }} -{{- end }} -{{- else if contains "NodePort" .Values.service.type }} - export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "payloads-processor.fullname" . }}) - export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") - echo http://$NODE_IP:$NODE_PORT -{{- else if contains "LoadBalancer" .Values.service.type }} - NOTE: It may take a few minutes for the LoadBalancer IP to be available. - You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "payloads-processor.fullname" . }}' - export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "payloads-processor.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") - echo http://$SERVICE_IP:{{ .Values.service.port }} -{{- else if contains "ClusterIP" .Values.service.type }} - export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "payloads-processor.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") - export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") - echo "Visit http://127.0.0.1:8080 to use your application" - kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT -{{- end }} diff --git a/k8s/payloads-processor/templates/hpa.yaml b/k8s/payloads-processor/templates/hpa.yaml deleted file mode 100644 index 65025c9..0000000 --- a/k8s/payloads-processor/templates/hpa.yaml +++ /dev/null @@ -1,28 +0,0 @@ -{{- if .Values.autoscaling.enabled }} -apiVersion: autoscaling/v2beta1 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "payloads-processor.fullname" . }} - labels: - {{- include "payloads-processor.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "payloads-processor.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} - metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} - {{- end }} -{{- end }} diff --git a/k8s/payloads-processor/templates/ingress.yaml b/k8s/payloads-processor/templates/ingress.yaml deleted file mode 100644 index 44dff68..0000000 --- a/k8s/payloads-processor/templates/ingress.yaml +++ /dev/null @@ -1,61 +0,0 @@ -{{- if .Values.ingress.enabled -}} -{{- $fullName := include "payloads-processor.fullname" . -}} -{{- $svcPort := .Values.service.port -}} -{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} - {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} - {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} - {{- end }} -{{- end }} -{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1 -{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} -apiVersion: networking.k8s.io/v1beta1 -{{- else -}} -apiVersion: extensions/v1beta1 -{{- end }} -kind: Ingress -metadata: - name: {{ $fullName }} - labels: - {{- include "payloads-processor.labels" . | nindent 4 }} - {{- with .Values.ingress.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -spec: - {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} - ingressClassName: {{ .Values.ingress.className }} - {{- end }} - {{- if .Values.ingress.tls }} - tls: - {{- range .Values.ingress.tls }} - - hosts: - {{- range .hosts }} - - {{ . | quote }} - {{- end }} - secretName: {{ .secretName }} - {{- end }} - {{- end }} - rules: - {{- range .Values.ingress.hosts }} - - host: {{ .host | quote }} - http: - paths: - {{- range .paths }} - - path: {{ .path }} - {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} - pathType: {{ .pathType }} - {{- end }} - backend: - {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} - service: - name: {{ $fullName }} - port: - number: {{ $svcPort }} - {{- else }} - serviceName: {{ $fullName }} - servicePort: {{ $svcPort }} - {{- end }} - {{- end }} - {{- end }} -{{- end }} diff --git a/k8s/payloads-processor/templates/service.yaml b/k8s/payloads-processor/templates/service.yaml deleted file mode 100644 index ac0708e..0000000 --- a/k8s/payloads-processor/templates/service.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ include "payloads-processor.fullname" . }} - labels: - {{- include "payloads-processor.labels" . | nindent 4 }} -spec: - type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.port }} - targetPort: http - protocol: TCP - name: http - selector: - {{- include "payloads-processor.selectorLabels" . | nindent 4 }} diff --git a/k8s/payloads-processor/templates/tests/test-connection.yaml b/k8s/payloads-processor/templates/tests/test-connection.yaml deleted file mode 100644 index 279e613..0000000 --- a/k8s/payloads-processor/templates/tests/test-connection.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: "{{ include "payloads-processor.fullname" . }}-test-connection" - labels: - {{- include "payloads-processor.labels" . | nindent 4 }} - annotations: - "helm.sh/hook": test -spec: - containers: - - name: wget - image: busybox - command: ['wget'] - args: ['{{ include "payloads-processor.fullname" . }}:{{ .Values.service.port }}'] - restartPolicy: Never diff --git a/src/payload-processor/cmd/BUILD.bazel b/src/payload-processor/cmd/BUILD.bazel new file mode 100644 index 0000000..515c17e --- /dev/null +++ b/src/payload-processor/cmd/BUILD.bazel @@ -0,0 +1,53 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library") +load("@rules_oci//oci:defs.bzl", "oci_image", "oci_push", "oci_tarball") +load("@rules_pkg//:pkg.bzl", "pkg_tar") + +go_library( + name = "cmd_lib", + srcs = ["main.go"], + importpath = "github.com/Baitinq/fs-tracer-backend/src/payload-processor/cmd", + visibility = ["//visibility:private"], + deps = [ + "//src/payload-processor/processor", + "@com_github_rabbitmq_amqp091_go//:amqp091-go", + ], +) + +go_cross_binary( + name = "cmd_arm64", + platform = "@io_bazel_rules_go//go/toolchain:linux_arm64", + target = ":cmd", + visibility = ["//visibility:public"], +) + +pkg_tar( + name = "tar", + srcs = [":cmd_arm64"], +) + +oci_image( + name = "image", + base = "@distroless_base", + entrypoint = ["/cmd_arm64"], + # architecture = "arm64", + # os = "linux", + tars = [":tar"], +) + +oci_tarball( + name = "tarball", + image = ":image", + repo_tags = [], +) + +oci_push( + name = "push", + image = ":image", + repository = "docker.io/baitinq/fs-tracer", +) + +go_binary( + name = "cmd", + embed = [":cmd_lib"], + visibility = ["//visibility:public"], +) diff --git a/src/payload-processor/cmd/main.go b/src/payload-processor/cmd/main.go new file mode 100644 index 0000000..1c9bfcb --- /dev/null +++ b/src/payload-processor/cmd/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/Baitinq/fs-tracer-backend/src/payload-processor/processor" + amqp "github.com/rabbitmq/amqp091-go" +) + +func main() { + rabbitmq_password, ok := os.LookupEnv("RABBITMQ_PASSWORD") + if !ok { + log.Fatal("RABBITMQ_PASSWORD not set") + } + log.Println("RabbitMQ password", rabbitmq_password) + conn, err := amqp.Dial(fmt.Sprintf("amqp://user:%s@rabbitmq:5672/", rabbitmq_password)) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + ch, err := conn.Channel() + if err != nil { + log.Fatal(err) + } + defer ch.Close() + + q, err := ch.QueueDeclare( + "hello", // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments + ) + if err != nil { + log.Fatal(err) + } + processor := processor.NewProcessor(ch, q.Name) + processor.ProcessMessages() +} diff --git a/src/payload-processor/processor/BUILD.bazel b/src/payload-processor/processor/BUILD.bazel new file mode 100644 index 0000000..64e3ac2 --- /dev/null +++ b/src/payload-processor/processor/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "processor", + srcs = ["processor.go"], + importpath = "github.com/Baitinq/fs-tracer-backend/src/payload-processor/processor", + visibility = ["//visibility:public"], + deps = ["@com_github_rabbitmq_amqp091_go//:amqp091-go"], +) diff --git a/src/payload-processor/processor/processor.go b/src/payload-processor/processor/processor.go new file mode 100644 index 0000000..9cd83f9 --- /dev/null +++ b/src/payload-processor/processor/processor.go @@ -0,0 +1,39 @@ +package processor + +import ( + "log" + + amqp "github.com/rabbitmq/amqp091-go" +) + +type Processor struct { + ch *amqp.Channel + queueName string +} + +func NewProcessor(ch *amqp.Channel, queueName string) *Processor { + log.Println("Created processor") + return &Processor{ + ch: ch, + queueName: queueName, + } +} + +func (p *Processor) ProcessMessages() { + msgs, err := p.ch.Consume( + p.queueName, // queue + "", // consumer + true, // auto-ack + false, // exclusive + false, // no-local + false, // no-wait + nil, // args + ) + if err != nil { + log.Fatal("Failed to register a consumer:", err) + } + + for msg := range msgs { + log.Printf("Received a message: %s", msg.Body) + } +} diff --git a/src/rest-api/cmd/main.go b/src/rest-api/cmd/main.go index 6740bea..f6d34e0 100644 --- a/src/rest-api/cmd/main.go +++ b/src/rest-api/cmd/main.go @@ -13,18 +13,18 @@ import ( func main() { rabbitmq_password, ok := os.LookupEnv("RABBITMQ_PASSWORD") if !ok { - panic("RABBITMQ_PASSWORD not set") + log.Fatal("RABBITMQ_PASSWORD not set") } log.Println("RabbitMQ password", rabbitmq_password) conn, err := amqp.Dial(fmt.Sprintf("amqp://user:%s@rabbitmq:5672/", rabbitmq_password)) if err != nil { - panic(err) + log.Fatal(err) } defer conn.Close() ch, err := conn.Channel() if err != nil { - panic(err) + log.Fatal(err) } defer ch.Close() @@ -37,7 +37,7 @@ func main() { nil, // arguments ) if err != nil { - panic(err) + log.Fatal(err) } handler := handler.NewHandler(ch, q.Name) diff --git a/src/rest-api/handler/handler.go b/src/rest-api/handler/handler.go index b5c3ae2..c904ff4 100644 --- a/src/rest-api/handler/handler.go +++ b/src/rest-api/handler/handler.go @@ -26,7 +26,7 @@ func NewHandler(ch *amqp.Channel, queueName string) *Handler { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { bytes, err := io.ReadAll(io.Reader(r.Body)) if err != nil { - panic(err) + log.Fatal(err) } body := fmt.Sprint("Hello World!", r.RemoteAddr, string(bytes)) |