Terraform changes – Continuous Deployment/ Delivery with Argo CD

The terraform directory now contains two more files:
• argocd.tf: This contains the Terraform configuration for deploying Argo CD
• app.tf: This contains the Terraform configuration for configuring Argo CD apps

Let’s explore both files in detail.

argocd.tf

This filesstarts with the time_sleep resource with an explicit dependency on the google_ container_cluster resource. It will sleep for 30 seconds after the cluster is created so that it is ready to serve requests:
resource “time_sleep” “wait_30_seconds” {
depends_on = [google_container_cluster.main]
create_duration = “30s”
}

To connect with GKE, we will use the gke_auth module provided by terraform-google-modules/kubernetes-engine/google//modules/auth. We will add an explicit dependency to the time_sleep module so that authentication happens 30 seconds after the cluster is created:
module “gke_auth” {
depends_on = [time_sleep.wait_30_seconds]
source = “terraform-google-modules/kubernetes-engine/google//modules/auth”
project_id = var.project_id
cluster_name = google_container_cluster.main.name
location = var.location
use_private_endpoint = false
}

Now that we’ve authenticated with the GKE cluster, we need to apply manifests to deploy Argo CD to the cluster. We will use the gavinbunney/kubectl plugin (https://registry.terraform. io/providers/gavinbunney/kubectl/latest/docs) for that.

We start by defining some data sources to help generate Kubernetes manifests that we will apply to install Argo CD. We will create two kubectl_file_documents data sources for the namespace and Argo CD app that point to the corresponding namespace.yaml and install.yaml files within the manifests/argocd directory:
data “kubectl_file_documents” “namespace” {
content = file(“../manifests/argocd/namespace.yaml”)
}
data “kubectl_file_documents” “argocd” {
content = file(“../manifests/argocd/install.yaml”)
}

Using these data sources, we can create two kubectl_manifest resources for the namespace and

Argo CD app. These resources will apply the manifests within the GKE cluster:
resource “kubectl_manifest” “namespace” {
for_each = data.kubectl_file_documents.namespace.manifests
yaml_body = each.value
override_namespace = “argocd”
}
resource “kubectl_manifest” “argocd” {
depends_on = [
kubectl_manifest.namespace,
]
for_each = data.kubectl_file_documents.argocd.manifests
yaml_body = each.value
override_namespace = “argocd”
}

Now that we’ve added the configuration to install Argo CD, we also need to configure argo CD Applications. To do that, we have the app.tf file.

app.tf

Similar to the Argo CD configuration, we have a kubectl_file_documents data source reading from the manifests/argocd/apps.yaml file; the kubectl_manifest resource will apply the manifest to the Kubernetes cluster:
data “kubectl_file_documents” “apps” {
content = file(“../manifests/argocd/apps.yaml”)
}
resource “kubectl_manifest” “apps” {
depends_on = [
kubectl_manifest.argocd,
]
for_each = data.kubectl_file_documents.apps.manifests
yaml_body = each.value
override_namespace = “argocd”
}

We’ve also modified the provider.tf file, so we’ll explore that next.

provider.tf

Within this file, we have included the kubectl provider, as follows:

provider “kubectl” {
host = module.gke_auth.host
cluster_ca_certificate = module.gke_auth.cluster_ca_certificate
token = module.gke_auth.token
load_config_file = false
}
terraform {
required_providers {
kubectl = {
source = “gavinbunney/kubectl”
version = “>= 1.7.0”
}
}…
}

Now, let’s inspect the manifests directory.

Leave a Reply

Your email address will not be published. Required fields are marked *



          Terms of Use | About Breannaworld | Privacy Policy | Cookies | Accessibility Help | Contact Breannaworld