Skip to content

Commit

Permalink
Modified advertise-client-urls & initial-advertise-peer-urls of confi…
Browse files Browse the repository at this point in the history
…gmap to use proper url format instead of @ separator
  • Loading branch information
anveshreddy18 committed Sep 2, 2024
1 parent edff58c commit 21945e3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pkg/member/member_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var _ = Describe("Membercontrol", func() {
quota-backend-bytes: 1073741824
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://0.0.0.0:2379
initial-advertise-peer-urls: http@etcd-main-peer@default@2380
initial-advertise-peer-urls: http://etcd-main-peer.default:2380
initial-cluster: etcd1=http://0.0.0.0:2380
initial-cluster-token: new
initial-cluster-state: new
Expand Down
19 changes: 13 additions & 6 deletions pkg/miscellaneous/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -541,12 +542,18 @@ func ReadConfigFileAsMap(path string) (map[string]interface{}, error) {

// ParsePeerURL forms a PeerURL, given podName by parsing the initial-advertise-peer-urls
func ParsePeerURL(initialAdvertisePeerURLs, podName string) (string, error) {
tokens := strings.Split(initialAdvertisePeerURLs, "@")
if len(tokens) < 4 {
return "", fmt.Errorf("invalid peer URL : %s", initialAdvertisePeerURLs)
}
domaiName := fmt.Sprintf("%s.%s.%s", tokens[1], tokens[2], "svc")
return fmt.Sprintf("%s://%s.%s:%s", tokens[0], podName, domaiName, tokens[3]), nil
var scheme, peerSvcName, namespace, peerPort string
re := regexp.MustCompile(`^([^:]+)://([^\.]+)\.([^:]+):(.+)$`)
tokens := re.FindStringSubmatch(initialAdvertisePeerURLs)
if len(tokens) != 5 {
return "", fmt.Errorf("invalid URL format: %s", initialAdvertisePeerURLs)
}
scheme = tokens[1]
peerSvcName = tokens[2]
namespace = tokens[3]
peerPort = tokens[4]
domainName := fmt.Sprintf("%s.%s.%s", peerSvcName, namespace, "svc")
return fmt.Sprintf("%s://%s.%s:%s", scheme, podName, domainName, peerPort), nil
}

// IsPeerURLTLSEnabled checks whether the peer address is TLS enabled or not.
Expand Down
8 changes: 4 additions & 4 deletions pkg/miscellaneous/miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,14 @@ var _ = Describe("Miscellaneous Tests", func() {

Context("parse peer url", func() {
It("parsing well-defined initial-advertise-peer-urls", func() {
initialAdPeerURL = "https@etcd-events-peer@shoot--dev--test@2380"
initialAdPeerURL = "https://etcd-events-peer.shoot--dev--test:2380"
peerURL, err := ParsePeerURL(initialAdPeerURL, podName)
Expect(err).To(BeNil())
Expect(peerURL).To(Equal("https://etcd-test-pod-0.etcd-events-peer.shoot--dev--test.svc:2380"))
})

It("parsing malformed initial-advertise-peer-urls", func() {
initialAdPeerURL = "https@etcd-events-peer@shoot--dev--test"
initialAdPeerURL = "https://etcd-events-peer.shoot--dev--test"
_, err := ParsePeerURL(initialAdPeerURL, podName)
Expect(err).ToNot(BeNil())
})
Expand Down Expand Up @@ -724,7 +724,7 @@ var _ = Describe("Miscellaneous Tests", func() {
Context("with non-TLS enabled peer url", func() {
BeforeEach(func() {
etcdConfigYaml := `name: etcd1
initial-advertise-peer-urls: http@etcd-main-peer@default@2380
initial-advertise-peer-urls: http://etcd-main-peer.default:2380
initial-cluster: etcd1=http://0.0.0.0:2380`
err := os.WriteFile(outfile, []byte(etcdConfigYaml), 0755)
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -740,7 +740,7 @@ initial-cluster: etcd1=http://0.0.0.0:2380`
Context("with TLS enabled peer url", func() {
BeforeEach(func() {
etcdConfigYaml := `name: etcd1
initial-advertise-peer-urls: https@etcd-main-peer@default@2380
initial-advertise-peer-urls: https://etcd-main-peer.default:2380
initial-cluster: etcd1=https://0.0.0.0:2380`
err := os.WriteFile(outfile, []byte(etcdConfigYaml), 0755)
Expect(err).ShouldNot(HaveOccurred())
Expand Down
25 changes: 10 additions & 15 deletions pkg/server/httpAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -439,7 +440,7 @@ func (h *HTTPHandler) serveConfig(rw http.ResponseWriter, req *http.Request) {
config["name"] = podName

initAdPeerURL := config["initial-advertise-peer-urls"]
protocol, svcName, namespace, peerPort, err := parsePeerURL(fmt.Sprint(initAdPeerURL))
protocol, svcName, namespace, peerPort, err := parseAdvertiseURL(fmt.Sprint(initAdPeerURL))
if err != nil {
h.Logger.Warnf("Unable to determine service name, namespace, peer port from advertise peer urls : %v", err)
rw.WriteHeader(http.StatusInternalServerError)
Expand All @@ -449,7 +450,7 @@ func (h *HTTPHandler) serveConfig(rw http.ResponseWriter, req *http.Request) {
config["initial-advertise-peer-urls"] = fmt.Sprintf("%s://%s.%s:%s", protocol, podName, domaiName, peerPort)

advClientURL := config["advertise-client-urls"]
protocol, svcName, namespace, clientPort, err := parseAdvClientURL(fmt.Sprint(advClientURL))
protocol, svcName, namespace, clientPort, err := parseAdvertiseURL(fmt.Sprint(advClientURL))
if err != nil {
h.Logger.Warnf("Unable to determine service name, namespace, peer port from advertise client url : %v", err)
rw.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -578,20 +579,14 @@ func getInitialCluster(ctx context.Context, initialCluster string, etcdConn brty
return initialCluster
}

func parsePeerURL(peerURL string) (string, string, string, string, error) {
tokens := strings.Split(peerURL, "@")
if len(tokens) < 4 {
return "", "", "", "", fmt.Errorf("total length of tokens is less than four")
// parseAdvertiseURL parses the advertise URL and returns the protocol, service name, namespace and port.
func parseAdvertiseURL(url string) (string, string, string, string, error) {
re := regexp.MustCompile(`^([^:]+)://([^\.]+)\.([^:]+):(.+)$`)
tokens := re.FindStringSubmatch(url)
if len(tokens) != 5 {
return "", "", "", "", fmt.Errorf("invalid URL format: %s", url)
}
return tokens[0], tokens[1], tokens[2], tokens[3], nil
}

func parseAdvClientURL(advClientURL string) (string, string, string, string, error) {
tokens := strings.Split(advClientURL, "@")
if len(tokens) < 4 {
return "", "", "", "", fmt.Errorf("total length of tokens is less than four")
}
return tokens[0], tokens[1], tokens[2], tokens[3], nil
return tokens[1], tokens[2], tokens[3], tokens[4], nil
}

// delegateReqToLeader forwards the incoming http/https request to BackupLeader.
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/integration/cloud_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ enable-v2: false
quota-backend-bytes: 1073741824
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://0.0.0.0:2379
initial-advertise-peer-urls: http@etcd-main-peer@default@2380
initial-advertise-peer-urls: http://etcd-main-peer.default:2380
initial-cluster: etcd1=http://0.0.0.0:2380
initial-cluster-token: new
initial-cluster-state: new
Expand Down

0 comments on commit 21945e3

Please sign in to comment.