OpenShift GitOps (Argo CD)

Deploy from Git with OpenShift GitOps. Detect drift, heal it, and promote a build from dev to production with a pull request.

Updated CompTech Team

Once a pipeline can build an image, the remaining question is who is allowed to change the cluster. OpenShift GitOps, which is Argo CD, answers it: Git is the record of what should be running, and a controller in the cluster makes it so. A manual oc scale shows up as drift. A promotion is a commit, so it has an author and a review.

This lab points two Applications at one repository, lets dev sync itself, and requires a human to sync production. The image tag arrives from GitLab CI or from OpenShift Pipelines.

Prerequisites

  • An OpenShift 4.x cluster with the OpenShift GitOps operator installed
  • A Git repository payments-gitops with Kustomize overlays overlays/dev and overlays/prod
  • The oc CLI. The Argo CD route is created in the openshift-gitops project

Each overlay contains a Deployment, a Service and a kustomization.yaml whose images.newTag is the commit the pipeline just built. The same repository is what you promote.

Two applications, two policies

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: payments
  namespace: openshift-gitops
spec:
  sourceRepos:
    - https://gitlab.apps.example.com/platform/payments-gitops.git
  destinations:
    - namespace: payments-dev
      server: https://kubernetes.default.svc
    - namespace: payments-prod
      server: https://kubernetes.default.svc
  clusterResourceWhitelist:
    - group: ""
      kind: Namespace
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: payments-dev
  namespace: openshift-gitops
spec:
  project: payments
  source:
    repoURL: https://gitlab.apps.example.com/platform/payments-gitops.git
    targetRevision: main
    path: overlays/dev
  destination:
    server: https://kubernetes.default.svc
    namespace: payments-dev
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions: [CreateNamespace=true]
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: payments-prod
  namespace: openshift-gitops
spec:
  project: payments
  source:
    repoURL: https://gitlab.apps.example.com/platform/payments-gitops.git
    targetRevision: main
    path: overlays/prod
  destination:
    server: https://kubernetes.default.svc
    namespace: payments-prod
  syncPolicy:
    syncOptions: [CreateNamespace=true]

Dev has automated sync, prune and self-heal. Production has a sync policy with no automated block, so a human clicks Sync, or a pipeline calls the API, after the overlay changes. The AppProject limits where this repository is allowed to deploy. A repository that can sync any namespace can sync openshift-gitops itself, so keep the destination list short.

oc apply -f apps.yaml
oc get application -n openshift-gitops
oc get route openshift-gitops-server -n openshift-gitops

See drift, then heal it

Change the live cluster without telling Git:

oc scale deployment/payments -n payments-dev --replicas=5
oc get application payments-dev -n openshift-gitops -o jsonpath='{.status.sync.status}{"\n"}'

The status goes to OutOfSync. With selfHeal: true, the controller scales the deployment back to the replica count in Git. Do the same in payments-prod and it stays out of sync until someone syncs or reverts the manual change. That difference is the point of the two policies.

To see the diff before it heals, open the Application in the GitOps UI, or run argocd app diff payments-dev after logging in to the route.

Promote

Promotion is a pull request that copies the dev tag into the prod overlay.

# overlays/prod/kustomization.yaml
images:
  - name: payments
    newName: image-registry.openshift-image-registry.svc:5000/payments/payments
    newTag: a1b2c3d
  1. Read newTag from overlays/dev after the pipeline has pushed a commit.
  2. Open a merge request that sets the same newTag in overlays/prod.
  3. Review it. The diff is one line, and the pipeline link in the commit message says what was tested.
  4. Merge to main. payments-prod becomes OutOfSync.
  5. Sync payments-prod. Watch oc rollout status deployment/payments -n payments-prod.

Roll back by reverting that commit and syncing again. How the rollout itself behaves (rolling, blue-green, canary) is the deployment strategies lab.

What to keep in Git

In Git Not only in the cluster
Deployments, Services, Routes, ConfigMaps Replica counts someone typed in a console
Kustomize overlays and the image tag A tag that exists only in a CI variable
AppProject and Application manifests An Application created in the UI and never exported
Namespace-scoped RBAC for the app Cluster-admin bindings "just for the demo"

Give the GitOps controller a role per destination project, not cluster-admin. The controller's service account is openshift-gitops-argocd-application-controller in openshift-gitops.

What's next

  1. Build the image that newTag points at with GitLab CI or OpenShift Pipelines.
  2. Choose how the synced Deployment rolls out in deployment strategies.
  3. Refuse an unsigned image at admission time in DevSecOps.