Gitops - Infra As Code Done Right
Tags: gitops, git, devops, iac
Published: 2023-09-09 17:07:05

## Table Of Contents
* [Introduction](#intro)
* [The Wrong Way of Doing IAC](#wrong)
* [Benefits of GitOps for IAC](#benifits)
  1. [Why Git ?](#git)
  2. [Gitops Workflow Explained](#workflow)
  3. [Automation and Deployment Processes ](#automation)
  4. [Easy Rollback](#rollback)
* [Conclusion](#conclusion)

## Introduction <a name="intro"></a>

In today's fast-paced tech world, efficiently handling infrastructure while maintaining stability and security can be a real challenge.
But what if I told you there's a way to make this process smoother and more collaborative? 
GitOps a methodology that merges the power of version control, git workflows and automation with infra as code.
In this article, I will try to uncover how GitOps can revolutionize the way you handle infrastructure  as a developer or operations professional.

#### Let's Get Started !
-------

## The Wrong Way of Doing IAC <a name="wrong"></a>

![banner](https://govdevsecopshub.com/wp-content/uploads/2021/02/shutterstock_481946590.jpg)

Before we dive into gitops, we need to know the need behind it and why doing IAC in a way other than gitops is problematic!

Picture this : instead of the tedious and error-prone manual setup of a Kubernetes cluster, you are harnessing the power of Infrastructure as Code (IAC) to define the deployment environment as code with Terraform, Ansible configurations, and Kubernetes manifests as your tools of choice for example.

So you have a bunch of yaml files and other configuration files that describe your infrastructure, your plateform and it's configuration.

* **Bad Practice n°1** : 

Storing them on **local machine** and not in a **git** repo!

-> No team collaboration and code reviews!

* **Bad Practice n°2** : 

Storing the config files in a git repo where everyone on the team has access to it but no Review/Approval process!

-> No pull/merge requests -> Commiting directly to main branch

And by consequences no automated tests to test the code changes

-> Break something in the infrastructure or the app environment making it instable.

* **Bad Practice n°3** : 

Updating the infrastructure and the app environment **manually**!

-> Everyone on the team has access to infrastructure to apply changes from their local machines making it hard to trace who executed what and when.

-> Find mistakes only once applied.

So as you see even though we are taking advantage of describing infrastructure as code which already has alot of benefits, our process is still mostly manual and inefficient. And that's where the **gitops** concept comes into practice to **treat Infrastructure As Code the same as Application Code**

-------

## Benefits of GitOps for IAC <a name="benifits"></a>

Let's start first by discussing why Git ?

### 1. Why Git ? <a name="git"></a>

First of all, Git provides robust version control capabilities, allowing you to track changes to your infrastructure code over time. This history is essential for collaboration and rollbacks.

Developers and operations professionals are already familiar with it, making it a convenient choice.

### 2. Gitops Workflow Explained <a name="workflow"></a>
![workflow](https://images.ctfassets.net/o7xu9whrs0u9/1KC5aSZKkNOGzlBsdOyehv/3e283da396b0ce017d31a6b23211bf37/gitops-workflow.png)


In a GitOps workflow, there is a dedicated Git repository for the Infrastructure as Code project, coupled with an associated DevOps pipeline. Let's break down the fundamental components of a typical GitOps workflow:

1. Establish a central Git repository that hold the Infrastructure as Code configuration files.

2. Create pull/merge requests to make changes and collaborate before pushing back to the main branch of the repository.

3. Trigger a CI pipeline to validate configuration files, and perform automated tests.

4. Review and approve the changes to ensure that the changes are well tested before being applied in an environment.

5. Trigger a CD pipeline for the continuous deployment of the infrastructure.

**=>** This approach will improve the quality of infrastructure and it's configuration where multiple people collaborate on the changes and things gets tested before applied.

----

### 3. Automation and Deployment Processes <a name="automation"></a>

Now we said that once the changes are merged into the main branch, they will be automatically applied to the infrastructure through the CD pipeline.

In gitops we have two ways to apply these changes : 

   * **Pull Deployment**
     ![pull](https://i.ibb.co/Yp6ynVH/Untitled-design-1.png)

In this model there is an agent installed in the deployment environment like k8s cluster that is actively pulling the changes from the git repository itself.

The agent regulary monitors and compares desired state with actual state present in the environment where its running, if it sees that there is a difference it will pull and applies the changes to get the environment to the desired state defined in the repository.

Mostly applicable in the context of Kubernetes and containerized environments.

Popular Examples : [Argocd](https://argo-cd.readthedocs.io/), [Fluxcd](https://fluxcd.io/)

   * **Push Deployment**
     ![push](https://www.unixarena.com/wp-content/uploads/2021/06/GitOps-Push-Deployment.jpg)  
 
This is the traditional way that we know. 
In this model we explicitly define jobs in the application CI/CD pipeline that will execute commands to update the infrastructure or deploy the new application version to the deployment environment

----

### 4. Easy Rollback <a name="rollback"></a>
![rollback](https://bertwagner.com/wp-content/uploads/2017/05/233da-1wccncrcdusku7im34r58lg.jpeg)

Now that we know that changes in the git repository are automatically synced with the deployment environment.
We can easily leverage the power of git in tracking the history of changes to rollback to any previous state by simply using **git revert**

----

## Conclusion <a name="conclusion"></a>

In conclusion, Gitops is a powerful approach that brings together  the principles of version control, git workflow and automation to make IAC or more generally xAC smoother, more stable and secure.