Skip to content

Commit 8b0cd83

Browse files
committed
add binary checks, add tests
1 parent d147183 commit 8b0cd83

File tree

14 files changed

+296
-24
lines changed

14 files changed

+296
-24
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ credentials.json
5050

5151
# File to populate env vars used by Docker test runs
5252
.envrc
53+
54+
# ignore generated ASM yamls in /workspace/test/fixtures/simple_regional_with_asm as it is a test
55+
# in a production scenario these files are expected to be checked in
56+
/test/fixtures/simple_regional_with_asm/asm-dir

.kitchen.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,13 @@ suites:
221221
systems:
222222
- name: sandbox_enabled
223223
backend: local
224+
- name: "simple_regional_with_asm"
225+
driver:
226+
root_module_directory: test/fixtures/simple_regional_with_asm
227+
verifier:
228+
systems:
229+
- name: simple_regional_with_asm
230+
backend: local
231+
controls:
232+
- gcloud
233+
- kubectl

build/int.cloudbuild.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,26 @@ steps:
384384
- verify workload-identity-local
385385
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
386386
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy workload-identity-local']
387+
- id: create simple-regional-with-asm-local
388+
waitFor:
389+
- prepare
390+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
391+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create simple-regional-with-asm-local']
392+
- id: converge simple-regional-with-asm-local
393+
waitFor:
394+
- create simple-regional-with-asm-local
395+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
396+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge simple-regional-with-asm-local']
397+
- id: verify simple-regional-with-asm-local
398+
waitFor:
399+
- converge simple-regional-with-asm-local
400+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
401+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify simple-regional-with-asm-local']
402+
- id: destroy simple-regional-with-asm-local
403+
waitFor:
404+
- verify simple-regional-with-asm-local
405+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
406+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy simple-regional-with-asm-local']
387407
tags:
388408
- 'ci'
389409
- 'integration'

examples/simple_regional_with_asm/main.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ module "gke" {
3232
project_id = var.project_id
3333
name = "${local.cluster_type}-cluster${var.cluster_name_suffix}"
3434
regional = true
35-
#TESTING
3635
release_channel = "REGULAR"
3736
region = var.region
3837
network = var.network
@@ -44,15 +43,19 @@ module "gke" {
4443
node_pools = [
4544
{
4645
name = "asm-node-pool"
46+
autoscaling = false
47+
# ASM requires minimum 4 nodes and n1-standard-4
48+
# As this is a regional cluster we have node_count * 3 = 6 nodes
49+
node_count = 2
4750
machine_type = "n1-standard-4"
48-
min_count = 6
4951
},
5052
]
5153
}
5254

5355
module "asm" {
5456
source = "../../modules/asm"
5557
cluster_name = module.gke.name
58+
cluster_endpoint = module.gke.endpoint
5659
project_id = var.project_id
5760
location = module.gke.location
5861
}

modules/asm/main.tf

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17+
data "google_container_cluster" "primary" {
18+
name = var.cluster_name
19+
project = var.project_id
20+
location = var.location
21+
}
22+
23+
data "google_client_config" "default" {
24+
}
25+
1726
module "asm_install" {
1827
source = "terraform-google-modules/gcloud/google"
1928
version = "~> 1.0"
29+
module_depends_on = [var.cluster_endpoint]
2030

2131
platform = "linux"
22-
gcloud_sdk_version = "292.0.0"
32+
gcloud_sdk_version = "293.0.0"
2333
skip_download = var.skip_gcloud_download
2434
upgrade = false
2535
use_tf_google_credentials_env_var = true
2636
additional_components = ["kubectl", "kpt", "anthoscli", "alpha"]
2737

2838
create_cmd_entrypoint = "${path.module}/scripts/install_asm.sh"
29-
create_cmd_body = "${var.project_id} ${var.cluster_name} ${var.location} ${var.asm_release_channel}"
30-
destroy_cmd_entrypoint = "gcloud"
31-
destroy_cmd_body = "version"
39+
create_cmd_body = "${var.project_id} ${var.cluster_name} ${var.location}"
40+
destroy_cmd_entrypoint = "${path.module}/scripts/kubectl_wrapper.sh"
41+
destroy_cmd_body = "https://${var.cluster_endpoint} ${data.google_client_config.default.access_token} ${data.google_container_cluster.primary.master_auth.0.cluster_ca_certificate} kubectl delete ns istio-system"
3242
}
3343

3444
resource "google_service_account" "gke_hub_sa" {
@@ -51,7 +61,7 @@ module "gke_hub_registration" {
5161
version = "~> 1.0"
5262

5363
platform = "linux"
54-
gcloud_sdk_version = "292.0.0"
64+
gcloud_sdk_version = "293.0.0"
5565
skip_download = var.skip_gcloud_download
5666
upgrade = false
5767
use_tf_google_credentials_env_var = true

modules/asm/scripts/gke_hub_registration.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ SERVICE_ACCOUNT_KEY=$4
2727

2828
#write temp key, cleanup at exit
2929
tmp_file=$(mktemp)
30+
# shellcheck disable=SC2064
3031
trap "rm -rf $tmp_file" EXIT
31-
echo ${SERVICE_ACCOUNT_KEY} | base64 --decode > $tmp_file
32+
echo "${SERVICE_ACCOUNT_KEY}" | base64 --decode > "$tmp_file"
3233

33-
gcloud container hub memberships register ${MEMBERSHIP_NAME} --gke-cluster=${CLUSTER_LOCATION}/${CLUSTER_NAME} --service-account-key-file=${tmp_file} --quiet
34+
gcloud container hub memberships register "${MEMBERSHIP_NAME}" --gke-cluster="${CLUSTER_LOCATION}"/"${CLUSTER_NAME}" --service-account-key-file="${tmp_file}" --quiet

modules/asm/scripts/install_asm.sh

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,52 @@
1515

1616
set -e
1717

18-
if [ "$#" -lt 4 ]; then
18+
if [ "$#" -lt 3 ]; then
1919
>&2 echo "Not all expected arguments set."
2020
exit 1
2121
fi
2222

2323
PROJECT_ID=$1
2424
CLUSTER_NAME=$2
2525
CLUSTER_LOCATION=$3
26-
ASM_CHANNEL=$4
2726
ASM_RESOURCES="asm-dir"
2827
BASE_DIR="asm-base-dir"
28+
# check for needed binaries
29+
# kustomize is a requirement for installing ASM and is not available via gcloud. Safely exit if not available.
30+
if [[ -z $(command -v kustomize) ]]; then
31+
echo "kustomize is unavailable. Skipping ASM installation. Please install kustomize, add to PATH and rerun terraform apply."
32+
exit 1
33+
fi
34+
# check docker which is optionally used for validating asm yaml using gcr.io/kustomize-functions/validate-asm:v0.1.0
35+
if [[ $(command -v docker) ]]; then
36+
echo "Docker is available. ASM yaml validation will be performed."
37+
else
38+
echo "ASM yaml validation will be skipped as Docker is unavailable"
39+
SKIP_ASM_VALIDATION=true
40+
fi
2941
mkdir -p $ASM_RESOURCES
3042
pushd $ASM_RESOURCES
31-
gcloud config set project ${PROJECT_ID}
43+
gcloud config set project "${PROJECT_ID}"
3244
if [[ -d ./asm-patch ]]; then
3345
echo "ASM patch directory exists. Skipping download..."
3446
else
3547
echo "Downloading ASM patch"
3648
kpt pkg get https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages.git/[email protected] .
3749
fi
38-
anthoscli export -c ${CLUSTER_NAME} -o ${BASE_DIR} -p ${PROJECT_ID} -l ${CLUSTER_LOCATION}
50+
anthoscli export -c "${CLUSTER_NAME}" -o ${BASE_DIR} -p "${PROJECT_ID}" -l "${CLUSTER_LOCATION}"
3951
kpt cfg set asm-patch/ base-dir ../${BASE_DIR}
40-
kpt cfg set asm-patch/ gcloud.core.project ${PROJECT_ID}
41-
kpt cfg set asm-patch/ gcloud.container.cluster ${CLUSTER_NAME}
42-
kpt cfg set asm-patch/ gcloud.compute.location ${CLUSTER_LOCATION}
52+
kpt cfg set asm-patch/ gcloud.core.project "${PROJECT_ID}"
53+
kpt cfg set asm-patch/ gcloud.container.cluster "${CLUSTER_NAME}"
54+
kpt cfg set asm-patch/ gcloud.compute.location "${CLUSTER_LOCATION}"
4355
kpt cfg list-setters asm-patch/
44-
pushd ${BASE_DIR} && kustomize create --autodetect --namespace ${PROJECT_ID} && popd
56+
pushd ${BASE_DIR} && kustomize create --autodetect --namespace "${PROJECT_ID}" && popd
4557
pushd asm-patch && kustomize build -o ../${BASE_DIR}/all.yaml && popd
46-
kpt fn source ${BASE_DIR} | kpt fn run --image gcr.io/kustomize-functions/validate-asm:v0.1.0
58+
# skip validate as we should investigate if we can check this without having to resort to dind
59+
if [[ ${SKIP_ASM_VALIDATION} ]]; then
60+
echo "Skipping ASM validation..."
61+
else
62+
echo "Running ASM validation..."
63+
kpt fn source ${BASE_DIR} | kpt fn run --image gcr.io/kustomize-functions/validate-asm:v0.1.0
64+
fi
4765
anthoscli apply -f ${BASE_DIR}
4866
kubectl wait --for=condition=available --timeout=600s deployment --all -n istio-system
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
set -e
18+
19+
if [ "$#" -lt 3 ]; then
20+
>&2 echo "Not all expected arguments set."
21+
exit 1
22+
fi
23+
24+
HOST=$1
25+
TOKEN=$2
26+
CA_CERTIFICATE=$3
27+
28+
shift 3
29+
30+
RANDOM_ID="${RANDOM}_${RANDOM}"
31+
export TMPDIR="/tmp/kubectl_wrapper_${RANDOM_ID}"
32+
33+
function cleanup {
34+
rm -rf "${TMPDIR}"
35+
}
36+
trap cleanup EXIT
37+
38+
mkdir "${TMPDIR}"
39+
40+
export KUBECONFIG="${TMPDIR}/config"
41+
42+
# shellcheck disable=SC1117
43+
base64 --help | grep "\--decode" && B64_ARG="--decode" || B64_ARG="-d"
44+
echo "${CA_CERTIFICATE}" | base64 ${B64_ARG} > "${TMPDIR}/ca_certificate"
45+
46+
kubectl config set-cluster kubectl-wrapper --server="${HOST}" --certificate-authority="${TMPDIR}/ca_certificate" --embed-certs=true 1>/dev/null
47+
rm -f "${TMPDIR}/ca_certificate"
48+
kubectl config set-context kubectl-wrapper --cluster=kubectl-wrapper --user=kubectl-wrapper --namespace=default 1>/dev/null
49+
kubectl config set-credentials kubectl-wrapper --token="${TOKEN}" 1>/dev/null
50+
kubectl config use-context kubectl-wrapper 1>/dev/null
51+
kubectl version 1>/dev/null
52+
53+
"$@"

modules/asm/variables.tf

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ variable "cluster_name" {
1919
type = string
2020
}
2121

22+
variable "cluster_endpoint" {
23+
description = "The GKE cluster endpoint."
24+
type = string
25+
}
26+
2227
variable "project_id" {
2328
description = "The project in which the resource belongs."
2429
type = string
@@ -35,12 +40,6 @@ variable "skip_gcloud_download" {
3540
default = true
3641
}
3742

38-
variable "asm_release_channel" {
39-
description = "ASM Release Channel (REGULAR/RAPID/STABLE)"
40-
type = string
41-
default = "REGULAR"
42-
}
43-
4443
variable "enable_gke_hub_registration" {
4544
description = "Enables GKE Hub Registration when set to true"
4645
type = bool

test/fixtures/simple_regional_with_asm/example.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
data "google_project" "project" {
18+
project_id = var.project_ids[2]
19+
}
20+
1721
module "example" {
1822
source = "../../../examples/simple_regional_with_asm"
1923

test/fixtures/simple_regional_with_asm/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ output "service_account" {
7878
description = "The service account to default running nodes as if not overridden in `node_pools`."
7979
value = module.example.service_account
8080
}
81+
82+
output "project_number" {
83+
description = "The project number of the ASM project"
84+
value = data.google_project.project.number
85+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
project_id = attribute('project_id')
16+
project_number = attribute('project_number')
17+
location = attribute('location')
18+
cluster_name = attribute('cluster_name')
19+
k8s_service_account_email = attribute('k8s_service_account_email')
20+
k8s_service_account_name = attribute('k8s_service_account_name')
21+
22+
control "gcloud" do
23+
title "Google Compute Engine GKE configuration"
24+
describe command("gcloud beta --project=#{project_id} container clusters --region=#{location} describe #{cluster_name} --format=json") do
25+
its(:exit_status) { should eq 0 }
26+
its(:stderr) { should eq '' }
27+
28+
let!(:data) do
29+
if subject.exit_status == 0
30+
JSON.parse(subject.stdout)
31+
else
32+
{}
33+
end
34+
end
35+
36+
describe "mesh id" do
37+
it "is correct" do
38+
expect(data['resourceLabels']["mesh_id"]).to eq "proj-#{project_number}"
39+
end
40+
end
41+
end
42+
43+
describe command("gcloud container hub memberships describe gke-asm-membership --format=json") do
44+
its(:exit_status) { should eq 0 }
45+
its(:stderr) { should eq '' }
46+
47+
let!(:hub) do
48+
if subject.exit_status == 0
49+
JSON.parse(subject.stdout)
50+
else
51+
{}
52+
end
53+
end
54+
it "membership has expected gke cluster" do
55+
expect(hub['endpoint']["gkeCluster"]["resourceLink"]).to eq "//container.googleapis.com/projects/#{project_id}/locations/#{location}/clusters/#{cluster_name}"
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)