Skip to content

Commit 13ffe1d

Browse files
committed
fix vpc nat gateway to correctly use subnet mapped attachment network
Signed-off-by: coldzerofear <coldzerofear@outlook.com>
1 parent 91f264e commit 13ffe1d

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

pkg/controller/vpc_nat_gateway.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func (c *Controller) execNatGwRules(pod *corev1.Pod, operation string, rules []s
660660
// setNatGwAPIAccess adds an interface with API access to the NAT gateway and attaches the standard externalNetwork to the gateway.
661661
// This interface is backed by a NetworkAttachmentDefinition (NAD) with a provider corresponding
662662
// to one that is configured on a subnet part of the default VPC (the K8S apiserver runs in the default VPC)
663-
func (c *Controller) setNatGwAPIAccess(annotations map[string]string, externalNetwork string) error {
663+
func (c *Controller) setNatGwAPIAccess(annotations map[string]string) error {
664664
// Check the NetworkAttachmentDefinition provider exists, must be user-configured
665665
if vpcNatAPINadProvider == "" {
666666
return errors.New("no NetworkAttachmentDefinition provided to access apiserver, check configmap ovn-vpc-nat-config and field 'apiNadProvider'")
@@ -676,12 +676,12 @@ func (c *Controller) setNatGwAPIAccess(annotations map[string]string, externalNe
676676
name, namespace := providerSplit[0], providerSplit[1]
677677

678678
// Craft the name of the NAD for the externalNetwork and the apiNetwork
679-
externalNetworkAttachment := fmt.Sprintf("%s/%s", c.config.PodNamespace, externalNetwork)
680-
apiNetworkAttachment := fmt.Sprintf("%s/%s", namespace, name)
681-
679+
networkAttachments := []string{fmt.Sprintf("%s/%s", namespace, name)}
680+
if externalNetworkAttachment, ok := annotations[nadv1.NetworkAttachmentAnnot]; ok {
681+
networkAttachments = append([]string{externalNetworkAttachment}, networkAttachments...)
682+
}
682683
// Attach the NADs to the Pod by adding them to the special annotation
683-
attachmentAnnotation := fmt.Sprintf("%s, %s", externalNetworkAttachment, apiNetworkAttachment)
684-
annotations[nadv1.NetworkAttachmentAnnot] = attachmentAnnotation
684+
annotations[nadv1.NetworkAttachmentAnnot] = strings.Join(networkAttachments, ",")
685685

686686
// Set the network route to the API, so we can reach it
687687
return c.setNatGwAPIRoute(annotations, namespace, name)
@@ -734,11 +734,17 @@ func (c *Controller) genNatGwStatefulSet(gw *kubeovnv1.VpcNatGateway, oldSts *v1
734734
if oldSts != nil && len(oldSts.Annotations) != 0 {
735735
annotations = maps.Clone(oldSts.Annotations)
736736
}
737-
738-
externalNetworkNad := util.GetNatGwExternalNetwork(gw.Spec.ExternalSubnets)
737+
externalNadNamespace := c.config.PodNamespace
738+
externalNadName := util.GetNatGwExternalNetwork(gw.Spec.ExternalSubnets)
739+
if externalSubnet, err := c.subnetsLister.Get(externalNadName); err == nil {
740+
if name, namespace, ok := util.GetNadBySubnetProvider(externalSubnet.Spec.Provider); ok {
741+
externalNadName = name
742+
externalNadNamespace = namespace
743+
}
744+
}
739745
podAnnotations := map[string]string{
740746
util.VpcNatGatewayAnnotation: gw.Name,
741-
nadv1.NetworkAttachmentAnnot: fmt.Sprintf("%s/%s", c.config.PodNamespace, externalNetworkNad),
747+
nadv1.NetworkAttachmentAnnot: fmt.Sprintf("%s/%s", externalNadNamespace, externalNadName),
742748
util.LogicalSwitchAnnotation: gw.Spec.Subnet,
743749
util.IPAddressAnnotation: gw.Spec.LanIP,
744750
}
@@ -752,7 +758,7 @@ func (c *Controller) genNatGwStatefulSet(gw *kubeovnv1.VpcNatGateway, oldSts *v1
752758

753759
// Add an interface that can reach the API server, we need access to it to probe Kube-OVN resources
754760
if gw.Spec.BgpSpeaker.Enabled {
755-
if err := c.setNatGwAPIAccess(podAnnotations, externalNetworkNad); err != nil {
761+
if err := c.setNatGwAPIAccess(podAnnotations); err != nil {
756762
klog.Error(err)
757763
return nil, err
758764
}
@@ -799,7 +805,7 @@ func (c *Controller) genNatGwStatefulSet(gw *kubeovnv1.VpcNatGateway, oldSts *v1
799805
return nil, err
800806
}
801807

802-
subnet, err := c.findSubnetByNetworkAttachmentDefinition(c.config.PodNamespace, externalNetworkNad, subnets)
808+
subnet, err := c.findSubnetByNetworkAttachmentDefinition(externalNadNamespace, externalNadName, subnets)
803809
if err != nil {
804810
klog.Error(err)
805811
return nil, err

pkg/util/subnet.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ func IsOvnProvider(provider string) bool {
1111
}
1212
return false
1313
}
14+
15+
func GetNadBySubnetProvider(provider string) (nadName, nadNamespace string, existNad bool) {
16+
fields := strings.Split(provider, ".")
17+
switch {
18+
case len(fields) == 3 && fields[2] == OvnProvider:
19+
return fields[0], fields[1], true
20+
case len(fields) == 2:
21+
return fields[0], fields[1], true
22+
}
23+
return "", "", false
24+
}

0 commit comments

Comments
 (0)