Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.

Commit d99ec1c

Browse files
authored
Merge pull request #44 from gruntwork-io/yori-fix-windows
Force powershell on local-exec
2 parents 720588a + 76abd07 commit d99ec1c

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defaults: &defaults
55
environment:
66
GRUNTWORK_INSTALLER_VERSION: v0.0.21
77
TERRATEST_LOG_PARSER_VERSION: v0.13.13
8-
KUBERGRUNT_VERSION: v0.5.1
8+
KUBERGRUNT_VERSION: v0.5.8
99
HELM_VERSION: v2.12.2
1010
MODULE_CI_VERSION: v0.14.1
1111
TERRAFORM_VERSION: 0.12.11

examples/k8s-tiller-kubergrunt-minikube/main.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ module "tiller" {
9696
# successfully deployed and up at that point.
9797
resource "null_resource" "wait_for_tiller" {
9898
provisioner "local-exec" {
99+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
100+
99101
command = <<-EOF
100102
${module.require_executables.executables["kubergrunt"]} helm wait-for-tiller ${local.esc_newl}
101103
--tiller-namespace ${module.tiller_namespace.name} ${local.esc_newl}
@@ -114,6 +116,8 @@ resource "null_resource" "grant_helm_access" {
114116
depends_on = [null_resource.wait_for_tiller]
115117

116118
provisioner "local-exec" {
119+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
120+
117121
command = <<-EOF
118122
${module.require_executables.executables["kubergrunt"]} helm grant ${local.esc_newl}
119123
--tiller-namespace ${module.tiller_namespace.name} ${local.esc_newl}
@@ -143,7 +147,8 @@ locals {
143147

144148
configure_args = var.helm_client_rbac_user != "" ? "--rbac-user ${var.helm_client_rbac_user}" : var.helm_client_rbac_group != "" ? "--rbac-group ${var.helm_client_rbac_group}" : var.helm_client_rbac_service_account != "" ? "--rbac-service-account ${var.helm_client_rbac_service_account}" : ""
145149

146-
esc_newl = module.os.name == "Windows" ? "`" : "\\"
150+
is_windows = module.os.name == "Windows"
151+
esc_newl = local.is_windows ? "`" : "\\"
147152
}
148153

149154
module "os" {

modules/k8s-tiller/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ https://www.terraform.io/docs/providers/tls/r/cert_request.html#common_name for
186186
https://github.com/gruntwork-io/kubergrunt for installation instructions for `kubergrunt`, and
187187
https://kubernetes.io/docs/tasks/tools/install-kubectl/ for installation instructions for `kubectl`.**
188188

189+
**NOTE: You must have kubergrunt version >=0.5.8**
190+
189191
This method of configuring the TLS certs utilizes [kubergrunt](https://github.com/gruntwork-io/kubergrunt) to generate
190192
the TLS CA, and a signed certificate key pair for Tiller using that CA. To use this method, set `tiller_tls_gen_method`
191193
to `"kubergrunt"`.

modules/k8s-tiller/main.tf

+55-28
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ resource "null_resource" "tiller_tls_ca_certs" {
245245
depends_on = [null_resource.dependency_getter]
246246

247247
provisioner "local-exec" {
248+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
249+
248250
command = <<-EOF
249251
${lookup(module.require_executables.executables, "kubergrunt", "")} tls gen ${local.esc_newl}
250252
${local.kubergrunt_auth_params} ${local.esc_newl}
@@ -254,12 +256,11 @@ resource "null_resource" "tiller_tls_ca_certs" {
254256
--secret-label gruntwork.io/tiller-namespace=${var.namespace} ${local.esc_newl}
255257
--secret-label gruntwork.io/tiller-credentials=true ${local.esc_newl}
256258
--secret-label gruntwork.io/tiller-credentials-type=ca ${local.esc_newl}
257-
--tls-subject-json '${jsonencode(local.tiller_tls_ca_certs_subject)}' ${local.esc_newl}
259+
--tls-subject-json '${local.tiller_tls_ca_certs_subject_json_as_arg}' ${local.esc_newl}
258260
--tls-private-key-algorithm ${var.private_key_algorithm} ${local.esc_newl}
259261
${local.tls_algorithm_config}
260262
EOF
261263

262-
263264
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
264265
environment = {
265266
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -269,14 +270,13 @@ resource "null_resource" "tiller_tls_ca_certs" {
269270
}
270271

271272
provisioner "local-exec" {
272-
when = destroy
273+
when = destroy
274+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
273275

274276
command = <<-EOF
275-
${var.kubectl_server_endpoint != "" ? "echo \"$KUBECTL_CA_DATA\" > ${path.module}/kubernetes_server_ca.pem" : ""}
276-
${lookup(module.require_executables.executables, "kubectl", "")} ${local.esc_newl}
277-
${local.kubectl_auth_params} ${local.esc_newl}
278-
--namespace ${var.tiller_tls_ca_cert_secret_namespace} ${local.esc_newl}
279-
delete secret ${local.tiller_tls_ca_certs_secret_name}
277+
${lookup(module.require_executables.executables, "kubergrunt", "")} k8s kubectl ${local.esc_newl}
278+
${local.kubergrunt_auth_params} ${local.esc_newl}
279+
-- delete secret ${local.tiller_tls_ca_certs_secret_name} -n ${var.tiller_tls_ca_cert_secret_namespace}
280280
EOF
281281

282282
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
@@ -298,6 +298,8 @@ resource "null_resource" "tiller_tls_certs" {
298298
}
299299

300300
provisioner "local-exec" {
301+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
302+
301303
command = <<-EOF
302304
${lookup(module.require_executables.executables, "kubergrunt", "")} tls gen ${local.esc_newl}
303305
${local.kubergrunt_auth_params} ${local.esc_newl}
@@ -308,12 +310,11 @@ resource "null_resource" "tiller_tls_certs" {
308310
--secret-label gruntwork.io/tiller-namespace=${var.namespace} ${local.esc_newl}
309311
--secret-label gruntwork.io/tiller-credentials=true ${local.esc_newl}
310312
--secret-label gruntwork.io/tiller-credentials-type=server ${local.esc_newl}
311-
--tls-subject-json '${jsonencode(var.tiller_tls_subject)}' ${local.esc_newl}
313+
--tls-subject-json '${local.tiller_tls_subject_json_as_arg}' ${local.esc_newl}
312314
--tls-private-key-algorithm ${var.private_key_algorithm} ${local.esc_newl}
313315
${local.tls_algorithm_config}
314316
EOF
315317

316-
317318
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
318319
environment = {
319320
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -323,17 +324,15 @@ resource "null_resource" "tiller_tls_certs" {
323324
}
324325

325326
provisioner "local-exec" {
326-
when = destroy
327+
when = destroy
328+
interpreter = local.is_windows ? ["PowerShell", "-Command"] : ["bash", "-c"]
327329

328330
command = <<-EOF
329-
${var.kubectl_server_endpoint != "" ? "echo \"$KUBECTL_CA_DATA\" > ${path.module}/kubernetes_server_ca.pem" : ""}
330-
${lookup(module.require_executables.executables, "kubectl", "")} ${local.esc_newl}
331-
${local.kubectl_auth_params} ${local.esc_newl}
332-
--namespace ${var.namespace} ${local.esc_newl}
333-
delete secret ${local.tiller_tls_certs_secret_name}
331+
${lookup(module.require_executables.executables, "kubergrunt", "")} k8s kubectl ${local.esc_newl}
332+
${local.kubergrunt_auth_params} ${local.esc_newl}
333+
-- delete secret ${local.tiller_tls_certs_secret_name} -n ${var.namespace}
334334
EOF
335335

336-
337336
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
338337
environment = {
339338
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -419,35 +418,63 @@ locals {
419418
locals {
420419
generated_tls_secret_name = var.tiller_tls_gen_method == "none" ? var.tiller_tls_secret_name : local.tiller_tls_certs_secret_name
421420

421+
# The CA TLS subject is the same as the Tiller server, except we append CA to the common name to differentiate it from
422+
# the server.
422423
tiller_tls_ca_certs_subject = merge(
423424
var.tiller_tls_subject,
424425
{
425426
"common_name" = "${var.tiller_tls_subject["common_name"]} CA"
426427
},
427428
)
429+
tiller_tls_ca_certs_subject_json = jsonencode(local.tiller_tls_ca_certs_subject)
430+
tiller_tls_subject_json = jsonencode(var.tiller_tls_subject)
431+
432+
# In Powershell, double quotes must be escaped so before we pass the json to the command, we pass it through a replace
433+
# call. Additionally, due to the weird quoting rules, we need to make sure there is a space after each colon.
434+
tiller_tls_ca_certs_subject_json_as_arg = (
435+
local.is_windows
436+
? replace(
437+
replace(local.tiller_tls_ca_certs_subject_json, "\"", "\\\""),
438+
":",
439+
": ",
440+
)
441+
: local.tiller_tls_ca_certs_subject_json
442+
)
443+
tiller_tls_subject_json_as_arg = (
444+
local.is_windows
445+
? replace(
446+
replace(local.tiller_tls_subject_json, "\"", "\\\""),
447+
":",
448+
": ",
449+
)
450+
: local.tiller_tls_subject_json
451+
)
428452

453+
# These Secret names are set based on what is expected by `kubergrunt helm grant`
429454
tiller_tls_ca_certs_secret_name = "${var.namespace}-namespace-tiller-ca-certs"
430455
tiller_tls_certs_secret_name = "${var.namespace}-namespace-tiller-certs"
431456

432457
tiller_listen_localhost_arg = var.tiller_listen_localhost ? ["--listen=localhost:44134"] : []
433458

459+
# Derive the CLI args for the TLS algorithm config from the input variables
434460
tls_algorithm_config = var.private_key_algorithm == "ECDSA" ? "--tls-private-key-ecdsa-curve ${var.private_key_ecdsa_curve}" : "--tls-private-key-rsa-bits ${var.private_key_rsa_bits}"
435461

462+
# Make sure we expand the ~
463+
kubectl_config_path = pathexpand(var.kubectl_config_path)
464+
465+
# Configure the CLI args to pass to kubergrunt to authenticate to the kubernetes cluster based on user input to the
466+
# module
436467
kubergrunt_auth_params = <<-EOF
437-
${var.kubectl_server_endpoint != "" ? "--kubectl-server-endpoint \"$KUBECTL_SERVER_ENDPOINT\" --kubectl-certificate-authority \"$KUBECTL_CA_DATA\" --kubectl-token \"$KUBECTL_TOKEN\"" : ""} ${local.esc_newl}
438-
${var.kubectl_config_path != "" ? "--kubeconfig ${var.kubectl_config_path}" : ""} ${local.esc_newl}
468+
${var.kubectl_server_endpoint != "" ? "--kubectl-server-endpoint \"${local.env_prefix}KUBECTL_SERVER_ENDPOINT\" --kubectl-certificate-authority \"${local.env_prefix}KUBECTL_CA_DATA\" --kubectl-token \"${local.env_prefix}KUBECTL_TOKEN\"" : ""} ${local.esc_newl}
469+
${var.kubectl_config_path != "" ? "--kubeconfig ${local.kubectl_config_path}" : ""} ${local.esc_newl}
439470
${var.kubectl_config_context_name != "" ? "--kubectl-context-name ${var.kubectl_config_context_name}" : ""} ${local.esc_newl}
440471
EOF
441472

442-
443-
kubectl_auth_params = <<-EOF
444-
${var.kubectl_server_endpoint != "" ? "--server \"$KUBECTL_SERVER_ENDPOINT\" --certificate-authority \"${path.module}/kubernetes_server_ca.pem\" --token \"$KUBECTL_TOKEN\"" : ""} ${local.esc_newl}
445-
${var.kubectl_config_path != "" ? "--kubeconfig ${var.kubectl_config_path}" : ""} ${local.esc_newl}
446-
${var.kubectl_config_context_name != "" ? "--context ${var.kubectl_config_context_name}" : ""} ${local.esc_newl}
447-
EOF
448-
449-
450-
esc_newl = module.os.name == "Windows" ? "`" : "\\"
473+
# The environment variable prefix and newline escape differs between bash and powershell, so we compute that here
474+
# based on the OS
475+
is_windows = module.os.name == "Windows"
476+
env_prefix = local.is_windows ? "$env:" : "$"
477+
esc_newl = local.is_windows ? "`" : "\\"
451478
}
452479

453480
# Identify the operating system platform we are executing on

modules/k8s-tiller/variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ variable "kubectl_config_context_name" {
169169
variable "kubectl_config_path" {
170170
description = "The path to the config file to use for kubectl. If empty, defaults to $HOME/.kube/config. Used when var.tiller_tls_gen_method is kubergrunt."
171171
type = string
172-
default = "~/.kube/config"
172+
default = ""
173173
}
174174

175175
variable "kubectl_server_endpoint" {

0 commit comments

Comments
 (0)