Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform Provider: Kubeconfig as Base64 string #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ terraform {
}

provider "harvester" {
# Path to kubeconfig file
kubeconfig = "/path/to/kubeconfig.yaml"

# Alternatively the base64 encoded contents of the kubeconfig file.
# CAUTION: When supplying the kubeconfig as base64 encoded string, the
# content will be preserved in the Terraform state files in the clear.
# Take appropriate measures to avoid leaking sensitive information.
#
# kubeconfig = "YXBpVmVyc2lvb...xvY2FsIgo="

kubecontext = "mycontext"
}
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Provider() *schema.Provider {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "kubeconfig file path, users can use the KUBECONFIG environment variable instead",
Description: "kubeconfig file path or content of the kubeconfig file as base64 encoded string, users can use the KUBECONFIG environment variable instead.",
},
constants.FieldProviderKubeContext: {
Type: schema.TypeString,
Expand Down
30 changes: 26 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"encoding/base64"

harvnetworkclient "github.com/harvester/harvester-network-controller/pkg/generated/clientset/versioned"
harvclient "github.com/harvester/harvester/pkg/generated/clientset/versioned"
"github.com/harvester/harvester/pkg/generated/clientset/versioned/scheme"
Expand All @@ -9,6 +11,7 @@ import (
"k8s.io/client-go/kubernetes"
storageclient "k8s.io/client-go/kubernetes/typed/storage/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

type Client struct {
Expand All @@ -21,11 +24,17 @@ type Client struct {
}

func NewClient(kubeConfig, kubeContext string) (*Client, error) {
clientConfig := kubeconfig.GetNonInteractiveClientConfigWithContext(kubeConfig, kubeContext)
restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
var (
restConfig *rest.Config
err error
)

if restConfig, err = restConfigFromBase64(kubeConfig); err != nil {
if restConfig, err = restConfigFromFile(kubeConfig, kubeContext); err != nil {
return nil, err
}
}

copyConfig := rest.CopyConfig(restConfig)
copyConfig.GroupVersion = &kubeschema.GroupVersion{Group: "subresources.kubevirt.io", Version: "v1"}
copyConfig.APIPath = "/apis"
Expand Down Expand Up @@ -59,3 +68,16 @@ func NewClient(kubeConfig, kubeContext string) (*Client, error) {
HarvesterNetworkClient: harvNetworkClient,
}, nil
}

func restConfigFromFile(kubeConfig, kubeContext string) (*rest.Config, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it make sense to move the homedir.Expand(d.Get(constants.FieldProviderKubeConfig).(string)) at

kubeConfig, err := homedir.Expand(d.Get(constants.FieldProviderKubeConfig).(string))
if err != nil {
return nil, diag.FromErr(err)
}
into this new function?

clientConfig := kubeconfig.GetNonInteractiveClientConfigWithContext(kubeConfig, kubeContext)
return clientConfig.ClientConfig()
}

func restConfigFromBase64(kubeConfigBase64 string) (*rest.Config, error) {
bytes, err := base64.StdEncoding.DecodeString(kubeConfigBase64)
if err != nil {
return nil, err
}
return clientcmd.RESTConfigFromKubeConfig(bytes)
}
5 changes: 3 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const (
NamespaceDefault = "default"
NamespaceHarvesterSystem = "harvester-system"

FieldProviderKubeConfig = "kubeconfig"
FieldProviderKubeContext = "kubecontext"
FieldProviderKubeConfig = "kubeconfig"
FieldProviderKubeContext = "kubecontext"
FieldProviderKubeConfigBase64 = "kubeconfig_base64"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this is being used.


FieldCommonName = "name"
FieldCommonNamespace = "namespace"
Expand Down
Loading