Skip to content

Commit

Permalink
feat(common): initial implementation (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwrau authored Apr 12, 2024
1 parent cc2e6a9 commit d7fe1c0
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .github/scripts/create-values-diff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ else
dryRun=false
fi

if yq -e '.type == "library"' "$chart/Chart.yaml" >/dev/null; then
echo "Skipping library chart '$chart'" >/dev/stderr
exit 0
fi

GITHUB_API_URL="${GITHUB_API_URL:-https://api.github.com}"

if command -v gh &>/dev/null; then
Expand All @@ -46,7 +51,7 @@ TMP_DIR="${TMP_DIR:-$(mktemp -d)}"
cd "$GITHUB_WORKSPACE"

function generateComment() {
local chart="charts/${1?}"
local chart="${1?}"
local -A diffs
local newResourcesDir
local originalResourcesDir
Expand Down
4 changes: 4 additions & 0 deletions .github/scripts/enforce-trusted-registries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function getUntrustedImages() {
function enforceTrustedImages() {
local chart="${1?}"
local untrustedImages=()
if yq -e '.type == "library"' "$chart/Chart.yaml" >/dev/null; then
echo "Skipping library chart '$chart'" >/dev/stderr
return 0
fi

mapfile -t untrustedImages < <(getUntrustedImages "$chart")
if [[ "${#untrustedImages[@]}" -gt 0 ]]; then
Expand Down
8 changes: 8 additions & 0 deletions .github/scripts/extract-artifacthub-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ function updateChartYaml() {
}

if [[ "$#" == 1 ]] && [[ -d "$1" ]]; then
if yq -e '.type == "library"' "$1/Chart.yaml" >/dev/null; then
echo "Skipping library chart '$1'" >/dev/stderr
exit 0
fi
if ! [[ -f "$1/ci/artifacthub-values.yaml" ]]; then
echo "There is no 'artifacthub-values.yaml' in 'charts/$1/ci', exiting" >/dev/stderr
exit 1
Expand All @@ -92,6 +96,10 @@ else
[[ "$chart" == "charts/*" ]] && continue
[[ -f "$chart/ci/artifacthub-values.yaml" ]] || continue

if yq -e '.type == "library"' "$chart/Chart.yaml" >/dev/null; then
echo "Skipping library chart '$chart'" >/dev/stderr
exit 0
fi
updateChartYaml "$chart"
done
fi
4 changes: 2 additions & 2 deletions .github/workflows/get-changed-chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
outputs:
chart: ${{ steps.getChangedChart.outputs.chart }}
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -26,7 +26,7 @@ jobs:
run: |
set -x
set -o pipefail
changed="$(ct list-changed --since "HEAD~" | cut -d / -f 2)"
changed="$(ct list-changed | cut -d / -f 2)"
if [[ -z "$changed" ]]; then
echo chart= | tee "$GITHUB_OUTPUT"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/get-changed-charts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
outputs:
charts: ${{ steps.getCharts.outputs.charts }}
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -28,5 +28,5 @@ jobs:
set -o pipefail
(
echo -n charts=
ct list-changed --since "HEAD~" | cut -d / -f 2 | jq -c -Rn '[inputs]'
ct list-changed | cut -d / -f 2 | jq -c -Rn '[inputs]'
) | tee "$GITHUB_OUTPUT"
10 changes: 8 additions & 2 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
matrix:
chart: ${{ fromJson(needs.getChangedCharts.outputs.charts) }}
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
CHART: ${{ matrix.chart }}
steps:
- uses: actions/checkout@v4
Expand All @@ -46,7 +46,13 @@ jobs:
- uses: helm/[email protected]

- name: Lint chart
run: ct lint --check-version-increment=false
run: |
if ! [[ -f "charts/$CHART/values.yaml" ]]; then
echo "No values.yaml found for $CHART, skipping 'ct lint'" >/dev/stderr
helm lint "charts/$CHART"
else
ct lint --check-version-increment=false
fi
env:
CT_CHART_REPOS: ${{ steps.helm-repos.outputs.repos }}
CT_CHARTS: charts/${{ matrix.chart }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr-comment-diff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:
runs-on: ubuntu-22.04
needs: getChangedChart
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
steps:
- uses: actions/checkout@v4
- run: pip install yq
- name: Install sponge
run: sudo apt-get -yq install moreutils
- run: ./.github/scripts/prepare-values.sh "charts/${{ needs.getChangedChart.outputs.chart }}"
- run: ./.github/scripts/create-values-diff.sh ${{ github.event.number }} ${{ needs.getChangedChart.outputs.chart }}
- run: ./.github/scripts/create-values-diff.sh ${{ github.event.number }} "charts/${{ needs.getChangedChart.outputs.chart }}"
6 changes: 5 additions & 1 deletion .github/workflows/release-update-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
needs: getChangedChart
name: update metadata file for release
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
CHART: ${{ needs.getChangedChart.outputs.chart }}
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -45,6 +45,10 @@ jobs:
- name: generate values.md
run: |
set -ex
if ! [[ -f "charts/$CHART/values.schema.json" ]]; then
echo "No values.schema.json found for $CHART" >/dev/stderr
exit 0
fi
generate-schema-doc --config-file .github/json-schema-to-md.yaml "charts/$CHART/values.schema.json" "charts/$CHART/values.md"
- name: generate Docs
uses: docker://jnorwood/helm-docs:latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate-pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
needs: getChangedChart
env:
CT_TARGET_BRANCH: ${{ github.event.repository.default_branch }}
CT_TARGET_BRANCH: ${{ github.event.pull_request.base.ref || github.event.repository.default_branch }}
steps:
- uses: actions/checkout@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions charts/common/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: common
repository: https://charts.bitnami.com/bitnami
version: 2.19.0
digest: sha256:a18a4c3aa2d73ddc357aba5bb326df478db26141604438075d3bfc9e7ee0eaca
generated: "2024-03-26T10:22:51.926182467+01:00"
16 changes: 16 additions & 0 deletions charts/common/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v2
name: common
description: A library chart for common resources
type: library
version: 1.0.0
maintainers:
- name: cwrau
email: [email protected]
- name: marvinWolff
email: [email protected]
- name: tasches
email: [email protected]
dependencies:
- name: common
version: 2.19.0
repository: https://charts.bitnami.com/bitnami
11 changes: 11 additions & 0 deletions charts/common/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[modeline]: # ( vim: set ft=markdown: )
{{ template "chart.header" . }}
{{ template "chart.deprecationWarning" . }}

{{ template "chart.badgesSection" . }}

{{ template "chart.description" . }}

{{ template "chart.homepageLine" . }}

{{ template "chart.maintainersSection" . }}
35 changes: 35 additions & 0 deletions charts/common/templates/_helm.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{/*
Creates a labels block for a HelmRelease resource.
{{ include "common.helm.labels" (dict) -}}
*/}}
{{- define "common.helm.labels" -}}
{{- include "common.labels.standard" (dict "Release" (dict "Name" "{{ .Release.Name }}" "Service" "{{ .Release.Service }}") "Chart" (dict "Name" "{{ .Chart.Name }}" "Version" "{{ .Chart.Version }}") "Values" (dict)) -}}
{{- end -}}

{{/*
Returns the chart version for a given chart in a given repository.
{{ include "common.helm.chartVersion" (dict "context" $ "repo" "bitnami" "chart" "redis") -}}
*/}}
{{- define "common.helm.chartVersion" -}}
{{- $_ := set . "Values" .context.Values -}}
{{- dig .repo "charts" .chart nil .Values.global.helmRepositories | required (printf "The repo '%s' is either missing or doesn't contain the chart '%s'" .repo .chart) -}}
{{- end -}}

{{/*
Returns a HelmRelease.spec.chart.spec for a given chart in a given repository.
{{ include "common.helm.chartSpec" (dict "context" $ "repo" "bitnami" "chart" "redis" "prependReleaseName" true) -}}
*/}}
{{- define "common.helm.chartSpec" -}}
{{- $_ := set . "Release" .context.Release -}}
{{- dict
"chart" .chart
"version" (include "common.helm.chartVersion" (dict "repo" .repo "chart" .chart "context" .context))
"sourceRef" (dict
"kind" "HelmRepository"
"name" (eq .prependReleaseName true | ternary (printf "%s-%s" .Release.Name .repo) .repo)
"namespace" .Release.Namespace
)
| toYaml
-}}
{{- end -}}

22 changes: 22 additions & 0 deletions charts/common/templates/_resources.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{/*
Return a resource request/limit object based on a given preset or provided resources.
{{ include "common.resources" (dict "resources" (dict) "resourcesPreset" "small") -}}
*/}}
{{- define "common.resources" -}}
{{- $resources := dict -}}
{{- if .resources -}}
{{- $resources = .resources | merge $resources -}}
{{- end -}}
{{- if and .resourcesPreset (ne .resourcesPreset "none") -}}
{{- $resources = include "common.resources.preset" (dict "type" .resourcesPreset) | fromYaml | merge $resources -}}
{{- end -}}
{{- toYaml $resources -}}
{{- end -}}

{{/*
Return a (dict resources resourcesPreset) for passing through to capable charts, e.g. bitnami
{{ include "common.resourcesWithPreset" (dict "resources" (dict) "resourcesPreset" "small") -}}
*/}}
{{- define "common.resourcesWithPreset" -}}
{{- pick . "resources" "resourcesPreset" | toYaml -}}
{{- end -}}
30 changes: 30 additions & 0 deletions charts/common/values.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"title": "common defs",
"additionalProperties": false,
"$defs": {
"resourcesPreset": {
"type": "string",
"enum": [
"nano",
"micro",
"small",
"medium",
"large",
"xlarge",
"2xlarge"
]
}
},
"properties": {
"global": {
"type": "object",
"description": "global values for all charts"
},
"common": {
"type": "object",
"description": "Values for sub-chart"
}
}
}

0 comments on commit d7fe1c0

Please sign in to comment.