Skip to content

Commit 5c24f49

Browse files
authored
feat: Move kubeconfig to the provider and fix empty metadata bug (#93)
1 parent 173a05d commit 5c24f49

File tree

12 files changed

+497
-279
lines changed

12 files changed

+497
-279
lines changed

docs/index.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,39 @@ description: |-
1919

2020
- `access_token` (String, Sensitive) Plural Console access token. Can be sourced from `PLURAL_ACCESS_TOKEN`.
2121
- `console_url` (String) Plural Console URL, i.e. `https://console.demo.onplural.sh`. Can be sourced from `PLURAL_CONSOLE_URL`.
22+
- `kubeconfig` (Attributes) Kubeconfig for cluster access. In order to source its fields from environment variables it has to be defined, at least as an empty object. (see [below for nested schema](#nestedatt--kubeconfig))
2223
- `use_cli` (Boolean) Use Plural CLI `plural cd login` command for authentication. Can be sourced from `PLURAL_USE_CLI`.
24+
25+
<a id="nestedatt--kubeconfig"></a>
26+
### Nested Schema for `kubeconfig`
27+
28+
Optional:
29+
30+
- `client_certificate` (String) The path to a client cert file for TLS. Can be sourced from `PLURAL_KUBE_CLIENT_CERT_DATA`.
31+
- `client_key` (String, Sensitive) The path to a client key file for TLS. Can be sourced from `PLURAL_KUBE_CLIENT_KEY_DATA`.
32+
- `cluster_ca_certificate` (String) The path to a cert file for the certificate authority. Can be sourced from `PLURAL_KUBE_CLUSTER_CA_CERT_DATA`.
33+
- `config_context` (String) kubeconfig context to use. Can be sourced from `PLURAL_KUBE_CTX`.
34+
- `config_context_auth_info` (String) Can be sourced from `PLURAL_KUBE_CTX_AUTH_INFO`.
35+
- `config_context_cluster` (String) Can be sourced from `PLURAL_KUBE_CTX_CLUSTER`.
36+
- `config_path` (String) Path to the kubeconfig file. Can be sourced from `PLURAL_KUBE_CONFIG_PATH`.
37+
- `exec` (Attributes List) Specifies a command to provide client credentials (see [below for nested schema](#nestedatt--kubeconfig--exec))
38+
- `host` (String) The complete address of the Kubernetes cluster, using scheme://hostname:port format. Can be sourced from `PLURAL_KUBE_HOST`.
39+
- `insecure` (Boolean) Skips the validity check for the server's certificate. This will make your HTTPS connections insecure. Can be sourced from `PLURAL_KUBE_INSECURE`.
40+
- `password` (String, Sensitive) The password for basic authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_PASSWORD`.
41+
- `proxy_url` (String) The URL to the proxy to be used for all requests made by this client. Can be sourced from `PLURAL_KUBE_PROXY_URL`.
42+
- `tls_server_name` (String) TLS server name is used to check server certificate. If it is empty, the hostname used to contact the server is used. Can be sourced from `PLURAL_KUBE_TLS_SERVER_NAME`.
43+
- `token` (String, Sensitive) Token is the bearer token for authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_TOKEN`.
44+
- `username` (String) The username for basic authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_USER`.
45+
46+
<a id="nestedatt--kubeconfig--exec"></a>
47+
### Nested Schema for `kubeconfig.exec`
48+
49+
Required:
50+
51+
- `api_version` (String) Preferred input version.
52+
- `command` (String) Command to execute.
53+
54+
Optional:
55+
56+
- `args` (List of String) Arguments to pass to the command when executing it.
57+
- `env` (Map of String) Defines environment variables to expose to the process.

docs/resources/cluster.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ A representation of a cluster you can deploy to.
2626
- `handle` (String) A short, unique human-readable name used to identify this cluster. Does not necessarily map to the cloud resource name.
2727
- `helm_repo_url` (String) Helm repository URL you'd like to use in deployment agent Helm install.
2828
- `helm_values` (String) Additional Helm values you'd like to use in deployment agent Helm installs. This is useful for BYOK clusters that need to use custom images or other constructs.
29-
- `kubeconfig` (Attributes) (see [below for nested schema](#nestedatt--kubeconfig))
29+
- `kubeconfig` (Attributes, Deprecated) (see [below for nested schema](#nestedatt--kubeconfig))
3030
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons). Use `jsonencode` and `jsondecode` methods to encode and decode data.
3131
- `project_id` (String) ID of the project that this cluster belongs to.
3232
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
@@ -71,21 +71,21 @@ Optional:
7171

7272
Optional:
7373

74-
- `client_certificate` (String) The path to a client cert file for TLS. Can be sourced from `PLURAL_KUBE_CLIENT_CERT_DATA`.
75-
- `client_key` (String, Sensitive) The path to a client key file for TLS. Can be sourced from `PLURAL_KUBE_CLIENT_KEY_DATA`.
76-
- `cluster_ca_certificate` (String) The path to a cert file for the certificate authority. Can be sourced from `PLURAL_KUBE_CLUSTER_CA_CERT_DATA`.
77-
- `config_context` (String) kubeconfig context to use. Can be sourced from `PLURAL_KUBE_CTX`.
78-
- `config_context_auth_info` (String) Can be sourced from `PLURAL_KUBE_CTX_AUTH_INFO`.
79-
- `config_context_cluster` (String) Can be sourced from `PLURAL_KUBE_CTX_CLUSTER`.
80-
- `config_path` (String) Path to the kubeconfig file. Can be sourced from `PLURAL_KUBE_CONFIG_PATH`.
74+
- `client_certificate` (String) The path to a client cert file for TLS.
75+
- `client_key` (String, Sensitive) The path to a client key file for TLS.
76+
- `cluster_ca_certificate` (String) The path to a cert file for the certificate authority.
77+
- `config_context` (String) kubeconfig context to use.
78+
- `config_context_auth_info` (String)
79+
- `config_context_cluster` (String)
80+
- `config_path` (String) Path to the kubeconfig file.
8181
- `exec` (Attributes List) Specifies a command to provide client credentials (see [below for nested schema](#nestedatt--kubeconfig--exec))
82-
- `host` (String) The complete address of the Kubernetes cluster, using scheme://hostname:port format. Can be sourced from `PLURAL_KUBE_HOST`.
83-
- `insecure` (Boolean) Skips the validity check for the server's certificate. This will make your HTTPS connections insecure. Can be sourced from `PLURAL_KUBE_INSECURE`.
84-
- `password` (String, Sensitive) The password for basic authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_PASSWORD`.
85-
- `proxy_url` (String) The URL to the proxy to be used for all requests made by this client. Can be sourced from `PLURAL_KUBE_PROXY_URL`.
86-
- `tls_server_name` (String) TLS server name is used to check server certificate. If it is empty, the hostname used to contact the server is used. Can be sourced from `PLURAL_KUBE_TLS_SERVER_NAME`.
87-
- `token` (String, Sensitive) Token is the bearer token for authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_TOKEN`.
88-
- `username` (String) The username for basic authentication to the Kubernetes cluster. Can be sourced from `PLURAL_KUBE_USER`.
82+
- `host` (String) The complete address of the Kubernetes cluster, using scheme://hostname:port format.
83+
- `insecure` (Boolean) Skips the validity check for the server's certificate. This will make your HTTPS connections insecure.
84+
- `password` (String, Sensitive) The password for basic authentication to the Kubernetes cluster.
85+
- `proxy_url` (String) The URL to the proxy to be used for all requests made by this client.
86+
- `tls_server_name` (String) TLS server name is used to check server certificate. If it is empty, the hostname used to contact the server is used.
87+
- `token` (String, Sensitive) Token is the bearer token for authentication to the Kubernetes cluster.
88+
- `username` (String) The username for basic authentication to the Kubernetes cluster.
8989

9090
<a id="nestedatt--kubeconfig--exec"></a>
9191
### Nested Schema for `kubeconfig.exec`

example/cluster/byok/main.tf

Lines changed: 0 additions & 34 deletions
This file was deleted.

example/kubeconfig/main.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
terraform {
2+
required_providers {
3+
plural = {
4+
source = "pluralsh/plural"
5+
version = "0.2.25"
6+
}
7+
}
8+
}
9+
10+
#####################################
11+
######### Deprecated method #########
12+
#####################################
13+
# provider "plural" {
14+
# alias = "deprecated"
15+
# use_cli = true
16+
# }
17+
#
18+
# resource "plural_cluster" "deprecated" {
19+
# provider = plural.deprecated
20+
# name = "byok"
21+
# protect = "false"
22+
# detach = true
23+
# kubeconfig = {
24+
# # It can no longer be sourced from environment variables.
25+
# config_path = pathexpand("~/.kube/config")
26+
# }
27+
# }
28+
29+
#####################################
30+
############ New method #############
31+
#####################################
32+
provider "plural" {
33+
alias = "new"
34+
use_cli = true
35+
kubeconfig = {
36+
# It can be sourced from environment variables instead, i.e.: export PLURAL_KUBE_CONFIG_PATH=$KUBECONFIG
37+
config_path = pathexpand("~/.kube/config")
38+
}
39+
}
40+
41+
resource "plural_cluster" "new" {
42+
provider = plural.new
43+
name = "byok"
44+
protect = "false"
45+
detach = true
46+
}

internal/resource/cluster_kubeconfig.go renamed to internal/common/kubeclient.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package resource
1+
package common
22

33
import (
44
"bytes"
@@ -23,23 +23,23 @@ import (
2323
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2424
)
2525

26-
type KubeConfig struct {
26+
type KubeClient struct {
2727
ClientConfig clientcmd.ClientConfig
2828
}
2929

30-
func (k *KubeConfig) ToClientSet() (*kubernetes.Clientset, error) {
30+
func (k *KubeClient) ToClientSet() (*kubernetes.Clientset, error) {
3131
config, err := k.ToRawKubeConfigLoader().ClientConfig()
3232
if err != nil {
3333
return nil, err
3434
}
3535
return kubernetes.NewForConfig(config)
3636
}
3737

38-
func (k *KubeConfig) ToRESTConfig() (*rest.Config, error) {
38+
func (k *KubeClient) ToRESTConfig() (*rest.Config, error) {
3939
return k.ToRawKubeConfigLoader().ClientConfig()
4040
}
4141

42-
func (k *KubeConfig) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
42+
func (k *KubeClient) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
4343
config, err := k.ToRESTConfig()
4444
if err != nil {
4545
return nil, err
@@ -48,7 +48,7 @@ func (k *KubeConfig) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, er
4848
return disk.NewCachedDiscoveryClientForConfig(config, os.TempDir(), os.TempDir(), 1*time.Minute)
4949
}
5050

51-
func (k *KubeConfig) ToRESTMapper() (meta.RESTMapper, error) {
51+
func (k *KubeClient) ToRESTMapper() (meta.RESTMapper, error) {
5252
client, err := k.ToDiscoveryClient()
5353
if err != nil {
5454
return nil, err
@@ -57,11 +57,11 @@ func (k *KubeConfig) ToRESTMapper() (meta.RESTMapper, error) {
5757
return restmapper.NewShortcutExpander(restmapper.NewDeferredDiscoveryRESTMapper(client), client, nil), nil
5858
}
5959

60-
func (k *KubeConfig) ToRawKubeConfigLoader() clientcmd.ClientConfig {
60+
func (k *KubeClient) ToRawKubeConfigLoader() clientcmd.ClientConfig {
6161
return k.ClientConfig
6262
}
6363

64-
func newKubeconfig(ctx context.Context, kubeconfig *Kubeconfig, namespace *string) (*KubeConfig, error) {
64+
func NewKubeClient(ctx context.Context, kubeconfig *Kubeconfig, namespace *string) (*KubeClient, error) {
6565
overrides := &clientcmd.ConfigOverrides{}
6666
loader := &clientcmd.ClientConfigLoadingRules{}
6767

@@ -186,5 +186,5 @@ func newKubeconfig(ctx context.Context, kubeconfig *Kubeconfig, namespace *strin
186186
}
187187

188188
tflog.Trace(ctx, "successfully initialized kubernetes config")
189-
return &KubeConfig{ClientConfig: client}, nil
189+
return &KubeClient{ClientConfig: client}, nil
190190
}

0 commit comments

Comments
 (0)