refactor(win-bridge): hns api processing
Signed-off-by: thxcode <thxcode0824@gmail.com>
This commit is contained in:
@ -81,23 +81,22 @@ func GenerateHnsEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcsshim.HNSEndpoint
|
|||||||
// run the IPAM plugin and get back the config to apply
|
// run the IPAM plugin and get back the config to apply
|
||||||
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epInfo.EndpointName)
|
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epInfo.EndpointName)
|
||||||
if err != nil && !hcsshim.IsNotExist(err) {
|
if err != nil && !hcsshim.IsNotExist(err) {
|
||||||
return nil, errors.Annotatef(err, "failed to get endpoint %q", epInfo.EndpointName)
|
return nil, errors.Annotatef(err, "failed to get HNSEndpoint %s", epInfo.EndpointName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hnsEndpoint != nil {
|
if hnsEndpoint != nil {
|
||||||
if hnsEndpoint.VirtualNetwork != epInfo.NetworkId {
|
if strings.EqualFold(hnsEndpoint.VirtualNetwork, epInfo.NetworkId) {
|
||||||
_, err = hnsEndpoint.Delete()
|
return nil, fmt.Errorf("HNSEndpoint %s is already existed", epInfo.EndpointName)
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotatef(err, "failed to delete endpoint %s", epInfo.EndpointName)
|
|
||||||
}
|
}
|
||||||
hnsEndpoint = nil
|
// remove endpoint if corrupted
|
||||||
|
if _, err = hnsEndpoint.Delete(); err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "failed to delete corrupted HNSEndpoint %s", epInfo.EndpointName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.LoopbackDSR {
|
if n.LoopbackDSR {
|
||||||
n.ApplyLoopbackDSR(&epInfo.IpAddress)
|
n.ApplyLoopbackDSR(&epInfo.IpAddress)
|
||||||
}
|
}
|
||||||
if hnsEndpoint == nil {
|
|
||||||
hnsEndpoint = &hcsshim.HNSEndpoint{
|
hnsEndpoint = &hcsshim.HNSEndpoint{
|
||||||
Name: epInfo.EndpointName,
|
Name: epInfo.EndpointName,
|
||||||
VirtualNetwork: epInfo.NetworkId,
|
VirtualNetwork: epInfo.NetworkId,
|
||||||
@ -107,7 +106,6 @@ func GenerateHnsEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcsshim.HNSEndpoint
|
|||||||
IPAddress: epInfo.IpAddress,
|
IPAddress: epInfo.IpAddress,
|
||||||
Policies: n.MarshalPolicies(),
|
Policies: n.MarshalPolicies(),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return hnsEndpoint, nil
|
return hnsEndpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,23 +117,22 @@ func RemoveHnsEndpoint(epName string, netns string, containerID string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName)
|
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName)
|
||||||
|
if err != nil {
|
||||||
if hcsshim.IsNotExist(err) {
|
if hcsshim.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
}
|
||||||
return errors.Annotatef(err, "failed to find HNSEndpoint %s", epName)
|
return errors.Annotatef(err, "failed to find HNSEndpoint %s", epName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// for shared endpoint, detach it from the container.
|
// for shared endpoint, detach it from the container
|
||||||
if netns != pauseContainerNetNS {
|
if netns != pauseContainerNetNS {
|
||||||
hnsEndpoint.ContainerDetach(containerID)
|
_ = hnsEndpoint.ContainerDetach(containerID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// for removing the endpoint completely, hot detach is used at first.
|
// for removing the endpoint completely, hot detach is used at first
|
||||||
hcsshim.HotDetachEndpoint(containerID, hnsEndpoint.Id)
|
_ = hcsshim.HotDetachEndpoint(containerID, hnsEndpoint.Id)
|
||||||
hnsEndpoint.Delete()
|
_, _ = hnsEndpoint.Delete()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,52 +140,53 @@ type HnsEndpointMakerFunc func() (*hcsshim.HNSEndpoint, error)
|
|||||||
|
|
||||||
// AddHnsEndpoint attaches an HNSEndpoint to a container specified by containerID.
|
// AddHnsEndpoint attaches an HNSEndpoint to a container specified by containerID.
|
||||||
func AddHnsEndpoint(epName string, expectedNetworkId string, containerID string, netns string, makeEndpoint HnsEndpointMakerFunc) (*hcsshim.HNSEndpoint, error) {
|
func AddHnsEndpoint(epName string, expectedNetworkId string, containerID string, netns string, makeEndpoint HnsEndpointMakerFunc) (*hcsshim.HNSEndpoint, error) {
|
||||||
// for shared endpoint, we expect that the endpoint already exists.
|
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName)
|
||||||
if netns != pauseContainerNetNS {
|
|
||||||
_, err := hcsshim.GetHNSEndpointByName(epName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !hcsshim.IsNotExist(err) {
|
||||||
return nil, errors.Annotatef(err, "failed to find HNSEndpoint %s", epName)
|
return nil, errors.Annotatef(err, "failed to find HNSEndpoint %s", epName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if endpoint already exists
|
// for shared endpoint, we expect that the endpoint already exists
|
||||||
createEndpoint := true
|
if netns != pauseContainerNetNS {
|
||||||
hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName)
|
if hnsEndpoint == nil {
|
||||||
if hnsEndpoint != nil && strings.EqualFold(hnsEndpoint.VirtualNetwork, expectedNetworkId) {
|
return nil, errors.Annotatef(err, "failed to find HNSEndpoint %s", epName)
|
||||||
createEndpoint = false
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if createEndpoint {
|
// verify the existing endpoint is corrupted or not
|
||||||
if hnsEndpoint != nil {
|
if hnsEndpoint != nil {
|
||||||
if _, err = hnsEndpoint.Delete(); err != nil {
|
if !strings.EqualFold(hnsEndpoint.VirtualNetwork, expectedNetworkId) {
|
||||||
return nil, errors.Annotate(err, "failed to delete the stale HNSEndpoint")
|
if _, err := hnsEndpoint.Delete(); err != nil {
|
||||||
|
return nil, errors.Annotatef(err, "failed to delete corrupted HNSEndpoint %s", epName)
|
||||||
|
}
|
||||||
|
hnsEndpoint = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create endpoint if not found
|
||||||
|
var isNewEndpoint bool
|
||||||
|
if hnsEndpoint == nil {
|
||||||
if hnsEndpoint, err = makeEndpoint(); err != nil {
|
if hnsEndpoint, err = makeEndpoint(); err != nil {
|
||||||
return nil, errors.Annotate(err, "failed to make a new HNSEndpoint")
|
return nil, errors.Annotate(err, "failed to make a new HNSEndpoint")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hnsEndpoint, err = hnsEndpoint.Create(); err != nil {
|
if hnsEndpoint, err = hnsEndpoint.Create(); err != nil {
|
||||||
return nil, errors.Annotate(err, "failed to create the new HNSEndpoint")
|
return nil, errors.Annotate(err, "failed to create the new HNSEndpoint")
|
||||||
}
|
}
|
||||||
|
isNewEndpoint = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// hot attach
|
// attach to container
|
||||||
if err := hcsshim.HotAttachEndpoint(containerID, hnsEndpoint.Id); err != nil {
|
if err := hcsshim.HotAttachEndpoint(containerID, hnsEndpoint.Id); err != nil {
|
||||||
if createEndpoint {
|
if isNewEndpoint {
|
||||||
err := RemoveHnsEndpoint(epName, netns, containerID)
|
if err := RemoveHnsEndpoint(epName, netns, containerID); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Annotatef(err, "failed to remove the new HNSEndpoint %s after attaching container %s failure", hnsEndpoint.Id, containerID)
|
return nil, errors.Annotatef(err, "failed to remove the new HNSEndpoint %s after attaching container %s failure", hnsEndpoint.Id, containerID)
|
||||||
}
|
}
|
||||||
}
|
} else if hcsshim.ErrComputeSystemDoesNotExist == err {
|
||||||
if hcsshim.ErrComputeSystemDoesNotExist == err {
|
|
||||||
return hnsEndpoint, nil
|
return hnsEndpoint, nil
|
||||||
}
|
}
|
||||||
return nil, errors.Annotatef(err, "failed to attach container %s to HNSEndpoint %s", containerID, hnsEndpoint.Id)
|
return nil, errors.Annotatef(err, "failed to attach container %s to HNSEndpoint %s", containerID, hnsEndpoint.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return hnsEndpoint, nil
|
return hnsEndpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user