Skip to content

Commit

Permalink
filter used qos when delete qos
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Dec 23, 2021
1 parent 90950da commit 46ba84e
Showing 1 changed file with 108 additions and 11 deletions.
119 changes: 108 additions & 11 deletions pkg/ovs/ovs-vsctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,67 @@ func ovsGet(table, record, column, key string) (string, error) {
return Exec(args...)
}

func ovsRemove(table, record, column, key string) error {
args := []string{"remove"}
if key == "" {
args = append(args, table, record, column)
} else {
args = append(args, table, record, column, key)
}
_, err := Exec(args...)
return err
}

// Bridges returns bridges created by Kube-OVN
func Bridges() ([]string, error) {
return ovsFind("bridge", "name", fmt.Sprintf("external-ids:vendor=%s", util.CniTypeName))
}

// ClearPodBandwidth remove qos related to this pod.
func ClearPodBandwidth(podName, podNamespace, ifaceID string) error {
var qosList, qosListByPod []string
func GetQosList(podName, podNamespace, ifaceID string) ([]string, error) {
var qosList []string
var err error

if ifaceID != "" {
qosList, err = ovsFind("qos", "_uuid", fmt.Sprintf(`external-ids:iface-id="%s"`, ifaceID))
if err != nil {
return err
return qosList, err
}
} else {
qosListByPod, err = ovsFind("qos", "_uuid", fmt.Sprintf(`external-ids:pod="%s/%s"`, podNamespace, podName))
qosList, err = ovsFind("qos", "_uuid", fmt.Sprintf(`external-ids:pod="%s/%s"`, podNamespace, podName))
if err != nil {
return err
return qosList, err
}
}
qosList = append(qosList, qosListByPod...)
qosList = util.UniqString(qosList)
for _, qos := range qosList {
if err := ovsDestroy("qos", qos); err != nil {

return qosList, nil
}

// ClearPodBandwidth remove qos related to this pod.
func ClearPodBandwidth(podName, podNamespace, ifaceID string) error {
qosList, err := GetQosList(podName, podNamespace, ifaceID)
if err != nil {
return err
}

// https://github.com/kubeovn/kube-ovn/issues/1191
usedQosList, err := ovsFind("port", "qos", "qos!=[]")
if err != nil {
return err
}

for _, qosId := range qosList {
found := false
for _, usedQosId := range usedQosList {
if qosId == usedQosId {
found = true
break
}
}
if found {
continue
}

if err := ovsDestroy("qos", qosId); err != nil {
return err
}
}
Expand Down Expand Up @@ -357,7 +394,24 @@ func ClearHtbQosQueue(podName, podNamespace, iface string) error {
}
}

// https://github.com/kubeovn/kube-ovn/issues/1191
qosQueueMap, err := ListQosQueueIds()
if err != nil {
return err
}

for _, queueId := range queueList {
found := false
for _, usedQueueId := range qosQueueMap {
if queueId == usedQueueId {
found = true
break
}
}
if found {
continue
}

if err := ovsDestroy("queue", queueId); err != nil {
return err
}
Expand Down Expand Up @@ -540,7 +594,7 @@ func SetNetemQos(podName, podNamespace, iface, latency, limit, loss string) erro
}

for _, ifName := range interfaceList {
qosList, err := ovsFind("qos", "_uuid", fmt.Sprintf("external-ids:iface-id=%s", iface))
qosList, err := GetQosList(podName, podNamespace, iface)
if err != nil {
return err
}
Expand Down Expand Up @@ -584,6 +638,22 @@ func SetNetemQos(podName, podNamespace, iface, latency, limit, loss string) erro
if err := ovsSet("qos", qos, qosCommandValues...); err != nil {
return err
}

if latencyMs == 0 {
if err := ovsRemove("qos", qos, "other_config", "latency"); err != nil {
return err
}
}
if limitPkts == 0 {
if err := ovsRemove("qos", qos, "other_config", "limit"); err != nil {
return err
}
}
if lossPercent == 0 {
if err := ovsRemove("qos", qos, "other_config", "loss"); err != nil {
return err
}
}
}
}
} else {
Expand Down Expand Up @@ -639,3 +709,30 @@ func ListExternalIds(table string) (map[string]string, error) {
}
return result, nil
}

func ListQosQueueIds() (map[string]string, error) {
args := []string{"--data=bare", "--format=csv", "--no-heading", "--columns=_uuid,queues", "find", "qos", "queues:0!=[]"}
output, err := Exec(args...)
if err != nil {
klog.Errorf("failed to list qos, %v", err)
return nil, err
}
lines := strings.Split(output, "\n")
result := make(map[string]string, len(lines))
for _, l := range lines {
if len(strings.TrimSpace(l)) == 0 {
continue
}
parts := strings.Split(strings.TrimSpace(l), ",")
if len(parts) != 2 {
continue
}
qosId := strings.TrimSpace(parts[0])
if !strings.Contains(strings.TrimSpace(parts[1]), "0=") {
continue
}
queueId := strings.TrimPrefix(strings.TrimSpace(parts[1]), "0=")
result[qosId] = queueId
}
return result, nil
}

0 comments on commit 46ba84e

Please sign in to comment.