Skip to content

Commit

Permalink
chore: refactor custom location
Browse files Browse the repository at this point in the history
  • Loading branch information
macox committed Jun 19, 2020
1 parent fa4b2bf commit 32a0b0c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ func main() {
}
}
```
To change the location of the kube config use the following:
To change the location of the kube config use the following instead:

```go
f.KubeConfigPath(kubeConfigPath)
f.KubeConfigFile(kubeConfigFile)
cfg, err := f.CreateKubeConfigFromCustomLocation(kubeConfigPath, kubeConfigFile)
```

Part of Jenkins X shared libraries.
3 changes: 1 addition & 2 deletions pkg/kubeclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ import (
type Factory interface {
// CreateKubeConfig creates the kubernetes configuration
CreateKubeConfig() (*rest.Config, error)
KubeConfigFile(string)
KubeConfigPath(string)
CreateKubeConfigFromCustomLocation(string, string) (*rest.Config, error)
}
31 changes: 10 additions & 21 deletions pkg/kubeclient/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,25 @@ const (
)

type factory struct {
kubeConfigCache *string
kubeConfigFile string
kubeConfigPath string
}

// NewFactory creates a factory with the default Kubernetes resources defined
func NewFactory() Factory {
f := &factory{}
f.kubeConfigFile = DefaultKubeConfigFile
f.kubeConfigPath = DefaultKubeConfigPath
return f
}

func (f *factory) KubeConfigFile(fileName string) {
f.kubeConfigFile = fileName
func (f *factory) CreateKubeConfigFromCustomLocation(kubeConfigPath, kubeConfigFile string) (*rest.Config, error) {
return f.createKubeConfig(kubeConfigPath, kubeConfigFile)
}

func (f *factory) KubeConfigPath(path string) {
f.kubeConfigPath = path
func (f *factory) CreateKubeConfig() (*rest.Config, error) {
return f.createKubeConfig(DefaultKubeConfigPath, DefaultKubeConfigFile)
}

// CreateKubeConfig figures out the kubernetes config from environment variables or default locations whether in or out
// of cluster
func (f *factory) CreateKubeConfig() (*rest.Config, error) {
func (f *factory) createKubeConfig(kubeConfigPath, kubeConfigFile string) (*rest.Config, error) {
masterURL := ""
kubeConfigEnv := os.Getenv("KUBECONFIG")
if kubeConfigEnv != "" {
Expand All @@ -59,7 +54,7 @@ func (f *factory) CreateKubeConfig() (*rest.Config, error) {
&clientcmd.ClientConfigLoadingRules{Precedence: pathList},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterURL}}).ClientConfig()
}
kubeconfig := f.createKubeConfigText()
kubeconfig := f.createKubeConfigPath(kubeConfigPath, kubeConfigFile)
var config *rest.Config
var err error
if kubeconfig != nil {
Expand Down Expand Up @@ -89,18 +84,12 @@ func (f *factory) CreateKubeConfig() (*rest.Config, error) {
return config, nil
}

func (f *factory) createKubeConfigText() *string {
var kubeconfig *string
if f.kubeConfigCache != nil {
return f.kubeConfigCache
}
text := ""
func (f *factory) createKubeConfigPath(kubeConfigPath, kubeConfigFile string) *string {
path := ""
if home := homeDir(); home != "" {
text = filepath.Join(home, f.kubeConfigPath, f.kubeConfigFile)
path = filepath.Join(home, kubeConfigPath, kubeConfigFile)
}
kubeconfig = &text
f.kubeConfigCache = kubeconfig
return kubeconfig
return &path
}

func fileExists(path string) (bool, error) {
Expand Down

0 comments on commit 32a0b0c

Please sign in to comment.