|
| 1 | +package hnsclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Azure/azure-container-networking/cns" |
| 10 | + "github.com/Microsoft/hcsshim" |
| 11 | +) |
| 12 | + |
| 13 | +// CreateHnsNetwork creates the HNS network with the provided configuration |
| 14 | +func CreateHnsNetwork(nwConfig cns.CreateHnsNetworkRequest) error { |
| 15 | + log.Printf("[Azure CNS] CreateHnsNetwork") |
| 16 | + // Initialize HNS network. |
| 17 | + hnsNetwork := &hcsshim.HNSNetwork{ |
| 18 | + Name: nwConfig.NetworkName, |
| 19 | + Type: nwConfig.NetworkType, |
| 20 | + NetworkAdapterName: nwConfig.NetworkAdapterName, |
| 21 | + SourceMac: nwConfig.SourceMac, |
| 22 | + DNSSuffix: nwConfig.DNSSuffix, |
| 23 | + DNSServerList: nwConfig.DNSServerList, |
| 24 | + DNSServerCompartment: nwConfig.DNSServerCompartment, |
| 25 | + ManagementIP: nwConfig.ManagementIP, |
| 26 | + AutomaticDNS: nwConfig.AutomaticDNS, |
| 27 | + } |
| 28 | + |
| 29 | + for _, policy := range nwConfig.Policies { |
| 30 | + hnsNetwork.Policies = append(hnsNetwork.Policies, policy) |
| 31 | + } |
| 32 | + |
| 33 | + for _, subnet := range nwConfig.Subnets { |
| 34 | + hnsSubnet := hcsshim.Subnet{ |
| 35 | + AddressPrefix: subnet.AddressPrefix, |
| 36 | + GatewayAddress: subnet.GatewayAddress, |
| 37 | + } |
| 38 | + |
| 39 | + hnsNetwork.Subnets = append(hnsNetwork.Subnets, hnsSubnet) |
| 40 | + } |
| 41 | + |
| 42 | + for _, macPool := range nwConfig.MacPools { |
| 43 | + hnsMacPool := hcsshim.MacPool{ |
| 44 | + StartMacAddress: macPool.StartMacAddress, |
| 45 | + EndMacAddress: macPool.EndMacAddress, |
| 46 | + } |
| 47 | + hnsNetwork.MacPools = append(hnsNetwork.MacPools, hnsMacPool) |
| 48 | + } |
| 49 | + |
| 50 | + return createHnsNetwork(hnsNetwork) |
| 51 | +} |
| 52 | + |
| 53 | +// DeleteHnsNetwork deletes the HNS network with the provided name |
| 54 | +func DeleteHnsNetwork(networkName string) error { |
| 55 | + log.Printf("[Azure CNS] DeleteHnsNetwork") |
| 56 | + |
| 57 | + return deleteHnsNetwork(networkName) |
| 58 | +} |
| 59 | + |
| 60 | +// CreateDefaultExtNetwork creates default HNS network named ext (if it doesn't exist already) |
| 61 | +// to create external switch on windows platform. |
| 62 | +// This allows orchestrators to start CNS which pre-provisions the network so that the |
| 63 | +// VM network blip / disconnect is avoided when calling cni add for the very first time. |
| 64 | +func CreateDefaultExtNetwork(networkType string) error { |
| 65 | + networkType = strings.ToLower(strings.TrimSpace(networkType)) |
| 66 | + if len(networkType) == 0 { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + if networkType != hnsL2Bridge && networkType != hnsL2Tunnel { |
| 71 | + return fmt.Errorf("Invalid hns network type %s", networkType) |
| 72 | + } |
| 73 | + |
| 74 | + log.Printf("[Azure CNS] CreateDefaultExtNetwork") |
| 75 | + extHnsNetwork, _ := hcsshim.GetHNSNetworkByName(ExtHnsNetworkName) |
| 76 | + |
| 77 | + if extHnsNetwork != nil { |
| 78 | + log.Printf("[Azure CNS] Found existing DefaultExtNetwork with type: %s", extHnsNetwork.Type) |
| 79 | + if !strings.EqualFold(networkType, extHnsNetwork.Type) { |
| 80 | + return fmt.Errorf("Network type mismatch with existing network: %s", extHnsNetwork.Type) |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + // create new hns network |
| 87 | + log.Printf("[Azure CNS] Creating DefaultExtNetwork with type %s", networkType) |
| 88 | + |
| 89 | + hnsNetwork := &hcsshim.HNSNetwork{ |
| 90 | + Name: ExtHnsNetworkName, |
| 91 | + Type: networkType, |
| 92 | + } |
| 93 | + |
| 94 | + hnsSubnet := hcsshim.Subnet{ |
| 95 | + AddressPrefix: ExtHnsNetworkAddressPrefix, |
| 96 | + GatewayAddress: ExtHnsNetworkGwAddress, |
| 97 | + } |
| 98 | + |
| 99 | + hnsNetwork.Subnets = append(hnsNetwork.Subnets, hnsSubnet) |
| 100 | + |
| 101 | + return createHnsNetwork(hnsNetwork) |
| 102 | +} |
| 103 | + |
| 104 | +// DeleteDefaultExtNetwork deletes the default HNS network |
| 105 | +func DeleteDefaultExtNetwork() error { |
| 106 | + log.Printf("[Azure CNS] DeleteDefaultExtNetwork") |
| 107 | + |
| 108 | + return deleteHnsNetwork(ExtHnsNetworkName) |
| 109 | +} |
| 110 | + |
| 111 | +// createHnsNetwork calls the hcshim to create the hns network |
| 112 | +func createHnsNetwork(hnsNetwork *hcsshim.HNSNetwork) error { |
| 113 | + // Marshal the request. |
| 114 | + buffer, err := json.Marshal(hnsNetwork) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + hnsRequest := string(buffer) |
| 119 | + |
| 120 | + // Create the HNS network. |
| 121 | + log.Printf("[Azure CNS] HNSNetworkRequest POST request:%+v", hnsRequest) |
| 122 | + hnsResponse, err := hcsshim.HNSNetworkRequest("POST", "", hnsRequest) |
| 123 | + log.Printf("[Azure CNS] HNSNetworkRequest POST response:%+v err:%v.", hnsResponse, err) |
| 124 | + |
| 125 | + return err |
| 126 | +} |
| 127 | + |
| 128 | +// deleteHnsNetwork calls HNS to delete the network with the provided name |
| 129 | +func deleteHnsNetwork(networkName string) error { |
| 130 | + hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName) |
| 131 | + if err == nil { |
| 132 | + // Delete the HNS network. |
| 133 | + var hnsResponse *hcsshim.HNSNetwork |
| 134 | + log.Printf("[Azure CNS] HNSNetworkRequest DELETE id:%v", hnsNetwork.Id) |
| 135 | + hnsResponse, err = hcsshim.HNSNetworkRequest("DELETE", hnsNetwork.Id, "") |
| 136 | + log.Printf("[Azure CNS] HNSNetworkRequest DELETE response:%+v err:%v.", hnsResponse, err) |
| 137 | + } |
| 138 | + |
| 139 | + return err |
| 140 | +} |
0 commit comments