Skip to content

Commit

Permalink
feat: ip cr support multi-nic
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Mar 17, 2020
1 parent b5620c2 commit dd1923c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 36 deletions.
3 changes: 3 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ spec:
shortNames:
- ip
additionalPrinterColumns:
- name: Provider
type: string
JSONPath: .spec.provider
- name: IP
type: string
JSONPath: .spec.ipAddress
Expand Down
17 changes: 10 additions & 7 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ type IP struct {
}

type IPSpec struct {
PodName string `json:"podName"`
Namespace string `json:"namespace"`
Subnet string `json:"subnet"`
NodeName string `json:"nodeName"`
IPAddress string `json:"ipAddress"`
MacAddress string `json:"macAddress"`
ContainerID string `json:"containerID"`
PodName string `json:"podName"`
Namespace string `json:"namespace"`
Subnet string `json:"subnet"`
AttachSubnets []string `json:"attach_subnets"`
NodeName string `json:"nodeName"`
IPAddress string `json:"ipAddress"`
AttachIPs []string `json:"attach_ips"`
MacAddress string `json:"macAddress"`
AttachMacs []string `json:"attach_macs"`
ContainerID string `json:"containerID"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
17 changes: 16 additions & 1 deletion pkg/apis/kubeovn/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func NewController(config *Configuration) *Controller {

ipInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddOrDelIP,
UpdateFunc: controller.enqueueUpdateIP,
DeleteFunc: controller.enqueueAddOrDelIP,
})

Expand Down
16 changes: 16 additions & 0 deletions pkg/controller/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@ func (c *Controller) enqueueAddOrDelIP(obj interface{}) {
ipObj := obj.(*kubeovnv1.IP)
klog.V(3).Infof("enqueue update status subnet %s", ipObj.Spec.Subnet)
c.updateSubnetStatusQueue.Add(ipObj.Spec.Subnet)
for _, as := range ipObj.Spec.AttachSubnets {
klog.V(3).Infof("enqueue update status subnet %s", as)
c.updateSubnetStatusQueue.Add(as)
}
}

func (c *Controller) enqueueUpdateIP(old, new interface{}) {
if !c.isLeader() {
return
}
ipObj := new.(*kubeovnv1.IP)
klog.V(3).Infof("enqueue update status subnet %s", ipObj.Spec.Subnet)
for _, as := range ipObj.Spec.AttachSubnets {
klog.V(3).Infof("enqueue update status subnet %s", as)
c.updateSubnetStatusQueue.Add(as)
}
}
1 change: 1 addition & 0 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (c *Controller) handleAddNode(key string) error {
Name: fmt.Sprintf("node-%s", key),
Labels: map[string]string{
util.SubnetNameLabel: c.config.NodeSwitch,
c.config.NodeSwitch: "",
},
},
Spec: kubeovnv1.IPSpec{
Expand Down
24 changes: 13 additions & 11 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,18 @@ func (c *Controller) handleAddSubnet(key string) error {
return err
}

if !isOvnSubnet(subnet) {
return nil
}

if err := c.ipam.AddOrUpdateSubnet(subnet.Name, subnet.Spec.CIDRBlock, subnet.Spec.ExcludeIps); err != nil {
return err
}

if err := calcSubnetStatusIP(subnet, c); err != nil {
klog.Error("init subnet status failed", err)
}

if !isOvnSubnet(subnet) {
return nil
}

exist, err := c.ovnClient.LogicalSwitchExists(subnet.Name)
if err != nil {
klog.Errorf("failed to list logical switch, %v", err)
Expand All @@ -378,9 +382,6 @@ func (c *Controller) handleAddSubnet(key string) error {
}

if !exist {
if err := calcSubnetStatusIP(subnet, c); err != nil {
klog.Error("init subnet status failed", err)
}
subnet.Status.EnsureStandardConditions()
if err = util.ValidateSubnet(*subnet); err != nil {
klog.Error(err)
Expand Down Expand Up @@ -606,6 +607,10 @@ func (c *Controller) handleUpdateSubnet(key string) error {
return err
}

if err := calcSubnetStatusIP(subnet, c); err != nil {
klog.Error("init subnet status failed", err)
}

if !isOvnSubnet(subnet) {
return nil
}
Expand All @@ -628,9 +633,6 @@ func (c *Controller) handleUpdateSubnet(key string) error {
return nil
}

if err := calcSubnetStatusIP(subnet, c); err != nil {
klog.Error("init subnet status failed", err)
}
if err = util.ValidateSubnet(*subnet); err != nil {
klog.Error(err)
subnet.TypeMeta.Kind = "Subnet"
Expand Down Expand Up @@ -849,7 +851,7 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
return err
}
podUsedIPs, err := c.config.KubeOvnClient.KubeovnV1().IPs().List(metav1.ListOptions{
LabelSelector: fields.OneTermEqualSelector(util.SubnetNameLabel, subnet.Name).String(),
LabelSelector: fields.OneTermEqualSelector(subnet.Name, "").String(),
})
if err != nil {
return err
Expand Down
23 changes: 7 additions & 16 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
return
}

ipCrd, err := csh.KubeOvnClient.KubeovnV1().IPs().Get(fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), metav1.GetOptions{})
ipCr, err := csh.KubeOvnClient.KubeovnV1().IPs().Get(fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
_, err := csh.KubeOvnClient.KubeovnV1().IPs().Create(&kubeovnv1.IP{
ObjectMeta: v1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", podRequest.PodName, podRequest.PodNamespace),
Labels: map[string]string{
util.SubnetNameLabel: subnet,
subnet: "",
},
},
Spec: kubeovnv1.IPSpec{
Expand All @@ -113,21 +114,11 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
return
}
} else {
if ipCrd.Labels != nil {
ipCrd.Labels[util.SubnetNameLabel] = subnet
} else {
ipCrd.Labels = map[string]string{
util.SubnetNameLabel: subnet,
}
}
ipCrd.Spec.PodName = podRequest.PodName
ipCrd.Spec.Namespace = podRequest.PodNamespace
ipCrd.Spec.Subnet = subnet
ipCrd.Spec.NodeName = csh.Config.NodeName
ipCrd.Spec.IPAddress = ip
ipCrd.Spec.MacAddress = macAddr
ipCrd.Spec.ContainerID = podRequest.ContainerID
_, err := csh.KubeOvnClient.KubeovnV1().IPs().Update(ipCrd)
ipCr.Labels[subnet] = ""
ipCr.Spec.AttachSubnets = append(ipCr.Spec.AttachSubnets, subnet)
ipCr.Spec.AttachIPs = append(ipCr.Spec.AttachIPs, ip)
ipCr.Spec.AttachMacs = append(ipCr.Spec.AttachMacs, macAddr)
_, err := csh.KubeOvnClient.KubeovnV1().IPs().Update(ipCr)
if err != nil {
errMsg := fmt.Errorf("failed to create ip crd for %s, %v", ip, err)
klog.Error(errMsg)
Expand Down
5 changes: 4 additions & 1 deletion yamls/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ spec:
subresources:
status: {}
additionalPrinterColumns:
- name: Provider
type: string
JSONPath: .spec.provider
- name: Protocol
type: string
JSONPath: .spec.protocol
Expand Down Expand Up @@ -76,4 +79,4 @@ spec:
cidrBlock:
type: "string"
gateway:
type: "string"
type: "string"

0 comments on commit dd1923c

Please sign in to comment.