Skip to content

Commit d4faeea

Browse files
committed
Added update-context and kubeconfig to status
1 parent baa724d commit d4faeea

File tree

6 files changed

+360
-4
lines changed

6 files changed

+360
-4
lines changed

cmd/minikube/cmd/status.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ import (
2727
cmdUtil "k8s.io/minikube/cmd/util"
2828
"k8s.io/minikube/pkg/minikube/cluster"
2929
"k8s.io/minikube/pkg/minikube/constants"
30+
kcfg "k8s.io/minikube/pkg/minikube/kubeconfig"
3031
"k8s.io/minikube/pkg/minikube/machine"
3132
)
3233

3334
var statusFormat string
3435

3536
type Status struct {
36-
MinikubeStatus string
37-
LocalkubeStatus string
37+
MinikubeStatus string
38+
LocalkubeStatus string
39+
KubeconfigStatus string
3840
}
3941

4042
// statusCmd represents the status command
@@ -57,14 +59,26 @@ var statusCmd = &cobra.Command{
5759
}
5860

5961
ls := state.None.String()
62+
ks := state.None.String()
6063
if ms == state.Running.String() {
6164
ls, err = cluster.GetLocalkubeStatus(api)
6265
if err != nil {
6366
glog.Errorln("Error localkube status:", err)
6467
cmdUtil.MaybeReportErrorAndExit(err)
6568
}
69+
ip, err := cluster.GetHostDriverIP(api)
70+
if err != nil {
71+
glog.Errorln("Error host driver ip status:", err)
72+
cmdUtil.MaybeReportErrorAndExit(err)
73+
}
74+
ks, err = kcfg.GetKubeConfigStatus(ip, constants.KubeconfigPath)
75+
if err != nil {
76+
glog.Errorln("Error kubeconfig status:", err)
77+
cmdUtil.MaybeReportErrorAndExit(err)
78+
}
6679
}
67-
status := Status{ms, ls}
80+
81+
status := Status{ms, ls, ks}
6882

6983
tmpl, err := template.New("status").Parse(statusFormat)
7084
if err != nil {

cmd/minikube/cmd/update-context.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2016 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/golang/glog"
24+
"github.com/spf13/cobra"
25+
cmdUtil "k8s.io/minikube/cmd/util"
26+
"k8s.io/minikube/pkg/minikube/cluster"
27+
"k8s.io/minikube/pkg/minikube/constants"
28+
kcfg "k8s.io/minikube/pkg/minikube/kubeconfig"
29+
"k8s.io/minikube/pkg/minikube/machine"
30+
)
31+
32+
// updateContextCmd represents the update-context command
33+
var updateContextCmd = &cobra.Command{
34+
Use: "update-context",
35+
Short: "Verify the IP address of the running cluster in kubeconfig.",
36+
Long: `Retrieves the IP address of the running cluster, checks it
37+
with IP in kubeconfig, and corrects kubeconfig if incorrect.`,
38+
Run: func(cmd *cobra.Command, args []string) {
39+
api, err := machine.NewAPIClient(clientType)
40+
if err != nil {
41+
fmt.Fprintf(os.Stderr, "Error getting client: %s\n", err)
42+
os.Exit(1)
43+
}
44+
defer api.Close()
45+
ip, err := cluster.GetHostDriverIP(api)
46+
if err != nil {
47+
glog.Errorln("Error host driver ip status:", err)
48+
cmdUtil.MaybeReportErrorAndExit(err)
49+
}
50+
ks, err := kcfg.UpdateKubeconfigIP(ip, constants.KubeconfigPath)
51+
if err != nil {
52+
glog.Errorln("Error kubeconfig status:", err)
53+
cmdUtil.MaybeReportErrorAndExit(err)
54+
}
55+
fmt.Println(ks)
56+
},
57+
}
58+
59+
func init() {
60+
RootCmd.AddCommand(updateContextCmd)
61+
}

pkg/minikube/cluster/cluster.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ func GetLocalkubeStatus(api libmachine.API) (string, error) {
165165
}
166166
}
167167

168+
// GetHostDriverIP gets the ip address of the current minikube cluster
169+
func GetHostDriverIP(api libmachine.API) (net.IP, error) {
170+
host, err := CheckIfApiExistsAndLoad(api)
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
ipStr, err := host.Driver.GetIP()
176+
if err != nil {
177+
return nil, errors.Wrap(err, "Error getting IP")
178+
}
179+
ip := net.ParseIP(ipStr)
180+
if ip == nil {
181+
return nil, errors.Wrap(err, "Error parsing IP")
182+
}
183+
return ip, nil
184+
}
185+
168186
// StartCluster starts a k8s cluster on the specified Host.
169187
func StartCluster(api libmachine.API, kubernetesConfig KubernetesConfig) error {
170188
h, err := CheckIfApiExistsAndLoad(api)

pkg/minikube/constants/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const (
9090
MinimumDiskSizeMB = 2000
9191
DefaultVMDriver = "virtualbox"
9292
DefaultStatusFormat = "minikube: {{.MinikubeStatus}}\n" +
93-
"localkube: {{.LocalkubeStatus}}\n"
93+
"localkube: {{.LocalkubeStatus}}\n" + "kubectl: {{.KubeconfigStatus}}\n"
9494
DefaultAddonListFormat = "- {{.AddonName}}: {{.AddonStatus}}\n"
9595
DefaultConfigViewFormat = "- {{.ConfigKey}}: {{.ConfigValue}}\n"
9696
GithubMinikubeReleasesURL = "https://storage.googleapis.com/minikube/releases.json"

pkg/minikube/kubeconfig/config.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ limitations under the License.
1717
package kubeconfig
1818

1919
import (
20+
"fmt"
2021
"io/ioutil"
22+
"net"
23+
"net/url"
2124
"os"
2225
"path/filepath"
26+
"strconv"
2327
"sync/atomic"
2428

2529
"github.com/golang/glog"
2630
"github.com/pkg/errors"
2731
"k8s.io/apimachinery/pkg/runtime"
2832
"k8s.io/client-go/tools/clientcmd/api"
2933
"k8s.io/client-go/tools/clientcmd/api/latest"
34+
"k8s.io/minikube/pkg/minikube/constants"
3035
)
3136

3237
type KubeConfigSetup struct {
@@ -178,3 +183,66 @@ func decode(data []byte) (*api.Config, error) {
178183

179184
return config.(*api.Config), nil
180185
}
186+
187+
// GetKubeConfigStatus verifys the ip stored in kubeconfig.
188+
func GetKubeConfigStatus(ip net.IP, filename string) (string, error) {
189+
if ip == nil {
190+
return "", fmt.Errorf("Error, empty ip passed")
191+
}
192+
kip, err := getIPFromKubeConfig(filename)
193+
if err != nil {
194+
return "", err
195+
}
196+
if kip.Equal(ip) {
197+
return "Correctly Configured: pointing to minikube-vm at " + kip.String(), nil
198+
}
199+
return "Misconfigured: pointing to stale minikube-vm at " + kip.String() +
200+
"\nTo fix the kubectl context, run minikube update-context", nil
201+
202+
}
203+
204+
// UpdateKubeconfigIP overwrites the IP stored in kubeconfig with the provided IP.
205+
func UpdateKubeconfigIP(ip net.IP, filename string) (string, error) {
206+
if ip == nil {
207+
return "", fmt.Errorf("Error, empty ip passed")
208+
}
209+
kip, err := getIPFromKubeConfig(filename)
210+
if err != nil {
211+
return "", err
212+
}
213+
if kip.Equal(ip) {
214+
return "Correctly Configured: pointing to minikube-vm at " + kip.String(), nil
215+
}
216+
con, err := ReadConfigOrNew(filename)
217+
if err != nil {
218+
return "", errors.Wrap(err, "Error getting kubeconfig status")
219+
}
220+
con.Clusters["minikube"].Server = "https://" + ip.String() + ":" + strconv.Itoa(constants.APIServerPort)
221+
err = WriteConfig(con, filename)
222+
if err != nil {
223+
return "Unable to reconfigure Kubeconfig IP", nil
224+
}
225+
return "Reconfigured: pointing to minikube-vm at " + ip.String(), nil
226+
}
227+
228+
// getIPFromKubeConfig returns the IP address stored for minikube in the kubeconfig specified
229+
func getIPFromKubeConfig(filename string) (net.IP, error) {
230+
con, err := ReadConfigOrNew(filename)
231+
if err != nil {
232+
return nil, errors.Wrap(err, "Error getting kubeconfig status")
233+
}
234+
cluster, ok := con.Clusters["minikube"]
235+
if !ok {
236+
return nil, errors.Errorf("Kubeconfig does not have a record of a minikube cluster")
237+
}
238+
kurl, err := url.Parse(cluster.Server)
239+
if err != nil {
240+
return nil, errors.Wrap(err, "Unable to parse current IP as a url")
241+
}
242+
kip, _, err := net.SplitHostPort(kurl.Host)
243+
if err != nil {
244+
return nil, errors.Wrap(err, "Unable to split host and port")
245+
}
246+
ip := net.ParseIP(kip)
247+
return ip, nil
248+
}

0 commit comments

Comments
 (0)