Skip to content

Commit

Permalink
parse api server address
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Jul 29, 2021
1 parent 7bc8200 commit 3a22dda
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 7 deletions.
4 changes: 2 additions & 2 deletions deployments/liqo/values.yaml
Expand Up @@ -9,8 +9,8 @@ pullPolicy: "IfNotPresent"
apiServer:
# -- The address that must be used to contact your API server, it needs to be reachable from the clusters that you will peer with (defaults to your master IP)
address: ""
# -- The port that must be used to contact your API server
port: "6443"
# -- The port that must be used to contact your API server (if not yet specified in the address field)
port: ""
# -- Indicates that the API Server is exposing a certificate issued by a trusted Certification Authority
trustedCA: false

Expand Down
11 changes: 6 additions & 5 deletions pkg/kubeconfig/create.go
Expand Up @@ -56,7 +56,8 @@ func CreateKubeConfigFromServiceAccount(apiServerConfigProvider utils.ApiServerC
// 1. from the ClusterConfig
// 2. defaults to 6443.
func GetApiServerURL(apiServerConfigProvider utils.ApiServerConfigProvider, clientset kubernetes.Interface) (string, error) {
address := apiServerConfigProvider.GetAPIServerConfig().Address
config := apiServerConfigProvider.GetAPIServerConfig()
address := utils.GetHost(config.Address)
if address == "" {
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{
LabelSelector: "node-role.kubernetes.io/master",
Expand All @@ -77,11 +78,11 @@ func GetApiServerURL(apiServerConfigProvider utils.ApiServerConfigProvider, clie
}
}

port := apiServerConfigProvider.GetAPIServerConfig().Port
if port == "" {
port = "6443"
defaultPort := config.Port
if defaultPort == "" {
defaultPort = "443"
}

port := utils.GetPort(config.Address, defaultPort)
return fmt.Sprintf("https://%v:%v", address, port), nil
}

Expand Down
48 changes: 48 additions & 0 deletions pkg/utils/address.go
@@ -0,0 +1,48 @@
package utils

import (
"fmt"
"net/url"
"strings"

"k8s.io/klog/v2"
)

// GetHost returns the host in a given URL string.
func GetHost(urlString string) string {
if urlString == "" {
return urlString
}

u, err := parseURL(urlString)
if err != nil {
klog.Error(err)
return ""
}
return u.Hostname()
}

// GetPort returns the port in a given URL string, if no port is provided, it returns the defaultValue.
func GetPort(urlString, defaultValue string) string {
if urlString == "" {
return defaultValue
}

u, err := parseURL(urlString)
if err != nil {
klog.Error(err)
return defaultValue
}
port := u.Port()
if port == "" {
return defaultValue
}
return port
}

func parseURL(urlString string) (*url.URL, error) {
if !strings.HasPrefix(urlString, "https://") {
urlString = fmt.Sprintf("https://%v", urlString)
}
return url.Parse(urlString)
}
61 changes: 61 additions & 0 deletions pkg/utils/address_test.go
@@ -0,0 +1,61 @@
package utils

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)

func TestAddress(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Address Suite")
}

var _ = Describe("Address", func() {

type addressTestcase struct {
address string
defaultPort string
expectedHost string
expectedPort string
}

DescribeTable("Address table",

func(c addressTestcase) {
Expect(GetHost(c.address)).To(Equal(c.expectedHost))
Expect(GetPort(c.address, c.defaultPort)).To(Equal(c.expectedPort))
},

Entry("no protocol and no port", addressTestcase{
address: "localhost",
defaultPort: "6443",
expectedHost: "localhost",
expectedPort: "6443",
}),

Entry("protocol and no port", addressTestcase{
address: "https://localhost",
defaultPort: "6443",
expectedHost: "localhost",
expectedPort: "6443",
}),

Entry("protocol and port", addressTestcase{
address: "https://localhost:6443",
defaultPort: "",
expectedHost: "localhost",
expectedPort: "6443",
}),

Entry("no protocol and port", addressTestcase{
address: "localhost:6443",
defaultPort: "",
expectedHost: "localhost",
expectedPort: "6443",
}),
)

})

0 comments on commit 3a22dda

Please sign in to comment.