Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from FinalCAD/add_auth_token_support
Browse files Browse the repository at this point in the history
Add auth token support
  • Loading branch information
yld authored May 14, 2019
2 parents c7fa55a + 59f5643 commit 0cff998
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ provider "k8s" {
soon as call is completed. This may impact performance if the code runs on a shared system because
and the global tempdir is used.

The k8s Terraform provider introduces a single Terraform resource, a `k8s_manifest`. The resource contains a `content` field, which contains a raw manifest.
Other supported provider options
- token
- certificate_authority
- kubeconfig_context
- server

The k8s Terraform provider introduces a single Terraform resource, a `k8s_manifest`. The resource contains a `content` field, which contains one or several raw manifests.

```hcl
variable "replicas" {
Expand Down
58 changes: 50 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"strings"
Expand All @@ -16,9 +17,12 @@ import (
)

type config struct {
kubeconfig string
kubeconfigContent string
kubeconfigContext string
kubeconfig string
kubeconfigContent string
kubeconfigContext string
kubeToken string
kubeServer string
kubeCertificateAuthority string
}

func main() {
Expand All @@ -38,15 +42,30 @@ func main() {
Type: schema.TypeString,
Optional: true,
},
"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"certificate_authority": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"server": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
ResourcesMap: map[string]*schema.Resource{
"k8s_manifest": resourceManifest(),
},
ConfigureFunc: func(d *schema.ResourceData) (interface{}, error) {
return &config{
kubeconfig: d.Get("kubeconfig").(string),
kubeconfigContent: d.Get("kubeconfig_content").(string),
kubeconfigContext: d.Get("kubeconfig_context").(string),
kubeconfig: d.Get("kubeconfig").(string),
kubeconfigContent: d.Get("kubeconfig_content").(string),
kubeconfigContext: d.Get("kubeconfig_context").(string),
kubeToken: d.Get("token").(string),
kubeCertificateAuthority: d.Get("certificate_authority").(string),
kubeServer: d.Get("server").(string),
}, nil
},
}
Expand Down Expand Up @@ -130,6 +149,21 @@ func kubectl(m interface{}, kubeconfig string, args ...string) *exec.Cmd {
args = append([]string{"--context", context}, args...)
}

ca := m.(*config).kubeCertificateAuthority
if ca != "" {
args = append([]string{"--certificate-authority", ca}, args...)
}

server := m.(*config).kubeServer
if server != "" {
args = append([]string{"--server", server}, args...)
}

token := m.(*config).kubeToken
if token != "" {
args = append([]string{"--token", token}, args...)
}

return exec.Command("kubectl", args...)
}

Expand Down Expand Up @@ -208,6 +242,15 @@ func resourceFromSelflink(s string) (resource, namespace string, ok bool) {
break
}
}

var err error
if resource, err = url.PathUnescape(resource); err != nil {
return "", "", false
}
if namespace, err = url.PathUnescape(namespace); err != nil {
return "", "", false
}

return resource, namespace, true
}

Expand All @@ -220,7 +263,7 @@ func (e errorList) Error() string {
func resourceManifestDelete(d *schema.ResourceData, m interface{}) error {
var errs []error
resources := strings.Split(d.Id(), resourceIDSelflinkDelim)
for i := len(resources)-1; i >= 0; i-- {
for i := len(resources) - 1; i >= 0; i-- {
if err := deleteResource(m, resources[i]); err != nil {
errs = append(errs, err)
}
Expand Down Expand Up @@ -291,4 +334,3 @@ func readResource(d *schema.ResourceData, m interface{}, selflink string) error
}
return nil
}

0 comments on commit 0cff998

Please sign in to comment.