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

Fix slr #3746

Merged
merged 4 commits into from
Feb 21, 2024
Merged

Fix slr #3746

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
69 changes: 42 additions & 27 deletions pkg/controller/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -202,7 +203,7 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
vip = util.JoinHostPort(lbVip, port.Port)

if !ignoreHealthCheck {
if checkIP, err = c.getHealthCheckVip(vpcName, subnetName, lbVip); err != nil {
if checkIP, err = c.getHealthCheckVip(subnetName, lbVip); err != nil {
return err
}

Expand Down Expand Up @@ -293,36 +294,50 @@ func (c *Controller) getVpcSubnetName(pods []*v1.Pod, endpoints *v1.Endpoints, s
return vpcName, subnetName
}

func (c *Controller) getHealthCheckVip(vpcName, subnetName, lbVip string) (string, error) {
func (c *Controller) getHealthCheckVip(subnetName, lbVip string) (string, error) {
var (
checkVip *kubeovnv1.Vip
checkIP string
err error
needCreateHealthCheckVip bool
checkVip *kubeovnv1.Vip
checkIP string
err error
)

if checkVip, err = c.config.KubeOvnClient.KubeovnV1().Vips().Get(context.Background(), subnetName, metav1.GetOptions{}); err != nil {
checkVip, err = c.virtualIpsLister.Get(subnetName)
if err != nil {
if errors.IsNotFound(err) {
if checkVip, err = c.config.KubeOvnClient.
KubeovnV1().
Vips().
Create(context.Background(),
&kubeovnv1.Vip{
ObjectMeta: metav1.ObjectMeta{
Name: subnetName,
},
Spec: kubeovnv1.VipSpec{
Subnet: subnetName,
},
},
metav1.CreateOptions{},
); err != nil {
klog.Errorf("failed to create health check vip from vpc %s subnet %s, %v", vpcName, subnetName, err)
return checkIP, err
}
needCreateHealthCheckVip = true
} else {
klog.Errorf("failed to get health check vip from vpc %s subnet %s, %v", vpcName, subnetName, err)
return checkIP, err
klog.Errorf("failed to get health check vip %s, %v", subnetName, err)
return "", err
}
}
if needCreateHealthCheckVip {
vip := &kubeovnv1.Vip{
ObjectMeta: metav1.ObjectMeta{
Name: subnetName,
},
Spec: kubeovnv1.VipSpec{
Subnet: subnetName,
},
}
if _, err = c.config.KubeOvnClient.KubeovnV1().Vips().Create(context.Background(), vip, metav1.CreateOptions{}); err != nil {
klog.Errorf("failed to create health check vip %s, %v", subnetName, err)
return "", err
}

// wait for vip created
time.Sleep(1 * time.Second)
checkVip, err = c.virtualIpsLister.Get(subnetName)
if err != nil {
klog.Errorf("failed to get health check vip %s, %v", subnetName, err)
return "", err
}
}

if checkVip.Status.V4ip == "" && checkVip.Status.V6ip == "" {
err = fmt.Errorf("failed to get health check vip %s address", subnetName)
klog.Error(err)
return "", err
}

switch util.CheckProtocol(lbVip) {
Expand All @@ -332,9 +347,9 @@ func (c *Controller) getHealthCheckVip(vpcName, subnetName, lbVip string) (strin
checkIP = checkVip.Status.V6ip
}
if checkIP == "" {
err = fmt.Errorf("failed to get health check vip from vpc %s subnet %s", vpcName, subnetName)
err = fmt.Errorf("failed to get health check vip subnet %s", subnetName)
klog.Error(err)
return checkIP, err
return "", err
}

return checkIP, nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ func (c *Controller) enqueueUpdatePod(oldObj, newObj interface{}) {
oldAAPs := strings.Split(oldPod.Annotations[util.AAPsAnnotation], ",")
newAAPs := strings.Split(newPod.Annotations[util.AAPsAnnotation], ",")
var vipNames []string
for _, oldAAp := range oldAAPs {
vipNames = append(vipNames, strings.TrimSpace(oldAAp))
for _, vipName := range oldAAPs {
vipNames = append(vipNames, strings.TrimSpace(vipName))
}
for _, newAAP := range newAAPs {
newAAP = strings.TrimSpace(newAAP)
if !slices.Contains(vipNames, newAAP) {
vipNames = append(vipNames, newAAP)
for _, vipName := range newAAPs {
vipName = strings.TrimSpace(vipName)
if !slices.Contains(vipNames, vipName) {
vipNames = append(vipNames, vipName)
}
}
for _, vipName := range vipNames {
Expand Down
38 changes: 25 additions & 13 deletions pkg/controller/vip.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func (c *Controller) handleAddVirtualIP(key string) error {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
if cachedVip.Status.Mac != "" {
Expand Down Expand Up @@ -288,7 +289,9 @@ func (c *Controller) handleAddVirtualIP(key string) error {
return err
}
if err := c.handleUpdateVirtualParents(key); err != nil {
return fmt.Errorf("error syncing virtual parents for vip '%s': %s", key, err.Error())
err := fmt.Errorf("error syncing virtual parents for vip '%s': %s", key, err.Error())
klog.Error(err)
return err
}
return nil
}
Expand Down Expand Up @@ -329,6 +332,7 @@ func (c *Controller) handleUpdateVirtualIP(key string) error {
}
ready := true
if err = c.patchVipStatus(key, vip.Spec.V4ip, ready); err != nil {
klog.Error(err)
return err
}
if err = c.handleAddVipFinalizer(key); err != nil {
Expand Down Expand Up @@ -372,19 +376,28 @@ func (c *Controller) handleUpdateVirtualParents(key string) error {
if k8serrors.IsNotFound(err) {
return nil
}
klog.Error(err)
return err
}
// only pods in the same namespace as vip are allowed to use aap
if (cachedVip.Status.V4ip == "" && cachedVip.Status.V6ip == "") || cachedVip.Spec.Namespace == "" {
return nil
}

// add new virtual port if not exist
ipStr := util.GetStringIP(cachedVip.Status.V4ip, cachedVip.Status.V6ip)
if err = c.OVNNbClient.CreateVirtualLogicalSwitchPort(cachedVip.Name, cachedVip.Spec.Subnet, ipStr); err != nil {
klog.Errorf("create virtual port with vip %s from logical switch %s: %v", cachedVip.Name, cachedVip.Spec.Subnet, err)
return err
}

// update virtual parents
if cachedVip.Spec.Type == util.SwitchLBRuleVip {
// switch lb rule vip no need to have virtual parents
return nil
}

// vip cloud use selector to select pods as its virtual parents
selectors := make(map[string]string)
for _, v := range cachedVip.Spec.Selector {
parts := strings.Split(strings.TrimSpace(v), ":")
Expand All @@ -403,9 +416,8 @@ func (c *Controller) handleUpdateVirtualParents(key string) error {
var virtualParents []string
for _, pod := range pods {
if pod.Annotations == nil {
err = fmt.Errorf("pod annotations have not been initialized")
klog.Error(err)
return err
// pod has no aaps
continue
}
if aaps := strings.Split(pod.Annotations[util.AAPsAnnotation], ","); !slices.Contains(aaps, cachedVip.Name) {
continue
Expand Down Expand Up @@ -459,14 +471,14 @@ func (c *Controller) createOrUpdateVipCR(key, ns, subnet, v4ip, v6ip, mac, pV4ip
ParentMac: pmac,
},
}, metav1.CreateOptions{}); err != nil {
errMsg := fmt.Errorf("failed to create crd vip '%s', %v", key, err)
klog.Error(errMsg)
return errMsg
err := fmt.Errorf("failed to create crd vip '%s', %v", key, err)
klog.Error(err)
return err
}
} else {
errMsg := fmt.Errorf("failed to get crd vip '%s', %v", key, err)
klog.Error(errMsg)
return errMsg
err := fmt.Errorf("failed to get crd vip '%s', %v", key, err)
klog.Error(err)
return err
}
} else {
vip := vipCR.DeepCopy()
Expand All @@ -489,9 +501,9 @@ func (c *Controller) createOrUpdateVipCR(key, ns, subnet, v4ip, v6ip, mac, pV4ip
vip.Status.Pmac = pmac
vip.Status.Type = vip.Spec.Type
if _, err := c.config.KubeOvnClient.KubeovnV1().Vips().Update(context.Background(), vip, metav1.UpdateOptions{}); err != nil {
errMsg := fmt.Errorf("failed to update vip '%s', %v", key, err)
klog.Error(errMsg)
return errMsg
err := fmt.Errorf("failed to update vip '%s', %v", key, err)
klog.Error(err)
return err
}
}
var needUpdateLabel bool
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
if c.config.EnableEipSnat {
externalSubnet, err = c.subnetsLister.Get(c.config.ExternalGatewaySwitch)
if err != nil {
klog.Errorf("failed to get external subnet %s: %v", c.config.ExternalGatewaySwitch, err)
klog.Warningf("enable-eip-snat need external subnet %s to be exist: %v", c.config.ExternalGatewaySwitch, err)
} else {
externalSubnetExist = true
}
Expand Down
Loading