Skip to content

Commit

Permalink
gc and clean vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
fanriming committed Nov 4, 2020
1 parent 7a0e28b commit 91fec56
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 82 deletions.
29 changes: 29 additions & 0 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (c *Controller) gc() error {
gcFunctions := []func() error{
c.gcNode,
c.gcLogicalSwitch,
c.gcCustomLogicalRouter,
c.gcLogicalSwitchPort,
c.gcLoadBalancer,
c.gcPortGroup,
Expand Down Expand Up @@ -66,6 +67,34 @@ func (c *Controller) gcLogicalSwitch() error {
return nil
}

func (c *Controller) gcCustomLogicalRouter() error {
klog.Infof("start to gc logical router")
var routerNames []string
c.vpcs.Range(func(key, value interface{}) bool {
routerNames = append(routerNames, value.(*Vpc).Router)
return true
})
lrs, err := c.ovnClient.ListLogicalRouter()
if err != nil {
klog.Errorf("failed to list logical router, %v", err)
return err
}
klog.Infof("lr in ovn %v", lrs)
for _, lr := range lrs {
if lr == c.config.ClusterRouter {
continue
}
if !util.IsStringIn(lr, routerNames) {
klog.Infof("gc router %s", lr)
if err := c.handleDeleteSubnet(lr); err != nil {
klog.Errorf("failed to gc router %s, %v", lr, err)
return err
}
}
}
return nil
}

func (c *Controller) gcNode() error {
klog.Infof("start to gc nodes")
nodes, err := c.nodesLister.List(labels.Everything())
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ func (c *Controller) handleDeletePod(key string) error {
klog.Errorf("failed to get pod subnet, %v", err)
return err
}
vpc, err := c.parseSubnetVpc(subnet)
if err != nil {
klog.Errorf("failed to get vpc ubnet, %v", err)
vpc, vpcFound := c.parseSubnetVpc(subnet)
if !vpcFound {
klog.Errorf("failed to get vpc of subnet, %v", err)
return err
}

Expand Down Expand Up @@ -487,8 +487,8 @@ func (c *Controller) handleUpdatePod(key string) error {
klog.Errorf("failed to get subnet %v", err)
return err
}
vpc, err := c.parseSubnetVpc(subnet)
if err != nil {
vpc, vpcFound := c.parseSubnetVpc(subnet)
if !vpcFound {
klog.Errorf("failed to get vpc %v", err)
return err
}
Expand Down
115 changes: 48 additions & 67 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Controller) enqueueDeleteSubnet(obj interface{}) {
c.deleteSubnetQueue.Add(key)
subnet := obj.(*kubeovnv1.Subnet)
if subnet.Spec.GatewayType == kubeovnv1.GWCentralizedType {
c.deleteRouteQueue.Add(subnet.Spec.CIDRBlock)
c.deleteRouteQueue.Add(obj)
}
}

Expand Down Expand Up @@ -147,16 +147,16 @@ func (c *Controller) processNextDeleteRoutePodWorkItem() bool {

err := func(obj interface{}) error {
defer c.deleteRouteQueue.Done(obj)
var key string
var subnet *kubeovnv1.Subnet
var ok bool
if key, ok = obj.(string); !ok {
if subnet, ok = obj.(*kubeovnv1.Subnet); !ok {
c.deleteRouteQueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
utilruntime.HandleError(fmt.Errorf("expected subnet in workqueue but got %#v", obj))
return nil
}
if err := c.handleDeleteRoute(key); err != nil {
c.deleteRouteQueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
if err := c.handleDeleteRoute(subnet); err != nil {
c.deleteRouteQueue.AddRateLimited(subnet)
return fmt.Errorf("error syncing '%s': %s, requeuing", subnet.Name, err.Error())
}
c.deleteRouteQueue.Forget(obj)
return nil
Expand Down Expand Up @@ -252,12 +252,26 @@ func formatSubnet(subnet *kubeovnv1.Subnet, c *Controller) error {
subnet.Spec.GatewayType = kubeovnv1.GWDistributedType
changed = true
}
vpc, err := c.parseSubnetVpc(subnet)
if err == nil && vpc.Default {
if subnet.Spec.Default && subnet.Name != vpc.DefaultLogicalSwitch {
vpc, vpcFound := c.parseSubnetVpc(subnet)
if vpcFound && vpc.Default {
// Some features only work in the default VPC
if subnet.Spec.Default && subnet.Name != c.config.DefaultLogicalSwitch {
subnet.Spec.Default = false
changed = true
}
if c.config.NetworkType == util.NetworkTypeVlan && subnet.Spec.Vlan == "" {
subnet.Spec.Vlan = c.config.DefaultVlanName
if c.config.DefaultVlanID == 0 {
subnet.Spec.UnderlayGateway = true
}
changed = true
}
if subnet.Spec.Vlan != "" {
if _, err := c.vlansLister.Get(subnet.Spec.Vlan); err != nil {
subnet.Spec.Vlan = ""
changed = true
}
}
}
if subnet.Spec.Gateway == "" {
gw, err := util.FirstSubnetIP(subnet.Spec.CIDRBlock)
Expand Down Expand Up @@ -286,21 +300,6 @@ func formatSubnet(subnet *kubeovnv1.Subnet, c *Controller) error {
}
}

if c.config.NetworkType == util.NetworkTypeVlan && subnet.Spec.Vlan == "" {
subnet.Spec.Vlan = c.config.DefaultVlanName
if c.config.DefaultVlanID == 0 {
subnet.Spec.UnderlayGateway = true
}
changed = true
}

if subnet.Spec.Vlan != "" {
if _, err := c.vlansLister.Get(subnet.Spec.Vlan); err != nil {
subnet.Spec.Vlan = ""
changed = true
}
}

if len(subnet.Labels) == 0 {
subnet.Labels = make(map[string]string)
}
Expand All @@ -313,14 +312,6 @@ func formatSubnet(subnet *kubeovnv1.Subnet, c *Controller) error {
changed = true
}

if _, labelSet := subnet.Labels[util.VpcDefaultSubnetLabel]; !labelSet {
if vpcDefaultSubnet, ok := subnet.Annotations[util.CustomVpcDefaultSubnetAnnotation]; ok {
subnet.Labels[util.VpcDefaultSubnetLabel] = vpcDefaultSubnet
}
subnet.Labels[util.VpcDefaultSubnetLabel] = c.config.DefaultLogicalSwitch
changed = true
}

if changed {
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Update(subnet)
if err != nil {
Expand Down Expand Up @@ -428,22 +419,15 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
c.patchSubnetStatus(subnet, "ValidateLogicalSwitchSuccess", "")
}

// init and check vpc
vpc, err := c.parseSubnetVpc(subnet)
if err != nil {
// check and init custom vpc
vpc, vpcFound := c.parseSubnetVpc(subnet)
if !vpcFound {
vpcName, customVpc := subnet.Annotations[util.CustomVpcAnnotation]
_, isVpcDefaultSubnet := subnet.Annotations[util.CustomVpcDefaultSubnetAnnotation]
if customVpc {
if isVpcDefaultSubnet {
// init vpc
vpc, err = c.initCustomVpc(vpcName, subnet.Name)
if err != nil {
klog.Errorf("failed to init vpc %v", err)
return err
}
} else {
err = fmt.Errorf("vpc default subnet not found")
klog.Error(err)
// init vpc
vpc, err = c.initCustomVpc(vpcName, subnet.Name)
if err != nil {
klog.Errorf("failed to init vpc %v", err)
return err
}
}
Expand All @@ -470,7 +454,7 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
klog.Errorf("failed to list nodes %v", err)
return err
}
if subnet.Spec.Vlan != "" && !subnet.Spec.UnderlayGateway {
if vpc.Default && subnet.Spec.Vlan != "" && !subnet.Spec.UnderlayGateway {
for _, node := range nodes {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP && util.CIDRContainIP(subnet.Spec.CIDRBlock, addr.Address) {
Expand Down Expand Up @@ -546,12 +530,16 @@ func (c *Controller) handleUpdateSubnetStatus(key string) error {
return calcSubnetStatusIP(subnet, c)
}

func (c *Controller) handleDeleteRoute(key string) error {
if _, _, err := net.ParseCIDR(key); err != nil {
func (c *Controller) handleDeleteRoute(subnet *kubeovnv1.Subnet) error {
vpc, vpcFound := c.parseSubnetVpc(subnet)
if !vpcFound {
return fmt.Errorf("faild to delete route, vpc not found")
}
if _, _, err := net.ParseCIDR(subnet.Spec.CIDRBlock); err != nil {
return nil
}

return c.ovnClient.DeleteStaticRoute(key, c.config.ClusterRouter)
return c.ovnClient.DeleteStaticRoute(subnet.Spec.CIDRBlock, vpc.Router)
}

func (c *Controller) handleDeleteSubnet(key string) error {
Expand All @@ -563,28 +551,21 @@ func (c *Controller) handleDeleteSubnet(key string) error {
return err
}

vpc, err := c.parseSubnetVpc(subnet)
isCustomVpcDefaultSubnet := false
if err == nil && !vpc.Default && vpc.DefaultLogicalSwitch == subnet.Name {
isCustomVpcDefaultSubnet = true
}

if isCustomVpcDefaultSubnet {
vpc, vpcFound := c.parseSubnetVpc(subnet)
if vpcFound {
labelSelector := labels.SelectorFromSet(labels.Set{util.VpcNameLabel: vpc.Name})
subnetList, err := c.subnetsLister.List(labelSelector)
if err != nil {
klog.Errorf("failed to list subnets %v", err)
return err
}
if len(subnetList) > 1 {
// The default VPC subnet should be removed last
return fmt.Errorf("the default VPC subnet should be removed last")
}

err = c.deleteCustomVpc(vpc.Name)
if err != nil {
klog.Errorf("failed to delete vpc %v", err)
return err
if len(subnetList) == 1 {
// delete vpc
err = c.deleteCustomVpc(vpc.Name)
if err != nil {
klog.Errorf("failed to delete vpc %v", err)
return err
}
}
}

Expand Down
20 changes: 12 additions & 8 deletions pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,21 @@ func (c *Controller) deleteVpcRouter(lr string) error {
return c.ovnClient.DeleteLogicalRouter(lr)
}

func (c *Controller) parseSubnetVpc(subnet *v1.Subnet) (*Vpc, error) {
vpcName, customVpc := subnet.Annotations[util.CustomVpcAnnotation]
if !customVpc {
func (c *Controller) parseSubnetVpc(subnet *v1.Subnet) (*Vpc, bool) {
var vpcName string
var customVpc bool
if 0 == len(subnet.Annotations) {
vpcName = "default"
} else {
vpcName, customVpc = subnet.Annotations[util.CustomVpcAnnotation]
if !customVpc {
vpcName = "default"
}
}

vpc, ok := c.vpcs.Load(vpcName)
if !ok {
err := fmt.Errorf("vpc %s not found", vpcName)
klog.Error(err)
return nil, err
klog.Infof("vpc %s not found", vpcName)
return nil, false
}
return vpc.(*Vpc), nil
return vpc.(*Vpc), true
}
3 changes: 1 addition & 2 deletions pkg/util/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const (
PortSecurityAnnotation = "ovn.kubernetes.io/port_security"
NorthGatewayAnnotation = "ovn.kubernetes.io/north_gateway"

CustomVpcAnnotation = "ovn.kubernetes.io/vpc"
CustomVpcDefaultSubnetAnnotation = "ovn.kubernetes.io/vpc_default_subnet"
CustomVpcAnnotation = "ovn.kubernetes.io/vpc"

AllocatedAnnotationTemplate = "%s.kubernetes.io/allocated"
MacAddressAnnotationTemplate = "%s.kubernetes.io/mac_address"
Expand Down

0 comments on commit 91fec56

Please sign in to comment.