Now that our CI/CD build and integration stage is ready we could promote the app version directly to a production stage. But with the help of the GitOps approach, we can leverage our Git system to handle promotion that is tracked through commits and can deploy and configure the whole production environment. This stage is just too critical to configure manually and without an audit.
So let’s start be installing the OpenShift GitOps Operator based on the project ArgoCD.
The installation of the GitOps Operator will give you a clusterwide ArgoCD instance available at the link in the top right menu, but since we want to have an instance to manage just our prod project we will create another ArgoCD instance in that specific project.
workshop-prod
workshop-prod
selected in the top menu click on Installed Operators and then Red Hat OpenShift GitOps.workshop-prod
project.We already have a second repository, called openshift-gitops-getting-started
in Gitea that holds the required Gitops yaml resources. We will use this repo to push changes to our workshop-prod
enivronment.
Have a quick look at the structure of this git project:
app - contains yaml files for the deployment, service and route resources needed by our application. These will be applied to the cluster. There is also a kustomization.yaml
defining that kustomize layers will be applied to all yamls
environments/dev - contains the kustomization.yaml
which will be modified by our builds with new Image versions. ArgoCD will pick up these changes and trigger new deployments.
Let’s setup the project that tells ArgoCD to watch our configuration repository and update resources in the workshop-prod
project accordingly.
workshop-prod
admin
and password will be in Secret argocd-cluster
in the Project workspace-prod
ArgoCD works with the concept of Applications. We will create an Application and point it to the configuration Git repository. ArgoCD will look for Kubernetes yaml files in the repository and path and deploy them to the defined project. Additionally, ArgoCD will also react to changes to the repository and reflect these to the project. You can also enable self-healing to prevent configuration drift. If you want find out more about OpenShift GitOps have look here.
openshift-gitops-getting-started
from GiteaWatch the resources (Deployment
, Service
, Route
) get rolled out to the project workshop-prod
. Notice, we also scaling our app to 2 pods in the production stage as we want some high availability. But the actual deployment will not succeed as shown by the ‘broken heart’ icons!
Since we have not published our image to the Quay workshop-prod
repository the initial Deployment will try to roll out non existant image from Quay. Once the first pipeline run is complete, our newly built image will be replaced in the Deployment and rolled out.
Our complete production stage is now configured and controlled through GitOps. But how do we tell ArgoCD that there is a new version of our app to deploy? Well, we will add a step to our build pipeline updating the configuration repository.
As we do not want to modify our original repository file we will use a tool called Kustomize that can add incremental change layers to YAML files. Since ArgoCD permanently watches this repository, it will pick up these Kustomize changes.
It is also possible to update the repository with a Pull request. Then you have an approval process for your production deployment.
We will need to initialize the workshop-prod/workshop
in Quay so the robo user will be able to push images there later on.
openshift_workshop-prod
on the rightopenshift_workshop-prod
as Organizationworkshop
as repo nameLet’s add a new custom Tekton task to the workshop-int
project that can update the Image tag
via Kustomize after the build process completed and then push the change to our git configuration repository.
We could add this through the OpenShift Web Console as well but to save time we will apply the file directly via the oc
command.
oc create -f https://raw.githubusercontent.com/devsecops-workshop/yaml/main/tekton-kustomize.yml
workshop-int
and then go to Pipelines > Tasks > Tasks and have a look at the just imported task git-update-deployment
. You should see the git commands how the configuration repository will be cloned, patched by Kustomize and then pushed again.So now we have a new Tekton Task in our task catalog to update a GitOps Git repository, but we still need to promote the actual image from our workshop-int
to workshop-prod
project. Otherwise the image will not be available for our deployment.
workshop_int
project, go to Pipelines > Pipelines > workshop and then YAMLYou can edit pipelines either directly in YAML or in the visual Pipeline Builder. We will see how to use the Builder later on, so let’s edit the YAML for now.
Add the new Task to your Pipeline by adding it to the YAML like this:
spec > params
section add the following (if the <DOMAIN>
placeholder hasn’t been replaced automatically, do it manually):- default: >-
https://repository-git.apps.<DOMAIN>/gitea/openshift-gitops-getting-started.git
name: GIT_CONFIG_REPO
type: string
tasks
level right after the deploy
taskGIT_CONFIG_REPO
to the Task parameter GIT_REPOSITORY
In the OpenShift YAML viewer/editor you can mark multiple lines and use tab to indent this lines for one step.
- name: skopeo-copy
params:
- name: srcImageURL
value: "docker://$(params.QUAY_URL)/openshift_workshop-int/workshop:latest"
- name: destImageURL
value: "docker://$(params.QUAY_URL)/openshift_workshop-prod/workshop:latest"
- name: srcTLSverify
value: "false"
- name: destTLSverify
value: "false"
runAfter:
- build
taskRef:
kind: ClusterTask
name: skopeo-copy
workspaces:
- name: images-url
workspace: workspace
- name: git-update-deployment
params:
- name: GIT_REPOSITORY
value: $(params.GIT_CONFIG_REPO)
- name: CURRENT_IMAGE
value: "quay.io/nexus6/hello-microshift:1.0.0-SNAPSHOT"
- name: NEW_IMAGE
value: $(params.QUAY_URL)/openshift_workshop-prod/workshop
- name: NEW_DIGEST
value: $(tasks.build.results.IMAGE_DIGEST)
- name: KUSTOMIZATION_PATH
value: environments/dev
runAfter:
- skopeo-copy
taskRef:
kind: Task
name: git-update-deployment
workspaces:
- name: workspace
workspace: workspace
The Pipeline
should now look like this. Notice that the new tasks runs in parallel to the deploy
task
Now, the pipeline is set. The last thing we need is authentication against the Gitea repository and the workshop-prod Quay org. We will add those from the start pipeline form next. Make sure to replace the
Click on Pipeline Start
openshift_workshop-prod
robo account openshift_workshop-prod+builder
as before)Run the pipeline by clicking Start and see that in your Gitea configuration repository the file /environment/dev/kustomize.yaml
is updated with the new image version
Notice that the deploy
and the git-update
steps now run in parallel. This is one of the strength of Tekton. It can scale natively with pods on OpenShift.
This will tell ArgoCD to update the Deployment with this new image version
Check that the new image is rolled out sucessfully now (you may need to sync manually in ArgoCD to speed things up)