From aa8c8c148905630be0bb92e01c8bb98c70e2da58 Mon Sep 17 00:00:00 2001 From: thxcode Date: Mon, 19 Apr 2021 10:57:50 +0800 Subject: [PATCH] chore(win-bridge): location related - group functions by HNS,HCN Signed-off-by: thxcode --- pkg/hns/endpoint_windows.go | 189 +++++++++++++++++++----------------- pkg/hns/netconf_windows.go | 64 ++++++------ 2 files changed, 127 insertions(+), 126 deletions(-) diff --git a/pkg/hns/endpoint_windows.go b/pkg/hns/endpoint_windows.go index d9a72f4d..c2941c4f 100644 --- a/pkg/hns/endpoint_windows.go +++ b/pkg/hns/endpoint_windows.go @@ -62,6 +62,20 @@ func GetIpString(ip *net.IP) string { } } +// GetDefaultDestinationPrefix returns the default destination prefix according to the given IP type. +func GetDefaultDestinationPrefix(ip *net.IP) string { + destinationPrefix := "0.0.0.0/0" + if ipv6 := ip.To4(); ipv6 == nil { + destinationPrefix = "::/0" + } + return destinationPrefix +} + +// ConstructEndpointName constructs endpoint id which is used to identify an endpoint from HNS/HCN. +func ConstructEndpointName(containerID string, netNs string, networkName string) string { + return GetSandboxContainerID(containerID, netNs) + "_" + networkName +} + // GenerateHnsEndpoint generates an HNSEndpoint with given info and config. func GenerateHnsEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcsshim.HNSEndpoint, error) { // run the IPAM plugin and get back the config to apply @@ -97,72 +111,6 @@ func GenerateHnsEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcsshim.HNSEndpoint return hnsEndpoint, nil } -// GenerateHcnEndpoint generates a HostComputeEndpoint with given info and config. -func GenerateHcnEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcn.HostComputeEndpoint, error) { - // run the IPAM plugin and get back the config to apply - hcnEndpoint, err := hcn.GetEndpointByName(epInfo.EndpointName) - if err != nil && !hcn.IsNotFoundError(err) { - return nil, errors.Annotatef(err, "failed to get endpoint %q", epInfo.EndpointName) - } - - if hcnEndpoint != nil { - // If the endpont already exists, then we should return error unless - // the endpoint is based on a different network then delete - // should that fail return error - if !strings.EqualFold(hcnEndpoint.HostComputeNetwork, epInfo.NetworkId) { - err = hcnEndpoint.Delete() - if err != nil { - return nil, errors.Annotatef(err, "failed to delete endpoint %s", epInfo.EndpointName) - } - } else { - return nil, fmt.Errorf("endpoint %q already exits", epInfo.EndpointName) - } - } - - if hcnEndpoint == nil { - routes := []hcn.Route{ - { - NextHop: GetIpString(&epInfo.Gateway), - DestinationPrefix: GetDefaultDestinationPrefix(&epInfo.Gateway), - }, - } - - hcnDns := hcn.Dns{ - Search: epInfo.DNS.Search, - ServerList: epInfo.DNS.Nameservers, - } - - hcnIpConfig := hcn.IpConfig{ - IpAddress: GetIpString(&epInfo.IpAddress), - } - ipConfigs := []hcn.IpConfig{hcnIpConfig} - - if n.LoopbackDSR { - n.ApplyLoopbackDSR(&epInfo.IpAddress) - } - hcnEndpoint = &hcn.HostComputeEndpoint{ - SchemaVersion: hcn.Version{Major: 2}, - Name: epInfo.EndpointName, - HostComputeNetwork: epInfo.NetworkId, - Dns: hcnDns, - Routes: routes, - IpConfigurations: ipConfigs, - Policies: func() []hcn.EndpointPolicy { - if n.HcnPolicyArgs == nil { - n.HcnPolicyArgs = []hcn.EndpointPolicy{} - } - return n.HcnPolicyArgs - }(), - } - } - return hcnEndpoint, nil -} - -// ConstructEndpointName constructs endpoint id which is used to identify an endpoint from HNS/HCN. -func ConstructEndpointName(containerID string, netNs string, networkName string) string { - return GetSandboxContainerID(containerID, netNs) + "_" + networkName -} - // RemoveHnsEndpoint detaches the given name endpoint from container specified by containerID, // or removes the given name endpoint completely. func RemoveHnsEndpoint(epName string, netns string, containerID string) error { @@ -244,30 +192,6 @@ func AddHnsEndpoint(epName string, expectedNetworkId string, containerID string, return hnsEndpoint, nil } -type HcnEndpointMakerFunc func() (*hcn.HostComputeEndpoint, error) - -// AddHcnEndpoint attaches a HostComputeEndpoint to the given namespace. -func AddHcnEndpoint(epName string, expectedNetworkId string, namespace string, makeEndpoint HcnEndpointMakerFunc) (*hcn.HostComputeEndpoint, error) { - hcnEndpoint, err := makeEndpoint() - if err != nil { - return nil, errors.Annotate(err, "failed to make a new HostComputeEndpoint") - } - - if hcnEndpoint, err = hcnEndpoint.Create(); err != nil { - return nil, errors.Annotate(err, "failed to create the new HostComputeEndpoint") - } - - err = hcn.AddNamespaceEndpoint(namespace, hcnEndpoint.Id) - if err != nil { - err := RemoveHcnEndpoint(epName) - if err != nil { - return nil, errors.Annotatef(err, "failed to remote the new HostComputeEndpoint %s after adding HostComputeNamespace %s failure", epName, namespace) - } - return nil, errors.Annotatef(err, "failed to add HostComputeEndpoint %s to HostComputeNamespace %s", epName, namespace) - } - return hcnEndpoint, nil -} - // ConstructHnsResult constructs the CNI result for the HNSEndpoint. func ConstructHnsResult(hnsNetwork *hcsshim.HNSNetwork, hnsEndpoint *hcsshim.HNSEndpoint) (*current.Result, error) { resultInterface := ¤t.Interface{ @@ -298,6 +222,67 @@ func ConstructHnsResult(hnsNetwork *hcsshim.HNSNetwork, hnsEndpoint *hcsshim.HNS return result, nil } +// GenerateHcnEndpoint generates a HostComputeEndpoint with given info and config. +func GenerateHcnEndpoint(epInfo *EndpointInfo, n *NetConf) (*hcn.HostComputeEndpoint, error) { + // run the IPAM plugin and get back the config to apply + hcnEndpoint, err := hcn.GetEndpointByName(epInfo.EndpointName) + if err != nil && !hcn.IsNotFoundError(err) { + return nil, errors.Annotatef(err, "failed to get endpoint %q", epInfo.EndpointName) + } + + if hcnEndpoint != nil { + // If the endpont already exists, then we should return error unless + // the endpoint is based on a different network then delete + // should that fail return error + if !strings.EqualFold(hcnEndpoint.HostComputeNetwork, epInfo.NetworkId) { + err = hcnEndpoint.Delete() + if err != nil { + return nil, errors.Annotatef(err, "failed to delete endpoint %s", epInfo.EndpointName) + } + } else { + return nil, fmt.Errorf("endpoint %q already exits", epInfo.EndpointName) + } + } + + if hcnEndpoint == nil { + routes := []hcn.Route{ + { + NextHop: GetIpString(&epInfo.Gateway), + DestinationPrefix: GetDefaultDestinationPrefix(&epInfo.Gateway), + }, + } + + hcnDns := hcn.Dns{ + Search: epInfo.DNS.Search, + ServerList: epInfo.DNS.Nameservers, + } + + hcnIpConfig := hcn.IpConfig{ + IpAddress: GetIpString(&epInfo.IpAddress), + } + ipConfigs := []hcn.IpConfig{hcnIpConfig} + + if n.LoopbackDSR { + n.ApplyLoopbackDSR(&epInfo.IpAddress) + } + hcnEndpoint = &hcn.HostComputeEndpoint{ + SchemaVersion: hcn.Version{Major: 2}, + Name: epInfo.EndpointName, + HostComputeNetwork: epInfo.NetworkId, + Dns: hcnDns, + Routes: routes, + IpConfigurations: ipConfigs, + Policies: func() []hcn.EndpointPolicy { + if n.HcnPolicyArgs == nil { + n.HcnPolicyArgs = []hcn.EndpointPolicy{} + } + return n.HcnPolicyArgs + }(), + } + } + return hcnEndpoint, nil +} + // RemoveHcnEndpoint removes the given name endpoint from namespace. func RemoveHcnEndpoint(epName string) error { hcnEndpoint, err := hcn.GetEndpointByName(epName) @@ -316,6 +301,30 @@ func RemoveHcnEndpoint(epName string) error { return nil } +type HcnEndpointMakerFunc func() (*hcn.HostComputeEndpoint, error) + +// AddHcnEndpoint attaches a HostComputeEndpoint to the given namespace. +func AddHcnEndpoint(epName string, expectedNetworkId string, namespace string, makeEndpoint HcnEndpointMakerFunc) (*hcn.HostComputeEndpoint, error) { + hcnEndpoint, err := makeEndpoint() + if err != nil { + return nil, errors.Annotate(err, "failed to make a new HostComputeEndpoint") + } + + if hcnEndpoint, err = hcnEndpoint.Create(); err != nil { + return nil, errors.Annotate(err, "failed to create the new HostComputeEndpoint") + } + + err = hcn.AddNamespaceEndpoint(namespace, hcnEndpoint.Id) + if err != nil { + err := RemoveHcnEndpoint(epName) + if err != nil { + return nil, errors.Annotatef(err, "failed to remote the new HostComputeEndpoint %s after adding HostComputeNamespace %s failure", epName, namespace) + } + return nil, errors.Annotatef(err, "failed to add HostComputeEndpoint %s to HostComputeNamespace %s", epName, namespace) + } + return hcnEndpoint, nil +} + // ConstructHcnResult constructs the CNI result for the HostComputeEndpoint. func ConstructHcnResult(hcnNetwork *hcn.HostComputeNetwork, hcnEndpoint *hcn.HostComputeEndpoint) (*current.Result, error) { resultInterface := ¤t.Interface{ diff --git a/pkg/hns/netconf_windows.go b/pkg/hns/netconf_windows.go index 6785d859..897a1528 100644 --- a/pkg/hns/netconf_windows.go +++ b/pkg/hns/netconf_windows.go @@ -63,12 +63,35 @@ type policy struct { Value json.RawMessage `json:"value"` } -func GetDefaultDestinationPrefix(ip *net.IP) string { - destinationPrefix := "0.0.0.0/0" - if ipv6 := ip.To4(); ipv6 == nil { - destinationPrefix = "::/0" +// MarshalPolicies converts the HNSEndpoint policies in Policies +// to HNS specific policies as Json raw bytes. +func (n *NetConf) MarshalPolicies() []json.RawMessage { + if n.Policies == nil { + n.Policies = make([]policy, 0) } - return destinationPrefix + + result := make([]json.RawMessage, 0, len(n.Policies)) + for _, p := range n.Policies { + if !strings.EqualFold(p.Name, "EndpointPolicy") { + continue + } + + result = append(result, p.Value) + } + + return result +} + +// GetDNS returns the DNS values if they are there use that else use netconf supplied DNS. +func (n *NetConf) GetDNS() types.DNS { + dnsResult := n.DNS + if len(n.RuntimeConfig.DNS.Nameservers) > 0 { + dnsResult.Nameservers = n.RuntimeConfig.DNS.Nameservers + } + if len(n.RuntimeConfig.DNS.Search) > 0 { + dnsResult.Search = n.RuntimeConfig.DNS.Search + } + return dnsResult } // ApplyLoopbackDSR configures the given IP to support loopback DSR. @@ -89,37 +112,6 @@ func (n *NetConf) ApplyLoopbackDSR(ip *net.IP) { } } -// GetDNS returns the DNS values if they are there use that else use netconf supplied DNS. -func (n *NetConf) GetDNS() types.DNS { - dnsResult := n.DNS - if len(n.RuntimeConfig.DNS.Nameservers) > 0 { - dnsResult.Nameservers = n.RuntimeConfig.DNS.Nameservers - } - if len(n.RuntimeConfig.DNS.Search) > 0 { - dnsResult.Search = n.RuntimeConfig.DNS.Search - } - return dnsResult -} - -// MarshalPolicies converts the HNSEndpoint policies in Policies -// to HNS specific policies as Json raw bytes. -func (n *NetConf) MarshalPolicies() []json.RawMessage { - if n.Policies == nil { - n.Policies = make([]policy, 0) - } - - result := make([]json.RawMessage, 0, len(n.Policies)) - for _, p := range n.Policies { - if !strings.EqualFold(p.Name, "EndpointPolicy") { - continue - } - - result = append(result, p.Value) - } - - return result -} - // ApplyOutboundNatPolicy applies the sNAT policy in HNS/HCN and configures the given CIDR as an exception. func (n *NetConf) ApplyOutboundNatPolicy(nwToNat string) { if n.Policies == nil {