Skip to content

Commit

Permalink
first Rust Axum quickstarter commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardcl committed Dec 19, 2023
1 parent 0597a29 commit e7f530d
Show file tree
Hide file tree
Showing 45 changed files with 1,162 additions and 0 deletions.
38 changes: 38 additions & 0 deletions be-rust-axum/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def odsNamespace = ''
def odsGitRef = ''
def odsImageTag = ''
def sharedLibraryRef = ''
def agentImageTag = ''

node {
odsNamespace = env.ODS_NAMESPACE ?: 'ods'
odsGitRef = env.ODS_GIT_REF ?: 'master'
odsImageTag = env.ODS_IMAGE_TAG ?: 'latest'
sharedLibraryRef = env.SHARED_LIBRARY_REF ?: odsImageTag
agentImageTag = env.AGENT_IMAGE_TAG ?: odsImageTag
}

library("ods-jenkins-shared-library@${sharedLibraryRef}")

odsQuickstarterPipeline(
imageStreamTag: "${odsNamespace}/jenkins-agent-rust:${agentImageTag}",
) { context ->

// https://cargo-generate.github.io/cargo-generate/index.html
stage('Cargo Generate project') {
sh(
script: "cargo generate --path ${context.sourceDir}/rust-template --name ${context.projectId}-${context.componentId}",
label: "Process Rust template"
)
sh(
script: "mv ${context.sourceDir}/${context.projectId}-${context.componentId} ${context.sourceDir}/files",
label: "Create files folder"
)
}

odsQuickstarterStageCopyFiles(context)

odsQuickstarterStageRenderJenkinsfile(context)

odsQuickstarterStageRenderSonarProperties(context)
}
72 changes: 72 additions & 0 deletions be-rust-axum/Jenkinsfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// See https://www.opendevstack.org/ods-documentation/ for usage and customization.

@Library('ods-jenkins-shared-library@@shared_library_ref@') _

odsComponentPipeline(
imageStreamTag: '@ods_namespace@/jenkins-agent-rust:@agent_image_tag@',
branchToEnvironmentMapping: [
'master': 'dev',
// 'release/': 'test'
]
) { context ->
odsComponentFindOpenShiftImageOrElse(context) {
stageTest(context)
odsComponentStageScanWithSonar(context)
stageBuild(context)
odsComponentStageBuildOpenShiftImage(context)
}
odsComponentStageRolloutOpenShiftDeployment(
context, [
'selector': "app.kubernetes.io/instance=${context.componentId}",
'helmEnvBasedValuesFiles': ["values.env.yaml"],
]
)
}

def stageBuild(def context) {
stage('Cargo Build') {
sh "cargo build --release"
sh "cp -r target/release/${context.projectId}-${context.componentId} docker/app"
}
}

def stageTest(def context) {
stage('Cargo Check') {
sh """
cargo check --all-targets
"""
}
stage('Cargo Format') {
sh """
cargo fmt --all -- --check
"""
}
stage('Cargo Clippy') {
sh """
cargo clippy --all-features

# Clippy reports for SonarQube
mkdir -p build/test-results/clippy
cargo clippy --message-format=json &> build/test-results/clippy/report.json
"""
}
stage('Cargo Test') {
sh """
# create report folders
mkdir -p build/test-results/test
mkdir -p build/test-results/coverage

# Tests with JUnit XML report as defined in .config/nextest.toml
cargo nextest run --profile ci

# Coverage with LLVM lcov report from tests
# generate the lcov.info report
cargo llvm-cov --lcov --output-path ./build/test-results/coverage/lcov.info
# generate the html report
cargo llvm-cov report --html

cp -r target/nextest/ci/results.xml build/test-results/test
cp -r target/llvm-cov/html/ build/test-results/coverage
"""
}
}
7 changes: 7 additions & 0 deletions be-rust-axum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Rust Axum Quickstarter (be-rust-axum)

Documentation is located in our [official documentation](https://www.opendevstack.org/ods-documentation/ods-quickstarters/latest/index.html)

Please update documentation in the [antora page directory](https://github.com/opendevstack/ods-quickstarters/tree/master/docs/modules/ROOT/pages)

Tested thru [automated tests](../tests/be-rust-axum)
4 changes: 4 additions & 0 deletions be-rust-axum/ocp.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MEMORY_LIMIT=128Mi
MEMORY_REQUEST=64Mi
CPU_LIMIT=100m
CPU_REQUEST=50m
4 changes: 4 additions & 0 deletions be-rust-axum/rust-template/.config/nextest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[profile.ci.junit]
path = "results.xml"
store-success-output = true
store-failure-output = true
14 changes: 14 additions & 0 deletions be-rust-axum/rust-template/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
5 changes: 5 additions & 0 deletions be-rust-axum/rust-template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target
/build
.vscode
.DS_Store
.env
5 changes: 5 additions & 0 deletions be-rust-axum/rust-template/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.16.1
hooks:
- id: gitleaks
42 changes: 42 additions & 0 deletions be-rust-axum/rust-template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "{{project-name}}"
version = "0.1.0"
edition = "2021"
description = "{{project-name}} - from the OpenDevStack Rust QuickStarter."
license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# one can add more dependencies via cargo to Cargo.toml as shown next: cargo add axum -F axum/http2
axum = { version = "0.7", features = ["http2"] }
tokio = { version = "1.35", features = ["rt-multi-thread", "macros"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
http = "1.0"
http-body-util = "0.1"
bytes = "1.5"
thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["tracing", "env-filter"] }
lazy_static = "1.4"

# Nice HTTP middlewares from Tower crate, check router.rs commented code.
# Uncomment as per need, check official docs.
# tower-http = { version = "0.5", features = [
# "trace",
# "compression-br",
# "propagate-header",
# "sensitive-headers",
# "cors",
# ] }

# SQLx is the recommended safe and performant package to work with relational DBs like PostgreSQL; check official docs
# sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "postgres", "uuid", "json", "time", "macros", "migrate" ] }

# One can use envy crate for more automated env vars management, see docs.
# envy = "*"
# One can use dotenvy crate for automating ingestion of env vars from .env file.
# dotenvy = "*"

[dev-dependencies]
tower = { version = "0.4", features = ["util"] }
23 changes: 23 additions & 0 deletions be-rust-axum/rust-template/chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions be-rust-axum/rust-template/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: {{project-name}}
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.0"
1 change: 1 addition & 0 deletions be-rust-axum/rust-template/chart/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Component {{ .Values.componentId }} on version {{ .Values.imageTag }} released with Helm!
62 changes: 62 additions & 0 deletions be-rust-axum/rust-template/chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
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 "chart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "chart.labels" -}}
helm.sh/chart: {{ include "chart.chart" . }}
{{ include "chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
78 changes: 78 additions & 0 deletions be-rust-axum/rust-template/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.componentId }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0%
maxSurge: 50%
selector:
matchLabels:
{{- include "chart.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "chart.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "chart.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Values.componentId }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.registry }}/{{ .Values.imageNamespace }}/{{ .Values.componentId }}:{{ .Values.imageTag }}"
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
livenessProbe:
failureThreshold: 3
httpGet:
path: "/status"
port: {{ .Values.service.port }}
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 3
readinessProbe:
failureThreshold: 1
httpGet:
path: "/status"
port: {{ .Values.service.port }}
scheme: HTTP
initialDelaySeconds: 3
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 3
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Loading

0 comments on commit e7f530d

Please sign in to comment.