Skip to content

Commit

Permalink
some optimization for provider network status update (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Dec 8, 2022
1 parent 5c661d4 commit 69ff0ee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
22 changes: 12 additions & 10 deletions pkg/apis/kubeovn/v1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (s *ProviderNetworkStatus) addNodeCondition(node string, ctype ConditionTyp
}

// setConditionValue updates or creates a new condition
func (s *ProviderNetworkStatus) setNodeConditionValue(node string, ctype ConditionType, status corev1.ConditionStatus, reason, message string) {
func (s *ProviderNetworkStatus) setNodeConditionValue(node string, ctype ConditionType, status corev1.ConditionStatus, reason, message string) bool {
var c *ProviderNetworkCondition
for i := range s.Conditions {
if s.Conditions[i].Node == node && s.Conditions[i].Type == ctype {
Expand All @@ -235,7 +235,7 @@ func (s *ProviderNetworkStatus) setNodeConditionValue(node string, ctype Conditi
} else {
// check message ?
if c.Status == status && c.Reason == reason && c.Message == message {
return
return false
}
now := metav1.Now()
c.LastUpdateTime = now
Expand All @@ -246,6 +246,8 @@ func (s *ProviderNetworkStatus) setNodeConditionValue(node string, ctype Conditi
c.Reason = reason
c.Message = message
}

return true
}

// RemoveCondition removes the condition with the provided type.
Expand Down Expand Up @@ -306,13 +308,13 @@ func (s *ProviderNetworkStatus) ConditionReason(node string, ctype ConditionType
}

// NodeReady - shortcut to set ready condition to true
func (s *ProviderNetworkStatus) SetNodeReady(node, reason, message string) {
s.SetNodeCondition(node, Ready, reason, message)
func (s *ProviderNetworkStatus) SetNodeReady(node, reason, message string) bool {
return s.SetNodeCondition(node, Ready, reason, message)
}

// NodeNotReady - shortcut to set ready condition to false
func (s *ProviderNetworkStatus) SetNodeNotReady(node, reason, message string) {
s.ClearNodeCondition(node, Ready, reason, message)
func (s *ProviderNetworkStatus) SetNodeNotReady(node, reason, message string) bool {
return s.ClearNodeCondition(node, Ready, reason, message)
}

// EnsureNodeCondition useful for adding default conditions
Expand All @@ -330,13 +332,13 @@ func (s *ProviderNetworkStatus) EnsureNodeStandardConditions(node string) bool {
}

// ClearNodeCondition updates or creates a new condition
func (s *ProviderNetworkStatus) ClearNodeCondition(node string, ctype ConditionType, reason, message string) {
s.setNodeConditionValue(node, ctype, corev1.ConditionFalse, reason, message)
func (s *ProviderNetworkStatus) ClearNodeCondition(node string, ctype ConditionType, reason, message string) bool {
return s.setNodeConditionValue(node, ctype, corev1.ConditionFalse, reason, message)
}

// SetNodeCondition updates or creates a new condition
func (s *ProviderNetworkStatus) SetNodeCondition(node string, ctype ConditionType, reason, message string) {
s.setNodeConditionValue(node, ctype, corev1.ConditionTrue, reason, message)
func (s *ProviderNetworkStatus) SetNodeCondition(node string, ctype ConditionType, reason, message string) bool {
return s.setNodeConditionValue(node, ctype, corev1.ConditionTrue, reason, message)
}

// RemoveNodeConditions updates or creates a new condition
Expand Down
65 changes: 44 additions & 21 deletions pkg/controller/provider-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"

"github.com/kubeovn/kube-ovn/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"

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

func (c *Controller) resyncProviderNetworkStatus() {
klog.Infof("start to sync ProviderNetwork status")
klog.V(3).Infof("start to sync ProviderNetwork status")
pns, err := c.providerNetworksLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list provider network: %v", err)
Expand All @@ -24,52 +26,73 @@ func (c *Controller) resyncProviderNetworkStatus() {
return
}

pods, err := c.podsLister.Pods("").List(labels.Set{"app": "kube-ovn-cni"}.AsSelector())
if err != nil {
klog.Errorf("failed to list kube-ovn-cni pods: %v", err)
return
}

podMap := make(map[string]*corev1.Pod, len(pods))
for _, pod := range pods {
podMap[pod.Spec.NodeName] = pod
}

for _, cachedPn := range pns {
pn := cachedPn.DeepCopy()
var readyNodes, notReadyNodes, expectNodes []string
pnReadyAnnotation := fmt.Sprintf(util.ProviderNetworkReadyTemplate, pn.Name)
pnErrMsgAnnotation := fmt.Sprintf(util.ProviderNetworkErrMessageTemplate, pn.Name)

var conditionsUpdated bool
for _, node := range nodes {
if util.ContainsString(pn.Spec.ExcludeNodes, node.Name) {
pn.Status.RemoveNodeConditions(node.Name)
if pn.Status.RemoveNodeConditions(node.Name) {
conditionsUpdated = true
}
continue
}
if node.Labels[fmt.Sprintf(util.ProviderNetworkReadyTemplate, pn.Name)] == "true" {
pn.Status.SetNodeReady(node.Name, "InitOVSBridgeSucceeded", "")
if node.Labels[pnReadyAnnotation] == "true" {
if pn.Status.SetNodeReady(node.Name, "InitOVSBridgeSucceeded", "") {
conditionsUpdated = true
}
readyNodes = append(readyNodes, node.Name)
} else {
pods, err := c.config.KubeClient.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{
LabelSelector: "app=kube-ovn-cni",
FieldSelector: fmt.Sprintf("spec.nodeName=%s", node.Name),
})
if err != nil {
klog.Errorf("failed to list pod: %v", err)
continue
var errMsg string
if pod := podMap[node.Name]; pod == nil {
errMsg = fmt.Sprintf("kube-ovn-cni pod on node %s not found", node.Name)
klog.Error(errMsg)
} else {
if len(pod.Annotations) != 0 {
errMsg = pod.Annotations[pnErrMsgAnnotation]
}
if errMsg == "" {
errMsg = "unknown error"
}
}

var errMsg string
if len(pods.Items) == 1 && pods.Items[0].Annotations != nil {
errMsg = pods.Items[0].Annotations[fmt.Sprintf(util.ProviderNetworkErrMessageTemplate, pn.Name)]
if pn.Status.SetNodeNotReady(node.Name, "InitOVSBridgeFailed", errMsg) {
conditionsUpdated = true
}
pn.Status.SetNodeNotReady(node.Name, "InitOVSBridgeFailed", errMsg)
notReadyNodes = append(notReadyNodes, node.Name)
}
}

expectNodes = append(readyNodes, notReadyNodes...)
conditionsChange := false
for _, c := range pn.Status.Conditions {
if !util.ContainsString(expectNodes, c.Node) {
pn.Status.RemoveNodeConditions(c.Node)
conditionsChange = true
if pn.Status.RemoveNodeConditions(c.Node) {
conditionsUpdated = true
}
}
}

if conditionsChange || len(util.DiffStringSlice(pn.Status.ReadyNodes, readyNodes)) != 0 ||
if conditionsUpdated || len(util.DiffStringSlice(pn.Status.ReadyNodes, readyNodes)) != 0 ||
len(util.DiffStringSlice(pn.Status.NotReadyNodes, notReadyNodes)) != 0 {
pn.Status.ReadyNodes = readyNodes
pn.Status.NotReadyNodes = notReadyNodes
pn.Status.Ready = (len(notReadyNodes) == 0)
if _, err = c.config.KubeOvnClient.KubeovnV1().ProviderNetworks().UpdateStatus(context.Background(), pn, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update provider network %s: %v", pn.Name, err)
klog.Errorf("failed to update status of provider network %s: %v", pn.Name, err)
}
}
}
Expand Down

0 comments on commit 69ff0ee

Please sign in to comment.