Skip to content

Commit 6d67a30

Browse files
committed
Check whether we are already attached/detached before hot attach/detach
1 parent 672e52e commit 6d67a30

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

hnsendpoint.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ func HNSListEndpointRequest() ([]HNSEndpoint, error) {
3939

4040
// HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
4141
func HotAttachEndpoint(containerID string, endpointID string) error {
42+
endpoint, err := GetHNSEndpointByID(endpointID)
43+
isAttached, err := endpoint.IsAttached(containerID)
44+
if isAttached {
45+
return err
46+
}
4247
return modifyNetworkEndpoint(containerID, endpointID, Add)
4348
}
4449

4550
// HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
4651
func HotDetachEndpoint(containerID string, endpointID string) error {
52+
endpoint, err := GetHNSEndpointByID(endpointID)
53+
isAttached, err := endpoint.IsAttached(containerID)
54+
if !isAttached {
55+
return err
56+
}
4757
return modifyNetworkEndpoint(containerID, endpointID, Remove)
4858
}
4959

internal/hns/hnsendpoint.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hns
33
import (
44
"encoding/json"
55
"net"
6+
"strings"
67

78
"github.com/sirupsen/logrus"
89
)
@@ -94,6 +95,27 @@ func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
9495
return nil, EndpointNotFoundError{EndpointName: endpointName}
9596
}
9697

98+
type endpointAttachInfo struct {
99+
SharedContainers json.RawMessage `json:",omitempty"`
100+
}
101+
102+
func (endpoint *HNSEndpoint) IsAttached(vID string) (bool, error) {
103+
attachInfo := endpointAttachInfo{}
104+
err := hnsCall("GET", "/endpoints/"+endpoint.Id, "", &attachInfo)
105+
106+
// Return false allows us to just return the err
107+
if err != nil {
108+
return false, err
109+
}
110+
111+
if strings.Contains(strings.ToLower(string(attachInfo.SharedContainers)), strings.ToLower(vID)) {
112+
return true, nil
113+
}
114+
115+
return false, nil
116+
117+
}
118+
97119
// Create Endpoint by sending EndpointRequest to HNS. TODO: Create a separate HNS interface to place all these methods
98120
func (endpoint *HNSEndpoint) Create() (*HNSEndpoint, error) {
99121
operation := "Create"

internal/hns/hnsfuncs.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@ import (
99
"github.com/sirupsen/logrus"
1010
)
1111

12-
func hnsCall(method, path, request string, returnResponse interface{}) error {
12+
func hnsCallRawResponse(method, path, request string) (*hnsResponse, error) {
1313
var responseBuffer *uint16
1414
logrus.Debugf("[%s]=>[%s] Request : %s", method, path, request)
1515

1616
err := _hnsCall(method, path, request, &responseBuffer)
1717
if err != nil {
18-
return hcserror.New(err, "hnsCall ", "")
18+
return nil, hcserror.New(err, "hnsCall ", "")
1919
}
2020
response := interop.ConvertAndFreeCoTaskMemString(responseBuffer)
2121

2222
hnsresponse := &hnsResponse{}
2323
if err = json.Unmarshal([]byte(response), &hnsresponse); err != nil {
24-
return err
24+
return nil, err
2525
}
26+
return hnsresponse, nil
27+
}
2628

29+
func hnsCall(method, path, request string, returnResponse interface{}) error {
30+
hnsresponse, err := hnsCallRawResponse(method, path, request)
31+
if err != nil {
32+
return fmt.Errorf("failed during hnsCallRawResponse: %v", err)
33+
}
2734
if !hnsresponse.Success {
28-
return fmt.Errorf("HNS failed with error : %s", hnsresponse.Error)
35+
return fmt.Errorf("hns failed with error : %s", hnsresponse.Error)
2936
}
3037

3138
if len(hnsresponse.Output) == 0 {

0 commit comments

Comments
 (0)