Skip to content

Commit c8d2c1f

Browse files
CNS - Wireserver "proxy" (#1825)
* adding wireserver proxy * using proxy for publish * injecting dependency. refactoring unpublish nc * default wireserver ip config * setting content type * better printing of requests and responses * fixing unit tests
1 parent 0863a81 commit c8d2c1f

File tree

14 files changed

+400
-464
lines changed

14 files changed

+400
-464
lines changed

cns/NetworkContainerContract.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,15 @@ type PublishNetworkContainerRequest struct {
500500
CreateNetworkContainerRequestBody []byte
501501
}
502502

503+
func (p PublishNetworkContainerRequest) String() string {
504+
// %q as a verb on a byte slice prints safely escaped text instead of individual bytes
505+
return fmt.Sprintf("{NetworkID:%s NetworkContainerID:%s JoinNetworkURL:%s CreateNetworkContainerURL:%s CreateNetworkContainerRequestBody:%q}",
506+
p.NetworkID, p.NetworkContainerID, p.JoinNetworkURL, p.CreateNetworkContainerURL, p.CreateNetworkContainerRequestBody)
507+
}
508+
503509
// NetworkContainerParameters parameters available in network container operations
504510
type NetworkContainerParameters struct {
511+
NCID string
505512
AuthToken string
506513
AssociatedInterfaceID string
507514
}
@@ -514,6 +521,12 @@ type PublishNetworkContainerResponse struct {
514521
PublishResponseBody []byte
515522
}
516523

524+
func (p PublishNetworkContainerResponse) String() string {
525+
// %q as a verb on a byte slice prints safely escaped text instead of individual bytes
526+
return fmt.Sprintf("{Response:%+v PublishErrStr:%s PublishStatusCode:%d PublishResponseBody:%q}",
527+
p.Response, p.PublishErrorStr, p.PublishStatusCode, p.PublishResponseBody)
528+
}
529+
517530
// UnpublishNetworkContainerRequest specifies request to unpublish network container via NMAgent.
518531
type UnpublishNetworkContainerRequest struct {
519532
NetworkID string
@@ -530,6 +543,12 @@ type UnpublishNetworkContainerResponse struct {
530543
UnpublishResponseBody []byte
531544
}
532545

546+
func (u UnpublishNetworkContainerResponse) String() string {
547+
// %q as a verb on a byte slice prints safely escaped text instead of individual bytes
548+
return fmt.Sprintf("{Response:%+v UnpublishErrorStr:%s UnpublishStatusCode:%d UnpublishResponseBody:%q}",
549+
u.Response, u.UnpublishErrorStr, u.UnpublishStatusCode, u.UnpublishResponseBody)
550+
}
551+
533552
// ValidAclPolicySetting - Used to validate ACL policy
534553
type ValidAclPolicySetting struct {
535554
Protocols string `json:","`

cns/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestMain(m *testing.M) {
169169
logger.InitLogger(logName, 0, 0, tmpLogDir+"/")
170170
config := common.ServiceConfig{}
171171

172-
httpRestService, err := restserver.NewHTTPRestService(&config, &fakes.WireserverClientFake{}, &fakes.NMAgentClientFake{}, nil, nil, nil)
172+
httpRestService, err := restserver.NewHTTPRestService(&config, &fakes.WireserverClientFake{}, &fakes.WireserverProxyFake{}, &fakes.NMAgentClientFake{}, nil, nil, nil)
173173
svc = httpRestService.(*restserver.HTTPRestService)
174174
svc.Name = "cns-test-server"
175175
fakeNNC := v1alpha.NodeNetworkConfig{

cns/configuration/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,7 @@ func SetCNSConfigDefaults(config *CNSConfig) {
193193
// set the default PopulateHomeAzCache retry interval to 15 seconds
194194
config.PopulateHomeAzCacheRetryIntervalSecs = 15
195195
}
196+
if config.WireserverIP == "" {
197+
config.WireserverIP = "168.63.129.16"
198+
}
196199
}

cns/configuration/configuration_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func TestSetCNSConfigDefaults(t *testing.T) {
207207
RefreshIntervalInHrs: 12,
208208
},
209209
PopulateHomeAzCacheRetryIntervalSecs: 15,
210+
WireserverIP: "168.63.129.16",
210211
},
211212
},
212213
{
@@ -250,6 +251,7 @@ func TestSetCNSConfigDefaults(t *testing.T) {
250251
RefreshIntervalInHrs: 3,
251252
},
252253
PopulateHomeAzCacheRetryIntervalSecs: 10,
254+
WireserverIP: "168.63.129.16",
253255
},
254256
},
255257
}

cns/fakes/nmagentclientfake.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,9 @@ import (
1414

1515
// NMAgentClientFake can be used to query to VM Host info.
1616
type NMAgentClientFake struct {
17-
PutNetworkContainerF func(context.Context, *nmagent.PutNetworkContainerRequest) error
18-
DeleteNetworkContainerF func(context.Context, nmagent.DeleteContainerRequest) error
19-
JoinNetworkF func(context.Context, nmagent.JoinNetworkRequest) error
20-
SupportedAPIsF func(context.Context) ([]string, error)
21-
GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error)
22-
GetHomeAzF func(context.Context) (nmagent.AzResponse, error)
23-
}
24-
25-
func (n *NMAgentClientFake) PutNetworkContainer(ctx context.Context, req *nmagent.PutNetworkContainerRequest) error {
26-
return n.PutNetworkContainerF(ctx, req)
27-
}
28-
29-
func (n *NMAgentClientFake) DeleteNetworkContainer(ctx context.Context, req nmagent.DeleteContainerRequest) error {
30-
return n.DeleteNetworkContainerF(ctx, req)
31-
}
32-
33-
func (n *NMAgentClientFake) JoinNetwork(ctx context.Context, req nmagent.JoinNetworkRequest) error {
34-
return n.JoinNetworkF(ctx, req)
17+
SupportedAPIsF func(context.Context) ([]string, error)
18+
GetNCVersionListF func(context.Context) (nmagent.NCVersionList, error)
19+
GetHomeAzF func(context.Context) (nmagent.AzResponse, error)
3520
}
3621

3722
func (n *NMAgentClientFake) SupportedAPIs(ctx context.Context) ([]string, error) {

cns/fakes/wireserverproxyfake.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package fakes
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io"
7+
"net/http"
8+
9+
"github.com/Azure/azure-container-networking/cns"
10+
)
11+
12+
type WireserverProxyFake struct {
13+
JoinNetworkFunc func(context.Context, string) (*http.Response, error)
14+
PublishNCFunc func(context.Context, cns.NetworkContainerParameters, []byte) (*http.Response, error)
15+
UnpublishNCFunc func(context.Context, cns.NetworkContainerParameters) (*http.Response, error)
16+
}
17+
18+
const defaultResponseBody = `{"httpStatusCode":"200"}`
19+
20+
func defaultResponse() *http.Response {
21+
return &http.Response{
22+
StatusCode: http.StatusOK,
23+
Body: io.NopCloser(bytes.NewBufferString(defaultResponseBody)),
24+
ContentLength: int64(len(defaultResponseBody)),
25+
}
26+
}
27+
28+
func (w *WireserverProxyFake) JoinNetwork(ctx context.Context, vnetID string) (*http.Response, error) {
29+
if w.JoinNetworkFunc != nil {
30+
return w.JoinNetworkFunc(ctx, vnetID)
31+
}
32+
33+
return defaultResponse(), nil
34+
}
35+
36+
func (w *WireserverProxyFake) PublishNC(ctx context.Context, ncParams cns.NetworkContainerParameters, payload []byte) (*http.Response, error) {
37+
if w.PublishNCFunc != nil {
38+
return w.PublishNCFunc(ctx, ncParams, payload)
39+
}
40+
41+
return defaultResponse(), nil
42+
}
43+
44+
func (w *WireserverProxyFake) UnpublishNC(ctx context.Context, ncParams cns.NetworkContainerParameters) (*http.Response, error) {
45+
if w.UnpublishNCFunc != nil {
46+
return w.UnpublishNCFunc(ctx, ncParams)
47+
}
48+
49+
return defaultResponse(), nil
50+
}

0 commit comments

Comments
 (0)