|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2024 NVIDIA |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "encoding/json" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "net" |
| 27 | + "os" |
| 28 | + "os/signal" |
| 29 | + "strconv" |
| 30 | + "sync" |
| 31 | + |
| 32 | + "github.com/nvidia/doca-platform/pkg/ipallocator" |
| 33 | + "github.com/nvidia/doca-platform/pkg/utils/networkhelper" |
| 34 | + dpucniprovisioner "github.com/nvidia/ovn-kubernetes-components/internal/cniprovisioner/dpu" |
| 35 | + "github.com/nvidia/ovn-kubernetes-components/internal/readyz" |
| 36 | + "github.com/nvidia/ovn-kubernetes-components/internal/utils/ovsclient" |
| 37 | + |
| 38 | + "github.com/vishvananda/netlink" |
| 39 | + "k8s.io/client-go/kubernetes" |
| 40 | + "k8s.io/klog/v2" |
| 41 | + "k8s.io/utils/clock" |
| 42 | + kexec "k8s.io/utils/exec" |
| 43 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 44 | +) |
| 45 | + |
| 46 | +const ( |
| 47 | + // vtepIPAllocationFilePath is the path to the file that contains the VTEP IP allocation done by the IP Allocator. |
| 48 | + // We should ensure that the IP Allocation request name is vtep to have this file created correctly. |
| 49 | + vtepIPAllocationFilePath = "/tmp/ips/vtep" |
| 50 | + // pfIPAllocationFilePath is the path to the file that contains the PF IP allocation done by the IP Allocator. |
| 51 | + // We should ensure that the IP Allocation request name is pf to have this file created correctly. |
| 52 | + pfIPAllocationFilePath = "/tmp/ips/pf" |
| 53 | +) |
| 54 | + |
| 55 | +func main() { |
| 56 | + if len(os.Args) != 2 { |
| 57 | + klog.Fatal("expecting mode to be specified via args") |
| 58 | + } |
| 59 | + |
| 60 | + modeRaw := os.Args[1] |
| 61 | + mode, err := parseMode(modeRaw) |
| 62 | + if err != nil { |
| 63 | + klog.Fatalf("error while parsing mode: %s", err.Error()) |
| 64 | + } |
| 65 | + |
| 66 | + klog.Info("Starting DPU CNI Provisioner") |
| 67 | + |
| 68 | + node := os.Getenv("NODE_NAME") |
| 69 | + if node == "" { |
| 70 | + klog.Fatal("NODE_NAME environment variable is not found. This is supposed to be configured via Kubernetes Downward API in production") |
| 71 | + } |
| 72 | + |
| 73 | + var vtepIPNet *net.IPNet |
| 74 | + var gateway net.IP |
| 75 | + var pfIPNet *net.IPNet |
| 76 | + var vtepCIDR *net.IPNet |
| 77 | + var ovnMTU int |
| 78 | + var gatewayDiscoveryNetwork *net.IPNet |
| 79 | + if mode == dpucniprovisioner.InternalIPAM { |
| 80 | + vtepIPNet, gateway, err = getInfoFromVTEPIPAllocation() |
| 81 | + if err != nil { |
| 82 | + klog.Fatalf("error while parsing info from the VTEP IP allocation file: %s", err.Error()) |
| 83 | + } |
| 84 | + |
| 85 | + pfIPNet, err = getPFIP() |
| 86 | + if err != nil { |
| 87 | + klog.Fatalf("error while the PF IP from the allocation file: %s", err.Error()) |
| 88 | + } |
| 89 | + |
| 90 | + ovnMTU, err = getOVNMTU() |
| 91 | + if err != nil { |
| 92 | + klog.Fatalf("error while parsing MTU %s", err.Error()) |
| 93 | + } |
| 94 | + } else { |
| 95 | + gatewayDiscoveryNetwork, err = getGatewayDiscoveryNetwork() |
| 96 | + if err != nil { |
| 97 | + klog.Fatalf("error while parsing the Gateway Discovery Network: %s", err.Error()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + vtepCIDR, err = getVTEPCIDR() |
| 102 | + if err != nil { |
| 103 | + klog.Fatalf("error while parsing VTEP CIDR: %s", err.Error()) |
| 104 | + } |
| 105 | + |
| 106 | + hostCIDR, err := getHostCIDR() |
| 107 | + if err != nil { |
| 108 | + klog.Fatalf("error while parsing Host CIDR %s", err.Error()) |
| 109 | + } |
| 110 | + |
| 111 | + exec := kexec.New() |
| 112 | + |
| 113 | + ovsClient, err := ovsclient.New(exec) |
| 114 | + if err != nil { |
| 115 | + klog.Fatal(err) |
| 116 | + } |
| 117 | + |
| 118 | + ctx, cancel := context.WithCancel(context.Background()) |
| 119 | + c := clock.RealClock{} |
| 120 | + |
| 121 | + config, err := config.GetConfig() |
| 122 | + if err != nil { |
| 123 | + klog.Fatal(err) |
| 124 | + } |
| 125 | + clientset, err := kubernetes.NewForConfig(config) |
| 126 | + if err != nil { |
| 127 | + klog.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + provisioner := dpucniprovisioner.New(ctx, mode, c, ovsClient, networkhelper.New(), exec, clientset, vtepIPNet, gateway, vtepCIDR, hostCIDR, pfIPNet, node, gatewayDiscoveryNetwork, ovnMTU) |
| 131 | + |
| 132 | + err = provisioner.RunOnce() |
| 133 | + if err != nil { |
| 134 | + klog.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + var wg sync.WaitGroup |
| 138 | + wg.Add(1) |
| 139 | + go func() { |
| 140 | + defer wg.Done() |
| 141 | + provisioner.EnsureConfiguration() |
| 142 | + }() |
| 143 | + |
| 144 | + err = readyz.ReportReady() |
| 145 | + if err != nil { |
| 146 | + klog.Fatal(err) |
| 147 | + } |
| 148 | + |
| 149 | + klog.Info("DPU CNI Provisioner is ready") |
| 150 | + |
| 151 | + ch := make(chan os.Signal, 1) |
| 152 | + signal.Notify(ch, os.Interrupt) |
| 153 | + <-ch |
| 154 | + klog.Info("Received termination signal, terminating.") |
| 155 | + cancel() |
| 156 | + provisioner.Stop() |
| 157 | + wg.Wait() |
| 158 | +} |
| 159 | + |
| 160 | +// getInfoFromVTEPIPAllocation returns the VTEP IP and gateway from a file that contains the VTEP IP allocation done |
| 161 | +// by the IP Allocator component. |
| 162 | +func getInfoFromVTEPIPAllocation() (*net.IPNet, net.IP, error) { |
| 163 | + content, err := os.ReadFile(vtepIPAllocationFilePath) |
| 164 | + if err != nil { |
| 165 | + return nil, nil, fmt.Errorf("error while reading file %s: %w", vtepIPAllocationFilePath, err) |
| 166 | + } |
| 167 | + |
| 168 | + results := []ipallocator.NVIPAMIPAllocatorResult{} |
| 169 | + if err := json.Unmarshal(content, &results); err != nil { |
| 170 | + return nil, nil, fmt.Errorf("error while unmarshalling IP Allocator results: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + if len(results) != 1 { |
| 174 | + return nil, nil, fmt.Errorf("expecting exactly 1 IP allocation for VTEP") |
| 175 | + } |
| 176 | + |
| 177 | + vtepIPRaw := results[0].IP |
| 178 | + vtepIP, err := netlink.ParseIPNet(vtepIPRaw) |
| 179 | + if err != nil { |
| 180 | + return nil, nil, fmt.Errorf("error while parsing VTEP IP to net.IPNet: %w", err) |
| 181 | + } |
| 182 | + |
| 183 | + gatewayRaw := results[0].Gateway |
| 184 | + gateway := net.ParseIP(gatewayRaw) |
| 185 | + if gateway == nil { |
| 186 | + return nil, nil, errors.New("error while parsing Gateway IP to net.IP: input is not valid") |
| 187 | + } |
| 188 | + |
| 189 | + return vtepIP, gateway, nil |
| 190 | +} |
| 191 | + |
| 192 | +// getPFIP() returns the PF IP from a file that contains the PF IP allocation done by the IP Allocator |
| 193 | +// component. |
| 194 | +func getPFIP() (*net.IPNet, error) { |
| 195 | + content, err := os.ReadFile(pfIPAllocationFilePath) |
| 196 | + if err != nil { |
| 197 | + return nil, fmt.Errorf("error while reading file %s: %w", vtepIPAllocationFilePath, err) |
| 198 | + } |
| 199 | + |
| 200 | + results := []ipallocator.NVIPAMIPAllocatorResult{} |
| 201 | + if err := json.Unmarshal(content, &results); err != nil { |
| 202 | + return nil, fmt.Errorf("error while unmarshalling IP Allocator results: %w", err) |
| 203 | + } |
| 204 | + |
| 205 | + if len(results) != 1 { |
| 206 | + return nil, fmt.Errorf("expecting exactly 1 IP allocation for PF") |
| 207 | + } |
| 208 | + |
| 209 | + pfIPRaw := results[0].IP |
| 210 | + pfIP, err := netlink.ParseIPNet(pfIPRaw) |
| 211 | + if err != nil { |
| 212 | + return nil, fmt.Errorf("error while parsing PF IP to net.IPNet: %w", err) |
| 213 | + } |
| 214 | + |
| 215 | + return pfIP, nil |
| 216 | +} |
| 217 | + |
| 218 | +// getVTEPCIDR returns the VTEP CIDR to be used by the provisioner |
| 219 | +func getVTEPCIDR() (*net.IPNet, error) { |
| 220 | + vtepCIDRRaw := os.Getenv("VTEP_CIDR") |
| 221 | + if vtepCIDRRaw == "" { |
| 222 | + return nil, errors.New("required VTEP_CIDR environment variable is not set") |
| 223 | + } |
| 224 | + |
| 225 | + _, vtepCIDR, err := net.ParseCIDR(vtepCIDRRaw) |
| 226 | + if err != nil { |
| 227 | + klog.Fatalf("error while parsing VTEP CIDR %s as net.IPNet: %s", vtepCIDRRaw, err.Error()) |
| 228 | + } |
| 229 | + |
| 230 | + return vtepCIDR, nil |
| 231 | +} |
| 232 | + |
| 233 | +// getHostCIDR returns the Host CIDR to be used by the provisioner |
| 234 | +func getHostCIDR() (*net.IPNet, error) { |
| 235 | + hostCIDRRaw := os.Getenv("HOST_CIDR") |
| 236 | + if hostCIDRRaw == "" { |
| 237 | + return nil, errors.New("required HOST_CIDR environment variable is not set") |
| 238 | + } |
| 239 | + |
| 240 | + _, hostCIDR, err := net.ParseCIDR(hostCIDRRaw) |
| 241 | + if err != nil { |
| 242 | + klog.Fatalf("error while parsing Host CIDR %s as net.IPNet: %s", hostCIDRRaw, err.Error()) |
| 243 | + } |
| 244 | + |
| 245 | + return hostCIDR, nil |
| 246 | +} |
| 247 | + |
| 248 | +// getGatewayDiscoveryNetwork returns the Network to be used by the provisioner to discover the gateway |
| 249 | +func getGatewayDiscoveryNetwork() (*net.IPNet, error) { |
| 250 | + gatewayDiscoveryNetworkRaw := os.Getenv("GATEWAY_DISCOVERY_NETWORK") |
| 251 | + if gatewayDiscoveryNetworkRaw == "" { |
| 252 | + return nil, errors.New("required GATEWAY_DISCOVERY_NETWORK environment variable is not set") |
| 253 | + } |
| 254 | + |
| 255 | + _, gatewayDiscoveryNetwork, err := net.ParseCIDR(gatewayDiscoveryNetworkRaw) |
| 256 | + if err != nil { |
| 257 | + klog.Fatalf("error while parsing Gateway Discovery Network %s as net.IPNet: %s", gatewayDiscoveryNetwork, err.Error()) |
| 258 | + } |
| 259 | + |
| 260 | + return gatewayDiscoveryNetwork, nil |
| 261 | +} |
| 262 | + |
| 263 | +// getOVNMTU returns the PF MTU to be used by the provisioner |
| 264 | +func getOVNMTU() (int, error) { |
| 265 | + mtuString := os.Getenv("OVN_MTU") |
| 266 | + if mtuString == "" { |
| 267 | + return 0, errors.New("required OVN_MTU environment variable is not set") |
| 268 | + } |
| 269 | + |
| 270 | + mtu, err := strconv.Atoi(mtuString) |
| 271 | + if err != nil { |
| 272 | + return 0, fmt.Errorf("parse environment variable OVN_MTU %s as int: %v", mtuString, err) |
| 273 | + } |
| 274 | + |
| 275 | + if mtu == 0 { |
| 276 | + return 0, errors.New("invalid OVN_MTU value: 0") |
| 277 | + } |
| 278 | + |
| 279 | + return mtu, nil |
| 280 | +} |
| 281 | + |
| 282 | +// parseMode parses the mode in which the binary should be started |
| 283 | +func parseMode(mode string) (dpucniprovisioner.Mode, error) { |
| 284 | + m := map[dpucniprovisioner.Mode]struct{}{ |
| 285 | + dpucniprovisioner.InternalIPAM: {}, |
| 286 | + dpucniprovisioner.ExternalIPAM: {}, |
| 287 | + } |
| 288 | + modeTyped := dpucniprovisioner.Mode(mode) |
| 289 | + if _, ok := m[modeTyped]; !ok { |
| 290 | + return "", errors.New("unknown mode") |
| 291 | + } |
| 292 | + |
| 293 | + return modeTyped, nil |
| 294 | +} |
0 commit comments