Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improving query performance of iptablesEips using caching #2713

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pkg/controller/qos_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -354,13 +354,13 @@ func (c *Controller) handleUpdateQoSPolicy(key string) error {
}
// should delete
if !cachedQos.DeletionTimestamp.IsZero() {
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(),
metav1.ListOptions{LabelSelector: fields.OneTermEqualSelector(util.QoSLabel, key).String()})
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.QoSLabel: key}))
if err != nil {
klog.Errorf("failed to get eip list, %v", err)
return err
}
if len(eips.Items) != 0 {
if len(eips) != 0 {
err = fmt.Errorf("qos policy %s is being used", key)
klog.Error(err)
return err
Expand Down Expand Up @@ -402,16 +402,16 @@ func (c *Controller) handleUpdateQoSPolicy(key string) error {
} else {
if cachedQos.Status.BindingType == kubeovnv1.QoSBindingTypeEIP {
// filter to eip
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(),
metav1.ListOptions{LabelSelector: fields.OneTermEqualSelector(util.QoSLabel, key).String()})
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.QoSLabel: key}))
if err != nil {
klog.Errorf("failed to get eip list, %v", err)
return err
}
if len(eips.Items) == 0 {
if len(eips) == 0 {
// not thing to do
} else if len(eips.Items) == 1 {
eip := &eips.Items[0]
} else if len(eips) == 1 {
eip := eips[0]
if err = c.reconcileEIPBandtithLimitRules(eip, added, deleted, updated); err != nil {
klog.Errorf("failed to reconcile eip %s bandwidth limit rules, %v", eip.Name, err)
return err
Expand Down
14 changes: 6 additions & 8 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1870,13 +1870,12 @@ func calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
usingIPs += float64(len(vips.Items))

if subnet.Name == util.VpcExternalNet {
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(), metav1.ListOptions{
LabelSelector: fields.OneTermEqualSelector(util.SubnetNameLabel, subnet.Name).String(),
})
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.SubnetNameLabel: subnet.Name}))
if err != nil {
return err
}
usingIPs += float64(len(eips.Items))
usingIPs += float64(len(eips))
}
v4availableIPs = v4availableIPs - usingIPs
if v4availableIPs < 0 {
Expand Down Expand Up @@ -1942,13 +1941,12 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
}
usingIPs += float64(len(vips.Items))
if subnet.Name == util.VpcExternalNet {
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(), metav1.ListOptions{
LabelSelector: fields.OneTermEqualSelector(util.SubnetNameLabel, subnet.Name).String(),
})
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.SubnetNameLabel: subnet.Name}))
if err != nil {
return err
}
usingIPs += float64(len(eips.Items))
usingIPs += float64(len(eips))
}

availableIPs = availableIPs - usingIPs
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/vpc_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ func (c *Controller) handleUpdateVpcEip(natGwKey string) error {
klog.Error(err)
return err
}
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(), metav1.ListOptions{})
eips, err := c.iptablesEipsLister.List(labels.Everything())
if err != nil {
err = fmt.Errorf("failed to get eip list, %v", err)
klog.Error(err)
return err
}
for _, eip := range eips.Items {
for _, eip := range eips {
if eip.Spec.NatGwDp == natGwKey && eip.Status.Redo != NAT_GW_CREATED_AT && eip.Annotations[util.VpcNatAnnotation] == "" {
klog.V(3).Infof("redo eip %s", eip.Name)
if err = c.patchEipStatus(eip.Name, "", NAT_GW_CREATED_AT, "", "", false); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/vpc_nat_gw_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -504,14 +505,14 @@ func (c *Controller) addEipQoS(eip *kubeovnv1.IptablesEIP, v4ip string) error {
var err error
qosPolicy, err := c.config.KubeOvnClient.KubeovnV1().QoSPolicies().Get(context.Background(), eip.Spec.QoSPolicy, metav1.GetOptions{})
if !qosPolicy.Status.Shared {
eips, err := c.config.KubeOvnClient.KubeovnV1().IptablesEIPs().List(context.Background(),
metav1.ListOptions{LabelSelector: fields.OneTermEqualSelector(util.QoSLabel, qosPolicy.Name).String()})
eips, err := c.iptablesEipsLister.List(
labels.SelectorFromSet(labels.Set{util.QoSLabel: qosPolicy.Name}))
if err != nil {
klog.Errorf("failed to get eip list, %v", err)
return err
}
if len(eips.Items) != 0 {
if eips.Items[0].Name != eip.Name {
if len(eips) != 0 {
if eips[0].Name != eip.Name {
err := fmt.Errorf("not support unshared qos policy %s to related to multiple eip", eip.Spec.QoSPolicy)
klog.Error(err)
return err
Expand Down