...

Source file src/github.com/Microsoft/hcsshim/internal/hns/hnsnetwork.go

Documentation: github.com/Microsoft/hcsshim/internal/hns

     1  //go:build windows
     2  
     3  package hns
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  	"net"
     9  
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // Subnet is associated with a network and represents a list
    14  // of subnets available to the network
    15  type Subnet struct {
    16  	AddressPrefix  string            `json:",omitempty"`
    17  	GatewayAddress string            `json:",omitempty"`
    18  	Policies       []json.RawMessage `json:",omitempty"`
    19  }
    20  
    21  // MacPool is associated with a network and represents a list
    22  // of macaddresses available to the network
    23  type MacPool struct {
    24  	StartMacAddress string `json:",omitempty"`
    25  	EndMacAddress   string `json:",omitempty"`
    26  }
    27  
    28  // HNSNetwork represents a network in HNS
    29  type HNSNetwork struct {
    30  	Id                   string            `json:"ID,omitempty"`
    31  	Name                 string            `json:",omitempty"`
    32  	Type                 string            `json:",omitempty"`
    33  	NetworkAdapterName   string            `json:",omitempty"`
    34  	SourceMac            string            `json:",omitempty"`
    35  	Policies             []json.RawMessage `json:",omitempty"`
    36  	MacPools             []MacPool         `json:",omitempty"`
    37  	Subnets              []Subnet          `json:",omitempty"`
    38  	DNSSuffix            string            `json:",omitempty"`
    39  	DNSServerList        string            `json:",omitempty"`
    40  	DNSServerCompartment uint32            `json:",omitempty"`
    41  	ManagementIP         string            `json:",omitempty"`
    42  	AutomaticDNS         bool              `json:",omitempty"`
    43  }
    44  
    45  type hnsResponse struct {
    46  	Success bool
    47  	Error   string
    48  	Output  json.RawMessage
    49  }
    50  
    51  // HNSNetworkRequest makes a call into HNS to update/query a single network
    52  func HNSNetworkRequest(method, path, request string) (*HNSNetwork, error) {
    53  	var network HNSNetwork
    54  	err := hnsCall(method, "/networks/"+path, request, &network)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return &network, nil
    60  }
    61  
    62  // HNSListNetworkRequest makes a HNS call to query the list of available networks
    63  func HNSListNetworkRequest(method, path, request string) ([]HNSNetwork, error) {
    64  	var network []HNSNetwork
    65  	err := hnsCall(method, "/networks/"+path, request, &network)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return network, nil
    71  }
    72  
    73  // GetHNSNetworkByID
    74  func GetHNSNetworkByID(networkID string) (*HNSNetwork, error) {
    75  	return HNSNetworkRequest("GET", networkID, "")
    76  }
    77  
    78  // GetHNSNetworkName filtered by Name
    79  func GetHNSNetworkByName(networkName string) (*HNSNetwork, error) {
    80  	hsnnetworks, err := HNSListNetworkRequest("GET", "", "")
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	for _, hnsnetwork := range hsnnetworks {
    85  		if hnsnetwork.Name == networkName {
    86  			return &hnsnetwork, nil
    87  		}
    88  	}
    89  	return nil, NetworkNotFoundError{NetworkName: networkName}
    90  }
    91  
    92  // Create Network by sending NetworkRequest to HNS.
    93  func (network *HNSNetwork) Create() (*HNSNetwork, error) {
    94  	operation := "Create"
    95  	title := "hcsshim::HNSNetwork::" + operation
    96  	logrus.Debugf(title+" id=%s", network.Id)
    97  
    98  	for _, subnet := range network.Subnets {
    99  		if (subnet.AddressPrefix != "") && (subnet.GatewayAddress == "") {
   100  			return nil, errors.New("network create error, subnet has address prefix but no gateway specified")
   101  		}
   102  	}
   103  
   104  	jsonString, err := json.Marshal(network)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	return HNSNetworkRequest("POST", "", string(jsonString))
   109  }
   110  
   111  // Delete Network by sending NetworkRequest to HNS
   112  func (network *HNSNetwork) Delete() (*HNSNetwork, error) {
   113  	operation := "Delete"
   114  	title := "hcsshim::HNSNetwork::" + operation
   115  	logrus.Debugf(title+" id=%s", network.Id)
   116  
   117  	return HNSNetworkRequest("DELETE", network.Id, "")
   118  }
   119  
   120  // Creates an endpoint on the Network.
   121  func (network *HNSNetwork) NewEndpoint(ipAddress net.IP, macAddress net.HardwareAddr) *HNSEndpoint {
   122  	return &HNSEndpoint{
   123  		VirtualNetwork: network.Id,
   124  		IPAddress:      ipAddress,
   125  		MacAddress:     string(macAddress),
   126  	}
   127  }
   128  
   129  func (network *HNSNetwork) CreateEndpoint(endpoint *HNSEndpoint) (*HNSEndpoint, error) {
   130  	operation := "CreateEndpoint"
   131  	title := "hcsshim::HNSNetwork::" + operation
   132  	logrus.Debugf(title+" id=%s, endpointId=%s", network.Id, endpoint.Id)
   133  
   134  	endpoint.VirtualNetwork = network.Id
   135  	return endpoint.Create()
   136  }
   137  
   138  func (network *HNSNetwork) CreateRemoteEndpoint(endpoint *HNSEndpoint) (*HNSEndpoint, error) {
   139  	operation := "CreateRemoteEndpoint"
   140  	title := "hcsshim::HNSNetwork::" + operation
   141  	logrus.Debugf(title+" id=%s", network.Id)
   142  	endpoint.IsRemoteEndpoint = true
   143  	return network.CreateEndpoint(endpoint)
   144  }
   145  

View as plain text