Skip to content

Commit

Permalink
fix subnet provider validation
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Apr 13, 2024
1 parent 748aac7 commit cf53cb5
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 94 deletions.
5 changes: 3 additions & 2 deletions pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (c *Controller) resyncExternalGateway() {
return
}
klog.Infof("last external gw configmap: %v", lastExGwCM)
if (lastExGwCM["type"] == "distributed" && cm.Data["type"] == "centralized") ||
if (lastExGwCM["type"] == kubeovnv1.GWDistributedType && cm.Data["type"] == kubeovnv1.GWCentralizedType) ||
lastExGwCM != nil && !reflect.DeepEqual(lastExGwCM["external-gw-nodes"], cm.Data["external-gw-nodes"]) {
klog.Info("external gw nodes list changed, start to remove ovn external gw")
if err := c.removeExternalGateway(); err != nil {
Expand Down Expand Up @@ -228,7 +229,7 @@ func (c *Controller) getGatewayChassis(config map[string]string) ([]string, erro
for _, node := range nodes {
gwNodes = append(gwNodes, node.Name)
}
if config["type"] != "distributed" {
if config["type"] != kubeovnv1.GWDistributedType {
nodeNames := strings.Split(config["external-gw-nodes"], ",")
for _, name := range nodeNames {
name = strings.TrimSpace(name)
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func (c *Controller) isOVNProvided(providerName string, pod *corev1.Pod) (bool,
klog.Errorf("parse annotation logical switch %s error %v", ls, err)
return false, err
}
if !strings.HasSuffix(subnet.Spec.Provider, util.OvnProvider) {
if !isOvnSubnet(subnet) {
return false, nil
}
return true, nil
Expand Down Expand Up @@ -818,7 +818,7 @@ func (c *Controller) getVMLsps() []string {
continue
}
for _, multiNet := range attachNets {
provider := fmt.Sprintf("%s.%s.ovn", multiNet.Name, multiNet.Namespace)
provider := fmt.Sprintf("%s.%s.%s", multiNet.Name, multiNet.Namespace, util.OvnProvider)
vmLsp := ovs.PodNameToPortName(vm.Name, ns.Name, provider)
vmLsps = append(vmLsps, vmLsp)
}
Expand All @@ -829,7 +829,7 @@ func (c *Controller) getVMLsps() []string {
if len(items) != 2 {
items = []string{vm.GetNamespace(), items[0]}
}
provider := fmt.Sprintf("%s.%s.ovn", items[1], items[0])
provider := fmt.Sprintf("%s.%s.%s", items[1], items[0], util.OvnProvider)
vmLsp := ovs.PodNameToPortName(vm.Name, ns.Name, provider)
vmLsps = append(vmLsps, vmLsp)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,7 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
allowLiveMigration := false
isDefault := util.IsDefaultNet(pod.Annotations[util.DefaultNetworkAnnotation], attach)

providerName = fmt.Sprintf("%s.%s.ovn", attach.Name, attach.Namespace)
providerName = fmt.Sprintf("%s.%s.%s", attach.Name, attach.Namespace, util.OvnProvider)
if pod.Annotations[fmt.Sprintf(util.LiveMigrationAnnotationTemplate, providerName)] == "true" {
allowLiveMigration = true
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *Controller) formatSubnet(subnet *kubeovnv1.Subnet) (*kubeovnv1.Subnet,
changed = true
}
if subnet.Spec.Vpc == "" {
if subnet.Spec.Provider != "" && !strings.HasSuffix(subnet.Spec.Provider, util.OvnProvider) {
if !isOvnSubnet(subnet) {
klog.Infof("subnet %s is not ovn subnet, no vpc", subnet.Name)
} else {
changed = true
Expand Down Expand Up @@ -2220,7 +2220,7 @@ func (c *Controller) calcSubnetStatusIP(subnet *kubeovnv1.Subnet) (*kubeovnv1.Su
}

func isOvnSubnet(subnet *kubeovnv1.Subnet) bool {
return subnet.Spec.Provider == "" || subnet.Spec.Provider == util.OvnProvider || strings.HasSuffix(subnet.Spec.Provider, "ovn")
return util.IsOvnProvider(subnet.Spec.Provider)
}

func checkAndFormatsExcludeIPs(subnet *kubeovnv1.Subnet) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (c *Controller) enqueuePod(oldObj, newObj interface{}) {
return
}
for _, multiNet := range attachNets {
provider := fmt.Sprintf("%s.%s.ovn", multiNet.Name, multiNet.Namespace)
provider := fmt.Sprintf("%s.%s.%s", multiNet.Name, multiNet.Namespace, util.OvnProvider)
if newPod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, provider)] == "true" {
if oldPod.Annotations[fmt.Sprintf(util.IngressRateAnnotationTemplate, provider)] != newPod.Annotations[fmt.Sprintf(util.IngressRateAnnotationTemplate, provider)] ||
oldPod.Annotations[fmt.Sprintf(util.EgressRateAnnotationTemplate, provider)] != newPod.Annotations[fmt.Sprintf(util.EgressRateAnnotationTemplate, provider)] ||
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/controller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (c *Controller) handlePod(key string) error {
return err
}
for _, multiNet := range attachNets {
provider := fmt.Sprintf("%s.%s.ovn", multiNet.Name, multiNet.Namespace)
provider := fmt.Sprintf("%s.%s.%s", multiNet.Name, multiNet.Namespace, util.OvnProvider)
if pod.Annotations[fmt.Sprintf(util.VMTemplate, provider)] != "" {
podName = pod.Annotations[fmt.Sprintf(util.VMTemplate, provider)]
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/daemon/controller_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c *Controller) handlePod(key string) error {
return err
}
for _, multiNet := range attachNets {
provider := fmt.Sprintf("%s.%s.ovn", multiNet.Name, multiNet.Namespace)
provider := fmt.Sprintf("%s.%s.%s", multiNet.Name, multiNet.Namespace, util.OvnProvider)
if pod.Annotations[fmt.Sprintf(util.VMTemplate, provider)] != "" {
podName = pod.Annotations[fmt.Sprintf(util.VMTemplate, provider)]
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func createCniServerHandler(config *Configuration, controller *Controller) *cniS
}

func (csh cniServerHandler) providerExists(provider string) (*kubeovnv1.Subnet, bool) {
if provider == "" || strings.HasSuffix(provider, util.OvnProvider) {
if util.IsOvnProvider(provider) {
return nil, true
}
subnets, _ := csh.Controller.subnetsLister.List(labels.Everything())
Expand Down Expand Up @@ -184,7 +184,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
isDefaultRoute = ifName == "eth0"
}

if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && strings.HasSuffix(podRequest.Provider, util.OvnProvider) {
if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && util.IsOvnProvider(podRequest.Provider) {
klog.Infof("wait route ready for pod %s/%s provider %s", podRequest.PodNamespace, podRequest.PodName, podRequest.Provider)
cniWaitRouteResult.WithLabelValues(nodeName).Inc()
time.Sleep(1 * time.Second)
Expand Down Expand Up @@ -217,7 +217,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
return
}

if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && strings.HasSuffix(podRequest.Provider, util.OvnProvider) {
if isDefaultRoute && pod.Annotations[fmt.Sprintf(util.RoutedAnnotationTemplate, podRequest.Provider)] != "true" && util.IsOvnProvider(podRequest.Provider) {
err := fmt.Errorf("route is not ready for pod %s/%s provider %s, please see kube-ovn-controller logs to find errors", pod.Namespace, pod.Name, podRequest.Provider)
klog.Error(err)
if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
Expand Down Expand Up @@ -451,7 +451,7 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon
return
}

if pod.Annotations != nil && (podRequest.Provider == util.OvnProvider || podRequest.CniType == util.CniTypeName) {
if pod.Annotations != nil && (util.IsOvnProvider(podRequest.Provider) || podRequest.CniType == util.CniTypeName) {
subnet := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podRequest.Provider)]
if subnet != "" {
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podRequest.Provider)]
Expand Down
3 changes: 1 addition & 2 deletions pkg/ovs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func GetLocalnetName(subnet string) string {
}

func trimCommandOutput(raw []byte) string {
output := strings.TrimSpace(string(raw))
return strings.Trim(output, "\"")
return strings.Trim(strings.TrimSpace(string(raw)), `"`)
}

func LogicalRouterPortName(lr, ls string) string {
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util

import "strings"

func IsOvnProvider(provider string) bool {
if provider == "" || provider == OvnProvider {
return true
}
if fields := strings.Split(provider, "."); len(fields) == 3 && fields[2] == OvnProvider {
return true
}
return false
}
Loading

0 comments on commit cf53cb5

Please sign in to comment.