Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minikube update-context: Fix nil pointer dereference #8989

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/minikube/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func UpdateEndpoint(contextName string, hostname string, port int, confpath stri

address := "https://" + hostname + ":" + strconv.Itoa(port)

// if the kubeconfig is missed, create new one
tstromberg marked this conversation as resolved.
Show resolved Hide resolved
if len(cfg.Clusters) == 0 {
// if the cluster setting is missed in the kubeconfig, create new one
if _, ok := cfg.Clusters[contextName]; !ok {
lp := localpath.Profile(contextName)
gp := localpath.MiniPath()
kcs := &Settings{
Expand All @@ -139,8 +139,10 @@ func UpdateEndpoint(contextName string, hostname string, port int, confpath stri
if err != nil {
return false, errors.Wrap(err, "populating kubeconfig")
}
} else {
cfg.Clusters[contextName].Server = address
}
cfg.Clusters[contextName].Server = address

err = writeToFile(cfg, confpath)
if err != nil {
return false, errors.Wrap(err, "write")
Expand Down
94 changes: 92 additions & 2 deletions pkg/minikube/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath"

"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -51,6 +52,40 @@ users:
client-key: /home/la-croix/apiserver.key
`)

var kubeConfigWithoutHTTPSUpdated = []byte(`
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/la-croix/apiserver.crt
server: 192.168.1.1:8080
name: la-croix
- cluster:
certificate-authority: /home/la-croix/.minikube/ca.crt
server: https://192.168.10.100:8080
name: minikube
contexts:
- context:
cluster: la-croix
user: la-croix
name: la-croix
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: la-croix
user:
client-certificate: /home/la-croix/apiserver.crt
client-key: /home/la-croix/apiserver.key
- name: minikube
user:
client-certificate: /home/la-croix/.minikube/profiles/minikube/client.crt
client-key: /home/la-croix/.minikube/profiles/minikube/client.key
`)

var kubeConfig192 = []byte(`
apiVersion: v1
clusters:
Expand Down Expand Up @@ -117,6 +152,37 @@ users:
client-key: /home/la-croix/apiserver.key
`)

var kubeConfigNoClusters = []byte(`
apiVersion: v1
clusters:
contexts:
kind: Config
preferences: {}
users:
`)

var kubeConfigNoClustersUpdated = []byte(`
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/la-croix/.minikube/ca.crt
server: https://192.168.10.100:8080
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /home/la-croix/.minikube/profiles/minikube/client.crt
client-key: /home/la-croix/.minikube/profiles/minikube/client.key
`)

func TestUpdate(t *testing.T) {
setupCfg := &Settings{
ClusterName: "test",
Expand Down Expand Up @@ -300,8 +366,8 @@ func TestUpdateIP(t *testing.T) {
hostname: "192.168.10.100",
port: 8080,
existing: kubeConfigWithoutHTTPS,
err: true,
expCfg: kubeConfigWithoutHTTPS,
status: true,
expCfg: kubeConfigWithoutHTTPSUpdated,
},
{
description: "same IP",
Expand All @@ -326,8 +392,20 @@ func TestUpdateIP(t *testing.T) {
status: true,
expCfg: kubeConfigLocalhost12345,
},
{
description: "no clusters",
hostname: "192.168.10.100",
port: 8080,
existing: kubeConfigNoClusters,
status: true,
expCfg: kubeConfigNoClustersUpdated,
},
}

os.Setenv(localpath.MinikubeHome, "/home/la-croix")

for _, test := range tests {
test := test
t.Run(test.description, func(t *testing.T) {
t.Parallel()
configFilename := tempFile(t, test.existing)
Expand All @@ -342,6 +420,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.Fatal("configs did not match")
}
})

}
Expand Down
84 changes: 78 additions & 6 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,14 +1007,86 @@ func validateCertSync(ctx context.Context, t *testing.T, profile string) {
func validateUpdateContextCmd(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile)

rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "update-context", "--alsologtostderr", "-v=2"))
if err != nil {
t.Errorf("failed to run minikube update-context: args %q: %v", rr.Command(), err)
tests := []struct {
name string
kubeconfig []byte
want []byte
}{
{
name: "no changes",
kubeconfig: nil,
want: []byte("No changes"),
},
{
name: "no minikube cluster",
kubeconfig: []byte(`
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/la-croix/apiserver.crt
server: 192.168.1.1:8080
name: la-croix
contexts:
- context:
cluster: la-croix
user: la-croix
name: la-croix
current-context: la-croix
kind: Config
preferences: {}
users:
- name: la-croix
user:
client-certificate: /home/la-croix/apiserver.crt
client-key: /home/la-croix/apiserver.key
`),
want: []byte("context has been updated"),
},
{
name: "no clusters",
kubeconfig: []byte(`
apiVersion: v1
clusters:
contexts:
kind: Config
preferences: {}
users:
`),
want: []byte("context has been updated"),
},
}

want := []byte("No changes")
if !bytes.Contains(rr.Stdout.Bytes(), want) {
t.Errorf("update-context: got=%q, want=*%q*", rr.Stdout.Bytes(), want)
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
c := exec.CommandContext(ctx, Target(), "-p", profile, "update-context", "--alsologtostderr", "-v=2")
if tc.kubeconfig != nil {
tf, err := ioutil.TempFile("", "kubeconfig")
if err != nil {
t.Fatal(err)
}

if err := ioutil.WriteFile(tf.Name(), tc.kubeconfig, 0644); err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
os.Remove(tf.Name())
})

c.Env = append(os.Environ(), fmt.Sprintf("KUBECONFIG=%s", tf.Name()))
}

rr, err := Run(t, c)
if err != nil {
t.Errorf("failed to run minikube update-context: args %q: %v", rr.Command(), err)
}

if !bytes.Contains(rr.Stdout.Bytes(), tc.want) {
t.Errorf("update-context: got=%q, want=*%q*", rr.Stdout.Bytes(), tc.want)
}
})
}
}

Expand Down