From 84e477a6e438ff1c992e288c3c4ec190a21a6739 Mon Sep 17 00:00:00 2001 From: Nandaja Varma Date: Thu, 23 Jun 2022 09:47:08 +0000 Subject: [PATCH] add DNS code --- .werft/installer-tests.ts | 7 +- install/infra/terraform/aks/output.tf | 34 ++- install/infra/terraform/eks/dns.tf | 76 +++++++ install/infra/terraform/eks/kubernetes.tf | 203 ------------------ install/infra/terraform/eks/network.tf | 103 --------- install/infra/terraform/eks/output.tf | 30 +++ install/infra/terraform/eks/variables.tf | 24 --- .../terraform/tools/external-dns/main.tf | 40 +--- install/tests/Makefile | 58 ++--- install/tests/main.tf | 45 ++-- 10 files changed, 191 insertions(+), 429 deletions(-) create mode 100644 install/infra/terraform/eks/dns.tf delete mode 100644 install/infra/terraform/eks/kubernetes.tf delete mode 100644 install/infra/terraform/eks/network.tf create mode 100644 install/infra/terraform/eks/output.tf delete mode 100644 install/infra/terraform/eks/variables.tf diff --git a/.werft/installer-tests.ts b/.werft/installer-tests.ts index bee652d807b197..c1432ba1fd52ef 100644 --- a/.werft/installer-tests.ts +++ b/.werft/installer-tests.ts @@ -214,7 +214,7 @@ const INFRA_PHASES: { [name: string]: InfraConfig } = { }, DESTROY: { phase: "destroy", - makeTarget: "cleanup", + makeTarget: `cleanup cloud=${cloud}`, description: "Destroy the created infrastucture", }, RESULTS: { @@ -279,7 +279,10 @@ function cleanup() { const phase = "destroy-infrastructure"; werft.phase(phase, "Destroying all the created resources"); - const response = exec(`make -C ${makefilePath} cleanup`, { slice: "run-terrafrom-destroy", dontCheckRc: true }); + const response = exec(`make -C ${makefilePath} cleanup cloud=${cloud}`, { + slice: "run-terrafrom-destroy", + dontCheckRc: true, + }); // if the destroy command fail, we check if any resources are pending to be removed // if nothing is yet to be cleaned, we return with success diff --git a/install/infra/terraform/aks/output.tf b/install/infra/terraform/aks/output.tf index 0ea0d4bf2f8b0e..83c60ee2205c81 100644 --- a/install/infra/terraform/aks/output.tf +++ b/install/infra/terraform/aks/output.tf @@ -36,14 +36,32 @@ output "external_dns_secrets" { } output "external_dns_settings" { - value = { - provider = "azure" - "azure.resourceGroup" = azurerm_resource_group.gitpod.name - "azure.subscriptionId" = data.azurerm_client_config.current.subscription_id - "azure.tenantId" = data.azurerm_client_config.current.tenant_id - "azure.useManagedIdentityExtension" = true - "azure.userAssignedIdentityID" = azurerm_kubernetes_cluster.k8s.kubelet_identity.0.client_id - } + value = [ + { + "name": "provider", + "value": "azure" + }, + { + "name": "azure.resourceGroup", + "value": azurerm_resource_group.gitpod.name, + }, + { + "name": "azure.subscriptionId", + "value": data.azurerm_client_config.current.subscription_id, + }, + { + "name": "azure.tenantId", + "value": data.azurerm_client_config.current.tenant_id, + }, + { + "name": "azure.useManagedIdentityExtension", + "value": true + }, + { + "name": "azure.userAssignedIdentityID", + "value": azurerm_kubernetes_cluster.k8s.kubelet_identity.0.client_id + }, + ] } output "k8s_connection" { diff --git a/install/infra/terraform/eks/dns.tf b/install/infra/terraform/eks/dns.tf new file mode 100644 index 00000000000000..ce4d78201c4740 --- /dev/null +++ b/install/infra/terraform/eks/dns.tf @@ -0,0 +1,76 @@ +variable "domain_name" {} +variable "cluster_name" {} + +terraform { + required_providers { + aws = { + version = " ~> 3.0" + source = "registry.terraform.io/hashicorp/aws" + } + } +} + +provider "aws" { + region = "eu-west-1" +} + +resource "aws_route53_zone" "gitpod" { + name = var.domain_name + + tags = { + Environment = "test" + } +} + +resource "aws_iam_policy" "gitpod" { + name = "role-${var.cluster_name}" + + # Terraform's "jsonencode" function converts a + # Terraform expression result to valid JSON syntax. + policy = jsonencode({ + Version = "2012-10-17", + Statement = [ + { + Effect = "Allow", + Action = [ + "route53:ChangeResourceRecordSets" + ], + Resource = [ + "arn:aws:route53:::hostedzone/*" + ] + }, + { + Effect = "Allow", + Action = [ + "route53:ListHostedZones", + "route53:ListResourceRecordSets" + ], + Resource = [ "*" ] + } + ], + }) +} + +resource "aws_iam_role" "gitpod" { + name = "iam-route53-${var.cluster_name}" + + assume_role_policy = < tmp_5_config.yml yq m -i tmp_config.yml tmp_5_config.yml -get-config-azure-storage: - export PASSWORD=$$(terraform output -json storage | yq r - 'password') && \ - export USERNAME=$$(terraform output -json storage | yq r - 'username') && \ - envsubst < ./manifests/kots-config-azure-storage.yaml > tmp_2_config.yml - yq m -i tmp_config.yml tmp_2_config.yml - -get-config-azure-db: - export DBHOST=$$(terraform output -json database | yq r - 'host') && \ - export DBPASS=$$(terraform output -json database | yq r - 'password') && \ - export DBUSER=$$(terraform output -json database | yq r - 'username') && \ - envsubst < ./manifests/kots-config-azure-db.yaml > tmp_2_config.yml - yq m -i tmp_config.yml tmp_2_config.yml - get-config-azure-registry: export SERVER=$$(terraform output -json registry | yq r - 'server') && \ export PASSWORD=$$(terraform output -json registry | yq r - 'password') && \ @@ -150,13 +137,6 @@ get-config-azure-db: envsubst < ./manifests/kots-config-azure-db.yaml > tmp_2_config.yml yq m -i tmp_config.yml tmp_2_config.yml -get-config-azure-registry: - export SERVER=$$(terraform output -json registry | yq r - 'server') && \ - export PASSWORD=$$(terraform output -json registry | yq r - 'password') && \ - export USERNAME=$$(terraform output -json registry | yq r - 'username') && \ - envsubst < ./manifests/kots-config-azure-registry.yaml > tmp_2_config.yml - yq m -i tmp_config.yml tmp_2_config.yml - storage ?= incluster registry ?= incluster db ?= incluster @@ -203,30 +183,30 @@ kots-uprgade: @echo "Upgrade gitpod KOTS app to latest" kubectl kots upstream upgrade --kubeconfig=${KUBECONFIG} gitpod -n gitpod --deploy -cleanup: destroy-gcp-externaldns destroy-gcpns destroy-aks-edns destroy-aks-issuer destroy-certmanager destroy-clusters +cloud ?= gcp +cleanup: destroy-$(cloud) select-workspace: terraform workspace select $(TF_VAR_TEST_ID) -destroy-gcp-externaldns: select-workspace +destroy-gcp: select-workspace ls ${KUBECONFIG} && terraform destroy -target=module.clouddns-externaldns -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" - -destroy-certmanager: select-workspace ls ${KUBECONFIG} && terraform destroy -target=module.certmanager -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" + terraform destroy -target=module.k3s -var kubeconfig=${KUBECONFIG} --auto-approve + terraform destroy -target=module.gke -var kubeconfig=${KUBECONFIG} --auto-approve -destroy-gcpns: select-workspace - terraform destroy -target=module.add_gcp_nameservers -var kubeconfig=${KUBECONFIG} --auto-approve - -destroy-edns: select-workspace - ls ${KUBECONFIG} && terraform destroy -target=module.externaldns -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" +destroy-aws: select-workspace + terraform destroy -target=module.aws-add-dns-record -var kubeconfig=${KUBECONFIG} --auto-approve + ls ${KUBECONFIG} && terraform destroy -target=module.aws-issuer -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" + ls ${KUBECONFIG} && terraform destroy -target=module.aws-externaldns -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" + ls ${KUBECONFIG} && terraform destroy -target=module.certmanager -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" + terraform destroy -target=module.eks -var kubeconfig=${KUBECONFIG} --auto-approve -destroy-aks-issuer: select-workspace +destroy-azure: select-workspace ls ${KUBECONFIG} && terraform destroy -target=module.azure-issuer -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" - -destroy-clusters: select-workspace - terraform destroy -target=module.gke -var kubeconfig=${KUBECONFIG} --auto-approve - terraform destroy -target=module.eks -var kubeconfig=${KUBECONFIG} --auto-approve - terraform destroy -target=module.k3s -var kubeconfig=${KUBECONFIG} --auto-approve + terraform destroy -target=module.azure-add-dns-record -var kubeconfig=${KUBECONFIG} --auto-approve + ls ${KUBECONFIG} && terraform destroy -target=module.azure-externaldns -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" + ls ${KUBECONFIG} && terraform destroy -target=module.certmanager -var kubeconfig=${KUBECONFIG} --auto-approve || echo "No kubeconfig file" [ -z "$$ARM_SUBSCRIPTION_ID" ] || terraform destroy -target=module.aks -var kubeconfig=${KUBECONFIG} --auto-approve get-results: diff --git a/install/tests/main.tf b/install/tests/main.tf index aca038e6b96f3a..86c1e116a605da 100644 --- a/install/tests/main.tf +++ b/install/tests/main.tf @@ -56,8 +56,8 @@ module "aks" { module "eks" { source = "../infra/terraform/eks" + domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" cluster_name = var.TEST_ID - kubeconfig = var.kubeconfig } module "certmanager" { @@ -75,25 +75,35 @@ module "clouddns-externaldns" { credentials = var.dns_sa_creds } -variable "cloud" { default = "azure" } +module "azure-externaldns" { + source = "../infra/terraform/tools/external-dns" + kubeconfig = var.kubeconfig + settings = module.aks.external_dns_settings + domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" + txt_owner_id = var.TEST_ID +} -module "externaldns" { - source = "../infra/terraform/tools/external-dns" - kubeconfig = var.kubeconfig - settings = module.aks.external_dns_settings - domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" - txt_owner_id = var.TEST_ID - cloud = var.cloud +module "aws-externaldns" { + source = "../infra/terraform/tools/external-dns" + kubeconfig = var.kubeconfig + settings = module.eks.external_dns_settings + domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" + txt_owner_id = var.TEST_ID } module "azure-issuer" { - source = "../infra/terraform/tools/issuer/azure" - kubeconfig = var.kubeconfig + source = "../infra/terraform/tools/issuer/azure" + kubeconfig = var.kubeconfig cert_manager_issuer = module.aks.cert_manager_issuer } -module "add_gcp_nameservers" { - # source = "github.com/gitpod-io/gitpod//install/infra/terraform/tools/cloud-dns-ns?ref=main" +module "aws-issuer" { + source = "../infra/terraform/tools/issuer/azure" + kubeconfig = var.kubeconfig + cert_manager_issuer = module.eks.cert_manager_issuer +} + +module "azure-add-dns-record" { source = "../infra/terraform/tools/cloud-dns-ns" credentials = var.dns_sa_creds nameservers = module.aks.domain_nameservers @@ -101,3 +111,12 @@ module "add_gcp_nameservers" { managed_dns_zone = "gitpod-self-hosted-com" domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" } + +module "aws-add-dns-record" { + source = "../infra/terraform/tools/cloud-dns-ns" + credentials = var.dns_sa_creds + nameservers = module.eks.domain_nameservers + dns_project = "dns-for-playgrounds" + managed_dns_zone = "gitpod-self-hosted-com" + domain_name = "${var.TEST_ID}.gitpod-self-hosted.com" +}