Skip to content

Commit

Permalink
feat: Fogg template for k8s (#873)
Browse files Browse the repository at this point in the history
* feat: Fogg template for k8s

* Added an example and renamed cluster_name to cluster_component_name
  • Loading branch information
alexlokshin-czi authored Jun 2, 2023
1 parent 47e70ef commit 2050d3c
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ type TfeProvider struct {
}

type KubernetesProvider struct {
CommonProvider `yaml:",inline"`
CommonProvider `yaml:",inline"`
ClusterComponentName *string `yaml:"cluster_component_name,omitempty"`
}

type HelmProvider struct {
Expand Down
9 changes: 9 additions & 0 deletions config/v2/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ func ResolveKubernetesProvider(commons ...Common) *KubernetesProvider {
p.CustomProvider = defaultEnabled(false)
}
}
clusterComponentName := lastNonNil(KubernetesProviderClusterComponentNameGetter, commons...)
var version *string
var enabled *bool

Expand All @@ -605,6 +606,7 @@ func ResolveKubernetesProvider(commons ...Common) *KubernetesProvider {

if version != nil {
return &KubernetesProvider{
ClusterComponentName: clusterComponentName,
CommonProvider: CommonProvider{
CustomProvider: lastNonNilBool(KubernetesProviderCustomProviderGetter, commons...),
Enabled: enabled,
Expand Down Expand Up @@ -1238,3 +1240,10 @@ func DependsOnComponentsGetter(comm Common) []string {
}
return comm.DependsOn.Components
}

func KubernetesProviderClusterComponentNameGetter(comm Common) *string {
if comm.Providers == nil || comm.Providers.Kubernetes == nil {
return nil
}
return comm.Providers.Kubernetes.ClusterComponentName
}
4 changes: 3 additions & 1 deletion plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ type TfeProvider struct {
}

type KubernetesProvider struct {
CommonProvider `yaml:",inline"`
CommonProvider `yaml:",inline"`
ClusterComponentName *string `yaml:"cluster_component_name,omitempty"`
}

type HelmProvider struct {
Expand Down Expand Up @@ -1078,6 +1079,7 @@ func resolveComponentCommon(commons ...v2.Common) ComponentCommon {
version = *k8sConfig.Version
}
k8sPlan = &KubernetesProvider{
ClusterComponentName: k8sConfig.ClusterComponentName,
CommonProvider: CommonProvider{
Version: version,
Enabled: k8sConfig.Enabled == nil || (k8sConfig.Enabled != nil && *k8sConfig.Enabled),
Expand Down
6 changes: 5 additions & 1 deletion plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ func TestPlanBasicV2Yaml(t *testing.T) {
r.NotNil(plan.Envs["staging"])

r.NotNil(plan.Envs["staging"].Components)
r.Len(plan.Envs["staging"].Components, 3)
r.Len(plan.Envs["staging"].Components, 4)

r.NotNil(plan.Envs["staging"])
r.NotNil(plan.Envs["staging"].Components["vpc"])
r.NotNil(plan.Envs["staging"].Components["k8s-comp"])
r.NotNil(plan.Envs["staging"].Components["k8s-comp"].ProviderConfiguration.Kubernetes)
r.NotNil(plan.Envs["staging"].Components["k8s-comp"].ProviderConfiguration.Kubernetes.ClusterComponentName)

logrus.Debugf("%#v\n", plan.Envs["staging"].Components["vpc"].ModuleSource)
r.NotNil(*plan.Envs["staging"].Components["vpc"].ModuleSource)
r.Equal("github.com/terraform-aws-modules/terraform-aws-vpc?ref=v1.30.0", *plan.Envs["staging"].Components["vpc"].ModuleSource)
Expand Down
16 changes: 16 additions & 0 deletions templates/templates/common/kubernetes_provider.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ define "kubernetes_provider" -}}
{{ if .ClusterComponentName -}}
data "aws_eks_cluster" "cluster" {
name = data.terraform_remote_state.{{ .ClusterComponentName }}.outputs.cluster_id
}

data "aws_eks_cluster_auth" "cluster" {
name = data.terraform_remote_state.{{ .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)
token = data.aws_eks_cluster_auth.cluster.token
}
{{ end }}{{ end }}
4 changes: 4 additions & 0 deletions templates/templates/component/terraform/fogg.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
{{ template "auth0_provider" .ProviderConfiguration.Auth0 }}
{{ end }}{{ end }}

{{- if .ProviderConfiguration.Kubernetes -}}{{- if not .ProviderConfiguration.Kubernetes.CustomProvider -}}
{{- template "kubernetes_provider" .ProviderConfiguration.Kubernetes -}}
{{- end -}}{{- end -}}

terraform {
required_version = "={{ .TerraformVersion }}"

Expand Down
6 changes: 6 additions & 0 deletions testdata/v2_full_yaml/fogg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ envs:
extra_vars:
foo: bar3
module_source: github.com/terraform-aws-modules/terraform-aws-vpc?ref=v1.30.0
k8s-comp:
providers:
kubernetes:
enabled: true
cluster_component_name: comp1
version: 2.19.0
extra_vars:
foo: bar2
modules:
Expand Down
2 changes: 1 addition & 1 deletion testdata/v2_full_yaml/terraform/envs/staging/Makefile

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

13 changes: 13 additions & 0 deletions testdata/v2_full_yaml/terraform/envs/staging/comp1/fogg.tf

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
paths = [
"../comp2",
"../k8s-comp",
"../vpc",
"../../../accounts/bar",
"../../../accounts/foo",
Expand Down
13 changes: 13 additions & 0 deletions testdata/v2_full_yaml/terraform/envs/staging/comp2/fogg.tf

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
paths = [
"../comp1",
"../k8s-comp",
"../vpc",
"../../../accounts/bar",
"../../../accounts/foo",
Expand Down
16 changes: 16 additions & 0 deletions testdata/v2_full_yaml/terraform/envs/staging/k8s-comp/Makefile

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

Empty file.
Loading

0 comments on commit 2050d3c

Please sign in to comment.