Skip to content

Kargo

Gate every Kargo promotion with one shared ClusterAnalysisTemplate. When Freight lands in a Stage, Kargo runs an AnalysisRun whose Job runs pacto impact --running on the Freight's contract bundle. The Freight verifies only when no consumer that runs breaks.

Kargo verifies after it promotes. A failed verification keeps the Freight from the next Stage and leaves this Stage as promoted: it does not roll back. Pacto writes nothing to Kargo, Argo or Git.

Before you start

  • Kargo v1.12 or later, with the Argo Rollouts CRDs and controller: Rollouts runs the AnalysisRun Job.
  • Each service publishes its contract bundle with pacto push, and a Kargo Warehouse subscribes to that bundle repository like an image, with the default SemVer selection.
  • Every cluster Kargo deploys to runs the Pacto operator with the registry value, so it publishes its signed target snapshot.

Install the template

Apply the template once per Kargo installation:

# This file is published verbatim in integrations/kubernetes/docs/gitops-kargo.md.
# Kargo runs it after each promotion: impact exit 0 verifies the Freight, exit 1
# fails it, exit 2 (unverified) fails it unless allow-unverified is "true".
# In each Kargo project namespace: kubectl -n <project> create serviceaccount pacto-impact
apiVersion: argoproj.io/v1alpha1
kind: ClusterAnalysisTemplate
metadata:
  name: pacto-impact
spec:
  args:
    - name: new
    - name: targets
      value: snapshot
    - name: allow-unverified
      value: "false"
  metrics:
    - name: pacto-impact
      count: 1
      failureLimit: 0
      provider:
        job:
          spec:
            backoffLimit: 0
            template:
              spec:
                restartPolicy: Never
                serviceAccountName: pacto-impact
                containers:
                  - name: pacto-impact
                    image: ghcr.io/trianalab/pacto/dashboard:4.6.1
                    command: [sh, -c]
                    args:
                      - |
                        set -u
                        case "$TARGETS" in
                          snapshot) set -- ;;
                          k8s) set -- --k8s ;;
                          *) echo "pacto-impact: targets must be snapshot or k8s, got $TARGETS"; exit 1 ;;
                        esac
                        pacto impact --running "$NEW" --local "" "$@"; rc=$?
                        case "$rc" in
                          0) exit 0 ;;
                          2) if [ "$ALLOW_UNVERIFIED" = true ]; then echo "impact exit 2 passes: allow-unverified is true"; exit 0; fi
                             echo "impact exit 2: unverified fails unless allow-unverified is true"; exit 1 ;;
                          *) echo "impact exit $rc"; exit 1 ;;
                        esac
                    # Args first: a literal below wins over any name an arg could repeat.
                    env:
                      - name: NEW
                        value: "{{args.new}}"
                      - name: TARGETS
                        value: "{{args.targets}}"
                      - name: ALLOW_UNVERIFIED
                        value: "{{args.allow-unverified}}"
                      - name: PACTO_INSECURE_REGISTRIES
                        value: ""
                      - name: PACTO_NO_UPDATE_CHECK
                        value: "1"
                      - name: DOCKER_CONFIG
                        value: /home/pacto/.docker
                      - name: PACTO_DELIVERY_TRUST
                        value: /etc/pacto/trust/trust.yaml
                    securityContext:
                      runAsNonRoot: true
                      runAsUser: 65532
                      readOnlyRootFilesystem: true
                      allowPrivilegeEscalation: false
                      capabilities:
                        drop: [ALL]
                    volumeMounts:
                      - name: pacto-cache
                        mountPath: /home/pacto/.cache
                      - name: pacto-registry
                        mountPath: /home/pacto/.docker
                        readOnly: true
                      - name: pacto-delivery-trust
                        mountPath: /etc/pacto/trust
                        readOnly: true
                volumes:
                  - name: pacto-cache
                    emptyDir: {}
                  - name: pacto-registry
                    secret:
                      secretName: pacto-registry
                      optional: true
                      items:
                        - key: .dockerconfigjson
                          path: config.json
                  - name: pacto-delivery-trust
                    secret:
                      secretName: pacto-delivery-trust
                      optional: true

In each Kargo project namespace, create the pacto-impact ServiceAccount, the pacto-delivery-trust Secret and, for a private registry, the pacto-registry Secret:

kubectl apply -f kargo-cluster-analysis-template.yaml
kubectl -n <project> create serviceaccount pacto-impact
kubectl -n <project> create secret generic pacto-delivery-trust --from-file=trust.yaml
kubectl -n <project> create secret docker-registry pacto-registry \
  --docker-server=ghcr.io --docker-username=<user> --docker-password=<token>

trust.yaml holds one entry per cluster, from its installation key and scoped to its UID: see Target snapshot consumers. Pacto hands each registry only its own entry from the mounted Docker config. The template takes no registry credentials as args and cannot mark a registry insecure.

Verify each Stage

Every Stage carries the same stanza. vars.pactoBundle is the only service-specific value, and the Stage already names it for its subscription:

spec:
  vars:
    - name: pactoBundle
      value: ghcr.io/acme/contracts/payments
  verification:
    analysisTemplates:
      - name: pacto-impact
        kind: ClusterAnalysisTemplate
    args:
      - name: new
        value: oci://${{ vars.pactoBundle }}@${{ imageFrom(vars.pactoBundle).Digest }}
pacto impact The Freight
0 clear Verified
1 blocked, or could not run Failed
2 unverified Failed unless the Stage sets allow-unverified: "true"

That is the policy of the pull request check: an unverified answer never passes by default.

What it judges against

Kargo cannot name a Stage's previously verified Freight, so --running judges the Freight against every revision of its service that runs on a known target, and the worst answer wins. A revision already running with the Freight's digest is not its own baseline, so Freight that Argo CD has synced is never judged against itself. With no other revision running, only the consumers' version ranges speak, and a running consumer that refuses the new version still blocks. Once every target runs the Freight, the revision it replaced is no longer observed, so a breaking change that consumers' ranges accept clears here; only the pull request check, which runs before promotion, catches it.

Choose where targets come from

The targets arg picks the source:

  • snapshot (default) reads the signed target snapshots in <registry>/pacto-targets beside the bundle and needs no cluster RBAC. A missing, unsigned or stale snapshot makes the answer partial, so unverified.
  • k8s reads the Job's own cluster. Use it only when Kargo and every target share this cluster. Apply the reader role and bind it to each project's ServiceAccount:
# This file is published verbatim in integrations/kubernetes/docs/gitops-kargo.md.
# Only for targets: k8s. It is what `pacto impact --k8s` reads: the cluster
# identity, the Pacto CRs and the workload lists. The default targets: snapshot
# needs none of it.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: pacto-impact-reader
rules:
  - apiGroups: [""]
    resources: [namespaces]
    resourceNames: [kube-system]
    verbs: [get]
  - apiGroups: [pacto.trianalab.io]
    resources: [pactos]
    verbs: [get, list]
  - apiGroups: [apps]
    resources: [deployments, statefulsets, daemonsets]
    verbs: [list]
  - apiGroups: [batch]
    resources: [cronjobs]
    verbs: [list]
# For each Kargo project namespace, after creating the pacto-impact
# ServiceAccount there:
#
# apiVersion: rbac.authorization.k8s.io/v1
# kind: ClusterRoleBinding
# metadata:
#   name: pacto-impact-<project>
# roleRef:
#   apiGroup: rbac.authorization.k8s.io
#   kind: ClusterRole
#   name: pacto-impact-reader
# subjects:
#   - kind: ServiceAccount
#     name: pacto-impact
#     namespace: <project>
#
# Narrower: a RoleBinding to the same ClusterRole in each namespace the project
# may read. Pacto lists every namespace, so a refused list makes the answer
# partial: at best unverified.
#
# apiVersion: rbac.authorization.k8s.io/v1
# kind: RoleBinding
# metadata:
#   name: pacto-impact-<project>
#   namespace: <namespace>
# roleRef:
#   apiGroup: rbac.authorization.k8s.io
#   kind: ClusterRole
#   name: pacto-impact-reader
# subjects:
#   - kind: ServiceAccount
#     name: pacto-impact
#     namespace: <project>

A ClusterRoleBinding gives that project read access to every namespace's Pacto CRs and to the names and images of its workloads. The role lists workloads because an unread workload may run a revision no contract describes, and an unread revision never clears.

Read the reason

The AnalysisRun only says the metric failed. The reason is in the Job log:

$ kubectl -n shop get analysisruns.argoproj.io -l kargo.akuity.io/stage=prod
NAME                 STATUS
prod.01jd...         Failed
$ kubectl -n shop logs job/<analysisrun-job>
Impact: payments 1.0.0 -> 2.0.0
...
Running: 1 revision (judged against oci://ghcr.io/acme/contracts/payments:1.0.0@sha256:4f1c...)
...
Release gate: blocked
impact exit 1

The Running: line names the judged revision by tag and digest.

Check that it took

kubectl get clusteranalysistemplates.argoproj.io pacto-impact

Then promote Freight whose contract you know is safe and watch its AnalysisRun reach Successful.

Fallback: a project-local template

A project that cannot use a cluster-scoped template copies the same metric into a namespaced AnalysisTemplate and references it with kind: AnalysisTemplate.

Limits

  • A ClusterAnalysisTemplate is cluster-wide: every project that references it gets the same exit policy unless its Stage overrides allow-unverified.
  • Verification follows promotion. Gate before a promotion in the pull request that publishes the contract.