@@ -245,6 +245,8 @@ resource "null_resource" "tiller_tls_ca_certs" {
245
245
depends_on = [null_resource. dependency_getter ]
246
246
247
247
provisioner "local-exec" {
248
+ interpreter = local. is_windows ? [" PowerShell" , " -Command" ] : [" bash" , " -c" ]
249
+
248
250
command = <<- EOF
249
251
${ lookup (module. require_executables . executables , " kubergrunt" , " " )} tls gen ${ local . esc_newl }
250
252
${ local . kubergrunt_auth_params } ${ local . esc_newl }
@@ -254,12 +256,11 @@ resource "null_resource" "tiller_tls_ca_certs" {
254
256
--secret-label gruntwork.io/tiller-namespace=${ var . namespace } ${ local . esc_newl }
255
257
--secret-label gruntwork.io/tiller-credentials=true ${ local . esc_newl }
256
258
--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 }
258
260
--tls-private-key-algorithm ${ var . private_key_algorithm } ${ local . esc_newl }
259
261
${ local . tls_algorithm_config }
260
262
EOF
261
263
262
-
263
264
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
264
265
environment = {
265
266
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -269,14 +270,13 @@ resource "null_resource" "tiller_tls_ca_certs" {
269
270
}
270
271
271
272
provisioner "local-exec" {
272
- when = destroy
273
+ when = destroy
274
+ interpreter = local. is_windows ? [" PowerShell" , " -Command" ] : [" bash" , " -c" ]
273
275
274
276
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 }
280
280
EOF
281
281
282
282
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
@@ -298,6 +298,8 @@ resource "null_resource" "tiller_tls_certs" {
298
298
}
299
299
300
300
provisioner "local-exec" {
301
+ interpreter = local. is_windows ? [" PowerShell" , " -Command" ] : [" bash" , " -c" ]
302
+
301
303
command = <<- EOF
302
304
${ lookup (module. require_executables . executables , " kubergrunt" , " " )} tls gen ${ local . esc_newl }
303
305
${ local . kubergrunt_auth_params } ${ local . esc_newl }
@@ -308,12 +310,11 @@ resource "null_resource" "tiller_tls_certs" {
308
310
--secret-label gruntwork.io/tiller-namespace=${ var . namespace } ${ local . esc_newl }
309
311
--secret-label gruntwork.io/tiller-credentials=true ${ local . esc_newl }
310
312
--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 }
312
314
--tls-private-key-algorithm ${ var . private_key_algorithm } ${ local . esc_newl }
313
315
${ local . tls_algorithm_config }
314
316
EOF
315
317
316
-
317
318
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
318
319
environment = {
319
320
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -323,17 +324,15 @@ resource "null_resource" "tiller_tls_certs" {
323
324
}
324
325
325
326
provisioner "local-exec" {
326
- when = destroy
327
+ when = destroy
328
+ interpreter = local. is_windows ? [" PowerShell" , " -Command" ] : [" bash" , " -c" ]
327
329
328
330
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 }
334
334
EOF
335
335
336
-
337
336
# Use environment variables for Kubernetes credentials to avoid leaking into the logs
338
337
environment = {
339
338
KUBECTL_SERVER_ENDPOINT = var.kubectl_server_endpoint
@@ -419,35 +418,63 @@ locals {
419
418
locals {
420
419
generated_tls_secret_name = var. tiller_tls_gen_method == " none" ? var. tiller_tls_secret_name : local. tiller_tls_certs_secret_name
421
420
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.
422
423
tiller_tls_ca_certs_subject = merge (
423
424
var. tiller_tls_subject ,
424
425
{
425
426
" common_name" = " ${ var . tiller_tls_subject [" common_name" ]} CA"
426
427
},
427
428
)
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
+ )
428
452
453
+ # These Secret names are set based on what is expected by `kubergrunt helm grant`
429
454
tiller_tls_ca_certs_secret_name = " ${ var . namespace } -namespace-tiller-ca-certs"
430
455
tiller_tls_certs_secret_name = " ${ var . namespace } -namespace-tiller-certs"
431
456
432
457
tiller_listen_localhost_arg = var. tiller_listen_localhost ? [" --listen=localhost:44134" ] : []
433
458
459
+ # Derive the CLI args for the TLS algorithm config from the input variables
434
460
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 } "
435
461
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
436
467
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 }
439
470
${ var . kubectl_config_context_name != " " ? " --kubectl-context-name ${ var . kubectl_config_context_name } " : " " } ${ local . esc_newl }
440
471
EOF
441
472
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 ? " `" : " \\ "
451
478
}
452
479
453
480
# Identify the operating system platform we are executing on
0 commit comments