Skip to content

Commit

Permalink
feat(natgw): throw error when nad undefined
Browse files Browse the repository at this point in the history
Signed-off-by: SkalaNetworks <[email protected]>
  • Loading branch information
SkalaNetworks committed Jul 21, 2024
1 parent f4ab271 commit 9e8a14e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ func (c *Controller) startWorkers(ctx context.Context) {
}, time.Second, ctx.Done())

go wait.Until(func() {
c.resyncVpcNatImage()
c.resyncVpcNatConfig()
}, time.Second, ctx.Done())

go wait.Until(func() {
Expand Down
8 changes: 7 additions & 1 deletion pkg/controller/vpc_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
var (
vpcNatImage = ""
vpcNatGwBgpSpeakerImage = ""
vpcNatApiNadProvider = ""

Check failure on line 14 in pkg/controller/vpc_nat.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var vpcNatApiNadProvider should be vpcNatAPINadProvider (revive)
)

func (c *Controller) resyncVpcNatImage() {
func (c *Controller) resyncVpcNatConfig() {
cm, err := c.configMapsLister.ConfigMaps(c.config.PodNamespace).Get(util.VpcNatConfig)
if err != nil {
err = fmt.Errorf("failed to get ovn-vpc-nat-config, %v", err)
klog.Error(err)
return
}

// Image we're using to provision the NAT gateways
image, exist := cm.Data["image"]
if !exist {
err = fmt.Errorf("%s should have image field", util.VpcNatConfig)
Expand All @@ -29,5 +31,9 @@ func (c *Controller) resyncVpcNatImage() {
}
vpcNatImage = image

// Image for the BGP sidecar of the gateway (optional)
vpcNatGwBgpSpeakerImage = cm.Data["bgpSpeakerImage"]

// NetworkAttachmentDefinition for the BGP speaker to call the API server
vpcNatApiNadProvider = cm.Data["apiNadProvider"]
}
24 changes: 18 additions & 6 deletions pkg/controller/vpc_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,14 +736,14 @@ func (c *Controller) execNatGwRules(pod *corev1.Pod, operation string, rules []s
return nil
}

func (c *Controller) setNatGwInterface(annotations map[string]string, externalNetwork string, defaultSubnet *kubeovnv1.Subnet) {
func (c *Controller) setNatGwInterface(annotations map[string]string, externalNetwork string, defaultSubnet *kubeovnv1.Subnet) error {
nad := fmt.Sprintf("%s/%s, %s/%s", c.config.PodNamespace, externalNetwork, corev1.NamespaceDefault, nadName)
annotations[util.AttachmentNetworkAnnotation] = nad

setNatGwRoute(annotations, defaultSubnet.Spec.Gateway)
return setNatGwRoute(annotations, defaultSubnet.Spec.Gateway)
}

func setNatGwRoute(annotations map[string]string, subnetGw string) {
func setNatGwRoute(annotations map[string]string, subnetGw string) error {
dst := os.Getenv("KUBERNETES_SERVICE_HOST")

protocol := util.CheckProtocol(dst)
Expand All @@ -755,18 +755,27 @@ func setNatGwRoute(annotations map[string]string, subnetGw string) {
dst = fmt.Sprintf("%s/128", dst)
}
}

// Check the API NetworkAttachmentDefinition exists, otherwise we won't be able to attach
// the BGP speaker to a network that has access to the K8S apiserver (and won't be able to detect EIPs)
if vpcNatApiNadProvider == "" {
return fmt.Errorf("no NetworkAttachmentDefinition provided to access apiserver, check configmap ovn-vpc-nat-config and field 'apiNadProvider'")
}

for _, gw := range strings.Split(subnetGw, ",") {
if util.CheckProtocol(gw) == protocol {
routes := []request.Route{{Destination: dst, Gateway: gw}}
buf, err := json.Marshal(routes)
if err != nil {
klog.Errorf("failed to marshal routes %+v: %v", routes, err)
return fmt.Errorf("failed to marshal routes %+v: %v", routes, err)
} else {

Check failure on line 771 in pkg/controller/vpc_nat_gateway.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
annotations[fmt.Sprintf(util.RoutesAnnotationTemplate, nadProvider)] = string(buf)
annotations[fmt.Sprintf(util.RoutesAnnotationTemplate, vpcNatApiNadProvider)] = string(buf)
}
break
}
}

return nil
}

func (c *Controller) genNatGwStatefulSet(gw *kubeovnv1.VpcNatGateway, oldSts *v1.StatefulSet) (*v1.StatefulSet, error) {
Expand All @@ -787,7 +796,10 @@ func (c *Controller) genNatGwStatefulSet(gw *kubeovnv1.VpcNatGateway, oldSts *v1
if err != nil {
return nil, fmt.Errorf("failed to get default subnet %s: %v", c.config.DefaultLogicalSwitch, err)
}
c.setNatGwInterface(podAnnotations, nadName, defaultSubnet)

if err := c.setNatGwInterface(podAnnotations, nadName, defaultSubnet); err != nil {
return nil, err
}
}

for key, value := range podAnnotations {
Expand Down

0 comments on commit 9e8a14e

Please sign in to comment.