@@ -298,6 +298,7 @@ def _kubeconfig_impl(repository_ctx):
298298 if not kubectl :
299299 fail ("Unable to find kubectl executable. PATH=%s" % repository_ctx .path )
300300 repository_ctx .symlink (kubectl , "kubectl" )
301+ repository_ctx .file (repository_ctx .path ("cluster" ), content = repository_ctx .attr .cluster , executable = False )
301302
302303 # TODO: figure out how to use BUILD_USER
303304 if "USER" in repository_ctx .os .environ :
@@ -333,6 +334,18 @@ def _kubeconfig_impl(repository_ctx):
333334 else :
334335 # fall back to the default
335336 server = "https://kubernetes.default"
337+ elif repository_ctx .attr .use_host_config :
338+ home = repository_ctx .path (repository_ctx .os .environ ["HOME" ])
339+ kubeconfig = home .get_child (".kube" ).get_child ("config" )
340+ if repository_ctx .path (kubeconfig ).exists :
341+ repository_ctx .symlink (kubeconfig , repository_ctx .path ("kubeconfig" ))
342+ else :
343+ _kubectl_config (repository_ctx , [
344+ "set-cluster" ,
345+ repository_ctx .attr .cluster ,
346+ "--server" ,
347+ server ,
348+ ])
336349 else :
337350 home = repository_ctx .path (repository_ctx .os .environ ["HOME" ])
338351 certs = home .get_child (".kube" ).get_child ("certs" )
@@ -344,14 +357,15 @@ def _kubeconfig_impl(repository_ctx):
344357 # --certificate-authority=... \
345358 # --server=https://dev3.k8s.tubemogul.info:443 \
346359 # --embed-certs",
347- _kubectl_config (repository_ctx , [
348- "set-cluster" ,
349- repository_ctx .attr .cluster ,
350- "--server" ,
351- server ,
352- "--certificate-authority" ,
353- ca_crt ,
354- ])
360+ if ca_crt :
361+ _kubectl_config (repository_ctx , [
362+ "set-cluster" ,
363+ repository_ctx .attr .cluster ,
364+ "--server" ,
365+ server ,
366+ "--certificate-authority" ,
367+ ca_crt ,
368+ ])
355369
356370 # config set-credentials {user} --token=...",
357371 if token :
@@ -381,17 +395,19 @@ def _kubeconfig_impl(repository_ctx):
381395 ])
382396
383397 # export repostory contents
384- repository_ctx .file ("BUILD" , """exports_files(["kubeconfig", "kubectl"])""" , False )
398+ repository_ctx .file ("BUILD" , """exports_files(["kubeconfig", "kubectl", "cluster" ])""" , False )
385399
386400 return {
387401 "cluster" : repository_ctx .attr .cluster ,
388402 "server" : repository_ctx .attr .server ,
403+ "use_host_config" : repository_ctx .attr .use_host_config ,
389404 }
390405
391406kubeconfig = repository_rule (
392407 attrs = {
393408 "cluster" : attr .string (),
394409 "server" : attr .string (),
410+ "use_host_config" : attr .bool (),
395411 },
396412 environ = [
397413 "HOME" ,
@@ -526,6 +542,7 @@ def _k8s_test_setup_impl(ctx):
526542 # add files referenced by rule attributes to runfiles
527543 files = [ctx .executable ._stamper , ctx .file .kubectl , ctx .file .kubeconfig , ctx .executable ._kustomize , ctx .executable ._it_sidecar , ctx .executable ._it_manifest_filter ]
528544 files += ctx .files ._set_namespace
545+ files += ctx .files ._cluster
529546
530547 push_statements , files , pushes_runfiles = imagePushStatements (ctx , [o for o in ctx .attr .objects if KustomizeInfo in o ], files )
531548
@@ -549,6 +566,7 @@ def _k8s_test_setup_impl(ctx):
549566 template = ctx .file ._namespace_template ,
550567 substitutions = {
551568 "%{it_sidecar}" : ctx .executable ._it_sidecar .short_path ,
569+ "%{cluster}" : ctx .file ._cluster .path ,
552570 "%{kubeconfig}" : ctx .file .kubeconfig .path ,
553571 "%{kubectl}" : ctx .file .kubectl .path ,
554572 "%{portforwards}" : " " .join (["-portforward=" + p for p in ctx .attr .portforward_services ]),
@@ -588,6 +606,10 @@ k8s_test_setup = rule(
588606 "portforward_services" : attr .string_list (),
589607 "setup_timeout" : attr .string (default = "10m" ),
590608 "wait_for_apps" : attr .string_list (),
609+ "_cluster" : attr .label (
610+ default = Label ("@k8s_test//:cluster" ),
611+ allow_single_file = True ,
612+ ),
591613 "_it_sidecar" : attr .label (
592614 default = Label ("//testing/it_sidecar:it_sidecar" ),
593615 cfg = "host" ,
0 commit comments