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 Feb 27, 2024
1 parent edff58c commit d904c2b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 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
23 changes: 17 additions & 6 deletions pkg/miscellaneous/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,23 @@ 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
tokens := strings.Split(initialAdvertisePeerURLs, "://")
if len(tokens) < 2 {
return "", fmt.Errorf("invalid URL format : %s", initialAdvertisePeerURLs)
}
protocol := tokens[0]
tokens = strings.Split(tokens[1], ".")
if len(tokens) < 2 {
return "", fmt.Errorf("invalid URL format : %s", initialAdvertisePeerURLs)
}
svcName := tokens[0]
tokens = strings.Split(tokens[1], ":")
if len(tokens) < 2 {
return "", fmt.Errorf("invalid URL format : %s", initialAdvertisePeerURLs)
}
namespace, peerPort := tokens[0], tokens[1]
domaiName := fmt.Sprintf("%s.%s.%s", svcName, namespace, "svc")
return fmt.Sprintf("%s://%s.%s:%s", protocol, podName, domaiName, 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
32 changes: 18 additions & 14 deletions pkg/server/httpAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,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 +449,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 +578,24 @@ 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) {
tokens := strings.Split(url, "://")
if len(tokens) < 2 {
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")
protocol := tokens[0]
tokens = strings.Split(tokens[1], ".")
if len(tokens) < 2 {
return "", "", "", "", fmt.Errorf("invalid URL format : %s", url)
}
svcName := tokens[0]
tokens = strings.Split(tokens[1], ":")
if len(tokens) < 2 {
return "", "", "", "", fmt.Errorf("invalid URL format : %s", url)
}
return tokens[0], tokens[1], tokens[2], tokens[3], nil
namespace, port := tokens[0], tokens[1]
return protocol, svcName, namespace, port, 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 d904c2b

Please sign in to comment.