Skip to content

Commit 1ece2a6

Browse files
making endpoint configurable, attempt to extract host
1 parent 8ae7b8a commit 1ece2a6

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

cns/configuration/cns_config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"UseHTTPS" : false,
1919
"TLSSubjectName" : "",
2020
"TLSCertificatePath" : "",
21-
"TLSEndpoint" : "localhost:10091"
21+
"TLSEndpoint" : "localhost:10091",
22+
"WireserverIP": "168.63.129.16"
2223
}

cns/configuration/configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type CNSConfig struct {
2424
TLSSubjectName string
2525
TLSCertificatePath string
2626
TLSEndpoint string
27+
WireserverIP string
2728
}
2829

2930
type TelemetrySettings struct {

cns/restserver/api.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"net"
1010
"net/http"
11+
"regexp"
1112
"runtime"
1213
"strings"
1314

@@ -1139,6 +1140,16 @@ func getAuthTokenFromCreateNetworkContainerURL(
11391140
return strings.Split(strings.Split(createNetworkContainerURL, "authenticationToken/")[1], "/")[0]
11401141
}
11411142

1143+
var rgx = regexp.MustCompile("^http[s]?://(.*?)/joinedVirtualNetworks.*?$")
1144+
1145+
func extractHostFromJoinNetworkURL(url string) string {
1146+
submatches := rgx.FindStringSubmatch(url)
1147+
if len(submatches) != 2 {
1148+
return ""
1149+
}
1150+
return submatches[1]
1151+
}
1152+
11421153
// Publish Network Container by calling nmagent
11431154
func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r *http.Request) {
11441155
logger.Printf("[Azure-CNS] PublishNetworkContainer")
@@ -1199,8 +1210,15 @@ func (service *HTTPRestService) publishNetworkContainer(w http.ResponseWriter, r
11991210
// Store ncGetVersionURL needed for calling NMAgent to check if vfp programming is completed for the NC
12001211
primaryInterfaceIdentifier := getInterfaceIdFromCreateNetworkContainerURL(req.CreateNetworkContainerURL)
12011212
authToken := getAuthTokenFromCreateNetworkContainerURL(req.CreateNetworkContainerURL)
1213+
1214+
// we attempt to extract the wireserver IP to use from the request, otherwise default to the well-known IP.
1215+
hostIP := extractHostFromJoinNetworkURL(req.JoinNetworkURL)
1216+
if hostIP == "" {
1217+
hostIP = nmagentclient.WireserverIP
1218+
}
1219+
12021220
ncGetVersionURL := fmt.Sprintf(nmagentclient.GetNetworkContainerVersionURLFmt,
1203-
nmagentclient.WireserverIP,
1221+
hostIP,
12041222
primaryInterfaceIdentifier,
12051223
req.NetworkContainerID,
12061224
authToken)

cns/restserver/api_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,16 @@ func publishNCViaCNS(t *testing.T,
553553
fmt.Printf("PublishNetworkContainer succeded with response %+v, raw:%+v\n", resp, w.Body)
554554
}
555555

556+
func TestExtractHost(t *testing.T) {
557+
joinURL := "http://127.0.0.1:9001/joinedVirtualNetworks/c9b8e695-2de1-11eb-bf54-000d3af666c8/api-version/1"
558+
559+
host := extractHostFromJoinNetworkURL(joinURL)
560+
expected := "127.0.0.1:9001"
561+
if host != expected {
562+
t.Fatalf("expected host %q, got %q", expected, host)
563+
}
564+
}
565+
556566
func TestUnpublishNCViaCNS(t *testing.T) {
557567
fmt.Println("Test: unpublishNetworkContainer")
558568

cns/service/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ func main() {
404404
configuration.SetCNSConfigDefaults(&cnsconfig)
405405
logger.Printf("[Azure CNS] Read config :%+v", cnsconfig)
406406

407+
if cnsconfig.WireserverIP != "" {
408+
nmagentclient.WireserverIP = cnsconfig.WireserverIP
409+
}
410+
407411
if cnsconfig.ChannelMode == cns.Managed {
408412
config.ChannelMode = cns.Managed
409413
privateEndpoint = cnsconfig.ManagedSettings.PrivateEndpoint

0 commit comments

Comments
 (0)