...

Source file src/github.com/Microsoft/hcsshim/hnsendpoint.go

Documentation: github.com/Microsoft/hcsshim

     1  //go:build windows
     2  
     3  package hcsshim
     4  
     5  import (
     6  	"github.com/Microsoft/hcsshim/internal/hns"
     7  )
     8  
     9  // HNSEndpoint represents a network endpoint in HNS
    10  type HNSEndpoint = hns.HNSEndpoint
    11  
    12  // HNSEndpointStats represent the stats for an networkendpoint in HNS
    13  type HNSEndpointStats = hns.EndpointStats
    14  
    15  // Namespace represents a Compartment.
    16  type Namespace = hns.Namespace
    17  
    18  // SystemType represents the type of the system on which actions are done
    19  type SystemType string
    20  
    21  // SystemType const
    22  const (
    23  	ContainerType      SystemType = "Container"
    24  	VirtualMachineType SystemType = "VirtualMachine"
    25  	HostType           SystemType = "Host"
    26  )
    27  
    28  // EndpointAttachDetachRequest is the structure used to send request to the container to modify the system
    29  // Supported resource types are Network and Request Types are Add/Remove
    30  type EndpointAttachDetachRequest = hns.EndpointAttachDetachRequest
    31  
    32  // EndpointResquestResponse is object to get the endpoint request response
    33  type EndpointResquestResponse = hns.EndpointResquestResponse
    34  
    35  // HNSEndpointRequest makes a HNS call to modify/query a network endpoint
    36  func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
    37  	return hns.HNSEndpointRequest(method, path, request)
    38  }
    39  
    40  // HNSListEndpointRequest makes a HNS call to query the list of available endpoints
    41  func HNSListEndpointRequest() ([]HNSEndpoint, error) {
    42  	return hns.HNSListEndpointRequest()
    43  }
    44  
    45  // HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
    46  func HotAttachEndpoint(containerID string, endpointID string) error {
    47  	endpoint, err := GetHNSEndpointByID(endpointID)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	isAttached, err := endpoint.IsAttached(containerID)
    52  	if isAttached {
    53  		return err
    54  	}
    55  	return modifyNetworkEndpoint(containerID, endpointID, Add)
    56  }
    57  
    58  // HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
    59  func HotDetachEndpoint(containerID string, endpointID string) error {
    60  	endpoint, err := GetHNSEndpointByID(endpointID)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	isAttached, err := endpoint.IsAttached(containerID)
    65  	if !isAttached {
    66  		return err
    67  	}
    68  	return modifyNetworkEndpoint(containerID, endpointID, Remove)
    69  }
    70  
    71  // ModifyContainer corresponding to the container id, by sending a request
    72  func modifyContainer(id string, request *ResourceModificationRequestResponse) error {
    73  	container, err := OpenContainer(id)
    74  	if err != nil {
    75  		if IsNotExist(err) {
    76  			return ErrComputeSystemDoesNotExist
    77  		}
    78  		return getInnerError(err)
    79  	}
    80  	defer container.Close()
    81  	err = container.Modify(request)
    82  	if err != nil {
    83  		if IsNotSupported(err) {
    84  			return ErrPlatformNotSupported
    85  		}
    86  		return getInnerError(err)
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func modifyNetworkEndpoint(containerID string, endpointID string, request RequestType) error {
    93  	requestMessage := &ResourceModificationRequestResponse{
    94  		Resource: Network,
    95  		Request:  request,
    96  		Data:     endpointID,
    97  	}
    98  	err := modifyContainer(containerID, requestMessage)
    99  
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // GetHNSEndpointByID get the Endpoint by ID
   108  func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
   109  	return hns.GetHNSEndpointByID(endpointID)
   110  }
   111  
   112  // GetHNSEndpointByName gets the endpoint filtered by Name
   113  func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
   114  	return hns.GetHNSEndpointByName(endpointName)
   115  }
   116  
   117  // GetHNSEndpointStats gets the endpoint stats by ID
   118  func GetHNSEndpointStats(endpointName string) (*HNSEndpointStats, error) {
   119  	return hns.GetHNSEndpointStats(endpointName)
   120  }
   121  

View as plain text