Skip to content

Commit

Permalink
add lsp with external_id
Browse files Browse the repository at this point in the history
  • Loading branch information
fanriming committed Mar 28, 2021
1 parent ec7f742 commit 5126aed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *Controller) handleAddNode(key string) error {
}

ipStr := util.GetStringIP(v4IP, v6IP)
if err := c.ovnClient.CreatePort(c.config.NodeSwitch, portName, ipStr, subnet.Spec.CIDRBlock, mac, tag, false); err != nil {
if err := c.ovnClient.CreatePort(c.config.NodeSwitch, portName, ipStr, subnet.Spec.CIDRBlock, mac, tag, "", "", false); err != nil {
return err
}

Expand Down
42 changes: 5 additions & 37 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (c *Controller) handleAddPod(key string) error {
}

ip := pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, subnet.Spec.Provider)]
if err := c.ovnClient.CreatePort(subnet.Name, ovs.PodNameToPortName(name, namespace, podNet.ProviderName), ip, subnet.Spec.CIDRBlock, mac, tag, portSecurity); err != nil {
if err := c.ovnClient.CreatePort(subnet.Name, ovs.PodNameToPortName(name, namespace, podNet.ProviderName), ip, subnet.Spec.CIDRBlock, mac, tag, pod.Name, pod.Namespace, portSecurity); err != nil {
c.recorder.Eventf(pod, v1.EventTypeWarning, "CreateOVNPortFailed", err.Error())
return err
}
Expand Down Expand Up @@ -494,11 +494,13 @@ func (c *Controller) handleDeletePod(key string) error {
}
}

ports, err := c.getPodPorts(name, namespace)
ports, err := c.ovnClient.ListPodLogicalSwitchPorts(name, namespace)
if err != nil {
klog.Errorf("failed to get attachPorts of pod '%s', %v", name, err)
klog.Errorf("failed to list lsps of pod '%s', %v", name, err)
return err
}
// Add additional default ports to compatible with previous versions
ports = append(ports, ovs.PodNameToPortName(name, namespace, util.OvnProvider))

for _, portName := range ports {
if err := c.ovnClient.DeleteLogicalSwitchPort(portName); err != nil {
Expand All @@ -516,40 +518,6 @@ func (c *Controller) handleDeletePod(key string) error {
return nil
}

func (c *Controller) getPodPorts(pod, namespace string) (ports []string, err error) {
defaultPort := ovs.PodNameToPortName(pod, namespace, util.OvnProvider)
if c.ovnClient.IsLogicalSwitchPortExist(defaultPort) {
ports = append(ports, defaultPort)
}

allNs, err := c.namespacesLister.List(labels.Everything())
if err != nil {
return nil, err
}
for _, ns := range allNs {
attachNets, err := c.config.AttachNetClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(ns.Name).List(context.Background(), metav1.ListOptions{})
if err != nil {
if k8serrors.IsForbidden(err) {
return nil, nil
} else {
klog.Errorf("failed to list attach-net-def, %v", err)
return nil, err
}
}
for _, attachNet := range attachNets.Items {
netCfg, err := loadNetConf([]byte(attachNet.Spec.Config))
if err != nil || netCfg.Type != util.CniTypeName {
continue
}
port := ovs.PodNameToPortName(pod, namespace, fmt.Sprintf("%s.%s.ovn", attachNet.Name, attachNet.Namespace))
if c.ovnClient.IsLogicalSwitchPortExist(port) {
ports = append(ports, port)
}
}
}
return ports, nil
}

func (c *Controller) handleUpdatePod(key string) error {
c.podKeyMutex.Lock(key)
defer c.podKeyMutex.Unlock(key)
Expand Down
24 changes: 23 additions & 1 deletion pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c Client) DeleteICLogicalRouterPort(az string) error {
}

// CreatePort create logical switch port in ovn
func (c Client) CreatePort(ls, port, ip, cidr, mac, tag string, portSecurity bool) error {
func (c Client) CreatePort(ls, port, ip, cidr, mac, tag, pod, namespace string, portSecurity bool) error {
var ovnCommand []string
if util.CheckProtocol(cidr) == kubeovnv1.ProtocolDual {
ips := strings.Split(ip, ",")
Expand Down Expand Up @@ -138,13 +138,35 @@ func (c Client) CreatePort(ls, port, ip, cidr, mac, tag string, portSecurity boo
"--", "set", "logical_switch_port", port, fmt.Sprintf("tag=%s", tag))
}

if pod != "" && namespace != "" {
ovnCommand = append(ovnCommand,
"--", "set", "logical_switch_port", port, fmt.Sprintf("external_ids:pod=%s/%s", namespace, pod))
}

if _, err := c.ovnNbCommand(ovnCommand...); err != nil {
klog.Errorf("create port %s failed %v", port, err)
return err
}
return nil
}

func (c Client) ListPodLogicalSwitchPorts(pod, namespace string) ([]string, error) {
output, err := c.ovnNbCommand("--format=csv", "--data=bare", "--no-heading", "--columns=name", "find", "logical_switch_port", fmt.Sprintf("external_ids:pod=%s/%s", namespace, pod))
if err != nil {
klog.Errorf("failed to list logical switch port, %v", err)
return nil, err
}
lines := strings.Split(output, "\n")
result := make([]string, 0, len(lines))
for _, l := range lines {
if len(strings.TrimSpace(l)) == 0 {
continue
}
result = append(result, strings.TrimSpace(l))
}
return result, nil
}

func (c Client) SetLogicalSwitchConfig(ls string, isUnderlayGW bool, lr, protocol, subnet, gateway string, excludeIps []string) error {
var err error
cidrBlocks := strings.Split(subnet, ",")
Expand Down

0 comments on commit 5126aed

Please sign in to comment.