From c41a5f19b778ea6fb57a063022c9caceb11fe8ed Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Thu, 13 Aug 2020 12:19:42 +0900 Subject: [PATCH] minikube upate-cotext: Fix nil pointer dereference --- go.mod | 1 - pkg/minikube/kubeconfig/kubeconfig.go | 25 ++++------------------ pkg/minikube/kubeconfig/kubeconfig_test.go | 12 +++++++++++ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 51b5d12766..78f91ae7da 100644 --- a/go.mod +++ b/go.mod @@ -72,7 +72,6 @@ require ( github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097 golang.org/x/build v0.0.0-20190927031335-2835ba2e683f golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 - golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a golang.org/x/sys v0.0.0-20200523222454-059865788121 diff --git a/pkg/minikube/kubeconfig/kubeconfig.go b/pkg/minikube/kubeconfig/kubeconfig.go index 566353eb32..42656e39fa 100644 --- a/pkg/minikube/kubeconfig/kubeconfig.go +++ b/pkg/minikube/kubeconfig/kubeconfig.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "net/url" "os" - "path" "path/filepath" "strconv" @@ -31,7 +30,6 @@ import ( "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/tools/clientcmd/api/latest" "k8s.io/minikube/pkg/minikube/constants" - "k8s.io/minikube/pkg/minikube/localpath" pkgutil "k8s.io/minikube/pkg/util" "k8s.io/minikube/pkg/util/lock" ) @@ -121,26 +119,11 @@ func UpdateEndpoint(contextName string, hostname string, port int, confpath stri return false, errors.Wrap(err, "read") } - address := "https://" + hostname + ":" + strconv.Itoa(port) - - // if the kubeconfig is missed, create new one - if len(cfg.Clusters) == 0 { - lp := localpath.Profile(contextName) - gp := localpath.MiniPath() - kcs := &Settings{ - ClusterName: contextName, - ClusterServerAddress: address, - ClientCertificate: path.Join(lp, "client.crt"), - ClientKey: path.Join(lp, "client.key"), - CertificateAuthority: path.Join(gp, "ca.crt"), - KeepContext: false, - } - err = PopulateFromSettings(kcs, cfg) - if err != nil { - return false, errors.Wrap(err, "populating kubeconfig") - } + if _, ok := cfg.Clusters[contextName]; !ok { + return false, errors.Errorf("%q does not appear in %s", contextName, confpath) } - cfg.Clusters[contextName].Server = address + + cfg.Clusters[contextName].Server = "https://" + hostname + ":" + strconv.Itoa(port) err = writeToFile(cfg, confpath) if err != nil { return false, errors.Wrap(err, "write") diff --git a/pkg/minikube/kubeconfig/kubeconfig_test.go b/pkg/minikube/kubeconfig/kubeconfig_test.go index e0ed479b88..7b60e1b23e 100644 --- a/pkg/minikube/kubeconfig/kubeconfig_test.go +++ b/pkg/minikube/kubeconfig/kubeconfig_test.go @@ -343,6 +343,18 @@ func TestUpdateIP(t *testing.T) { if test.status != statusActual { t.Errorf("Expected status %t, but got %t", test.status, statusActual) } + + actual, err := readOrNew(configFilename) + if err != nil { + t.Fatal(err) + } + expected, err := decode(test.expCfg) + if err != nil { + t.Fatal(err) + } + if !configEquals(actual, expected) { + t.Errorf("Expected cfg %v, but got %v", expected, actual) + } }) }