Skip to content

Agent Mesh

charts: seed the mesh policy only when the control plane has none

Tomas Tormo4d5cf4ee7f2b
diff --git a/charts/agentmesh/templates/bootstrap-job.yaml b/charts/agentmesh/templates/bootstrap-job.yaml
index fd422455..4dd49596 100644
--- a/charts/agentmesh/templates/bootstrap-job.yaml
+++ b/charts/agentmesh/templates/bootstrap-job.yaml
@@ -100,31 +100,34 @@ spec:
sleep 2
done
- echo "Seeding default policies..."
- {{- $bindings := .Values.bootstrap.bindings -}}
- {{- if not $bindings -}}
- {{- $defaultRouterSA := printf "user:system:serviceaccount:%s:%s-router-sa" .Release.Namespace (include "agentmesh.fullname" .) -}}
- {{- $bindings = list
- (dict "role" "mesh:role:router" "members" (list "group:routers" $defaultRouterSA))
- (dict "role" "mesh:role:agentmesh-box" "members" (list (printf "user:system:serviceaccount:%s:agentmesh-box-sa" .Release.Namespace)))
- -}}
- {{- if .Values.bootstrap.nodeMembers -}}
- {{- $bindings = append $bindings (dict "role" "mesh:role:node" "members" .Values.bootstrap.nodeMembers) -}}
- {{- end -}}
- {{- end }}
- curl -sf -X POST \
- -H "Content-Type: application/json" \
- -H "Authorization: Bearer ${ADMIN_TOKEN}" \
- -d '{
- "roles": [
- {"name": "mesh-admin", "allowed_services": ["*"], "allowed_targets": ["*"]},
- {"name": "mesh:role:agentmesh-box", "allowed_services": ["*"], "allowed_publish": ["*"], "allowed_targets": ["*"]},
- {"name": "mesh:role:router", "allowed_services": ["*"], "allowed_targets": ["*"]},
- {"name": "mesh:role:node", "allowed_services": {{ toJson .Values.bootstrap.nodeServices }}, "allowed_publish": {{ toJson .Values.bootstrap.nodePublish }}, "allowed_targets": ["*"], "allowed_labels": {{ toJson .Values.bootstrap.nodeLabels }}}
- ],
- "bindings": {{ toJson $bindings }}
- }' \
- "${CP_URL}/policies" >/dev/null
+ # Seed only a mesh with no policy: after that the database is the source
+ # of truth, and an upgrade must not overwrite edits made in the console.
+ POLICY=$(curl -sf -H "Authorization: Bearer ${ADMIN_TOKEN}" "${CP_URL}/admin/policy")
+ if echo "${POLICY}" | grep -q '"roles"[[:space:]]*:'; then
+ echo "Mesh policy already present; not seeding it (edit it in the console or with POST /policies)."
+ else
+ echo "Seeding default policies..."
+ {{- $bindings := .Values.bootstrap.bindings -}}
+ {{- if not $bindings -}}
+ {{- $defaultRouterSA := printf "user:system:serviceaccount:%s:%s-router-sa" .Release.Namespace (include "agentmesh.fullname" .) -}}
+ {{- $bindings = list (dict "role" "mesh:role:router" "members" (list "group:routers" $defaultRouterSA)) -}}
+ {{- if .Values.bootstrap.nodeMembers -}}
+ {{- $bindings = append $bindings (dict "role" "mesh:role:node" "members" .Values.bootstrap.nodeMembers) -}}
+ {{- end -}}
+ {{- end }}
+ curl -sf -X POST \
+ -H "Content-Type: application/json" \
+ -H "Authorization: Bearer ${ADMIN_TOKEN}" \
+ -d '{
+ "roles": [
+ {"name": "mesh-admin", "allowed_services": ["*"], "allowed_targets": ["*"]},
+ {"name": "mesh:role:router", "allowed_services": ["*"], "allowed_targets": ["*"]},
+ {"name": "mesh:role:node", "allowed_services": {{ toJson .Values.bootstrap.nodeServices }}, "allowed_publish": {{ toJson .Values.bootstrap.nodePublish }}, "allowed_targets": ["*"], "allowed_labels": {{ toJson .Values.bootstrap.nodeLabels }}}
+ ],
+ "bindings": {{ toJson $bindings }}
+ }' \
+ "${CP_URL}/policies" >/dev/null
+ fi
echo "Generating bootstrap token for agentmesh-router..."
diff --git a/charts/agentmesh/tests/bootstrap-job_test.yaml b/charts/agentmesh/tests/bootstrap-job_test.yaml
index 251d0e51..06474186 100644
--- a/charts/agentmesh/tests/bootstrap-job_test.yaml
+++ b/charts/agentmesh/tests/bootstrap-job_test.yaml
@@ -110,3 +110,21 @@ tests:
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: '"name": "mesh:role:node".*"allowed_labels": \["region=\*","team=platform"\]'
+
+ - it: seeds the policy only when the control plane has none
+ documentSelector:
+ path: kind
+ value: Job
+ asserts:
+ - matchRegex:
+ path: spec.template.spec.containers[0].args[0]
+ pattern: '(?s)"\$\{CP_URL\}/admin/policy"\)\s+if echo "\$\{POLICY\}" \| grep -q .*\belse\b.*"\$\{CP_URL\}/policies" >/dev/null\s+fi'
+
+ - it: seeds no role for the removed agentmesh-box
+ documentSelector:
+ path: kind
+ value: Job
+ asserts:
+ - notMatchRegex:
+ path: spec.template.spec.containers[0].args[0]
+ pattern: 'agentmesh-box'
diff --git a/charts/agentmesh/values.yaml b/charts/agentmesh/values.yaml
index 20010595..3bd102e3 100644
--- a/charts/agentmesh/values.yaml
+++ b/charts/agentmesh/values.yaml
@@ -200,6 +200,9 @@ console:
port: 8081
nodePort: null
+# The bootstrap Job seeds this policy only when the control plane has none.
+# After that, edit the policy in the console or with POST /policies: changing
+# these values on a later helm upgrade does not change it.
bootstrap:
bindings: []
# Identities that may enroll as mesh:role:node ("user:<sub>", "group:<name>",
diff --git a/site/content/docs/guides/kubernetes.md b/site/content/docs/guides/kubernetes.md
index 2454048c..374934d1 100644
--- a/site/content/docs/guides/kubernetes.md
+++ b/site/content/docs/guides/kubernetes.md
@@ -67,8 +67,9 @@ The four `bootstrap.*` values write the initial policy:
If you want to bind more roles than the node role, `bootstrap.bindings`
replaces the whole bindings list. Everything the bootstrap job seeds can be
changed later through the console or `POST /policies`. The job runs on
-install and on upgrade, but once the database has a policy, the database is
-the source of truth.
+install and on upgrade, but it seeds the policy only when the database has
+none. After that, the database is the source of truth, and changing the
+`bootstrap.*` values on a `helm upgrade` does not change the policy.
The chart generates an admin token and a database password and stores them
in the `agentmesh-secrets` Secret. The release notes print the command to