func()

in network/endpoint_windows.go [423:521]


func (nw *network) newEndpointImplHnsV2(cli apipaClient, epInfo *EndpointInfo) (*endpoint, error) {
	hcnEndpoint, err := nw.configureHcnEndpoint(epInfo)
	if err != nil {
		logger.Error("Failed to configure hcn endpoint due to", zap.Error(err))
		return nil, err
	}

	// Create the HCN endpoint.
	logger.Info("Creating hcn endpoint", zap.Any("hcnEndpoint", hcnEndpoint), zap.String("computenetwork", hcnEndpoint.HostComputeNetwork))
	hnsResponse, err := Hnsv2.CreateEndpoint(hcnEndpoint)
	if err != nil {
		return nil, fmt.Errorf("Failed to create endpoint: %s due to error: %v", hcnEndpoint.Name, err)
	}

	logger.Info("Successfully created hcn endpoint with response", zap.Any("hnsResponse", hnsResponse))

	defer func() {
		if err != nil {
			logger.Info("Deleting hcn endpoint with id", zap.String("id", hnsResponse.Id))
			err = Hnsv2.DeleteEndpoint(hnsResponse)
			logger.Error("Completed hcn endpoint deletion for id with error", zap.String("id", hnsResponse.Id), zap.Error(err))
		}
	}()

	var namespace *hcn.HostComputeNamespace
	if namespace, err = Hnsv2.GetNamespaceByID(epInfo.NetNsPath); err != nil {
		return nil, fmt.Errorf("Failed to get hcn namespace: %s due to error: %v", epInfo.NetNsPath, err)
	}

	if err = Hnsv2.AddNamespaceEndpoint(namespace.Id, hnsResponse.Id); err != nil {
		return nil, fmt.Errorf("Failed to add endpoint: %s to hcn namespace: %s due to error: %v", hnsResponse.Id, namespace.Id, err) //nolint
	}

	defer func() {
		if err != nil {
			if errRemoveNsEp := Hnsv2.RemoveNamespaceEndpoint(namespace.Id, hnsResponse.Id); errRemoveNsEp != nil {
				logger.Error("Failed to remove endpoint from namespace due to error",
					zap.String("id", hnsResponse.Id), zap.String("id", hnsResponse.Id), zap.Error(errRemoveNsEp))
			}
		}
	}()

	// If the Host - container connectivity is requested, create endpoint in HostNCApipaNetwork
	if epInfo.AllowInboundFromHostToNC || epInfo.AllowInboundFromNCToHost {
		if err = nw.createHostNCApipaEndpoint(cli, epInfo); err != nil {
			return nil, fmt.Errorf("Failed to create HostNCApipaEndpoint due to error: %v", err)
		}
	}

	var vlanid int
	if epInfo.Data != nil {
		if vlanData, ok := epInfo.Data[VlanIDKey]; ok {
			vlanid = vlanData.(int)
		}
	}

	var gateway net.IP
	if len(hnsResponse.Routes) > 0 {
		gateway = net.ParseIP(hnsResponse.Routes[0].NextHop)
	}

	nicName := epInfo.IfName
	// infra nic nicname will look like eth0, but delegated/secondary nics will look like "vEthernet x" where x is 1-7
	if epInfo.NICType != cns.InfraNIC {
		nicName = epInfo.MasterIfName
	}

	// Create the endpoint object.
	ep := &endpoint{
		Id:                       hcnEndpoint.Name,
		HnsId:                    hnsResponse.Id,
		SandboxKey:               epInfo.ContainerID,
		IfName:                   nicName,
		IPAddresses:              epInfo.IPAddresses,
		Gateways:                 []net.IP{gateway},
		DNS:                      epInfo.EndpointDNS,
		VlanID:                   vlanid,
		EnableSnatOnHost:         epInfo.EnableSnatOnHost,
		NetNs:                    epInfo.NetNsPath,
		AllowInboundFromNCToHost: epInfo.AllowInboundFromNCToHost,
		AllowInboundFromHostToNC: epInfo.AllowInboundFromHostToNC,
		NetworkContainerID:       epInfo.NetworkContainerID,
		ContainerID:              epInfo.ContainerID,
		PODName:                  epInfo.PODName,
		PODNameSpace:             epInfo.PODNameSpace,
		HNSNetworkID:             epInfo.HNSNetworkID,
		NICType:                  epInfo.NICType,
	}

	for _, route := range epInfo.Routes {
		ep.Routes = append(ep.Routes, route)
	}

	ep.MacAddress, _ = net.ParseMAC(hnsResponse.MacAddress)

	epInfo.HNSEndpointID = hnsResponse.Id // we use the ep info hns id later in stateless to clean up in ADD if there is an error

	return ep, nil
}