From 97d611e934232c3e14c4cf80dea7ea1c6ce24f8f Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Wed, 13 Jan 2021 09:13:51 +1000 Subject: [PATCH] Adds KUBECONFIG field for using existing .kube/config --- internal/terraform/config/config.go | 10 ++++++++++ internal/terraform/config/fields.go | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/internal/terraform/config/config.go b/internal/terraform/config/config.go index 53692d97..da3a061b 100644 --- a/internal/terraform/config/config.go +++ b/internal/terraform/config/config.go @@ -13,11 +13,21 @@ import ( "github.com/kubernetes-sigs/aws-iam-authenticator/pkg/token" "github.com/pkg/errors" "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) // Func which configures the Kubernetes provider. // https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/kubernetes/provider.go func Func(d *schema.ResourceData) (interface{}, error) { + if v, ok := d.GetOk(FieldKubeConfig); ok { + cfg, err := clientcmd.BuildConfigFromFlags("", v.(string)) + if err != nil { + return nil, err + } + + return NewForConfig(cfg) + } + cfg := &rest.Config{} // Overriding with static configuration diff --git a/internal/terraform/config/fields.go b/internal/terraform/config/fields.go index 62ebe859..ff2343e3 100644 --- a/internal/terraform/config/fields.go +++ b/internal/terraform/config/fields.go @@ -3,6 +3,8 @@ package config import "github.com/hashicorp/terraform/helper/schema" const ( + // FieldKubeConfig defines the path to the Kubernetes config file. + FieldKubeConfig = "kubeconfig" // FieldHost identifier for host field. FieldHost = "host" // FieldInsecure identifier for insecure field. @@ -20,6 +22,12 @@ const ( // Fields which are used to configure the Kubernetes client. func Fields() map[string]*schema.Schema { return map[string]*schema.Schema{ + FieldKubeConfig: { + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("KUBECONFIG", ""), + Description: "The path to the Kubernetes config file.", + }, FieldHost: { Type: schema.TypeString, Optional: true,