Skip to content

Commit

Permalink
recover ips CR on IPAM initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Mar 30, 2022
1 parent 48eb70a commit 8529bf8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
19 changes: 12 additions & 7 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (c *Controller) InitIPAM() error {
return err
}
for _, pod := range pods {
if pod.Spec.HostNetwork {
if pod.Spec.HostNetwork || !isPodAlive(pod) {
continue
}
podName := c.getNameByPod(pod)
Expand All @@ -302,14 +302,19 @@ func (c *Controller) InitIPAM() error {
if !isOvnSubnet(podNet.Subnet) {
continue
}
if isPodAlive(pod) && pod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName)] == "true" {
_, _, _, err := c.ipam.GetStaticAddress(
fmt.Sprintf("%s/%s", pod.Namespace, podName),
pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)],
pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)],
pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podNet.ProviderName)])
if pod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName)] == "true" {
key := fmt.Sprintf("%s/%s", pod.Namespace, podName)
ip := pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)]
mac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)]
subnet := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podNet.ProviderName)]
_, _, _, err := c.ipam.GetStaticAddress(key, ip, mac, subnet)
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)], err)
} else {
err = c.createOrUpdateCrdIPs(key, ip, mac, subnet, pod.Namespace, pod.Spec.NodeName, podNet.ProviderName)
if err != nil {
klog.Errorf("failed to create/update ips CR %s.%s with ip address %s: %v", podName, pod.Namespace, ip, err)
}
}

if err = c.initAppendPodExternalIds(pod); err != nil {
Expand Down
32 changes: 18 additions & 14 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,29 @@ func (c *Controller) createOrUpdateCrdIPs(key, ip, mac, subnetName, ns, nodeName
return errMsg
}
} else {
if ipCr.Labels != nil {
ipCr.Labels[util.SubnetNameLabel] = subnetName
newIpCr := ipCr.DeepCopy()
if newIpCr.Labels != nil {
newIpCr.Labels[util.SubnetNameLabel] = subnetName
} else {
ipCr.Labels = map[string]string{
newIpCr.Labels = map[string]string{
util.SubnetNameLabel: subnetName,
}
}
ipCr.Spec.PodName = 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
ipCr.Spec.MacAddress = mac
ipCr.Spec.ContainerID = ""
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), ipCr, metav1.UpdateOptions{})
newIpCr.Spec.PodName = key
newIpCr.Spec.Namespace = ns
newIpCr.Spec.Subnet = subnetName
newIpCr.Spec.NodeName = nodeName
newIpCr.Spec.IPAddress = ip
newIpCr.Spec.V4IPAddress = v4IP
newIpCr.Spec.V6IPAddress = v6IP
newIpCr.Spec.MacAddress = mac
newIpCr.Spec.ContainerID = ""
newIpCr.Spec.AttachIPs = []string{}
newIpCr.Spec.AttachMacs = []string{}
newIpCr.Spec.AttachSubnets = []string{}
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), newIpCr, metav1.UpdateOptions{})
if err != nil {
errMsg := fmt.Errorf("failed to create ip crd for %s, %v", ip, err)
errMsg := fmt.Errorf("failed to update ips cr %s: %v", newIpCr.Name, err)
klog.Error(errMsg)
return errMsg
}
Expand Down

0 comments on commit 8529bf8

Please sign in to comment.