Skip to content

Commit

Permalink
Merge pull request #1 from project-octal/feature/kubernetes_manifest
Browse files Browse the repository at this point in the history
Feature/kubernetes manifest
  • Loading branch information
dylanturn authored Nov 24, 2021
2 parents 65ccfa9 + a4ce5b9 commit 50d4a02
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 73 deletions.
18 changes: 18 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ The only hard requirement is a Kubernetes cluster with a functioning ArgoCD depl

---

### TODO:
- Update the Terraform version to 1.0+
- Deprecate the `k8s` provider in favor of the `kubernetes_manifest` resource in the `kubernetes` provider.
### Upate from >v1.0.5
When upgrading from v1.0.5 or older to v1.1.0+ you will need to import all the argocd application manifests

**Import the resource state from the cluster**
```shell
# Import the ArgoCD application using the new provider
terraform import -var-file=secrets.tfvars 'module.octal-extras.module.octal_extras_kubedb[0].module.kubedb_argocd_application.kubernetes_manifest.argo_application' "apiVersion=argoproj.io/v1alpha1,kind=Application,namespace=kube-argocd,name=kubedb"

# Delete the state reference to the old k8s_manifest object
terraform state rm 'module.octal-extras.module.octal_extras_kubedb[0].module.kubedb_argocd_application.k8s_manifest.argo_application'
```

### Example
```hcl-terraform
Expand Down Expand Up @@ -62,14 +70,13 @@ module "argocd_application" {

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_k8s"></a> [k8s](#requirement\_k8s) | >= 0.8.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.8, < 2.0.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_k8s"></a> [k8s](#provider\_k8s) | >= 0.8.0 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.6.1 |

## Modules

Expand All @@ -79,7 +86,7 @@ No modules.

| Name | Type |
|------|------|
| [k8s_manifest.argo_application](https://registry.terraform.io/providers/banzaicloud/k8s/latest/docs/resources/manifest) | resource |
| [kubernetes_manifest.argo_application](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest) | resource |

## Inputs

Expand All @@ -92,7 +99,6 @@ No modules.
| <a name="input_chart"></a> [chart](#input\_chart) | The name of the Helm chart | `string` | n/a | yes |
| <a name="input_destination_server"></a> [destination\_server](#input\_destination\_server) | n/a | `string` | `"https://kubernetes.default.svc"` | no |
| <a name="input_helm_parameters"></a> [helm\_parameters](#input\_helm\_parameters) | Parameters that will override helm\_values | <pre>list(object({<br> name : string,<br> value : any,<br> force_string : bool,<br> }))</pre> | `[]` | no |
| <a name="input_helm_template_version"></a> [helm\_template\_version](#input\_helm\_template\_version) | Optional Helm version to template with. If omitted it will fallback to look at the 'apiVersion' in Chart.yaml and decide which Helm binary to use automatically. This field can be either 'v2' or 'v3'. | `string` | `null` | no |
| <a name="input_helm_values"></a> [helm\_values](#input\_helm\_values) | Helm values as a block of yaml | `any` | `{}` | no |
| <a name="input_ignore_differences"></a> [ignore\_differences](#input\_ignore\_differences) | Ignore differences at the specified json pointers | `list(object({ kind : string, group : string, name : string, jsonPointers : list(string) }))` | `[]` | no |
| <a name="input_labels"></a> [labels](#input\_labels) | n/a | `map(string)` | `{}` | no |
Expand Down
54 changes: 2 additions & 52 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
locals {

labels = {
labels = merge({
# Other labels can go here.
}
}, var.labels)

helm_parameters = [
for parameter in var.helm_parameters :
Expand All @@ -12,54 +12,4 @@ locals {
forceString = parameter["force_string"]
}
]

helm_application = {
apiVersion = "argoproj.io/v1alpha1"
kind = "Application"
metadata = {
name = var.name
namespace = var.argocd_namespace
labels = local.labels
}
finalizers = var.cascade_delete ? ["resources-finalizer.argocd.argoproj.io"] : []
spec = {
project = var.project
source = {
repoURL = var.repo_url
targetRevision = var.target_revision
chart = var.chart
path = var.path
helm = {
version = var.helm_template_version
releaseName = var.release_name == null ? var.name : var.release_name
parameters = local.helm_parameters
values = yamlencode(merge({ labels = merge(local.labels, var.labels) }, var.helm_values))
}
}
destination = {
server = var.destination_server
namespace = var.namespace
}
ignoreDifferences = var.ignore_differences
syncPolicy = {
automated = {
prune = var.automated_prune
selfHeal = var.automated_self_heal
}
syncOptions = concat(var.sync_options, [
var.sync_option_validate ? "Validate=true" : "Validate=false",
var.sync_option_create_namespace ? "CreateNamespace=true" : "CreateNamespace=false",
])
retry = {
limit = var.retry_limit
backoff = {
duration = var.retry_backoff_duration
factor = var.retry_backoff_factor
maxDuration = var.retry_backoff_max_duration
}
}
}
ignoreDifferences = var.ignore_differences
}
}
}
57 changes: 55 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
resource "k8s_manifest" "argo_application" {
content = yamlencode(local.helm_application)
resource "kubernetes_manifest" "argo_application" {
computed_fields = [
"metadata.labels",
"metadata.annotations",
"metadata.finalizers",
"spec.source.helm.version"
]
manifest = {
apiVersion = "argoproj.io/v1alpha1"
kind = "Application"
metadata = {
name = var.name
namespace = var.argocd_namespace
labels = local.labels
finalizers = var.cascade_delete == true ? ["resources-finalizer.argocd.argoproj.io"] : []
}
spec = {
project = var.project
source = {
repoURL = var.repo_url
targetRevision = var.target_revision
chart = var.chart
path = var.path
helm = {
releaseName = var.release_name == null ? var.name : var.release_name
parameters = local.helm_parameters
values = yamlencode(merge({ labels = local.labels }, var.helm_values))
}
}
destination = {
server = var.destination_server
namespace = var.namespace
}
ignoreDifferences = var.ignore_differences
syncPolicy = {
automated = {
prune = var.automated_prune
selfHeal = var.automated_self_heal
}
syncOptions = concat(var.sync_options, [
var.sync_option_validate ? "Validate=true" : "Validate=false",
var.sync_option_create_namespace ? "CreateNamespace=true" : "CreateNamespace=false",
])
retry = {
limit = var.retry_limit
backoff = {
duration = var.retry_backoff_duration
factor = var.retry_backoff_factor
maxDuration = var.retry_backoff_max_duration
}
}
}
ignoreDifferences = var.ignore_differences
}
}
}
5 changes: 0 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ variable "release_name" {
description = "Release name override (defaults to application name)"
default = null
}
variable "helm_template_version" {
type = string
description = "Optional Helm version to template with. If omitted it will fallback to look at the 'apiVersion' in Chart.yaml and decide which Helm binary to use automatically. This field can be either 'v2' or 'v3'."
default = null
}
variable "helm_parameters" {
type = list(object({
name : string,
Expand Down
10 changes: 4 additions & 6 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
terraform {
required_version = ">= 0.13, < 2.0.0"
required_version = ">= 0.14.8, < 2.0.0"
required_providers {
# This should be replaced by https://github.com/hashicorp/terraform-provider-kubernetes-alpha eventually...
k8s = {
version = ">= 0.8.0"
source = "banzaicloud/k8s"
kubernetes = {
source = "hashicorp/kubernetes"
}
}
}
}

0 comments on commit 50d4a02

Please sign in to comment.