From effc11156a089053bf3649ecb3c0947a617d8824 Mon Sep 17 00:00:00 2001 From: changluyi <47097611+changluyi@users.noreply.github.com> Date: Tue, 13 Jun 2023 13:17:42 +0800 Subject: [PATCH] u2o support specify u2o ip on release-1.11 (#2937) --- dist/images/install.sh | 2 ++ kubeovn-helm/templates/kube-ovn-crd.yaml | 2 ++ pkg/apis/kubeovn/v1/types.go | 3 ++- pkg/controller/subnet.go | 32 ++++++++++++++++++++---- pkg/util/validator.go | 12 +++++++++ yamls/crd.yaml | 2 ++ 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/dist/images/install.sh b/dist/images/install.sh index 83a9a71b694..a50d3648f69 100644 --- a/dist/images/install.sh +++ b/dist/images/install.sh @@ -1495,6 +1495,8 @@ spec: - reject u2oInterconnection: type: boolean + u2oInterconnectionIP: + type: string scope: Cluster names: plural: subnets diff --git a/kubeovn-helm/templates/kube-ovn-crd.yaml b/kubeovn-helm/templates/kube-ovn-crd.yaml index c021d016188..aea01ce5d1e 100644 --- a/kubeovn-helm/templates/kube-ovn-crd.yaml +++ b/kubeovn-helm/templates/kube-ovn-crd.yaml @@ -1266,6 +1266,8 @@ spec: - reject u2oInterconnection: type: boolean + u2oInterconnectionIP: + type: string scope: Cluster names: plural: subnets diff --git a/pkg/apis/kubeovn/v1/types.go b/pkg/apis/kubeovn/v1/types.go index 2121691366d..0a37e77427c 100644 --- a/pkg/apis/kubeovn/v1/types.go +++ b/pkg/apis/kubeovn/v1/types.go @@ -138,7 +138,8 @@ type SubnetSpec struct { Acls []Acl `json:"acls,omitempty"` - U2OInterconnection bool `json:"u2oInterconnection,omitempty"` + U2OInterconnection bool `json:"u2oInterconnection,omitempty"` + U2OInterconnectionIP string `json:"u2oInterconnectionIP,omitempty"` } type Acl struct { diff --git a/pkg/controller/subnet.go b/pkg/controller/subnet.go index a8f4a44b992..880f9db0632 100644 --- a/pkg/controller/subnet.go +++ b/pkg/controller/subnet.go @@ -94,7 +94,9 @@ func (c *Controller) enqueueUpdateSubnet(old, new interface{}) { oldSubnet.Spec.IPv6RAConfigs != newSubnet.Spec.IPv6RAConfigs || oldSubnet.Spec.Protocol != newSubnet.Spec.Protocol || !reflect.DeepEqual(oldSubnet.Spec.Acls, newSubnet.Spec.Acls) || - oldSubnet.Spec.U2OInterconnection != newSubnet.Spec.U2OInterconnection { + oldSubnet.Spec.U2OInterconnection != newSubnet.Spec.U2OInterconnection || + (newSubnet.Spec.U2OInterconnection && newSubnet.Spec.U2OInterconnectionIP != "" && + oldSubnet.Spec.U2OInterconnectionIP != newSubnet.Spec.U2OInterconnectionIP) { klog.V(3).Infof("enqueue update subnet %s", key) c.addOrUpdateSubnetQueue.Add(key) } @@ -312,6 +314,11 @@ func formatSubnet(subnet *kubeovnv1.Subnet, c *Controller) error { } } } + if subnet.Spec.U2OInterconnectionIP != "" && !subnet.Spec.U2OInterconnection { + subnet.Spec.U2OInterconnectionIP = "" + changed = true + } + klog.Infof("format subnet %v, changed %v", subnet.Name, changed) if changed { _, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Update(context.Background(), subnet, metav1.UpdateOptions{}) @@ -1530,14 +1537,29 @@ func (c *Controller) reconcileU2OInterconnectionIP(subnet *kubeovnv1.Subnet) err klog.Infof("reconcile underlay subnet %s to overlay interconnection with U2OInterconnection %v U2OInterconnectionIP %s ", subnet.Name, subnet.Spec.U2OInterconnection, subnet.Status.U2OInterconnectionIP) if subnet.Spec.U2OInterconnection { - if subnet.Status.U2OInterconnectionIP == "" { - u2oInterconnName := fmt.Sprintf(util.U2OInterconnName, subnet.Spec.Vpc, subnet.Name) - u2oInterconnLrpName := fmt.Sprintf("%s-%s", subnet.Spec.Vpc, subnet.Name) - v4ip, v6ip, _, err := c.acquireIpAddress(subnet.Name, u2oInterconnName, u2oInterconnLrpName) + u2oInterconnName := fmt.Sprintf(util.U2OInterconnName, subnet.Spec.Vpc, subnet.Name) + u2oInterconnLrpName := fmt.Sprintf("%s-%s", subnet.Spec.Vpc, subnet.Name) + var v4ip, v6ip string + var err error + if subnet.Spec.U2OInterconnectionIP == "" && subnet.Status.U2OInterconnectionIP == "" { + v4ip, v6ip, _, err = c.acquireIpAddress(subnet.Name, u2oInterconnName, u2oInterconnLrpName) if err != nil { klog.Errorf("failed to acquire underlay to overlay interconnection ip address for subnet %s, %v", subnet.Name, err) return err } + } else if subnet.Spec.U2OInterconnectionIP != "" && subnet.Status.U2OInterconnectionIP != subnet.Spec.U2OInterconnectionIP { + if subnet.Status.U2OInterconnectionIP != "" { + c.ipam.ReleaseAddressByPod(u2oInterconnName) + } + + v4ip, v6ip, _, err = c.acquireStaticIpAddress(subnet.Name, u2oInterconnName, u2oInterconnLrpName, subnet.Spec.U2OInterconnectionIP) + if err != nil { + klog.Errorf("failed to acquire static underlay to overlay interconnection ip address for subnet %s, %v", subnet.Name, err) + return err + } + } + + if v4ip != "" || v6ip != "" { switch subnet.Spec.Protocol { case kubeovnv1.ProtocolIPv4: subnet.Status.U2OInterconnectionIP = v4ip diff --git a/pkg/util/validator.go b/pkg/util/validator.go index 223e8bfd89c..abe145f5d1b 100644 --- a/pkg/util/validator.go +++ b/pkg/util/validator.go @@ -105,6 +105,18 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error { } } } + + if subnet.Spec.LogicalGateway && subnet.Spec.U2OInterconnection { + return fmt.Errorf("logicalGateway and u2oInterconnection can't be opened at the same time") + } + + if subnet.Spec.U2OInterconnectionIP != "" { + if !CIDRContainIP(subnet.Spec.CIDRBlock, subnet.Spec.U2OInterconnectionIP) { + return fmt.Errorf("u2oInterconnectionIP %s is not in subnet %s cidr %s", + subnet.Spec.U2OInterconnectionIP, + subnet.Name, subnet.Spec.CIDRBlock) + } + } return nil } diff --git a/yamls/crd.yaml b/yamls/crd.yaml index 804f38c1f7c..29595369efd 100644 --- a/yamls/crd.yaml +++ b/yamls/crd.yaml @@ -1274,6 +1274,8 @@ spec: - reject u2oInterconnection: type: boolean + u2oInterconnectionIP: + type: string scope: Cluster names: plural: subnets