TExcelTAD
Home › DevOps Capstone: Ship a Production Service › GitOps Delivery with Argo CD

GitOps Delivery with Argo CD

The platform root installed Argo CD; CI pushes images and commits version bumps to linkly-deploy. This lesson fills in that repository: what each environment runs, the database and Gateway objects that belong to the application rather than to OpenTofu, and the Argo CD objects that tie it to the cluster. The GitOps course teaches Argo CD and Flux in depth; its mini project built the promotion flow this lesson reuses.

The repository

text
linkly-deploy/
├── argocd/
│   ├── root.yaml                 the one Application you apply by hand (app of apps)
│   ├── project.yaml              AppProject "linkly": allowed sources and destinations
│   ├── platform.yaml             Application for platform/
│   ├── linkly-staging.yaml       Application: chart from OCI + values from this repo
│   └── linkly-production.yaml
├── platform/
│   └── gateway.yaml              GatewayClass + the shared Gateway
└── apps/linkly/
    ├── staging/
    │   ├── values.yaml           written by the CI bot
    │   └── resources/database.yaml   CloudNativePG Cluster
    └── production/
        ├── values.yaml           changed only by reviewed promotion PRs
        └── resources/database.yaml

Build it

The session writes the repository, commits it, validates every object against the Kubernetes, Gateway API and Argo CD schemas, then plays two later events: the CI bot bumping staging, and a promotion to production.

bash · try it yourself
git init -q linkly-deploy && cd linkly-deploy
mkdir -p argocd platform apps/linkly/staging/resources apps/linkly/production/resources
cat > platform/gateway.yaml <<'X'
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata: {name: eg}
spec: {controllerName: gateway.envoyproxy.io/gatewayclass-controller}
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata: {name: public, namespace: gateway-system}
spec:
  gatewayClassName: eg
  listeners:
    - name: http
      protocol: HTTP
      port: 80
      allowedRoutes:
        namespaces:
          from: Selector
          selector: {matchLabels: {app.kubernetes.io/part-of: linkly}}
X
for env in staging production; do
  instances=1; [ $env = production ] && instances=3
  cat > apps/linkly/$env/resources/database.yaml <<X
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata: {name: linkly-db}
spec:
  instances: $instances
  storage: {size: 1Gi}
  bootstrap:
    initdb: {database: linkly, owner: linkly}
X
  cat > argocd/linkly-$env.yaml <<X
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: linkly-$env
  namespace: argocd
  finalizers: [resources-finalizer.argocd.argoproj.io]
spec:
  project: linkly
  destination: {server: https://kubernetes.default.svc, namespace: linkly-$env}
  sources:
    - repoURL: ghcr.io/acme/charts
      chart: linkly
      targetRevision: 1.4.0
      helm:
        releaseName: linkly
        valueFiles: [\$values/apps/linkly/$env/values.yaml]
    - repoURL: https://github.com/acme/linkly-deploy.git
      targetRevision: main
      ref: values
    - repoURL: https://github.com/acme/linkly-deploy.git
      targetRevision: main
      path: apps/linkly/$env/resources
  syncPolicy:
    automated: {prune: true, selfHeal: true}
    syncOptions: [ServerSideApply=true]
X
done
cat > apps/linkly/staging/values.yaml <<'X'
replicaCount: 1
image:
  tag: v1.4.0
  digest: sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945
config: {logLevel: debug, baseUrl: "https://lnk.staging.example.com", redisAddr: "linkly-redis:6379"}
database: {existingSecret: linkly-db-app, secretKey: uri}
httpRoute: {hostnames: [lnk.staging.example.com]}
podDisruptionBudget: {enabled: false}
serviceMonitor: {enabled: true}
X
cat > apps/linkly/production/values.yaml <<'X'
image:
  tag: v1.4.0
  digest: sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945
config: {logLevel: info, baseUrl: "https://lnk.example.com", redisAddr: "linkly-redis:6379"}
database: {existingSecret: linkly-db-app, secretKey: uri}
httpRoute: {hostnames: [lnk.example.com]}
autoscaling: {enabled: true, minReplicas: 3, maxReplicas: 12}
serviceMonitor: {enabled: true}
X
cat > argocd/project.yaml <<'X'
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata: {name: linkly, namespace: argocd}
spec:
  sourceRepos: [https://github.com/acme/linkly-deploy.git, ghcr.io/acme/charts]
  destinations:
    - {server: https://kubernetes.default.svc, namespace: linkly-staging}
    - {server: https://kubernetes.default.svc, namespace: linkly-production}
    - {server: https://kubernetes.default.svc, namespace: gateway-system}
  clusterResourceWhitelist: [{group: gateway.networking.k8s.io, kind: GatewayClass}]
X
cat > argocd/platform.yaml <<'X'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata: {name: platform, namespace: argocd}
spec:
  project: linkly
  source: {repoURL: https://github.com/acme/linkly-deploy.git, targetRevision: main, path: platform}
  destination: {server: https://kubernetes.default.svc, namespace: gateway-system}
  syncPolicy:
    automated: {prune: true, selfHeal: true}
    syncOptions: [CreateNamespace=true]
X
cat > argocd/root.yaml <<'X'
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata: {name: root, namespace: argocd}
spec:
  project: default
  source: {repoURL: https://github.com/acme/linkly-deploy.git, targetRevision: main, path: argocd}
  destination: {server: https://kubernetes.default.svc, namespace: argocd}
  syncPolicy: {automated: {prune: true, selfHeal: true}}
X
git add . && git commit -qm "linkly-deploy: staging and production on v1.4.0"
echo "== validate"
kubeconform -strict -summary -n 1 argocd/*.yaml platform/*.yaml
kubeconform -strict -summary -n 1 -ignore-missing-schemas apps/linkly/*/resources/*.yaml

echo "== CI bot: staging gets v1.5.0"
yq -i '.image.tag = "v1.5.0" | .image.digest = "sha256:9b2f3c1e8d7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3a2b1c"' apps/linkly/staging/values.yaml
git -c user.name=linkly-deploy-bot commit -qam "staging: linkly v1.5.0"

echo "== promotion: production gets what staging runs"
cat > promote.sh <<'X'
#!/usr/bin/env bash
# promote.sh FROM TO: copy the image (tag and digest) from one environment to another
set -euo pipefail
tag=$(yq '.image.tag' "apps/linkly/$1/values.yaml")
digest=$(yq '.image.digest' "apps/linkly/$1/values.yaml")
yq -i ".image.tag = \"$tag\" | .image.digest = \"$digest\"" "apps/linkly/$2/values.yaml"
git add "apps/linkly/$2/values.yaml"
git commit -qm "$2: promote linkly $(yq '.image.tag' "apps/linkly/$2/values.yaml") from $1"
X
chmod +x promote.sh && git add promote.sh && git commit -qm "Add promote.sh"
git switch -qc promote-v1.5.0 && ./promote.sh staging production
git diff main --stat
git diff main -- apps/linkly/production/values.yaml | grep '^[-+] '
git log --oneline --all --graph
== validate
Summary: 7 resources found in 6 files - Valid: 7, Invalid: 0, Errors: 0, Skipped: 0
Summary: 2 resources found in 2 files - Valid: 0, Invalid: 0, Errors: 0, Skipped: 2
== CI bot: staging gets v1.5.0
== promotion: production gets what staging runs
 apps/linkly/production/values.yaml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
-  tag: v1.4.0
-  digest: sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945
+  tag: v1.5.0
+  digest: sha256:9b2f3c1e8d7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3a2b1c
* 39d47dd production: promote linkly v1.5.0 from staging
* 9484bf5 Add promote.sh
* 973bc10 staging: linkly v1.5.0
* f5df1b1 linkly-deploy: staging and production on v1.4.0

Walkthrough

Three sources per Application. The first source is the chart, pulled from the OCI registry at an exact version; Argo CD 3 reads Helm charts from OCI registries with the repoURL: ghcr.io/acme/charts form (no oci:// prefix) plus chart:. The second is this repository, named values with ref:, so $values/apps/linkly/<env>/values.yaml can feed the chart. The third deploys the plain manifests in resources/ (the PostgreSQL Cluster) into the same namespace. One Application, one sync, one health status for "linkly in production".

The database is an application object. CloudNativePG turns the 8-line Cluster into a PostgreSQL instance (three with streaming replication in production), a linkly-db-rw Service and the linkly-db-app Secret whose uri key the chart reads. kubeconform skipped the two Cluster files: the sandbox has no CloudNativePG schemas, so -ignore-missing-schemas validates what it can and reports the rest as skipped instead of failing.

The AppProject is the guard rail. Even with write access to linkly-deploy, nobody can point an Application in project linkly at another repository, another namespace or a cluster-scoped kind other than GatewayClass. The root Application stays in the default project because it manages Argo CD objects themselves.

Automated sync with prune and self-heal. A merged commit reaches the cluster within Argo CD's polling interval (a webhook makes it seconds); objects removed from Git are deleted; a kubectl edit in the cluster is reverted (Sync Policies). ServerSideApply=true avoids the annotation size limit and field-ownership fights with the HPA.

Promotion is a branch and a diff. promote.sh copies staging's image (tag and digest) to production on a branch; the pull request's diff shows exactly two changed lines. Review, merge, and Argo CD rolls production. Because both environments reference the image by digest, production runs the identical bytes staging tested.

Bootstrapping on the cluster (sample)

After tofu apply of the platform root, one manual step hands the cluster to Git:

bash
$ kubectl apply -n argocd -f argocd/root.yaml
application.argoproj.io/root created

$ argocd app list -o name          # after logging in (argocd login --core, or via the UI)
argocd/linkly-production
argocd/linkly-staging
argocd/platform
argocd/root

$ argocd app get linkly-staging
Name:               argocd/linkly-staging
Project:            linkly
Server:             https://kubernetes.default.svc
Namespace:          linkly-staging
Sources:
- Repo:             ghcr.io/acme/charts
  Target:           1.4.0
- Repo:             https://github.com/acme/linkly-deploy.git
  Target:           main
- Repo:             https://github.com/acme/linkly-deploy.git
  Target:           main
  Path:             apps/linkly/staging/resources
SyncWindow:         Sync Allowed
Sync Policy:        Automated (Prune)
Sync Status:        Synced to 1.4.0, main
Health Status:      Healthy

GROUP                         KIND                 NAMESPACE       NAME        STATUS  HEALTH   HOOK  MESSAGE
                              ConfigMap            linkly-staging  linkly      Synced                 configmap/linkly serverside-applied
postgresql.cnpg.io            Cluster              linkly-staging  linkly-db   Synced  Healthy        cluster.postgresql.cnpg.io/linkly-db serverside-applied
                              Service              linkly-staging  linkly      Synced  Healthy        service/linkly serverside-applied
apps                          Deployment           linkly-staging  linkly      Synced  Healthy        deployment.apps/linkly serverside-applied
gateway.networking.k8s.io     HTTPRoute            linkly-staging  linkly      Synced                 httproute.gateway.networking.k8s.io/linkly serverside-applied
networking.k8s.io             NetworkPolicy        linkly-staging  linkly      Synced                 networkpolicy.networking.k8s.io/linkly serverside-applied
monitoring.coreos.com         ServiceMonitor       linkly-staging  linkly      Synced                 servicemonitor.monitoring.coreos.com/linkly serverside-applied

On the very first sync, linkly's pods don't start cleanly. Until CloudNativePG has created the linkly-db-app Secret, Kubernetes holds them in CreateContainerConfigError; once the Secret exists but PostgreSQL doesn't accept connections yet, the startup migration fails and linkly exits, so the pods restart a few times with back-off. Both resolve on their own within a minute or two, without anyone intervening. If you prefer a clean first sync, give the Cluster an earlier Argo CD sync wave than the chart's objects; Production Hardening also moves the migration out of the app's startup.

Note: Private registries and repositories need credentials in Argo CD (a repository Secret for ghcr.io with enableOCI: "true", and one for the Git repository). Keep them out of linkly-deploy: create them with the platform root or an ExternalSecret.

Test yourself

What does the ref: values source in the Application do?

  1. Deploys every file in the repository
  2. Makes the deploy repository available as $values, so the chart can read the environment's values file from it
  3. Pins the chart version
  4. Creates a Helm release named values

Why does the promotion copy the digest as well as the tag?

  1. Argo CD requires both
  2. So production runs exactly the bytes that were tested in staging, even if the tag is re-pushed
  3. Digests are shorter
  4. To trigger the HPA

Why did kubeconform report the CloudNativePG Cluster files as skipped?

  1. They are invalid
  2. No schema for that CRD is available offline, and -ignore-missing-schemas turns that into a skip instead of an error
  3. kubeconform skips namespaced objects
  4. They have no apiVersion