From 8cd443b8fe06005b49dc3d21021fae6b52eb6106 Mon Sep 17 00:00:00 2001 From: Prince Pereira Date: Mon, 9 Sep 2024 12:03:16 +0530 Subject: [PATCH] Sample changes for dualstack testing. --- cni/cni.go | 25 +++++++ example/cni_dualstack_sample.conf | 78 ++++++++++++++++++++ scripts/autogencniconf/generateCNIConfig.ps1 | 14 +++- 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 example/cni_dualstack_sample.conf diff --git a/cni/cni.go b/cni/cni.go index 4a58dcc1..c4df4d61 100644 --- a/cni/cni.go +++ b/cni/cni.go @@ -70,6 +70,7 @@ type NetworkConfig struct { Name string `json:"name"` // Name is the Network Name. We would also use this as the Type of HNS Network Type string `json:"type"` // As per SPEC, Type is Name of the Binary Ipam IpamConfig `json:"ipam"` + Ipamv6 IpamConfig `json:"ipamv6"` DNS cniTypes.DNS `json:"dns"` OptionalFlags OptionalFlags `json:"optionalFlags"` RuntimeConfig RuntimeConfig `json:"runtimeConfig"` @@ -225,6 +226,30 @@ func (config *NetworkConfig) GetNetworkInfo(podNamespace string) (ninfo *network subnets = append(subnets, subnet) } + if config.OptionalFlags.EnableDualStack && config.Ipamv6.Subnet != "" { + ip, s, _ := net.ParseCIDR(config.Ipamv6.Subnet) + if ip.To4() == nil && len(ip) == net.IPv6len { + gatewayIP := make(net.IP, len(ip)) + copy(gatewayIP, ip) + // Find the first IP in the subnet (usually the gateway) + for i := len(gatewayIP) - 1; i >= 0; i-- { + gatewayIP[i]++ + if gatewayIP[i] != 0 { + break + } + } + if config.Ipamv6.Routes != nil && len(config.Ipamv6.Routes) > 0 && config.Ipamv6.Routes[0].GW != nil { + gatewayIP = config.Ipamv6.Routes[0].GW + } + subnet := network.SubnetInfo{ + AddressPrefix: *s, + GatewayAddress: gatewayIP, + Policies: []network.Policy{}, + } + subnets = append(subnets, subnet) + } + } + if len(config.DNS.Search) > 0 { if podNamespace != "" { config.DNS.Search[0] = podNamespace + "." + config.DNS.Search[0] diff --git a/example/cni_dualstack_sample.conf b/example/cni_dualstack_sample.conf new file mode 100644 index 00000000..1f49a60e --- /dev/null +++ b/example/cni_dualstack_sample.conf @@ -0,0 +1,78 @@ +{ + "cniVersion": "0.2.0", + "name": "ContainerHostA", + "type": "sdnbridge", + "master": "Ethernet", + "capabilities": { + "portMappings": true, + "dns" : true + }, + "ipam": { + "environment": "azure", + "subnet": "192.168.100.0/24", + "routes": [ + { + "GW": "192.168.100.2" + } + ] + }, + "ipamv6": { + "environment": "azure", + "subnet": "10::06/64", + "routes": [ + { + "GW": "10::02" + } + ] + }, + "dns": { + "Nameservers": [ + "168.63.129.16" + ], + "Search": [ + "svc.cluster.local" + ] + }, + "optionalFlags" : { + "localRoutedPortMapping" : true, + "allowAclPortMapping" : true, + "enableDualStack" : true + }, + "AdditionalArgs": [ + { + "Name": "EndpointPolicy", + "Value": { + "Type": "OutBoundNAT", + "Settings": { + "Exceptions": [ + "192.168.100.0/24", + "192.168.100.2/32" + ] + } + } + } + ,{ + "Name": "EndpointPolicy", + "Value": { + "Type":"ACL", + "Settings": { + "Action": "Allow", + "Direction": "Out", + "Priority": 2000 + } + } + } + ,{ + "Name": "EndpointPolicy", + "Value": { + "Type":"ACL", + "Settings": { + "Action": "Allow", + "Direction": "In", + "Priority": 2000 + } + } + } + + ] +} diff --git a/scripts/autogencniconf/generateCNIConfig.ps1 b/scripts/autogencniconf/generateCNIConfig.ps1 index e31becae..fa3b4ced 100644 --- a/scripts/autogencniconf/generateCNIConfig.ps1 +++ b/scripts/autogencniconf/generateCNIConfig.ps1 @@ -70,6 +70,7 @@ enum WKOptionalKeysFlag { Ipam = 2 #[WKOptionalKeysFlag]::NoWKOptKeys -shl 1 Dns = 4 #[WKOptionalKeysFlag]::NoWKOptKeys -shl 2 MaxFlags = 8 #[WKOptionalKeysFlag]::NoWKOptKeys -shl 3 + Ipamv6 = 16 #[WKOptionalKeysFlag]::NoWKOptKeys -shl 4 } class Policy { @@ -181,7 +182,7 @@ class CniConf { # Set optional fields to be populated $this.OptKeyParams = $this.OptKeyParams -bor [OptionalKeysFlag]::Capabilities -bor [OptionalKeysFlag]::Master # Set wellknown optional fields to be populated - $this.WKOptKeyParams = $this.WKOptKeyParams -bor [WKOptionalKeysFlag]::Dns -bor [WKOptionalKeysFlag]::Ipam + $this.WKOptKeyParams = $this.WKOptKeyParams -bor [WKOptionalKeysFlag]::Dns -bor [WKOptionalKeysFlag]::Ipam -bor [WKOptionalKeysFlag]::Ipamv6 } EnsureMandatoryParametersPresent([System.Object] $cniArgs) { @@ -245,6 +246,17 @@ class CniConf { $this.CniBase.Add('ipam', $ipamFields) } + ([WKOptionalKeysFlag]::Ipamv6).value__ { + $ipamFields = [System.Collections.Specialized.OrderedDictionary]::new() + $ipamFields.Add('environment', 'azure') + $ipamFields.Add('subnet', $this.Args.Subnet) + $routes = @() + $routes += (@{'GW'=$this.Args.Gateway;}) + $ipamFields.Add('routes', $routes) + + $this.CniBase.Add('ipamv6', $ipamFields) + } + ([WKOptionalKeysFlag]::Dns).value__ { $dnsFields = [System.Collections.Specialized.OrderedDictionary]::new() $nameservers = @()