Skip to content

Commit

Permalink
create ip crd in kube-ovn-controller (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Mar 29, 2022
1 parent 25abbce commit 19ecaee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
34 changes: 21 additions & 13 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (c *Controller) handleAddNode(key string) error {
return err
}

if err := c.createOrUpdateCrdIPs(key, ipStr, mac); err != nil {
if err := c.createOrUpdateCrdIPs(key, ipStr, mac, c.config.NodeSwitch, "", node.Name, ""); err != nil {
klog.Errorf("failed to create or update IPs node-%s: %v", key, err)
return err
}
Expand Down Expand Up @@ -627,23 +627,31 @@ func (c *Controller) handleUpdateNode(key string) error {
return nil
}

func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
func (c *Controller) createOrUpdateCrdIPs(key, ip, mac, subnetName, ns, nodeName, providerName string) error {
var ipName string
if subnetName == c.config.NodeSwitch {
ipName = fmt.Sprintf("node-%s", key)
} else {
ipName = ovs.PodNameToPortName(key, ns, providerName)
}

v4IP, v6IP := util.SplitStringIP(ip)
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), fmt.Sprintf("node-%s", key), metav1.GetOptions{})
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), ipName, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Create(context.Background(), &kubeovnv1.IP{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("node-%s", key),
Name: ipName,
Labels: map[string]string{
util.SubnetNameLabel: c.config.NodeSwitch,
c.config.NodeSwitch: "",
util.SubnetNameLabel: subnetName,
subnetName: "",
},
},
Spec: kubeovnv1.IPSpec{
PodName: key,
Subnet: c.config.NodeSwitch,
NodeName: key,
Subnet: subnetName,
NodeName: nodeName,
Namespace: ns,
IPAddress: ip,
V4IPAddress: v4IP,
V6IPAddress: v6IP,
Expand All @@ -665,16 +673,16 @@ func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
}
} else {
if ipCr.Labels != nil {
ipCr.Labels[util.SubnetNameLabel] = c.config.NodeSwitch
ipCr.Labels[util.SubnetNameLabel] = subnetName
} else {
ipCr.Labels = map[string]string{
util.SubnetNameLabel: c.config.NodeSwitch,
util.SubnetNameLabel: subnetName,
}
}
ipCr.Spec.PodName = key
ipCr.Spec.Namespace = ""
ipCr.Spec.Subnet = c.config.NodeSwitch
ipCr.Spec.NodeName = key
ipCr.Spec.Namespace = ns
ipCr.Spec.Subnet = subnetName
ipCr.Spec.NodeName = nodeName
ipCr.Spec.IPAddress = ip
ipCr.Spec.V4IPAddress = v4IP
ipCr.Spec.V6IPAddress = v6IP
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ func (c *Controller) handleAddPod(key string) error {
return err
}

if err := c.createOrUpdateCrdIPs(pod.Name, ipStr, mac, subnet.Name, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName); err != nil {
klog.Errorf("failed to create IP %s.%s: %v", pod.Name, pod.Namespace, err)
}

if podNet.Type != providerTypeIPAM {
if (subnet.Spec.Vlan == "" || subnet.Spec.LogicalGateway) && subnet.Spec.Vpc != "" {
pod.Annotations[fmt.Sprintf(util.LogicalRouterAnnotationTemplate, podNet.ProviderName)] = subnet.Spec.Vpc
Expand Down

0 comments on commit 19ecaee

Please sign in to comment.