endpoint.go 1.7 KB
Newer Older
T
Thomas Stromberg 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
Copyright 2020 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package driver

import (
D
Dean Coakley 已提交
20
	"fmt"
T
Thomas Stromberg 已提交
21 22 23 24 25 26 27
	"net"

	"k8s.io/minikube/pkg/drivers/kic/oci"
	"k8s.io/minikube/pkg/minikube/config"
	"k8s.io/minikube/pkg/minikube/constants"
)

28 29
// ControlPlaneEndpoint returns the location where callers can reach this cluster
func ControlPlaneEndpoint(cc *config.ClusterConfig, cp *config.Node, driverName string) (string, net.IP, int, error) {
T
Thomas Stromberg 已提交
30 31 32 33
	if NeedsPortForward(driverName) {
		port, err := oci.ForwardedPort(cc.Driver, cc.Name, cp.Port)
		hostname := oci.DefaultBindIPV4
		ip := net.ParseIP(hostname)
D
Dean Coakley 已提交
34 35 36
		if ip == nil {
			return hostname, ip, port, fmt.Errorf("failed to parse ip for %q", hostname)
		}
T
Thomas Stromberg 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49

		// https://github.com/kubernetes/minikube/issues/3878
		if cc.KubernetesConfig.APIServerName != constants.APIServerName {
			hostname = cc.KubernetesConfig.APIServerName
		}
		return hostname, ip, port, err
	}

	// https://github.com/kubernetes/minikube/issues/3878
	hostname := cp.IP
	if cc.KubernetesConfig.APIServerName != constants.APIServerName {
		hostname = cc.KubernetesConfig.APIServerName
	}
D
Dean Coakley 已提交
50 51 52 53 54
	ip := net.ParseIP(cp.IP)
	if ip == nil {
		return hostname, ip, cp.Port, fmt.Errorf("failed to parse ip for %q", cp.IP)
	}
	return hostname, ip, cp.Port, nil
T
Thomas Stromberg 已提交
55
}