Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
handle localhost in kubeconfig for docker-desktop
Browse files Browse the repository at this point in the history
replace the master server URL for docker desktop from localhost to host.docker.internal
so that it can be discovered from within the container executing CNAB install
  • Loading branch information
Swapnil Bawaskar committed Jun 5, 2019
1 parent 9508582 commit b9dfa62
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)

const (
Expand Down Expand Up @@ -97,7 +98,7 @@ func install(path string) {
}
}

func uninstall() {
func uninstall() {
knbClient, err := createKnbClient()
if err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -146,6 +147,11 @@ func getRestConfig() (*rest.Config, error) {
}
flag.Parse()

err := patchForDockerDesktop(*kubeconfig)
if err != nil {
return nil, err
}

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
Expand All @@ -154,6 +160,31 @@ func getRestConfig() (*rest.Config, error) {
return config, nil
}

// replace the master server URL for docker desktop from localhost to host.docker.internal
// so that it can be discovered from within the container executing CNAB install
func patchForDockerDesktop(kubeconfig string) error {

loader := clientcmd.NewDefaultClientConfigLoadingRules()
loader.ExplicitPath = kubeconfig

config, err := loader.Load()
if err != nil {
return err
}
currContext := config.CurrentContext
if currContext == kab.DOCKER_DESKTOP_NAME || currContext == kab.DOCKER_FOR_DESKTOP_NAME {
var cluster *api.Cluster
cluster = config.Clusters[config.CurrentContext]
cluster.Server = strings.ReplaceAll(cluster.Server, "localhost", "host.docker.internal")
err = clientcmd.WriteToFile(*config, kubeconfig)
if err != nil {
return err
}
}

return nil
}

func getCoreClient(config *rest.Config) (kubernetes.Interface, error) {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
Expand Down

0 comments on commit b9dfa62

Please sign in to comment.