github.com/sixgill/terraform@v0.9.0-beta2.0.20170316214032-033f6226ae50/builtin/providers/kubernetes/provider.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/mitchellh/go-homedir"
    12  	kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
    13  	"k8s.io/kubernetes/pkg/client/restclient"
    14  	"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
    15  	clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
    16  )
    17  
    18  func Provider() terraform.ResourceProvider {
    19  	return &schema.Provider{
    20  		Schema: map[string]*schema.Schema{
    21  			"host": {
    22  				Type:        schema.TypeString,
    23  				Optional:    true,
    24  				DefaultFunc: schema.EnvDefaultFunc("KUBE_HOST", ""),
    25  				Description: "The hostname (in form of URI) of Kubernetes master.",
    26  			},
    27  			"username": {
    28  				Type:        schema.TypeString,
    29  				Optional:    true,
    30  				DefaultFunc: schema.EnvDefaultFunc("KUBE_USER", ""),
    31  				Description: "The username to use for HTTP basic authentication when accessing the Kubernetes master endpoint.",
    32  			},
    33  			"password": {
    34  				Type:        schema.TypeString,
    35  				Optional:    true,
    36  				DefaultFunc: schema.EnvDefaultFunc("KUBE_PASSWORD", ""),
    37  				Description: "The password to use for HTTP basic authentication when accessing the Kubernetes master endpoint.",
    38  			},
    39  			"insecure": {
    40  				Type:        schema.TypeBool,
    41  				Optional:    true,
    42  				DefaultFunc: schema.EnvDefaultFunc("KUBE_INSECURE", false),
    43  				Description: "Whether server should be accessed without verifying the TLS certificate.",
    44  			},
    45  			"client_certificate": {
    46  				Type:        schema.TypeString,
    47  				Optional:    true,
    48  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CLIENT_CERT_DATA", ""),
    49  				Description: "PEM-encoded client certificate for TLS authentication.",
    50  			},
    51  			"client_key": {
    52  				Type:        schema.TypeString,
    53  				Optional:    true,
    54  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CLIENT_KEY_DATA", ""),
    55  				Description: "PEM-encoded client certificate key for TLS authentication.",
    56  			},
    57  			"cluster_ca_certificate": {
    58  				Type:        schema.TypeString,
    59  				Optional:    true,
    60  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CLUSTER_CA_CERT_DATA", ""),
    61  				Description: "PEM-encoded root certificates bundle for TLS authentication.",
    62  			},
    63  			"config_path": {
    64  				Type:        schema.TypeString,
    65  				Optional:    true,
    66  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG", "~/.kube/config"),
    67  				Description: "Path to the kube config file, defaults to ~/.kube/config",
    68  			},
    69  			"config_context_auth_info": {
    70  				Type:        schema.TypeString,
    71  				Optional:    true,
    72  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CTX_AUTH_INFO", ""),
    73  				Description: "",
    74  			},
    75  			"config_context_cluster": {
    76  				Type:        schema.TypeString,
    77  				Optional:    true,
    78  				DefaultFunc: schema.EnvDefaultFunc("KUBE_CTX_CLUSTER", ""),
    79  				Description: "",
    80  			},
    81  		},
    82  
    83  		ResourcesMap: map[string]*schema.Resource{
    84  			"kubernetes_config_map": resourceKubernetesConfigMap(),
    85  			"kubernetes_namespace":  resourceKubernetesNamespace(),
    86  		},
    87  		ConfigureFunc: providerConfigure,
    88  	}
    89  }
    90  
    91  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    92  	// Config file loading
    93  	cfg, err := tryLoadingConfigFile(d)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	if cfg == nil {
    98  		cfg = &restclient.Config{}
    99  	}
   100  
   101  	// Overriding with static configuration
   102  	cfg.UserAgent = fmt.Sprintf("HashiCorp/1.0 Terraform/%s", terraform.VersionString())
   103  
   104  	if v, ok := d.GetOk("host"); ok {
   105  		cfg.Host = v.(string)
   106  	}
   107  	if v, ok := d.GetOk("username"); ok {
   108  		cfg.Username = v.(string)
   109  	}
   110  	if v, ok := d.GetOk("password"); ok {
   111  		cfg.Password = v.(string)
   112  	}
   113  	if v, ok := d.GetOk("insecure"); ok {
   114  		cfg.Insecure = v.(bool)
   115  	}
   116  	if v, ok := d.GetOk("cluster_ca_certificate"); ok {
   117  		cfg.CAData = bytes.NewBufferString(v.(string)).Bytes()
   118  	}
   119  	if v, ok := d.GetOk("client_certificate"); ok {
   120  		cfg.CertData = bytes.NewBufferString(v.(string)).Bytes()
   121  	}
   122  	if v, ok := d.GetOk("client_key"); ok {
   123  		cfg.KeyData = bytes.NewBufferString(v.(string)).Bytes()
   124  	}
   125  
   126  	k, err := kubernetes.NewForConfig(cfg)
   127  	if err != nil {
   128  		return nil, fmt.Errorf("Failed to configure: %s", err)
   129  	}
   130  
   131  	return k, nil
   132  }
   133  
   134  func tryLoadingConfigFile(d *schema.ResourceData) (*restclient.Config, error) {
   135  	path, err := homedir.Expand(d.Get("config_path").(string))
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	loader := &clientcmd.ClientConfigLoadingRules{
   141  		ExplicitPath: path,
   142  	}
   143  	overrides := &clientcmd.ConfigOverrides{}
   144  	ctxSuffix := "; no context"
   145  	authInfo, authInfoOk := d.GetOk("config_context_auth_info")
   146  	cluster, clusterOk := d.GetOk("config_context_cluster")
   147  	if authInfoOk || clusterOk {
   148  		overrides.Context = clientcmdapi.Context{}
   149  		if authInfoOk {
   150  			overrides.Context.AuthInfo = authInfo.(string)
   151  		}
   152  		if clusterOk {
   153  			overrides.Context.Cluster = cluster.(string)
   154  		}
   155  		ctxSuffix = fmt.Sprintf("; auth_info: %s, cluster: %s",
   156  			overrides.Context.AuthInfo, overrides.Context.Cluster)
   157  	}
   158  	log.Printf("[DEBUG] Using override context: %#v", *overrides)
   159  
   160  	cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loader, overrides)
   161  	cfg, err := cc.ClientConfig()
   162  	if err != nil {
   163  		if pathErr, ok := err.(*os.PathError); ok && os.IsNotExist(pathErr.Err) {
   164  			log.Printf("[INFO] Unable to load config file as it doesn't exist at %q", path)
   165  			return nil, nil
   166  		}
   167  		return nil, fmt.Errorf("Failed to load config (%s%s): %s", path, ctxSuffix, err)
   168  	}
   169  
   170  	log.Printf("[INFO] Successfully loaded config file (%s%s)", path, ctxSuffix)
   171  	return cfg, nil
   172  }