mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Merge pull request #70633 from leblancd/kubeadm_etcd_v6_fix
Fix kubeadm etcd manifests to use brackets around IPv6 addrs
This commit is contained in:
@@ -24,6 +24,7 @@ go_test(
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
|
||||
"//cmd/kubeadm/app/constants:go_default_library",
|
||||
"//cmd/kubeadm/test:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@@ -20,7 +20,9 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -171,7 +173,7 @@ func NewFromCluster(client clientset.Interface, certificatesDir string) (*Client
|
||||
// Get the list of etcd endpoints from cluster status
|
||||
endpoints := []string{}
|
||||
for _, e := range clusterStatus.APIEndpoints {
|
||||
endpoints = append(endpoints, fmt.Sprintf("https://%s:%d", e.AdvertiseAddress, constants.EtcdListenClientPort))
|
||||
endpoints = append(endpoints, GetClientURLByIP(e.AdvertiseAddress))
|
||||
}
|
||||
klog.V(1).Infof("etcd endpoints read from pods: %s", strings.Join(endpoints, ","))
|
||||
|
||||
@@ -358,3 +360,21 @@ func (c Client) WaitForClusterAvailable(delay time.Duration, retries int, retryI
|
||||
func CheckConfigurationIsHA(cfg *kubeadmapi.Etcd) bool {
|
||||
return cfg.External != nil && len(cfg.External.Endpoints) > 1
|
||||
}
|
||||
|
||||
// GetClientURL creates an HTTPS URL that uses the configured advertise
|
||||
// address and client port for the API controller
|
||||
func GetClientURL(cfg *kubeadmapi.InitConfiguration) string {
|
||||
return "https://" + net.JoinHostPort(cfg.LocalAPIEndpoint.AdvertiseAddress, strconv.Itoa(constants.EtcdListenClientPort))
|
||||
}
|
||||
|
||||
// GetPeerURL creates an HTTPS URL that uses the configured advertise
|
||||
// address and peer port for the API controller
|
||||
func GetPeerURL(cfg *kubeadmapi.InitConfiguration) string {
|
||||
return "https://" + net.JoinHostPort(cfg.LocalAPIEndpoint.AdvertiseAddress, strconv.Itoa(constants.EtcdListenPeerPort))
|
||||
}
|
||||
|
||||
// GetClientURLByIP creates an HTTPS URL based on an IP address
|
||||
// and the client listening port.
|
||||
func GetClientURLByIP(ip string) string {
|
||||
return "https://" + net.JoinHostPort(ip, strconv.Itoa(constants.EtcdListenClientPort))
|
||||
}
|
||||
|
@@ -17,12 +17,15 @@ limitations under the License.
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
|
||||
)
|
||||
|
||||
@@ -308,3 +311,90 @@ func TestCheckConfigurationIsHA(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testGetURL(t *testing.T, getURLFunc func(*kubeadmapi.InitConfiguration) string, port int) {
|
||||
portStr := strconv.Itoa(port)
|
||||
var tests = []struct {
|
||||
name string
|
||||
advertiseAddress string
|
||||
expectedURL string
|
||||
}{
|
||||
{
|
||||
name: "IPv4",
|
||||
advertiseAddress: "10.10.10.10",
|
||||
expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv6",
|
||||
advertiseAddress: "2001:db8::2",
|
||||
expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv4 localhost",
|
||||
advertiseAddress: "127.0.0.1",
|
||||
expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv6 localhost",
|
||||
advertiseAddress: "::1",
|
||||
expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
cfg := &kubeadmapi.InitConfiguration{
|
||||
LocalAPIEndpoint: kubeadmapi.APIEndpoint{
|
||||
AdvertiseAddress: test.advertiseAddress,
|
||||
},
|
||||
}
|
||||
url := getURLFunc(cfg)
|
||||
if url != test.expectedURL {
|
||||
t.Errorf("expected %s, got %s", test.expectedURL, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientURL(t *testing.T) {
|
||||
testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
|
||||
}
|
||||
|
||||
func TestGetPeerURL(t *testing.T) {
|
||||
testGetURL(t, GetClientURL, constants.EtcdListenClientPort)
|
||||
}
|
||||
|
||||
func TestGetClientURLByIP(t *testing.T) {
|
||||
portStr := strconv.Itoa(constants.EtcdListenClientPort)
|
||||
var tests = []struct {
|
||||
name string
|
||||
ip string
|
||||
expectedURL string
|
||||
}{
|
||||
{
|
||||
name: "IPv4",
|
||||
ip: "10.10.10.10",
|
||||
expectedURL: fmt.Sprintf("https://10.10.10.10:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv6",
|
||||
ip: "2001:db8::2",
|
||||
expectedURL: fmt.Sprintf("https://[2001:db8::2]:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv4 localhost",
|
||||
ip: "127.0.0.1",
|
||||
expectedURL: fmt.Sprintf("https://127.0.0.1:%s", portStr),
|
||||
},
|
||||
{
|
||||
name: "IPv6 localhost",
|
||||
ip: "::1",
|
||||
expectedURL: fmt.Sprintf("https://[::1]:%s", portStr),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
url := GetClientURLByIP(test.ip)
|
||||
if url != test.expectedURL {
|
||||
t.Errorf("expected %s, got %s", test.expectedURL, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user