Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Builds and Pipelines

GitOps with OpenShift GitOps (Argo CD)

Deploy and sync applications from Git with the OpenShift GitOps Operator and Argo CD: Applications, sync policies, drift detection, app of apps and the CI/CD handoff.

A pipeline can build and test an image. GitOps answers the next question: how does a new version safely reach each environment? The rule is simple: Git is the source of truth for what should run. A controller inside the cluster continuously compares Git with the live state and makes the cluster match. OpenShift GitOps is Red Hat's supported distribution of Argo CD, installed by an Operator.

Push vs pull deployments

In a classic pipeline the CI server has credentials for the cluster and pushes changes with oc apply. In GitOps a controller inside the cluster pulls from Git. Benefits:

  • Every change is a Git commit: reviewable, attributable and revertible with git revert.
  • CI does not need cluster-admin credentials.
  • Drift (someone edited an object by hand) is detected and can be corrected automatically.
  • Rebuilding a cluster means pointing the controller at the same repository.
flowchart LR
  Dev["Developer"] -->|"pull request"| Git["Git repository<br/>manifests"]
  CI["Pipeline<br/>build + test"] -->|"update image tag"| Git
  Git <-->|"watch and compare"| Argo["Argo CD in the cluster"]
  Argo -->|"apply"| K["OpenShift objects"]

Figure: CI updates Git; Argo CD syncs the cluster from Git.

Installing

Install the Red Hat OpenShift GitOps Operator from OperatorHub (console: Operators, OperatorHub). It creates a default Argo CD instance in the openshift-gitops namespace, and the console gets a link to the Argo CD UI. You can log in with OpenShift credentials through single sign-on on a standard install. Installing Operators needs cluster-admin, so use a lab cluster such as OpenShift Local.

Terminal
oc get pods -n openshift-gitops
oc get route openshift-gitops-server -n openshift-gitops
oc get argocd -n openshift-gitops

You can also install the argocd CLI to work from a terminal.

An Application

The core Argo CD object is the Application: deploy this path in this Git repo to this namespace of this cluster.

YAML
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: shop-dev
  namespace: openshift-gitops
spec:
  project: default
  source:
    repoURL: https://github.com/example/shop-config.git
    targetRevision: main
    path: overlays/dev
  destination:
    server: https://kubernetes.default.svc
    namespace: shop-dev
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true

Important pieces:

  • source.path can hold plain YAML, a Kustomize overlay (as in YAML manifests and oc apply) or a Helm chart.
  • destination.server: https://kubernetes.default.svc means "this same cluster".
  • automated sync applies changes when Git changes. Without it you press Sync manually.
  • prune deletes live objects that were removed from Git.
  • selfHeal reverts manual changes made directly in the cluster.

The namespace that Argo CD deploys into needs to allow the Argo CD service account to manage it. OpenShift GitOps documents the label or role binding you must add for a new namespace (for the default instance, you label the target namespace with argocd.argoproj.io/managed-by=openshift-gitops).

Terminal
oc label namespace shop-dev argocd.argoproj.io/managed-by=openshift-gitops
oc apply -f application.yaml
oc get applications -n openshift-gitops

Health and sync status

Argo CD reports two separate things for every Application:

Status Values Meaning
Sync Synced, OutOfSync Does the cluster match Git?
Health Healthy, Progressing, Degraded, Missing Do the objects work (Pods ready, Deployment rolled out)?

An app can be Synced and Degraded (the manifests were applied, but Pods crash) or OutOfSync and Healthy (someone changed a replica count by hand). Read both.

Environments and promotion

A common repository layout uses Kustomize overlays:

TEXT
shop-config/
  base/
  overlays/
    dev/
    test/
    prod/

Each environment has its own Application. The CI pipeline commits a new image tag to overlays/dev, Argo CD deploys it, and a pull request that copies the tag to overlays/prod is the promotion step. Production sync can be manual or require approvals.

App of apps and ApplicationSet let one object create many Applications, for example one per environment or per cluster, which scales GitOps across a fleet.

The CI and CD handoff

Stage Tool Output
Build, test, scan OpenShift Pipelines Image in a registry, digest
Update config Pipeline task or a bot Commit to the config repo
Deploy and reconcile Argo CD Cluster matches Git

Keep application code and deployment config in separate repositories (or at least separate folders) so a config change does not rebuild the image and vice versa.

Secrets in GitOps

Never commit plain Secrets. Common choices are Sealed Secrets (encrypted in Git, decrypted in the cluster), the External Secrets Operator (fetches from a vault or cloud secret manager) and Vault integrations. Pick one early.

Stand-in: diff Git against the cluster

The heart of Argo CD is a diff and reconcile loop. This stand-in compares a "desired" state with a "live" state and applies the sync policy. It is a simplified teaching model.

Python Stand-in: sync, prune and self-heal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: promote by pull request

Python Stand-in: promotion by editing a config file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every line in the log corresponds to a Git commit in real GitOps, which is what gives you an audit trail.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main idea of GitOps?

    Git is the source of truth, and a controller continuously reconciles the cluster with it.
  2. 2. An Argo CD app is Synced but Degraded. What does that mean?

    Sync status compares Git with the cluster; health reports whether the objects actually work.
  3. 3. What does selfHeal do?

    With selfHeal, drift from Git is corrected automatically.
  4. 4. How is a release usually promoted from test to prod in a GitOps repo?

    Promotion is a change in Git, so it is reviewed, recorded and revertible.
  5. 5. Which Operator installs Argo CD on OpenShift?

    OpenShift GitOps is the Argo CD distribution; OpenShift Pipelines is Tekton.

Summary

  • GitOps pulls desired state from Git; Argo CD reconciles the cluster with it.
  • An Application ties a Git path to a destination namespace, with optional automated sync, prune and selfHeal.
  • Read both Sync and Health status.
  • Use Kustomize overlays per environment, promote through pull requests, and keep secrets out of Git.
  • Pipelines build artifacts; GitOps deploys them.

Next: Services and Endpoints in OpenShift