@@ -17,16 +17,21 @@ limitations under the License.
17
17
package kubeconfig
18
18
19
19
import (
20
+ "fmt"
20
21
"io/ioutil"
22
+ "net"
23
+ "net/url"
21
24
"os"
22
25
"path/filepath"
26
+ "strconv"
23
27
"sync/atomic"
24
28
25
29
"github.com/golang/glog"
26
30
"github.com/pkg/errors"
27
31
"k8s.io/apimachinery/pkg/runtime"
28
32
"k8s.io/client-go/tools/clientcmd/api"
29
33
"k8s.io/client-go/tools/clientcmd/api/latest"
34
+ "k8s.io/minikube/pkg/minikube/constants"
30
35
)
31
36
32
37
type KubeConfigSetup struct {
@@ -178,3 +183,66 @@ func decode(data []byte) (*api.Config, error) {
178
183
179
184
return config .(* api.Config ), nil
180
185
}
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
+ "\n To 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