ArgoCD: Create an application declaratively

argocd kubernetes Application

3 min read | by Jordi Prats

On ArgoCD an Application is a group of resources defined from a given source, for example, a helm chart. To create it we can use the argocd cli tool or create it declaratively as any other Kubernetes object using a manifest

Access to the git repository

Since we are going to retrieve the helm chart from a git repository, first we will have to create a Secret with some credentials to be able to retrieve it (if needed). On this example we are going to use github's possibility to create some Deploy keys (ssh key) to be able to get the repository.

Assuming we have the private key on the following path ~/.ssh/deploy_key.private_key for the git repository git@github.com:jordiprats/helm-pet2cattle.git, we can create the secret like this:

cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Secret metadata:  annotations:  managed-by: argocd.argoproj.io  labels:  argocd.argoproj.io/secret-type: repository  name: github-helm-pet2cattle  namespace: argocd type: Opaque data:  insecure: $(echo -n false | base64)  project: $(echo -n default | base64)  sshPrivateKey: $(base64 -w0 ~/.ssh/deploy_key.private_key)  type: $(echo -n git | base64)  url: $(echo -n ssh://git@github.com:jordiprats/helm-pet2cattle.git | base64) EOF 

With this secret available, ArgoCD we will be able to retrieve the helm chart from the repository using the ssh key we are specifying.

At this point we can now create the Application definition. We will need to specify:

  • Repository where the helm chart is located and a commit or tag that we want to use
  • Target Kubernetes cluster
  • Sync options

Repository

Using spec.source we can set where do we want to fetch the helm chart from. On the following example we are retrieving the tag 3.5 from the git@github.com:jordiprats/helm-pet2cattle.git repository.

We can specify any values we want to set for the helm chart using spec.source.helm.values:

spec:  source:  repoURL: ssh://git@github.com:jordiprats/helm-pet2cattle.git  path: .  targetRevision: 3.5  helm:  values: |  ingress:  enabled: true 

Target cluster

ArgoCD can be configured to deploy to multiple Kubernetes clusters, on this example we are going to use the local kubernetes cluster where ArgoCD is currently installed.

Using spec.destination.namespace we can deploy to any namespace:

spec:  destination:  server: https://kubernetes.default.svc  namespace: pet2cattle 

Sync policy

With the spec.syncPolicy we can specify how ArgoCD is going to handle changes on the source repository. For this example we have configured an automated sync with self heal and prune enabled.

We can also configure it so that if the target namespace does not exist it will create it:

spec:  syncPolicy:  automated:  selfHeal: true  prune: true  syncOptions:  - CreateNamespace=true 

Resulting Application definition

Putting all the pieces together the application will look like this:

apiVersion: argoproj.io/v1alpha1 kind: Application metadata:  name: blog  namespace: argocd spec:  project: default  source:  repoURL: ssh://git@github.com:jordiprats/helm-pet2cattle.git  path: .  targetRevision: 3.5  helm:  values: |  ingress:  enabled: true  destination:  server: https://kubernetes.default.svc  namespace: pet2cattle  syncPolicy:  automated:  selfHeal: true  prune: true  syncOptions:  - CreateNamespace=true 

At this point we just need to apply this manifest to deploy this application. We can use kubectl get application to get it's current status:

$ kubectl get application -n argocd NAME SYNC STATUS HEALTH STATUS blog Synced Healthy 

Posted on 06/06/2022

Categories