Skip to content

Commit b0a97a4

Browse files
iQQBotroboquat
authored andcommitted
new dns terraform
1 parent 112ffbf commit b0a97a4

File tree

14 files changed

+138
-217
lines changed

14 files changed

+138
-217
lines changed

.werft/build.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import { exec, ExecOptions } from './util/shell';
55
import { Werft } from './util/werft';
66
import { waitForDeploymentToSucceed, wipeAndRecreateNamespace, setKubectlContextNamespace, deleteNonNamespaceObjects, findFreeHostPorts, createNamespace, helmInstallName } from './util/kubectl';
7-
import { newIssueCertficate, installCertficate, IssueCertificateParams, InstallCertificateParams } from './util/certs';
7+
import { issueCertficate, installCertficate, IssueCertificateParams, InstallCertificateParams } from './util/certs';
88
import { reportBuildFailureInSlack } from './util/slack';
99
import * as semver from 'semver';
1010
import * as util from 'util';
@@ -77,6 +77,7 @@ const installerSlices = {
7777
INSTALLER_POST_PROCESSING: "installer post processing",
7878
APPLY_INSTALL_MANIFESTS: "installer apply",
7979
DEPLOYMENT_WAITING: "monitor server deployment",
80+
DNS_ADD_RECORD: "add dns record"
8081
}
8182

8283
const vmSlices = {
@@ -303,6 +304,8 @@ export async function build(context, version) {
303304
withVM,
304305
};
305306

307+
exec(`kubectl --namespace keys get secret host-key -o yaml > /workspace/host-key.yaml`)
308+
306309
if (withVM) {
307310
werft.phase(phases.VM, "Start VM");
308311

@@ -431,6 +434,7 @@ export async function deployToDevWithInstaller(deploymentConfig: DeploymentConfi
431434
// in a VM, the secrets have alreay been copied
432435
// If using core-dev, we want to execute further kubectl operations only in the created namespace
433436
setKubectlContextNamespace(namespace, metaEnv({ slice: installerSlices.SET_CONTEXT }));
437+
werft.done(installerSlices.SET_CONTEXT)
434438
try {
435439
werft.log(installerSlices.ISSUE_CERTIFICATES, "organizing a certificate for the preview environment...");
436440

@@ -535,7 +539,7 @@ export async function deployToDevWithInstaller(deploymentConfig: DeploymentConfi
535539

536540
werft.log("SSH gateway hostkey", "copy host-key from secret")
537541
try {
538-
exec(`kubectl --namespace keys get secret host-key -o yaml \
542+
exec(`cat /workspace/host-key.yaml \
539543
| yq w - metadata.namespace ${namespace} \
540544
| yq d - metadata.uid \
541545
| yq d - metadata.resourceVersion \
@@ -610,6 +614,8 @@ export async function deployToDevWithInstaller(deploymentConfig: DeploymentConfi
610614
werft.fail(installerSlices.DEPLOYMENT_WAITING, err);
611615
}
612616

617+
await addDNSRecord(deploymentConfig.namespace, deploymentConfig.domain, !withVM)
618+
613619
// TODO: Fix sweeper, it does not appear to be doing clean-up
614620
werft.log('sweeper', 'installing Sweeper');
615621
const sweeperVersion = deploymentConfig.sweeperImage.split(":")[1];
@@ -695,7 +701,7 @@ export async function deployToDevWithHelm(deploymentConfig: DeploymentConfig, wo
695701
await issueMetaCerts(namespace, domain);
696702
await installMetaCertificates(namespace);
697703
werft.done('certificate');
698-
704+
await addDNSRecord(deploymentConfig.namespace, deploymentConfig.domain, false)
699705
werft.done('prep');
700706
} catch (err) {
701707
werft.fail('prep', err);
@@ -893,10 +899,50 @@ export async function deployToDevWithHelm(deploymentConfig: DeploymentConfig, wo
893899
}
894900
}
895901

902+
async function addDNSRecord(namespace: string, domain: string, isLoadbalancer: boolean) {
903+
let wsProxyLBIP = null
904+
if (isLoadbalancer === true) {
905+
werft.log(installerSlices.DNS_ADD_RECORD, "Getting ws-proxy loadbalancer IP");
906+
for (let i = 0; i < 60; i++) {
907+
try {
908+
let lb = exec(`kubectl -n ${namespace} get service ws-proxy -o=jsonpath='{.status.loadBalancer.ingress[0].ip}'`, { silent: true })
909+
if (lb.length > 4) {
910+
wsProxyLBIP = lb
911+
break
912+
}
913+
await sleep(1000)
914+
} catch (err) {
915+
await sleep(1000)
916+
}
917+
}
918+
if (wsProxyLBIP == null) {
919+
werft.fail(installerSlices.DNS_ADD_RECORD, new Error("Can't get ws-proxy loadbalancer IP"));
920+
}
921+
werft.log(installerSlices.DNS_ADD_RECORD, "Get ws-proxy loadbalancer IP: " + wsProxyLBIP);
922+
} else {
923+
wsProxyLBIP = getCoreDevIngressIP()
924+
}
925+
926+
var cmd = `set -x \
927+
&& cd /workspace/.werft/dns \
928+
&& rm -rf .terraform* \
929+
&& export GOOGLE_APPLICATION_CREDENTIALS="${GCLOUD_SERVICE_ACCOUNT_PATH}" \
930+
&& terraform init -backend-config='prefix=${namespace}' -migrate-state -upgrade \
931+
&& terraform apply -auto-approve \
932+
-var 'dns_zone_domain=gitpod-dev.com' \
933+
-var 'domain=${domain}' \
934+
-var 'ingress_ip=${getCoreDevIngressIP()}' \
935+
-var 'ws_proxy_ip=${wsProxyLBIP}'`;
936+
937+
werft.log(installerSlices.DNS_ADD_RECORD, "Terraform command for create dns record: " + cmd)
938+
exec(cmd, { ...metaEnv(), slice: installerSlices.DNS_ADD_RECORD });
939+
werft.done(installerSlices.DNS_ADD_RECORD);
940+
}
941+
896942
export async function issueMetaCerts(namespace: string, domain: string) {
897943
let additionalSubdomains: string[] = ["", "*.", "*.ws-dev."]
898944
var metaClusterCertParams = new IssueCertificateParams();
899-
metaClusterCertParams.pathToTerraform = "/workspace/.werft/certs";
945+
metaClusterCertParams.pathToTemplate = "/workspace/.werft/util/templates";
900946
metaClusterCertParams.gcpSaPath = GCLOUD_SERVICE_ACCOUNT_PATH;
901947
metaClusterCertParams.namespace = namespace;
902948
metaClusterCertParams.certNamespace = "certs";
@@ -905,7 +951,7 @@ export async function issueMetaCerts(namespace: string, domain: string) {
905951
metaClusterCertParams.ip = getCoreDevIngressIP();
906952
metaClusterCertParams.bucketPrefixTail = ""
907953
metaClusterCertParams.additionalSubdomains = additionalSubdomains
908-
await newIssueCertficate(werft, metaClusterCertParams, metaEnv());
954+
await issueCertficate(werft, metaClusterCertParams, metaEnv());
909955
}
910956

911957
async function installMetaCertificates(namespace: string) {

.werft/certs/cert/main.tf

Lines changed: 0 additions & 57 deletions
This file was deleted.

.werft/certs/cert/outputs.tf

Lines changed: 0 additions & 3 deletions
This file was deleted.

.werft/certs/cert/variables.tf

Lines changed: 0 additions & 46 deletions
This file was deleted.

.werft/certs/cert/versions.tf

Lines changed: 0 additions & 16 deletions
This file was deleted.

.werft/certs/main.tf

Lines changed: 0 additions & 27 deletions
This file was deleted.

.werft/dns/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# https://www.terraform.io/docs/providers/google/guides/provider_reference.html
2+
provider "google" {
3+
project = "gitpod-dev"
4+
region = "europe-west-3"
5+
# Relies on GOOGLE_APPLICATION_CREDENTIALS pointing to the service account file
6+
}
7+
8+
# Added for compatibility with old branches, can be deleted if compatibility is not needed
9+
provider "kubectl" {
10+
load_config_file = true
11+
}
12+
13+
locals {
14+
# As we did create the zone and IP manually beforehand: have the zone name statically determined
15+
dns_zone_name = replace(trimsuffix(var.dns_zone_domain, ".-"), ".", "-")
16+
project = "gitpod-dev"
17+
region = "europe-west-3"
18+
}
19+
20+
#
21+
# DNS records
22+
#
23+
24+
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set
25+
resource "google_dns_record_set" "gitpod" {
26+
count = length(var.ingress_subdomains)
27+
name = "${var.ingress_subdomains[count.index]}${var.domain}."
28+
type = "A"
29+
ttl = 300
30+
managed_zone = local.dns_zone_name
31+
rrdatas = [var.ingress_ip]
32+
project = local.project
33+
}
34+
resource "google_dns_record_set" "gitpod_ws" {
35+
name = "${var.ws_proxy_subdomain}${var.domain}."
36+
type = "A"
37+
ttl = 300
38+
managed_zone = local.dns_zone_name
39+
rrdatas = [var.ws_proxy_ip]
40+
project = local.project
41+
}
42+
43+
#
44+
# End
45+
#
46+
resource "null_resource" "done" {
47+
depends_on = [
48+
google_dns_record_set.gitpod,
49+
google_dns_record_set.gitpod_ws,
50+
]
51+
}
52+
53+
54+
output "done" {
55+
value = null_resource.done.id
56+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
variable "namespace" {
2-
type = string
3-
}
4-
51
# e.g.: gitpod-dev.com
62
variable "dns_zone_domain" {
73
type = string
@@ -12,16 +8,21 @@ variable "domain" {
128
type = string
139
}
1410

15-
# e.g.: ["", "*.", "*.ws."]
16-
variable "subdomains" {
11+
# e.g.: ["", "*.", "*.ws-dev."]
12+
variable "ingress_subdomains" {
1713
type = list(string)
14+
default = ["", "*."]
15+
}
16+
17+
variable "ws_proxy_subdomain" {
18+
type = string
19+
default = "*.ws-dev."
1820
}
1921

20-
variable "public_ip" {
22+
variable "ingress_ip" {
2123
type = string
2224
}
2325

24-
variable "cert_namespace" {
26+
variable "ws_proxy_ip" {
2527
type = string
26-
default = "certs"
2728
}

.werft/certs/versions.tf renamed to .werft/dns/versions.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ terraform {
77
source = "hashicorp/google"
88
version = "3.63.0"
99
}
10+
11+
# Added for compatibility with old branches, can be deleted if compatibility is not needed
1012
kubectl = {
1113
source = "gavinbunney/kubectl"
1214
version = "1.10.1"

.werft/post-process.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,17 @@ while [ "$i" -le "$DOCS" ]; do
216216
if [[ "ws-proxy" == "$NAME" ]] && [[ "$KIND" == "Service" ]]; then
217217
WORK="overrides for $NAME $KIND"
218218
echo "$WORK"
219-
yq w -i k8s.yaml -d "$i" "metadata.annotations[cloud.google.com/neg]" '{"exposed_ports": {"22":{}}}'
219+
# Provide harvester compatibility by adding ports instead of modifying the original ports
220+
yq w -i k8s.yaml -d "$i" "spec.ports[+].name" http-lb
221+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).port" 80
222+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).protocol" TCP
223+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).targetPort" 8080
224+
225+
yq w -i k8s.yaml -d "$i" "spec.ports[+].name" https-lb
226+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).port" 443
227+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).protocol" TCP
228+
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).targetPort" 9090
229+
yq w -i k8s.yaml -d "$i" "metadata.annotations[cloud.google.com/neg]" '{"exposed_ports": {"22":{},"80":{},"443":{}}}'
220230
yq w -i k8s.yaml -d "$i" spec.type LoadBalancer
221231
fi
222232

0 commit comments

Comments
 (0)