From d832a3c3b1bdb70e925de50604d1c3a80ff6fe0d Mon Sep 17 00:00:00 2001 From: Jake Heath <76011913+jakeyheath@users.noreply.github.com> Date: Fri, 26 Apr 2024 15:02:08 -0700 Subject: [PATCH] feat: generate provider configs for kubectl & helm (#1044) * feat: allow variables to specify values in main.tf * filter out nonexistent variables * loop * feat: generate provider configs for kubectl & helm * remove custom provider * tidy * conditionals * index --- config/v2/resolvers.go | 1 - templates/templates/common/helm_provider.tmpl | 9 ++++++ .../templates/common/kubectl_provider.tmpl | 9 ++++++ .../templates/common/kubernetes_provider.tmpl | 22 ++++++++++---- .../component/terraform/fogg.tf.tmpl | 2 +- testdata/v2_full_yaml/fogg.yml | 4 +++ .../terraform/envs/staging/k8s-comp/fogg.tf | 30 ++++++++++++++++++- 7 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 templates/templates/common/helm_provider.tmpl create mode 100644 templates/templates/common/kubectl_provider.tmpl diff --git a/config/v2/resolvers.go b/config/v2/resolvers.go index 618cd6d44..e215780b1 100644 --- a/config/v2/resolvers.go +++ b/config/v2/resolvers.go @@ -659,7 +659,6 @@ func ResolveHelmProvider(commons ...Common) *HelmProvider { } } } - if version != nil { return &HelmProvider{ CommonProvider: CommonProvider{ diff --git a/templates/templates/common/helm_provider.tmpl b/templates/templates/common/helm_provider.tmpl new file mode 100644 index 000000000..43b9d7db8 --- /dev/null +++ b/templates/templates/common/helm_provider.tmpl @@ -0,0 +1,9 @@ +{{ define "helm_provider" -}} +provider "helm" { + kubernetes { + host = data.aws_eks_cluster.cluster.endpoint + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) + token = data.aws_eks_cluster_auth.cluster.token + } +} +{{ end }} diff --git a/templates/templates/common/kubectl_provider.tmpl b/templates/templates/common/kubectl_provider.tmpl new file mode 100644 index 000000000..293a640a3 --- /dev/null +++ b/templates/templates/common/kubectl_provider.tmpl @@ -0,0 +1,9 @@ +{{ define "kubectl_provider" -}} +provider "kubectl" { + host = data.aws_eks_cluster.cluster.endpoint + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) + token = data.aws_eks_cluster_auth.cluster.token + load_config_file = false + apply_retry_count = 15 +} +{{ end }} \ No newline at end of file diff --git a/templates/templates/common/kubernetes_provider.tmpl b/templates/templates/common/kubernetes_provider.tmpl index ddd27f0e5..2b63b70ac 100644 --- a/templates/templates/common/kubernetes_provider.tmpl +++ b/templates/templates/common/kubernetes_provider.tmpl @@ -1,16 +1,28 @@ {{ define "kubernetes_provider" -}} -{{ if .ClusterComponentName -}} +{{ if .Kubernetes.ClusterComponentName -}} data "aws_eks_cluster" "cluster" { - name = data.terraform_remote_state.{{ .ClusterComponentName }}.outputs.cluster_id + name = data.terraform_remote_state.{{ .Kubernetes.ClusterComponentName }}.outputs.cluster_id } data "aws_eks_cluster_auth" "cluster" { - name = data.terraform_remote_state.{{ .ClusterComponentName }}.outputs.cluster_id + name = data.terraform_remote_state.{{ .Kubernetes.ClusterComponentName }}.outputs.cluster_id } provider "kubernetes" { host = data.aws_eks_cluster.cluster.endpoint - cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data) + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) token = data.aws_eks_cluster_auth.cluster.token } -{{ end }}{{ end }} +{{if .Helm -}}{{if not .Helm.CustomProvider -}} +{{template "helm_provider" .Helm -}} +{{ end }} +{{ end }} + +{{- if .Kubectl -}}{{- if not .Kubectl.CustomProvider -}} +{{template "kubectl_provider" .Kubectl -}} +{{- end -}} +{{- end -}} + + +{{- end -}} +{{- end -}} diff --git a/templates/templates/component/terraform/fogg.tf.tmpl b/templates/templates/component/terraform/fogg.tf.tmpl index 8a4097787..dda93400f 100644 --- a/templates/templates/component/terraform/fogg.tf.tmpl +++ b/templates/templates/component/terraform/fogg.tf.tmpl @@ -77,7 +77,7 @@ variable "TFC_PROJECT_NAME" { {{ end }}{{ end }} {{- if .ProviderConfiguration.Kubernetes -}}{{- if not .ProviderConfiguration.Kubernetes.CustomProvider -}} - {{- template "kubernetes_provider" .ProviderConfiguration.Kubernetes -}} + {{- template "kubernetes_provider" .ProviderConfiguration -}} {{- end -}}{{- end -}} terraform { diff --git a/testdata/v2_full_yaml/fogg.yml b/testdata/v2_full_yaml/fogg.yml index 97ad3985c..8880a4dd0 100644 --- a/testdata/v2_full_yaml/fogg.yml +++ b/testdata/v2_full_yaml/fogg.yml @@ -124,6 +124,10 @@ envs: enabled: true cluster_component_name: comp1 version: 2.19.0 + helm: + enabled: true + kubectl: + enabled: true extra_vars: foo: bar2 modules: diff --git a/testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/fogg.tf b/testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/fogg.tf index 80f88e503..2a3544d13 100644 --- a/testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/fogg.tf +++ b/testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/fogg.tf @@ -65,9 +65,23 @@ data "aws_eks_cluster_auth" "cluster" { } provider "kubernetes" { host = data.aws_eks_cluster.cluster.endpoint - cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data) + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) token = data.aws_eks_cluster_auth.cluster.token } +provider "helm" { + kubernetes { + host = data.aws_eks_cluster.cluster.endpoint + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) + token = data.aws_eks_cluster_auth.cluster.token + } +} +provider "kubectl" { + host = data.aws_eks_cluster.cluster.endpoint + cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority[0].data) + token = data.aws_eks_cluster_auth.cluster.token + load_config_file = false + apply_retry_count = 15 +} terraform { required_version = "=0.100.0" @@ -103,6 +117,20 @@ terraform { } + helm = { + source = "hashicorp/helm" + + version = "2.9.0" + + } + + kubectl = { + source = "gavinbunney/kubectl" + + version = "1.14.0" + + } + kubernetes = { source = "hashicorp/kubernetes"