|
| 1 | +// Copyright 2017 CNI authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package hns |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "net" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/Microsoft/hcsshim" |
| 23 | + "github.com/containernetworking/cni/pkg/types/current" |
| 24 | + "github.com/juju/errors" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + pauseContainerNetNS = "none" |
| 29 | +) |
| 30 | + |
| 31 | +// GetSandboxContainerID returns the sandbox ID of this pod |
| 32 | +func GetSandboxContainerID(containerID string, netNs string) string { |
| 33 | + if len(netNs) != 0 && netNs != pauseContainerNetNS { |
| 34 | + splits := strings.SplitN(netNs, ":", 2) |
| 35 | + if len(splits) == 2 { |
| 36 | + containerID = splits[1] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return containerID |
| 41 | +} |
| 42 | + |
| 43 | +// ConstructEndpointName constructs enpointId which is used to identify an endpoint from HNS |
| 44 | +// There is a special consideration for netNs name here, which is required for Windows Server 1709 |
| 45 | +// containerID is the Id of the container on which the endpoint is worked on |
| 46 | +func ConstructEndpointName(containerID string, netNs string, networkName string) string { |
| 47 | + return GetSandboxContainerID(containerID, netNs) + "_" + networkName |
| 48 | +} |
| 49 | + |
| 50 | +// DeprovisionEndpoint removes an endpoint from the container by sending a Detach request to HNS |
| 51 | +// For shared endpoint, ContainerDetach is used |
| 52 | +// for removing the endpoint completely, HotDetachEndpoint is used |
| 53 | +func DeprovisionEndpoint(epName string, netns string, containerID string) error { |
| 54 | + if len(netns) == 0 { |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName) |
| 59 | + if err != nil { |
| 60 | + return errors.Annotatef(err, "failed to find HNSEndpoint %s", epName) |
| 61 | + } |
| 62 | + |
| 63 | + if netns != pauseContainerNetNS { |
| 64 | + // Shared endpoint removal. Do not remove the endpoint. |
| 65 | + hnsEndpoint.ContainerDetach(containerID) |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + // Do not consider this as failure, else this would leak endpoints |
| 70 | + hcsshim.HotDetachEndpoint(containerID, hnsEndpoint.Id) |
| 71 | + |
| 72 | + // Do not return error |
| 73 | + hnsEndpoint.Delete() |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +type EndpointMakerFunc func() (*hcsshim.HNSEndpoint, error) |
| 79 | + |
| 80 | +// ProvisionEndpoint provisions an endpoint to a container specified by containerID. |
| 81 | +// If an endpoint already exists, the endpoint is reused. |
| 82 | +// This call is idempotent |
| 83 | +func ProvisionEndpoint(epName string, expectedNetworkId string, containerID string, makeEndpoint EndpointMakerFunc) (*hcsshim.HNSEndpoint, error) { |
| 84 | + // check if endpoint already exists |
| 85 | + createEndpoint := true |
| 86 | + hnsEndpoint, err := hcsshim.GetHNSEndpointByName(epName) |
| 87 | + if hnsEndpoint != nil && hnsEndpoint.VirtualNetwork == expectedNetworkId { |
| 88 | + createEndpoint = false |
| 89 | + } |
| 90 | + |
| 91 | + if createEndpoint { |
| 92 | + if hnsEndpoint != nil { |
| 93 | + if _, err = hnsEndpoint.Delete(); err != nil { |
| 94 | + return nil, errors.Annotate(err, "failed to delete the stale HNSEndpoint") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if hnsEndpoint, err = makeEndpoint(); err != nil { |
| 99 | + return nil, errors.Annotate(err, "failed to make a new HNSEndpoint") |
| 100 | + } |
| 101 | + |
| 102 | + if hnsEndpoint, err = hnsEndpoint.Create(); err != nil { |
| 103 | + return nil, errors.Annotate(err, "failed to create the new HNSEndpoint") |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + // hot attach |
| 109 | + if err := hcsshim.HotAttachEndpoint(containerID, hnsEndpoint.Id); err != nil { |
| 110 | + if hcsshim.ErrComputeSystemDoesNotExist == err { |
| 111 | + return hnsEndpoint, nil |
| 112 | + } |
| 113 | + |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + return hnsEndpoint, nil |
| 118 | +} |
| 119 | + |
| 120 | +// ConstructResult constructs the CNI result for the endpoint |
| 121 | +func ConstructResult(hnsNetwork *hcsshim.HNSNetwork, hnsEndpoint *hcsshim.HNSEndpoint) (*current.Result, error) { |
| 122 | + resultInterface := ¤t.Interface{ |
| 123 | + Name: hnsEndpoint.Name, |
| 124 | + Mac: hnsEndpoint.MacAddress, |
| 125 | + } |
| 126 | + _, ipSubnet, err := net.ParseCIDR(hnsNetwork.Subnets[0].AddressPrefix) |
| 127 | + if err != nil { |
| 128 | + return nil, errors.Annotatef(err, "failed to parse CIDR from %s", hnsNetwork.Subnets[0].AddressPrefix) |
| 129 | + } |
| 130 | + |
| 131 | + var ipVersion string |
| 132 | + if ipv4 := hnsEndpoint.IPAddress.To4(); ipv4 != nil { |
| 133 | + ipVersion = "4" |
| 134 | + } else if ipv6 := hnsEndpoint.IPAddress.To16(); ipv6 != nil { |
| 135 | + ipVersion = "6" |
| 136 | + } else { |
| 137 | + return nil, fmt.Errorf("IPAddress of HNSEndpoint %s isn't a valid ipv4 or ipv6 Address", hnsEndpoint.Name) |
| 138 | + } |
| 139 | + |
| 140 | + resultIPConfig := ¤t.IPConfig{ |
| 141 | + Version: ipVersion, |
| 142 | + Address: net.IPNet{ |
| 143 | + IP: hnsEndpoint.IPAddress, |
| 144 | + Mask: ipSubnet.Mask}, |
| 145 | + Gateway: net.ParseIP(hnsEndpoint.GatewayAddress), |
| 146 | + } |
| 147 | + result := ¤t.Result{} |
| 148 | + result.Interfaces = []*current.Interface{resultInterface} |
| 149 | + result.IPs = []*current.IPConfig{resultIPConfig} |
| 150 | + |
| 151 | + return result, nil |
| 152 | +} |
0 commit comments