Skip to content

Commit

Permalink
feat: generate provider configs for kubectl & helm (#1044)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jakeyheath authored Apr 26, 2024
1 parent 4ca082b commit d832a3c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
1 change: 0 additions & 1 deletion config/v2/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,6 @@ func ResolveHelmProvider(commons ...Common) *HelmProvider {
}
}
}

if version != nil {
return &HelmProvider{
CommonProvider: CommonProvider{
Expand Down
9 changes: 9 additions & 0 deletions templates/templates/common/helm_provider.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}
9 changes: 9 additions & 0 deletions templates/templates/common/kubectl_provider.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}
22 changes: 17 additions & 5 deletions templates/templates/common/kubernetes_provider.tmpl
Original file line number Diff line number Diff line change
@@ -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 -}}
2 changes: 1 addition & 1 deletion templates/templates/component/terraform/fogg.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions testdata/v2_full_yaml/fogg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 29 additions & 1 deletion testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/fogg.tf

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

0 comments on commit d832a3c

Please sign in to comment.