Repository navigation
Expand file tree
/
Copy pathgenerate-manifests.sh
More file actions
executable file
·121 lines (102 loc) · 4.29 KB
/
Copy pathgenerate-manifests.sh
File metadata and controls
executable file
·121 lines (102 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Credit: https://github.com/DataDog/datadog-agent/blob/7.32.4/Dockerfiles/manifests/generate.sh
set -euo pipefail
vdev_cmd="${VDEV:-cargo vdev}"
if sed --version 2>/dev/null | grep -q "GNU sed"; then
SED="sed"
elif gsed --version 2>/dev/null | grep -q "GNU sed"; then
SED="gsed"
fi
cd "$(dirname "${BASH_SOURCE[0]}")/.."
chart_version=""
while [[ $# -gt 0 ]]; do
case "$1" in
--chart-version)
chart_version="${2:?}"
shift 2
;;
*)
echo "unknown argument: $1" >&2
exit 1
;;
esac
done
helm repo add vector https://helm.vector.dev
helm repo update
# Pin every template/show invocation to one chart version so the generated
# manifests and READMEs cannot diverge from it if the repo publishes a newer
# chart between runs of this script. Default to the latest published chart.
if [[ -z "$chart_version" ]]; then
chart_version=$(helm search repo vector/vector -o json | yq -p json '.[0].version')
fi
readonly chart_version
echo "Generating manifests from chart version ${chart_version}"
# Refuse to regenerate from a chart older than the one the checked-in
# manifests were generated from; that would silently downgrade them. The README
# records the version it was generated with. Equal is allowed so retries and
# idempotent re-runs from the same chart pass.
current_version=$(sed -nE 's/^version ([0-9][^ ]*) with the following.*/\1/p' \
distribution/kubernetes/vector-agent/README.md)
if [[ -n "$current_version" ]]; then
$vdev_cmd version check-newer --allow-equal --what chart \
--new "$chart_version" --current "$current_version"
fi
TMPDIR=$(mktemp -d)
trap 'rm -r $TMPDIR' EXIT
cat > "$TMPDIR/values-vector-agent.yaml" <<EOF
role: Agent
service:
enabled: false
EOF
cat > "$TMPDIR/values-vector-aggregator.yaml" <<EOF
EOF
cat > "$TMPDIR/values-vector-stateless-aggregator.yaml" <<EOF
role: Stateless-Aggregator
EOF
CLEANUP_INSTRUCTIONS='del(.metadata.labels."helm.sh/chart") | del(.metadata.labels."app.kubernetes.io/managed-by") | del(.metadata.annotations.checksum/*) | del(.spec.template.metadata.annotations.checksum/*)'
for values in "$TMPDIR"/values-*.yaml; do
type=${values##*values-}
type=${type%.yaml}
dest="distribution/kubernetes/$type"
mkdir -p "$dest"
# Remove manifests the chart no longer emits; kustomization.yaml and
# README.md are regenerated below.
find "$dest" -maxdepth 1 -type f ! -name kustomization.yaml ! -name README.md -delete
helm template --namespace default vector vector/vector --version "$chart_version" --values "$values" --output-dir "$TMPDIR/generated_$type"
for file in "$TMPDIR/generated_$type"/vector/templates/*.yaml; do
# Skip files containing only comments
if [[ "$(yq eval '. | length' "$file")" == 0 ]]; then
rm "$file"
continue
fi
# We want this to output without expansion
# shellcheck disable=SC2016
${SED:-sed} -E -i 's/^# Source: (.*)/# This file has been generated by `helm template vector vector\/vector` from \1. Please re-run `make generate-kubernetes-manifests` rather than modifying this file manually./' "$file"
yq eval "$CLEANUP_INSTRUCTIONS" "$file" > "$dest/$(basename "$file")"
done
# Regenerate kustomization.yaml's resource list from the emitted files
# so templates the chart no longer renders don't leave stale references
# (which break `kubectl apply -k`) and newly added templates aren't
# silently omitted.
{
echo "apiVersion: kustomize.config.k8s.io/v1beta1"
echo "kind: Kustomization"
echo
echo "namespace: vector"
echo
echo "resources:"
find "$dest" -maxdepth 1 -type f ! -name README.md ! -name kustomization.yaml -exec basename {} \; |
sort | while read -r res; do
echo " - $res"
done
} > "$dest/kustomization.yaml"
cat > "$dest/README.md" <<EOF
The kubernetes manifests found in this directory have been automatically generated
from the [helm chart \`vector/vector\`](https://github.com/vectordotdev/helm-charts/tree/master/charts/vector)
version $(helm show chart vector/vector --version "$chart_version" | yq e '.version' -) with the following \`values.yaml\`:
\`\`\`yaml
$(<"$values")
\`\`\`
EOF
rm -rf "${type:?}"
done