Skip to content

Commit

Permalink
Refactor plugin utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkhahn committed Jan 9, 2018
1 parent c8581b7 commit d867e20
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package framework_test
package plugin_utils_test

import (
. "github.com/onsi/ginkgo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016 The Kubernetes Authors.
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package framework
package plugin_utils

import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"time"

restclient "k8s.io/client-go/rest"
Expand All @@ -35,7 +37,13 @@ func InitConfig() (*restclient.Config, error) {
// then the value of the KUBECONFIG env var (if any), and defaulting
// to ~/.kube/config as a last resort.
home := os.Getenv("HOME")
kubeconfig := home + "/.kube/config"
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}
kubeconfig := filepath.Join(home, ".kube", "config")

kubeconfigEnv := os.Getenv("KUBECONFIG")
if len(kubeconfigEnv) > 0 {
Expand Down Expand Up @@ -68,14 +76,6 @@ func InitConfig() (*restclient.Config, error) {
}

func clientFromConfig(path string) (*restclient.Config, string, error) {
if path == "-" {
cfg, err := restclient.InClusterConfig()
if err != nil {
return nil, "", fmt.Errorf("cluster config not available: %v", err)
}
return cfg, "", nil
}

rules := &clientcmd.ClientConfigLoadingRules{ExplicitPath: path}
credentials, err := rules.Load()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package framework_test
package plugin_utils_test

import (
"os"
"time"

"k8s.io/kubectl/pkg/plugin_utils"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/kubectl/pkg/framework"
)

var _ = Describe("InitConfig", func() {
var ()

BeforeEach(func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_KUBECONFIG", "assets/config")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_KUBECONFIG", "test_data/config")
})

Describe("InitConfig", func() {
Context("When nothing is overridden by the calling framework", func() {
It("finds and parses the preexisting config", func() {
config, err := framework.InitConfig()
config, err := plugin_utils.InitConfig()
Expect(err).NotTo(HaveOccurred())

Expect(config.Host).To(Equal("https://notreal.com:1234"))
Expand All @@ -33,9 +32,9 @@ var _ = Describe("InitConfig", func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_AS", "apple")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_AS_GROUP", "[\"banana\",\"cherry\"]")

os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CERTIFICATE_AUTHORITY", "assets/apiserver_ca.crt")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLIENT_CERTIFICATE", "assets/client.crt")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLIENT_KEY", "assets/client.key")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CERTIFICATE_AUTHORITY", "test_data/apiserver_ca.crt")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLIENT_CERTIFICATE", "test_data/client.crt")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLIENT_KEY", "test_data/client.key")

os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_REQUEST_TIMEOUT", "45s")
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_SERVER", "some-other-server.com")
Expand All @@ -46,15 +45,15 @@ var _ = Describe("InitConfig", func() {
os.Setenv("KUBECTL_PLUGINS_GLOBAL_FLAG_CLUSTER", "")
})
It("overrides the config settings with the passed in settings", func() {
config, err := framework.InitConfig()
config, err := plugin_utils.InitConfig()
Expect(err).NotTo(HaveOccurred())

Expect(config.Impersonate.UserName).To(Equal("apple"))
Expect(config.Impersonate.Groups).Should(ConsistOf("banana", "cherry"))

Expect(config.CertFile).To(Equal("assets/client.crt"))
Expect(config.KeyFile).To(Equal("assets/client.key"))
Expect(config.CAFile).To(Equal("assets/apiserver_ca.crt"))
Expect(config.CertFile).To(Equal("test_data/client.crt"))
Expect(config.KeyFile).To(Equal("test_data/client.key"))
Expect(config.CAFile).To(Equal("test_data/apiserver_ca.crt"))

Expect(config.Timeout).To(Equal(45 * time.Second))
Expect(config.ServerName).To(Equal("some-other-server.com"))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d867e20

Please sign in to comment.