Skip to content

Commit

Permalink
Avoid recording multiple events
Browse files Browse the repository at this point in the history
In case update operation fail in node status writer
we should not record an event.

move event sending logic to its own method
and call it after status has been successfully updated.

Signed-off-by: adrianc <[email protected]>
  • Loading branch information
adrianchiris committed Oct 18, 2023
1 parent bf595e4 commit 7fd62a4
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions pkg/daemon/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,21 @@ func (w *NodeStateStatusWriter) pollNicStatus(platformType utils.PlatformType) e

func (w *NodeStateStatusWriter) updateNodeStateStatusRetry(f func(*sriovnetworkv1.SriovNetworkNodeState)) (*sriovnetworkv1.SriovNetworkNodeState, error) {
var nodeState *sriovnetworkv1.SriovNetworkNodeState
var oldStatus, newStatus, lastError string

err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
n, getErr := w.getNodeState()
if getErr != nil {
return getErr
}
oldStatus = n.Status.SyncStatus

// Call the status modifier.
f(n)

newStatus = n.Status.SyncStatus
lastError = n.Status.LastSyncError

var err error
nodeState, err = w.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).UpdateStatus(context.Background(), n, metav1.UpdateOptions{})
if err != nil {
Expand All @@ -163,6 +169,8 @@ func (w *NodeStateStatusWriter) updateNodeStateStatusRetry(f func(*sriovnetworkv
return nil, fmt.Errorf("unable to update node %v: %v", nodeState, err)
}

w.recordStatusChangeEvent(oldStatus, newStatus, lastError)

return nodeState, nil
}

Expand All @@ -173,31 +181,33 @@ func (w *NodeStateStatusWriter) setNodeStateStatus(msg Message) (*sriovnetworkv1
// clear lastSyncError when sync Succeeded
nodeState.Status.LastSyncError = msg.lastSyncError
}
oldStatus := nodeState.Status.SyncStatus
newStatus := msg.syncStatus
nodeState.Status.SyncStatus = newStatus
glog.V(0).Infof("setNodeStateStatus(): syncStatus: %s, lastSyncError: %s", nodeState.Status.SyncStatus, nodeState.Status.LastSyncError)
nodeState.Status.SyncStatus = msg.syncStatus

if oldStatus != newStatus {
if oldStatus == "" {
oldStatus = Unknown
}
if newStatus == "" {
newStatus = Unknown
}
eventMsg := fmt.Sprintf("Status changed from: %s to: %s", oldStatus, newStatus)
if nodeState.Status.LastSyncError != "" {
eventMsg = fmt.Sprintf("%s. Last Error: %s", eventMsg, nodeState.Status.LastSyncError)
}
w.eventRecorder.SendEvent("SyncStatusChanged", eventMsg)
}
glog.V(0).Infof("setNodeStateStatus(): syncStatus: %s, lastSyncError: %s", nodeState.Status.SyncStatus, nodeState.Status.LastSyncError)
})
if err != nil {
return nil, err
}
return nodeState, nil
}

// recordStatusChangeEvent sends event in case oldStatus differs from newStatus
func (w *NodeStateStatusWriter) recordStatusChangeEvent(oldStatus, newStatus, lastError string) {
if oldStatus != newStatus {
if oldStatus == "" {
oldStatus = Unknown
}
if newStatus == "" {
newStatus = Unknown
}
eventMsg := fmt.Sprintf("Status changed from: %s to: %s", oldStatus, newStatus)
if lastError != "" {
eventMsg = fmt.Sprintf("%s. Last Error: %s", eventMsg, lastError)
}
w.eventRecorder.SendEvent("SyncStatusChanged", eventMsg)
}
}

// getNodeState queries the kube apiserver to get the SriovNetworkNodeState CR
func (w *NodeStateStatusWriter) getNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error) {
var lastErr error
Expand Down

0 comments on commit 7fd62a4

Please sign in to comment.