Skip to content

Commit

Permalink
create ip crd in kube-ovn-controller (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Mar 29, 2022
1 parent 01163c1 commit 2fe7fff
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
56 changes: 20 additions & 36 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,48 +290,32 @@ func (c *Controller) InitIPAM() error {
return err
}
for _, pod := range pods {
if isPodAlive(pod) && pod.Annotations[util.AllocatedAnnotation] == "true" {
podName := c.getNameByPod(pod)
if pod.Annotations[util.LogicalSwitchAnnotation] != "" {
if pod.Spec.HostNetwork {
continue
}
podName := c.getNameByPod(pod)
podNets, err := c.getPodKubeovnNets(pod)
if err != nil {
klog.Errorf("failed to get pod kubeovn nets %s.%s address %s: %v", pod.Name, pod.Namespace, pod.Annotations[util.IpAddressAnnotation], err)
}
for _, podNet := range podNets {
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[util.IpAddressAnnotation],
pod.Annotations[util.MacAddressAnnotation],
pod.Annotations[util.LogicalSwitchAnnotation])
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 err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[util.IpAddressAnnotation], err)
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, podNet.ProviderName)], err)
}
}
attachNetworks := pod.Annotations[util.AttachmentNetworkAnnotation]
if attachNetworks != "" {
attachments, err := util.ParsePodNetworkAnnotation(attachNetworks, pod.Namespace)
if err != nil {
klog.Errorf("failed to parse attach net for pod '%s', %v", podName, err)
continue
}
for _, attach := range attachments {
var builder strings.Builder
builder.WriteString(attach.Name)
builder.WriteString(".")
if attach.Namespace == "" {
builder.WriteString("default")
} else {
builder.WriteString(attach.Namespace)
}

_, _, _, err := c.ipam.GetStaticAddress(
fmt.Sprintf("%s/%s", pod.Namespace, podName),
pod.Annotations[fmt.Sprintf(util.IpAddressAnnotationTemplate, builder.String())],
pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, builder.String())],
pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, builder.String())])
if err != nil {
klog.Errorf("failed to init pod %s.%s address %s: %v", podName, pod.Namespace, pod.Annotations[util.IpAddressAnnotation], err)
}

if err = c.initAppendPodExternalIds(pod); err != nil {
klog.Errorf("failed to init append pod %s.%s externalIds: %v", podName, pod.Namespace, err)
}
}
if err = c.initAppendPodExternalIds(pod); err != nil {
klog.Errorf("failed to init append pod %s.%s externalIds: %v", podName, pod.Namespace, err)
}
}
}

Expand Down
34 changes: 21 additions & 13 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,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 @@ -567,23 +567,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 @@ -605,16 +613,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 @@ -486,6 +486,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 != "" {
vlan, err := c.vlansLister.Get(subnet.Spec.Vlan)
Expand Down
3 changes: 3 additions & 0 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (subnet *Subnet) GetRandomMac(podName string) string {

func (subnet *Subnet) GetStaticMac(podName, mac string) error {
if p, ok := subnet.MacToPod[mac]; ok && p != podName {
klog.Errorf("existPod %s, allocated pod %s, mac %s", p, podName, mac)
return ConflictError
}
subnet.MacToPod[mac] = podName
Expand Down Expand Up @@ -303,6 +304,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force
if v4 {
if existPod, ok := subnet.V4IPToPod[ip]; ok {
if existPod != podName {
klog.Errorf("existPod %s, allocated pod %s, ip %s", existPod, podName, ip)
return ip, mac, ConflictError
}
if !force {
Expand Down Expand Up @@ -332,6 +334,7 @@ func (subnet *Subnet) GetStaticAddress(podName string, ip IP, mac string, force
} else if v6 {
if existPod, ok := subnet.V6IPToPod[ip]; ok {
if existPod != podName {
klog.Errorf("existPod %s, allocated pod %s, ip %s", existPod, podName, ip)
return ip, mac, ConflictError
}
if !force {
Expand Down

0 comments on commit 2fe7fff

Please sign in to comment.