diff --git a/docs/skills/k0s-sysext-ops.md b/docs/skills/k0s-sysext-ops.md index 7b806f80..3706c74d 100644 --- a/docs/skills/k0s-sysext-ops.md +++ b/docs/skills/k0s-sysext-ops.md @@ -51,6 +51,8 @@ k0s kubectl get pods -A - **Extension not merged**: Check `systemd-sysext status`. Verify the persistent image is `/var/lib/k0s/k0s.raw`; the boot activation unit copies it into `/run/extensions/k0s.raw`. - **Manifests not applied**: Check `/var/lib/k0s/manifests/`. Ensure files end in `.yaml` (not `.yml`). - **Service failed**: Check `journalctl -u k0scontroller -e`. +- **`argocd-server` stuck at `0/1` with `connection refused` on `/healthz`**: it is waiting on a dependency, not the probe. Confirm `argocd-redis`, `argocd-repo-server` and `argocd-application-controller` are `Running`, then check `k0s kubectl -n argocd logs deploy/argocd-server` for RBAC `forbidden` errors — the server only binds `:8080` once its configmap/secret and Application informers have synced. +- **An Argo CD component cannot reach redis**: the `argocd-redis` NetworkPolicy allows ingress on `6379` only from pods labelled `app.kubernetes.io/name in (argocd-server, argocd-repo-server, argocd-application-controller)`. redis carries no `requirepass`, so that policy is the only thing keeping the cache private from the KubeStellar and Postgres workloads sharing this node — widen the label set rather than deleting the policy. If `argocd-redis` itself never goes `Ready`, suspect the kubelet `tcpSocket` probe: it dials from the node, not from a pod, and needs the CNI's local-node exemption. Check enforcement with `k0s kubectl -n argocd describe networkpolicy argocd-redis`. ## See also diff --git a/docs/skills/k0s-sysext.md b/docs/skills/k0s-sysext.md index 1e38f94c..fcb9640e 100644 --- a/docs/skills/k0s-sysext.md +++ b/docs/skills/k0s-sysext.md @@ -59,7 +59,7 @@ Design choices: | `files/k0s/sysext/k0scontroller.service` | systemd unit for the k0s single-node controller/worker. Not enabled by default. | | `files/k0s/sysext/extension-release.k0s` | Static sysext identity (`ID=_any`); `VERSION_ID=`/`ARCHITECTURE=` are appended at build time. | | `files/k0s/sysext/k0s-manifests.conf` | tmpfiles rule that copies declarative stacks to `/var/lib/k0s/manifests/`. | -| `files/k0s/manifests/argocd/` | Raw YAML manifests for Argo CD. | +| `files/k0s/manifests/argocd/` | Raw YAML manifests for the Argo CD core stack: CRDs, RBAC, redis, repo-server, application controller, and server. All four workloads are required — the server blocks on its informers and never binds `:8080` without them. | | `files/k0s/manifests/kubestellar/` | Raw YAML manifests for KubeFlex, Postgres, KubeStellar core, and Console. | | `files/os/sysupdate.k0s.d/70-k0s.transfer` | sysupdate transfer track for the k0s sysext component. | | `Justfile` | `build-sysext` / `export-sysext` targets. | diff --git a/files/k0s/manifests/argocd/install.yaml b/files/k0s/manifests/argocd/install.yaml index ceb45aff..80551d95 100644 --- a/files/k0s/manifests/argocd/install.yaml +++ b/files/k0s/manifests/argocd/install.yaml @@ -1,8 +1,133 @@ +# Argo CD core stack for the single-node k0s sysext. +# +# argocd-server cannot serve :8080 on its own: before it binds the listener it +# waits for its AppProject/Application informers to sync and for the settings +# manager to read (and seed) argocd-cm / argocd-secret. Without the CRDs, the +# namespace RBAC and the supporting workloads it stays 0/1 and the readiness +# probe reports "connection refused". This stack therefore ships the four +# components Argo CD needs to reach a healthy state: redis, repo-server, +# application-controller and server, plus their CRDs, RBAC and config. +# +# CRD schemas are intentionally trimmed to `x-kubernetes-preserve-unknown-fields` +# (same approach as the KubeFlex CRDs in ../kubestellar/00-kubeflex-crds.yaml) so +# the sysext payload stays small; the API server still stores and serves the full +# resource. The ApplicationSet CRD is registered for API completeness — the +# applicationset controller is not part of this stack. +# +# Application and AppProject deliberately declare no status subresource, which +# matches upstream (`subresources: {}` on the Application CRD, none at all on +# AppProject; only ApplicationSet has one). The application controller persists +# health, sync and operationState by updating/patching the Application itself, +# not its /status endpoint — enabling the subresource would make the API server +# silently drop `.status` from those writes, and the RBAC below grants no +# applications/status anyway. Adding it here reports as "more correct" and +# quietly breaks every Application the stack is meant to run. apiVersion: v1 kind: Namespace metadata: name: argocd --- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: applications.argoproj.io + labels: + app.kubernetes.io/part-of: argocd +spec: + group: argoproj.io + names: + kind: Application + listKind: ApplicationList + plural: applications + singular: application + shortNames: + - app + - apps + scope: Namespaced + versions: + - name: v1alpha1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + x-kubernetes-preserve-unknown-fields: true + status: + type: object + x-kubernetes-preserve-unknown-fields: true + operation: + type: object + x-kubernetes-preserve-unknown-fields: true +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: appprojects.argoproj.io + labels: + app.kubernetes.io/part-of: argocd +spec: + group: argoproj.io + names: + kind: AppProject + listKind: AppProjectList + plural: appprojects + singular: appproject + shortNames: + - appproj + - appprojs + scope: Namespaced + versions: + - name: v1alpha1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + x-kubernetes-preserve-unknown-fields: true + status: + type: object + x-kubernetes-preserve-unknown-fields: true +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: applicationsets.argoproj.io + labels: + app.kubernetes.io/part-of: argocd +spec: + group: argoproj.io + names: + kind: ApplicationSet + listKind: ApplicationSetList + plural: applicationsets + singular: applicationset + shortNames: + - appset + - appsets + scope: Namespaced + versions: + - name: v1alpha1 + served: true + storage: true + subresources: + status: {} + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + x-kubernetes-preserve-unknown-fields: true + status: + type: object + x-kubernetes-preserve-unknown-fields: true +--- apiVersion: v1 kind: ServiceAccount metadata: @@ -10,6 +135,24 @@ metadata: namespace: argocd --- apiVersion: v1 +kind: ServiceAccount +metadata: + name: argocd-application-controller + namespace: argocd +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: argocd-repo-server + namespace: argocd +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: argocd-redis + namespace: argocd +--- +apiVersion: v1 kind: ConfigMap metadata: name: argocd-cm @@ -20,6 +163,464 @@ metadata: data: application.instanceLabelKey: argocd.argoproj.io/instance --- +apiVersion: v1 +kind: ConfigMap +metadata: + name: argocd-rbac-cm + namespace: argocd + labels: + app.kubernetes.io/name: argocd-rbac-cm + app.kubernetes.io/part-of: argocd +data: + policy.default: role:readonly +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: argocd-ssh-known-hosts-cm + namespace: argocd + labels: + app.kubernetes.io/name: argocd-ssh-known-hosts-cm + app.kubernetes.io/part-of: argocd +data: + ssh_known_hosts: "" +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: argocd-tls-certs-cm + namespace: argocd + labels: + app.kubernetes.io/name: argocd-tls-certs-cm + app.kubernetes.io/part-of: argocd +--- +# Seeded empty on purpose: argocd-server generates `server.secretkey` and the +# initial admin bcrypt hash into this Secret on first start. The `C+` tmpfiles +# rule (files/k0s/sysext/k0s-manifests.conf) re-copies this manifest file into +# /var/lib/k0s/manifests on every boot. k0s's own docs describe the Manifest +# Deployer as "quite similar to the use of the kubectl apply command", which +# would mean a three-way merge against the last-applied configuration -- and +# since that configuration never included `data`, the generated secretkey and +# admin hash would not be in the diff and would survive the reapply. Expected +# behaviour, not observed: confirm on the same first-boot VM pass this stack +# still needs (see the redis NetworkPolicy comment below). If a boot ever +# does wipe `server.secretkey`, that behaviour was wrong and this Secret needs +# a `C` (create-only) tmpfiles rule instead of `C+`. +apiVersion: v1 +kind: Secret +metadata: + name: argocd-secret + namespace: argocd + labels: + app.kubernetes.io/name: argocd-secret + app.kubernetes.io/part-of: argocd +type: Opaque +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: argocd-server + namespace: argocd + labels: + app.kubernetes.io/name: argocd-server + app.kubernetes.io/part-of: argocd +rules: +- apiGroups: [""] + resources: [secrets, configmaps] + verbs: [create, get, list, watch, update, patch, delete] +- apiGroups: [""] + resources: [events] + verbs: [create, list] +- apiGroups: [""] + resources: [pods, pods/log] + verbs: [get, list, watch] +- apiGroups: [argoproj.io] + resources: [applications, applicationsets, appprojects] + verbs: [create, get, list, watch, update, patch, delete] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: argocd-application-controller + namespace: argocd + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +rules: +- apiGroups: [""] + resources: [secrets, configmaps] + verbs: [get, list, watch] +- apiGroups: [""] + resources: [events] + verbs: [create, list] +- apiGroups: [argoproj.io] + resources: [applications, applicationsets, appprojects] + verbs: [create, get, list, watch, update, patch, delete] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: argocd-server + namespace: argocd + labels: + app.kubernetes.io/name: argocd-server + app.kubernetes.io/part-of: argocd +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: argocd-server +subjects: +- kind: ServiceAccount + name: argocd-server + namespace: argocd +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: argocd-application-controller + namespace: argocd + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: argocd-application-controller +subjects: +- kind: ServiceAccount + name: argocd-application-controller + namespace: argocd +--- +# Matches the upstream argocd-server ClusterRole: the UI reads live resources +# and performs resource actions, but it does not need cluster-wide list/watch — +# the application controller's cache serves the resource tree. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: argocd-server + labels: + app.kubernetes.io/name: argocd-server + app.kubernetes.io/part-of: argocd +rules: +- apiGroups: ["*"] + resources: ["*"] + verbs: [get, patch, delete] +- apiGroups: [""] + resources: [events] + verbs: [list] +- apiGroups: [""] + resources: [pods, pods/log] + verbs: [get] +--- +# The application controller reconciles arbitrary user-supplied manifests, so it +# needs full access to every API group — this mirrors the upstream Argo CD +# ClusterRole for argocd-application-controller. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: argocd-application-controller + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +rules: +- apiGroups: ["*"] + resources: ["*"] + verbs: ["*"] +- nonResourceURLs: ["*"] + verbs: ["*"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: argocd-server + labels: + app.kubernetes.io/name: argocd-server + app.kubernetes.io/part-of: argocd +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: argocd-server +subjects: +- kind: ServiceAccount + name: argocd-server + namespace: argocd +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: argocd-application-controller + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: argocd-application-controller +subjects: +- kind: ServiceAccount + name: argocd-application-controller + namespace: argocd +--- +apiVersion: v1 +kind: Service +metadata: + name: argocd-redis + namespace: argocd + labels: + app.kubernetes.io/name: argocd-redis + app.kubernetes.io/part-of: argocd +spec: + type: ClusterIP + ports: + - port: 6379 + targetPort: redis + name: tcp-redis + selector: + app.kubernetes.io/name: argocd-redis +--- +# Cache only — no persistence and no requirepass. Argo CD reads REDIS_PASSWORD +# from the optional `argocd-redis` Secret, which this stack deliberately does +# not create; the NetworkPolicy below is what keeps the cache private instead. +# +# ClusterIP and single-node were the original argument, and they are not +# enough: they address reach from off-host, but the threat is in-cluster. +# Every pod on this node — kubeflex-controller-manager, postgres, +# kubestellar-controller-manager, kubestellar-console, kiosk-proxy — can dial +# a ClusterIP, and the application controller that reads this cache holds +# `*/*/*` cluster-admin (see its ClusterRole above), so a poisoned cache entry +# is reachable from any of them. +apiVersion: apps/v1 +kind: Deployment +metadata: + name: argocd-redis + namespace: argocd + labels: + app.kubernetes.io/name: argocd-redis + app.kubernetes.io/part-of: argocd +spec: + replicas: 1 + strategy: + type: Recreate + selector: + matchLabels: + app.kubernetes.io/name: argocd-redis + template: + metadata: + labels: + app.kubernetes.io/name: argocd-redis + spec: + serviceAccountName: argocd-redis + securityContext: + runAsNonRoot: true + runAsUser: 999 + runAsGroup: 1000 + containers: + - name: redis + image: docker.io/library/redis:7.0.15-alpine + args: + - --save + - "" + - --appendonly + - "no" + ports: + - containerPort: 6379 + name: redis + readinessProbe: + tcpSocket: + port: redis + initialDelaySeconds: 5 + periodSeconds: 10 +--- +# Restricts the unauthenticated cache to the three Argo CD components that use +# it, so a compromised workload elsewhere on this single node cannot poison +# what the cluster-admin application controller reads. +# +# Selecting on pod labels, not ServiceAccounts: NetworkPolicy has no notion of +# a ServiceAccount. These are the labels the four workloads in this file +# already carry, and each is also its Deployment/StatefulSet selector, so they +# cannot drift without breaking the workload first. +# +# This depends on the CNI enforcing NetworkPolicy. k0s runs with no +# `spec.network.provider` set (files/k0s/sysext/k0scontroller.service passes +# only `--enable-worker --single --disable-components=helm,autopilot`), so it +# defaults to kube-router with its NetworkPolicy controller on. If that ever +# changes to a provider without enforcement, this object silently becomes +# decorative -- at which point redis needs a real `requirepass`, seeded the way +# files/k0s/kubeflex/generate-postgres-secret.sh seeds the postgres password. +# +# Confirm on a first-boot VM: redis's readiness probe is a kubelet tcpSocket +# dial originating from the node, not from a pod, so it matches no rule here. +# kube-router is expected to permit local-node traffic for exactly this reason, +# but if `kubectl -n argocd get pod -l app.kubernetes.io/name=argocd-redis` +# stays NotReady after this policy lands, that exemption is the first thing to +# check -- and the fix is an added ipBlock rule for the node CIDR, not dropping +# the policy. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: argocd-redis + namespace: argocd + labels: + app.kubernetes.io/name: argocd-redis + app.kubernetes.io/part-of: argocd +spec: + podSelector: + matchLabels: + app.kubernetes.io/name: argocd-redis + policyTypes: + - Ingress + ingress: + - from: + - podSelector: + matchExpressions: + - key: app.kubernetes.io/name + operator: In + values: + - argocd-application-controller + - argocd-repo-server + - argocd-server + ports: + - protocol: TCP + port: 6379 +--- +apiVersion: v1 +kind: Service +metadata: + name: argocd-repo-server + namespace: argocd + labels: + app.kubernetes.io/name: argocd-repo-server + app.kubernetes.io/part-of: argocd +spec: + type: ClusterIP + ports: + - port: 8081 + targetPort: server + name: server + - port: 8084 + targetPort: metrics + name: metrics + selector: + app.kubernetes.io/name: argocd-repo-server +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: argocd-repo-server + namespace: argocd + labels: + app.kubernetes.io/name: argocd-repo-server + app.kubernetes.io/part-of: argocd +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: argocd-repo-server + template: + metadata: + labels: + app.kubernetes.io/name: argocd-repo-server + spec: + # Deliberately omits upstream's gpg-keys/gpg-keyring emptyDir mounts at + # /app/config/gpg/{keys,source}: this stack ships no GPG commit-verification + # config, and the quay.io/argoproj/argocd:v2.12.3 image already chowns + # /app/config/gpg/keys, so the repo-server starts clean without them. Add + # both mounts back if GPG signature verification is ever enabled here. + serviceAccountName: argocd-repo-server + containers: + - name: argocd-repo-server + image: quay.io/argoproj/argocd:v2.12.3 + command: [argocd-repo-server, --redis, "argocd-redis:6379"] + ports: + - containerPort: 8081 + name: server + - containerPort: 8084 + name: metrics + readinessProbe: + tcpSocket: + port: server + initialDelaySeconds: 5 + periodSeconds: 10 + volumeMounts: + - name: ssh-known-hosts + mountPath: /app/config/ssh + - name: tls-certs + mountPath: /app/config/tls + - name: tmp + mountPath: /tmp + - name: helm-working-dir + mountPath: /helm-working-dir + volumes: + - name: ssh-known-hosts + configMap: + name: argocd-ssh-known-hosts-cm + - name: tls-certs + configMap: + name: argocd-tls-certs-cm + - name: tmp + emptyDir: {} + - name: helm-working-dir + emptyDir: {} +--- +apiVersion: v1 +kind: Service +metadata: + name: argocd-application-controller + namespace: argocd + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +spec: + type: ClusterIP + ports: + - port: 8082 + targetPort: metrics + name: metrics + selector: + app.kubernetes.io/name: argocd-application-controller +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: argocd-application-controller + namespace: argocd + labels: + app.kubernetes.io/name: argocd-application-controller + app.kubernetes.io/part-of: argocd +spec: + serviceName: argocd-application-controller + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: argocd-application-controller + template: + metadata: + labels: + app.kubernetes.io/name: argocd-application-controller + spec: + serviceAccountName: argocd-application-controller + containers: + - name: argocd-application-controller + image: quay.io/argoproj/argocd:v2.12.3 + command: + - argocd-application-controller + - --redis + - argocd-redis:6379 + - --repo-server + - argocd-repo-server:8081 + env: + - name: ARGOCD_CONTROLLER_REPLICAS + value: "1" + ports: + - containerPort: 8082 + name: metrics + readinessProbe: + httpGet: + path: /healthz + port: metrics + initialDelaySeconds: 5 + periodSeconds: 10 +--- apiVersion: apps/v1 kind: Deployment metadata: @@ -42,16 +643,49 @@ spec: containers: - name: argocd-server image: quay.io/argoproj/argocd:v2.12.3 - command: [argocd-server, --insecure, --staticassets, /shared/app] + command: + - argocd-server + - --insecure + - --staticassets + - /shared/app + - --redis + - argocd-redis:6379 + - --repo-server + - argocd-repo-server:8081 ports: - containerPort: 8080 name: server + # Plain /healthz on both probes: the "full" variant also fails when + # redis or the repo-server is still pulling, which would restart the + # API server in a loop on a cold first boot. readinessProbe: httpGet: path: /healthz - port: 8080 + port: server initialDelaySeconds: 5 periodSeconds: 10 + livenessProbe: + httpGet: + path: /healthz + port: server + initialDelaySeconds: 30 + periodSeconds: 30 + volumeMounts: + - name: ssh-known-hosts + mountPath: /app/config/ssh + - name: tls-certs + mountPath: /app/config/tls + - name: tmp + mountPath: /tmp + volumes: + - name: ssh-known-hosts + configMap: + name: argocd-ssh-known-hosts-cm + - name: tls-certs + configMap: + name: argocd-tls-certs-cm + - name: tmp + emptyDir: {} --- apiVersion: v1 kind: Service @@ -65,7 +699,7 @@ spec: type: ClusterIP ports: - port: 80 - targetPort: 8080 + targetPort: server name: http selector: app.kubernetes.io/name: argocd-server diff --git a/tests/unit/test_k0s_manifests.py b/tests/unit/test_k0s_manifests.py index 149ca421..71900a87 100644 --- a/tests/unit/test_k0s_manifests.py +++ b/tests/unit/test_k0s_manifests.py @@ -6,6 +6,11 @@ import yaml ROOT = Path(__file__).resolve().parents[2] +ARGOCD_INSTALL = ROOT / "files" / "k0s" / "manifests" / "argocd" / "install.yaml" + + +def argocd_docs(): + return [d for d in yaml.safe_load_all(ARGOCD_INSTALL.read_text()) if d] def test_k0s_service_unit(): @@ -40,6 +45,181 @@ def test_k0s_manifest_files(): assert (ks_dir / "41-kubestellar-kiosk-proxy.yaml").is_file() +def test_argocd_stack_is_complete(): + # #177: argocd-server never binds :8080 — and so never passes its readiness + # probe — unless redis, the repo server, the application controller and the + # CRDs it watches are all part of the stack. + docs = argocd_docs() + workloads = {(d["kind"], d["metadata"]["name"]) for d in docs} + for name in ( + "argocd-redis", + "argocd-repo-server", + "argocd-server", + ): + assert ("Deployment", name) in workloads, f"{name} Deployment missing" + assert ("Service", name) in workloads, f"{name} Service missing" + assert ("StatefulSet", "argocd-application-controller") in workloads + + crds = {d["metadata"]["name"] for d in docs if d["kind"] == "CustomResourceDefinition"} + assert crds == { + "applications.argoproj.io", + "appprojects.argoproj.io", + "applicationsets.argoproj.io", + } + + +def test_argocd_application_crds_have_no_status_subresource(): + # The application controller writes health/sync/operationState by + # updating the Application itself, not its /status endpoint. A status + # subresource on Application or AppProject makes the API server strip + # `.status` from those writes, so Applications never record sync state -- + # argocd-server still goes ready, so #177 looks fixed while the stack is + # useless. Upstream v2.12 ships `subresources: {}` on Application and none + # on AppProject; only ApplicationSet has one. + versions = { + d["metadata"]["name"]: d["spec"]["versions"][0] + for d in argocd_docs() + if d["kind"] == "CustomResourceDefinition" + } + for name in ("applications.argoproj.io", "appprojects.argoproj.io"): + assert not versions[name].get("subresources"), ( + f"{name} must not enable a status subresource: the API server would " + "silently drop status from the controller's writes" + ) + assert versions["applicationsets.argoproj.io"]["subresources"] == {"status": {}} + + +def test_argocd_rbac_is_bound(): + # Without namespace RBAC the server's configmap/secret and Application + # informers never sync, which is the state issue #177 reported. + docs = argocd_docs() + accounts = {d["metadata"]["name"] for d in docs if d["kind"] == "ServiceAccount"} + assert accounts == { + "argocd-server", + "argocd-application-controller", + "argocd-repo-server", + "argocd-redis", + } + + for kind, binding_kind in (("Role", "RoleBinding"), ("ClusterRole", "ClusterRoleBinding")): + roles = {d["metadata"]["name"] for d in docs if d["kind"] == kind} + assert {"argocd-server", "argocd-application-controller"} <= roles, f"{kind} missing" + for binding in (d for d in docs if d["kind"] == binding_kind): + assert binding["roleRef"]["kind"] == kind + assert binding["roleRef"]["name"] in roles, "binding references an unknown role" + for subject in binding["subjects"]: + assert subject["name"] in accounts, "binding references an unknown ServiceAccount" + + +def test_argocd_workloads_are_wired_together(): + docs = argocd_docs() + pod_specs = { + d["metadata"]["name"]: d["spec"]["template"]["spec"] + for d in docs + if d["kind"] in ("Deployment", "StatefulSet") + } + services = {d["metadata"]["name"]: d["spec"] for d in docs if d["kind"] == "Service"} + config_maps = {d["metadata"]["name"] for d in docs if d["kind"] == "ConfigMap"} + + for name, spec in pod_specs.items(): + assert spec["serviceAccountName"] == name, f"{name} runs under the wrong ServiceAccount" + declared = {v["name"] for v in spec.get("volumes", [])} + for container in spec["containers"]: + for mount in container.get("volumeMounts", []): + assert mount["name"] in declared, f"{name} mounts undeclared volume {mount['name']}" + for volume in spec.get("volumes", []): + if "configMap" in volume: + assert volume["configMap"]["name"] in config_maps, f"{name} mounts a missing ConfigMap" + + # Clients address redis and the repo server through the shipped Services. + for name in ("argocd-server", "argocd-application-controller", "argocd-repo-server"): + command = " ".join(pod_specs[name]["containers"][0]["command"]) + assert "argocd-redis:6379" in command, f"{name} does not point at the redis Service" + assert services["argocd-redis"]["ports"][0]["port"] == 6379 + assert services["argocd-repo-server"]["ports"][0]["port"] == 8081 + + +def test_argocd_statefulset_governing_services_exist(): + # A StatefulSet's spec.serviceName names its governing headless Service. + # Kubernetes does not reject a dangling reference -- the pods run and only + # their per-pod DNS records silently fail to resolve -- so nothing catches + # this at apply time. Upstream v2.12 ships no Service by this name at all + # (the serviceName dangles there too; the controller's metrics live on a + # separate argocd-metrics Service). This repo ships its own ClusterIP + # Service so the reference resolves and Prometheus has something to scrape + # on the controller's own metrics port -- it does not, and does not claim + # to, provide per-pod DNS, which would require a headless Service instead. + docs = argocd_docs() + services = {d["metadata"]["name"]: d["spec"] for d in docs if d["kind"] == "Service"} + for statefulset in (d for d in docs if d["kind"] == "StatefulSet"): + name = statefulset["metadata"]["name"] + governing = statefulset["spec"]["serviceName"] + assert governing in services, ( + f"{name} declares serviceName {governing}, but no such Service is shipped" + ) + labels = statefulset["spec"]["template"]["metadata"]["labels"] + assert services[governing]["selector"].items() <= labels.items(), ( + f"{governing} does not select {name}'s pods" + ) + + # The controller exposes only metrics; the port must match its container. + (port,) = services["argocd-application-controller"]["ports"] + container = next( + d for d in docs if d["kind"] == "StatefulSet" + )["spec"]["template"]["spec"]["containers"][0] + assert port["port"] == 8082 + assert port["targetPort"] == container["ports"][0]["name"] == "metrics" + + +def test_argocd_redis_ingress_is_restricted_to_its_clients(): + # redis carries no requirepass, and the application controller that reads + # the cache holds */*/* cluster-admin, so ClusterIP on a single node is not + # a boundary: every other workload on the node can dial it. The policy is + # derived from, and must stay equal to, the set of pods that actually talk + # to redis -- so adding a fourth client fails here rather than at runtime. + docs = argocd_docs() + pod_specs = { + d["metadata"]["name"]: d["spec"]["template"] + for d in docs + if d["kind"] in ("Deployment", "StatefulSet") + } + clients = { + template["metadata"]["labels"]["app.kubernetes.io/name"] + for name, template in pod_specs.items() + if "argocd-redis:6379" in " ".join(template["spec"]["containers"][0].get("command", [])) + } + assert clients == { + "argocd-server", + "argocd-application-controller", + "argocd-repo-server", + } + + policy = next(d for d in docs if d["kind"] == "NetworkPolicy") + assert policy["metadata"]["namespace"] == "argocd" + assert policy["spec"]["policyTypes"] == ["Ingress"] + + # It must select the redis pod itself, by the label its Deployment uses. + redis_labels = pod_specs["argocd-redis"]["metadata"]["labels"] + assert policy["spec"]["podSelector"]["matchLabels"].items() <= redis_labels.items() + + (rule,) = policy["spec"]["ingress"] + (selector,) = rule["from"] + assert "namespaceSelector" not in selector, "ingress must not be opened to other namespaces" + (expression,) = selector["podSelector"]["matchExpressions"] + assert expression["key"] == "app.kubernetes.io/name" + assert expression["operator"] == "In" + assert set(expression["values"]) == clients + assert rule["ports"] == [{"protocol": "TCP", "port": 6379}] + + +def test_argocd_secret_is_seeded_empty(): + # argocd-server writes server.secretkey into this Secret on first start; it + # must exist but must never carry a committed credential. + secret = next(d for d in argocd_docs() if d["kind"] == "Secret") + assert secret["metadata"]["name"] == "argocd-secret" + assert not secret.get("data") and not secret.get("stringData") + + def test_postgres_password_not_hardcoded(): # #98: the postgres superuser password must not be committed to git in # plaintext, and must not be the publicly documented kubeflex default.