Skip to content

Commit a32973a

Browse files
authored
Cache the internal CR token to avoid jumbox reconciliations (#246)
1 parent cb1c3e5 commit a32973a

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ destroy_%:
210210

211211
.PHONY: destroy_tfstate
212212
destroy_tfstate:
213-
find . -name terraform.tfstate.d -exec rm -rf {} +
214-
find . -name terraform.tfstate -delete
213+
find . -name *tfstate* -exec rm -rf {} +
215214

216215
.PHONY: destroy_tfcache
217216
destroy_tfcache:

modules/aws/jumpbox/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ data "aws_availability_zones" "available" {}
230230
module "internal_registry" {
231231
source = "../../internal_registry"
232232
tsb_version = var.tsb_version
233+
# The internal registry token is needed only if the TSB version is a development version, and only once when the
234+
# jumpbox bootstraps the first time. It is not needed later as all images are already pushed to the registry (and
235+
# cloud-init won't run again anyway).
236+
# Since the token is short-lived, successive calls to this module would cause the jumpbox to reconcile, restart, and
237+
# eventually changing the IP address, etc, unnecessarily.
238+
# By setting this, subsequent calls to this module will return the token returned on the initial run, if present, avoiding
239+
# the jumbox reconcile.
240+
cached_by = "${var.name_prefix}-internal-registry.tfstate.tokencache"
233241
}
234242

235243
resource "aws_instance" "jumpbox" {

modules/azure/jumpbox/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ resource "tls_private_key" "generated" {
9696
module "internal_registry" {
9797
source = "../../internal_registry"
9898
tsb_version = var.tsb_version
99+
# The internal registry token is needed only if the TSB version is a development version, and only once when the
100+
# jumpbox bootstraps the first time. It is not needed later as all images are already pushed to the registry (and
101+
# cloud-init won't run again anyway).
102+
# Since the token is short-lived, successive calls to this module would cause the jumpbox to reconcile, restart, and
103+
# eventually changing the IP address, etc, unnecessarily.
104+
# By setting this, subsequent calls to this module will return the token returned on the initial run, if present, avoiding
105+
# the jumbox reconcile.
106+
cached_by = "${var.name_prefix}-internal-registry.tfstate.tokencache"
99107
}
100108

101109
resource "azurerm_linux_virtual_machine" "jumpbox" {

modules/gcp/jumpbox/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ data "google_compute_default_service_account" "default" {
2626
module "internal_registry" {
2727
source = "../../internal_registry"
2828
tsb_version = var.tsb_version
29+
# The internal registry token is needed only if the TSB version is a development version, and only once when the
30+
# jumpbox bootstraps the first time. It is not needed later as all images are already pushed to the registry (and
31+
# cloud-init won't run again anyway).
32+
# Since the token is short-lived, successive calls to this module would cause the jumpbox to reconcile, restart, and
33+
# eventually changing the IP address, etc, unnecessarily.
34+
# By setting this, subsequent calls to this module will return the token returned on the initial run, if present, avoiding
35+
# the jumbox reconcile.
36+
cached_by = "${var.name_prefix}-internal-registry.tfstate.tokencache"
2937
}
3038

3139
resource "google_compute_instance" "jumpbox" {

modules/internal_registry/internal-cr-token.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
set -e
44

5-
TSB_VERSION=$(jq -r '.tsb_version')
5+
eval "$(jq -r '@sh "TSB_VERSION=\(.tsb_version) CACHED_BY=\(.cached_by)"')"
6+
7+
# If a cached value was requested and is present, just return it
8+
if [[ -f "${CACHED_BY}" ]]; then
9+
cat "${CACHED_BY}"
10+
exit 0
11+
fi
612

713
if [[ "${TSB_VERSION}" =~ .*"-dev" ]]; then
814
TSB_GCR_INTERNAL_REGISTRY="gcr.io/tetrate-internal-containers"
915
TSB_GCR_INTERNAL_TOKEN=$(gcloud auth print-access-token)
10-
echo "{\"token\":\"${TSB_GCR_INTERNAL_TOKEN}\",\"registry\":\"${TSB_GCR_INTERNAL_REGISTRY}\"}"
16+
OUT="{\"token\":\"${TSB_GCR_INTERNAL_TOKEN}\",\"registry\":\"${TSB_GCR_INTERNAL_REGISTRY}\"}"
17+
else
18+
OUT="{\"token\":\"\",\"registry\":\"\"}"
19+
fi
20+
21+
if [[ -n "${CACHED_BY}" ]]; then
22+
echo "${OUT}" | tee "${CACHED_BY}"
1123
else
12-
echo "{\"token\":\"\",\"registry\":\"\"}"
24+
echo "${OUT}"
1325
fi

modules/internal_registry/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ data "external" "gcr_token" {
33
program = ["bash", "${path.module}/internal-cr-token.sh"]
44
query = {
55
"tsb_version" = var.tsb_version
6+
"cached_by" = var.cached_by
67
}
78
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
variable "tsb_version" {
22
}
3+
variable "cached_by" {
4+
default = ""
5+
}

0 commit comments

Comments
 (0)