Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fluxv2 addons #25

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ or `brew install terraform-docs`.
| Name | Version |
|------|---------|
| terraform | >= 0.13 |
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
Expand All @@ -74,11 +76,14 @@ or `brew install terraform-docs`.

| Name | Version |
|------|---------|
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
| random | n/a |
| time | n/a |
| tls | n/a |

## Modules

Expand All @@ -88,16 +93,25 @@ No Modules.

| Name |
|------|
| [flux_install](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/install) |
| [flux_sync](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/sync) |
| [github_branch_default](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default) |
| [github_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) |
| [github_repository_deploy_key](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key) |
| [github_repository_file](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file) |
| [helm_release](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) |
| [kubectl_file_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/file_documents) |
| [kubectl_manifest](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) |
| [kubectl_path_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/path_documents) |
| [kubernetes_namespace](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) |
| [kubernetes_network_policy](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/network_policy) |
| [kubernetes_priority_class](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/priority_class) |
| [kubernetes_role](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/role) |
| [kubernetes_role_binding](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/role_binding) |
| [kubernetes_secret](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret) |
| [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) |
| [time_sleep](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) |
| [tls_private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) |

## Inputs

Expand All @@ -108,6 +122,7 @@ No Modules.
| cluster-name | Name of the Kubernetes cluster | `string` | `"sample-cluster"` | no |
| external-dns | Map of map for external-dns configuration: see `external_dns.tf` for supported values | `any` | `{}` | no |
| flux | Customize Flux chart, see `flux.tf` for supported values | `any` | `{}` | no |
| flux2 | Customize Flux chart, see `flux2.tf` for supported values | `any` | `{}` | no |
| helm\_defaults | Customize default Helm behavior | `any` | `{}` | no |
| ingress-nginx | Customize ingress-nginx chart, see `nginx-ingress.tf` for supported values | `any` | `{}` | no |
| istio-operator | Customize istio operator deployment, see `istio_operator.tf` for supported values | `any` | `{}` | no |
Expand Down
223 changes: 223 additions & 0 deletions flux2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
locals {

known_hosts = "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=="
flux2 = merge(
{
enabled = false
create_ns = true
namespace = "flux-system"
target_path = "production"
default_network_policy = true
version = "v0.8.0"
github_url = "https://github.com/particuleio/gitops"
create_github_repository = false
github_token = ""
repository = "gitops"
repository_visibility = "public"
branch = "main"
},
var.flux2
)

apply = [for v in data.kubectl_file_documents.apply[0].documents : {
data : yamldecode(v)
content : v
}
]

sync = [for v in data.kubectl_file_documents.sync[0].documents : {
data : yamldecode(v)
content : v
}
]
}

resource "kubernetes_namespace" "flux2" {
count = local.flux2["enabled"] && local.flux2["create_ns"] ? 1 : 0

metadata {
labels = {
name = local.flux2["namespace"]
}

name = local.flux2["namespace"]
}
lifecycle {
ignore_changes = [
metadata[0].labels,
]
}
}

resource "tls_private_key" "identity" {
count = local.flux2["enabled"] ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}

data "flux_install" "main" {
count = local.flux2["enabled"] ? 1 : 0
namespace = local.flux2["namespace"]
target_path = local.flux2["target_path"]
network_policy = false
version = local.flux2["version"]
}

# Split multi-doc YAML with
# https://registry.terraform.io/providers/gavinbunney/kubectl/latest
data "kubectl_file_documents" "apply" {
count = local.flux2["enabled"] ? 1 : 0
content = data.flux_install.main[0].content
}

# Apply manifests on the cluster
resource "kubectl_manifest" "apply" {
for_each = { for v in local.apply : lower(join("/", compact([v.data.apiVersion, v.data.kind, lookup(v.data.metadata, "namespace", ""), v.data.metadata.name]))) => v.content }
depends_on = [kubernetes_namespace.flux2]
yaml_body = each.value
}

# Generate manifests
data "flux_sync" "main" {
count = local.flux2["enabled"] ? 1 : 0
target_path = local.flux2["target_path"]
url = local.flux2["github_url"]
branch = local.flux2["branch"]
namespace = local.flux2["namespace"]
}

# Split multi-doc YAML with
# https://registry.terraform.io/providers/gavinbunney/kubectl/latest
data "kubectl_file_documents" "sync" {
count = local.flux2["enabled"] ? 1 : 0
content = data.flux_sync.main[0].content
}

# Apply manifests on the cluster
resource "kubectl_manifest" "sync" {
for_each = { for v in local.sync : lower(join("/", compact([v.data.apiVersion, v.data.kind, lookup(v.data.metadata, "namespace", ""), v.data.metadata.name]))) => v.content }
depends_on = [kubernetes_namespace.flux2]
yaml_body = each.value
}

# Generate a Kubernetes secret with the Git credentials
resource "kubernetes_secret" "main" {
count = local.flux2["enabled"] ? 1 : 0
depends_on = [kubectl_manifest.apply]

metadata {
name = data.flux_sync.main[0].name
namespace = data.flux_sync.main[0].namespace
}

data = {
"identity.pub" = tls_private_key.identity[0].public_key_pem
identity = tls_private_key.identity[0].private_key_pem
username = "git"
password = local.flux2["github_token"]
}
}

# GitHub
resource "github_repository" "main" {
count = local.flux2["enabled"] && local.flux2["create_github_repository"] ? 1 : 0
name = local.flux2["repository"]
visibility = local.flux2["repository_visibility"]
auto_init = true
}

resource "github_branch_default" "main" {
count = local.flux2["enabled"] && local.flux2["create_github_repository"] ? 1 : 0
repository = local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]
branch = local.flux2["branch"]
}

resource "github_repository_deploy_key" "main" {
count = local.flux2["enabled"] ? 1 : 0
title = "flux-${local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]}-${local.flux2["branch"]}"
repository = local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]
key = tls_private_key.identity[0].public_key_openssh
read_only = true
}

resource "github_repository_file" "install" {
count = local.flux2["enabled"] ? 1 : 0
repository = local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]
file = data.flux_install.main[0].path
content = data.flux_install.main[0].content
branch = local.flux2["branch"]
}

resource "github_repository_file" "sync" {
count = local.flux2["enabled"] ? 1 : 0
repository = local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]
file = data.flux_sync.main[0].path
content = data.flux_sync.main[0].content
branch = local.flux2["branch"]
}

resource "github_repository_file" "kustomize" {
count = local.flux2["enabled"] ? 1 : 0
repository = local.flux2["create_github_repository"] ? github_repository.main[0].name : local.flux2["repository"]
file = data.flux_sync.main[0].kustomize_path
content = data.flux_sync.main[0].kustomize_content
branch = local.flux2["branch"]
}

resource "kubernetes_network_policy" "flux2_allow_monitoring" {
count = local.flux2["enabled"] && local.flux2["default_network_policy"] && local.kube-prometheus-stack["enabled"] ? 1 : 0

metadata {
name = "${local.flux2["create_ns"] ? kubernetes_namespace.flux2.*.metadata.0.name[count.index] : local.flux2["namespace"]}-allow-monitoring"
namespace = local.flux2["create_ns"] ? kubernetes_namespace.flux2.*.metadata.0.name[count.index] : local.flux2["namespace"]
}

spec {
pod_selector {
}

ingress {
ports {
port = "3030"
protocol = "TCP"
}

from {
namespace_selector {
match_labels = {
"${local.labels_prefix}/component" = "monitoring"
}
}
}
}

policy_types = ["Ingress"]
}
}

resource "kubernetes_network_policy" "flux2_allow_namespace" {
count = local.flux2["enabled"] && local.flux2["default_network_policy"] ? 1 : 0

metadata {
name = "${local.flux2["create_ns"] ? kubernetes_namespace.flux2.*.metadata.0.name[count.index] : local.flux2["namespace"]}-allow-namespace"
namespace = local.flux2["create_ns"] ? kubernetes_namespace.flux2.*.metadata.0.name[count.index] : local.flux2["namespace"]
}

spec {
pod_selector {
}

ingress {
from {
namespace_selector {
match_labels = {
name = local.flux2["create_ns"] ? kubernetes_namespace.flux2.*.metadata.0.name[count.index] : local.flux2["namespace"]
}
}
}
}

policy_types = ["Ingress"]
}
}

12 changes: 12 additions & 0 deletions modules/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ This module can uses [IRSA](https://aws.amazon.com/blogs/opensource/introducing-
|------|---------|
| terraform | >= 0.13 |
| aws | ~> 3.0 |
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
Expand All @@ -31,6 +33,8 @@ This module can uses [IRSA](https://aws.amazon.com/blogs/opensource/introducing-
| Name | Version |
|------|---------|
| aws | ~> 3.0 |
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
Expand Down Expand Up @@ -67,7 +71,14 @@ This module can uses [IRSA](https://aws.amazon.com/blogs/opensource/introducing-
| [aws_iam_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) |
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) |
| [aws_region](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) |
| [flux_install](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/install) |
| [flux_sync](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/sync) |
| [github_branch_default](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default) |
| [github_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) |
| [github_repository_deploy_key](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key) |
| [github_repository_file](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file) |
| [helm_release](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) |
| [kubectl_file_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/file_documents) |
| [kubectl_manifest](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) |
| [kubectl_path_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/path_documents) |
| [kubernetes_namespace](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) |
Expand Down Expand Up @@ -101,6 +112,7 @@ This module can uses [IRSA](https://aws.amazon.com/blogs/opensource/introducing-
| eks | EKS cluster inputs | `any` | `{}` | no |
| external-dns | Map of map for external-dns configuration: see `external_dns.tf` for supported values | `any` | `{}` | no |
| flux | Customize Flux chart, see `flux.tf` for supported values | `any` | `{}` | no |
| flux2 | Customize Flux chart, see `flux2.tf` for supported values | `any` | `{}` | no |
| helm\_defaults | Customize default Helm behavior | `any` | `{}` | no |
| ingress-nginx | Customize ingress-nginx chart, see `nginx-ingress.tf` for supported values | `any` | `{}` | no |
| istio-operator | Customize istio operator deployment, see `istio_operator.tf` for supported values | `any` | `{}` | no |
Expand Down
1 change: 1 addition & 0 deletions modules/aws/flux2.tf
8 changes: 8 additions & 0 deletions modules/aws/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ terraform {
source = "gavinbunney/kubectl"
version = "~> 1.0"
}
flux = {
source = "fluxcd/flux"
version = "~> 0.0"
}
github = {
source = "integrations/github"
version = "~> 4.5"
}
}
}
14 changes: 14 additions & 0 deletions modules/scaleway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ User guides, feature documentation and examples are available [here](https://git
| Name | Version |
|------|---------|
| terraform | >= 0.13 |
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
Expand All @@ -28,11 +30,14 @@ User guides, feature documentation and examples are available [here](https://git

| Name | Version |
|------|---------|
| flux | ~> 0.0 |
| github | ~> 4.5 |
| helm | ~> 2.0 |
| kubectl | ~> 1.0 |
| kubernetes | ~> 2.0 |
| random | n/a |
| time | n/a |
| tls | n/a |

## Modules

Expand All @@ -42,7 +47,14 @@ No Modules.

| Name |
|------|
| [flux_install](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/install) |
| [flux_sync](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/data-sources/sync) |
| [github_branch_default](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default) |
| [github_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) |
| [github_repository_deploy_key](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key) |
| [github_repository_file](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file) |
| [helm_release](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) |
| [kubectl_file_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/file_documents) |
| [kubectl_manifest](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) |
| [kubectl_path_documents](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/data-sources/path_documents) |
| [kubernetes_namespace](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) |
Expand All @@ -53,6 +65,7 @@ No Modules.
| [kubernetes_secret](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret) |
| [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) |
| [time_sleep](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) |
| [tls_private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) |

## Inputs

Expand All @@ -64,6 +77,7 @@ No Modules.
| cluster-name | Name of the Kubernetes cluster | `string` | `"sample-cluster"` | no |
| external-dns | Map of map for external-dns configuration: see `external_dns.tf` for supported values | `any` | `{}` | no |
| flux | Customize Flux chart, see `flux.tf` for supported values | `any` | `{}` | no |
| flux2 | Customize Flux chart, see `flux2.tf` for supported values | `any` | `{}` | no |
| helm\_defaults | Customize default Helm behavior | `any` | `{}` | no |
| ingress-nginx | Customize ingress-nginx chart, see `nginx-ingress.tf` for supported values | `any` | `{}` | no |
| istio-operator | Customize istio operator deployment, see `istio_operator.tf` for supported values | `any` | `{}` | no |
Expand Down
Loading