• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

k8snetworkplumbingwg / sriov-network-operator / 13526205039

25 Feb 2025 04:34PM UTC coverage: 48.882% (+0.9%) from 48.008%
13526205039

Pull #788

github

web-flow
Merge 3e6b91cf3 into d7c9458e0
Pull Request #788: Daemon redesign - using controller-runtime

273 of 615 new or added lines in 18 files covered. (44.39%)

199 existing lines in 12 files now uncovered.

7324 of 14983 relevant lines covered (48.88%)

0.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.46
/pkg/daemon/status.go
1
package daemon
2

3
import (
4
        "context"
5
        "fmt"
6
        "reflect"
7

8
        "k8s.io/client-go/util/retry"
9
        "sigs.k8s.io/controller-runtime/pkg/client"
10
        "sigs.k8s.io/controller-runtime/pkg/log"
11

12
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
13
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
14
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
15
)
16

17
const (
18
        Unknown = "Unknown"
19
)
20

21
func (dn *NodeReconciler) updateSyncState(ctx context.Context, desiredNodeState *sriovnetworkv1.SriovNetworkNodeState, status, failedMessage string) error {
1✔
22
        funcLog := log.Log.WithName("updateSyncState")
1✔
23
        currentNodeState := &sriovnetworkv1.SriovNetworkNodeState{}
1✔
24
        desiredNodeState.Status.SyncStatus = status
1✔
25
        desiredNodeState.Status.LastSyncError = failedMessage
1✔
26

1✔
27
        retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
2✔
28
                if err := dn.client.Get(ctx, client.ObjectKey{desiredNodeState.Namespace, desiredNodeState.Name}, currentNodeState); err != nil {
1✔
NEW
29
                        funcLog.Error(err, "failed to get latest node state",
×
NEW
30
                                "SyncStatus", status,
×
NEW
31
                                "LastSyncError", failedMessage)
×
NEW
32
                        return err
×
NEW
33
                }
×
34

35
                funcLog.V(2).Info("update nodeState status",
1✔
36
                        "CurrentSyncStatus", currentNodeState.Status.SyncStatus,
1✔
37
                        "CurrentLastSyncError", currentNodeState.Status.LastSyncError,
1✔
38
                        "NewSyncStatus", status,
1✔
39
                        "NewFailedMessage", failedMessage)
1✔
40

1✔
41
                err := dn.client.Status().Patch(ctx, desiredNodeState, client.MergeFrom(currentNodeState))
1✔
42
                if err != nil {
1✔
NEW
43
                        funcLog.Error(err, "failed to update node state status",
×
NEW
44
                                "SyncStatus", status,
×
NEW
45
                                "LastSyncError", failedMessage)
×
NEW
46
                        return err
×
NEW
47
                }
×
48
                return nil
1✔
49
        })
50

51
        if retryErr != nil {
1✔
NEW
52
                funcLog.Error(retryErr, "failed to update node state status")
×
NEW
53
                return retryErr
×
NEW
54
        }
×
55

56
        dn.recordStatusChangeEvent(ctx, currentNodeState.Status.SyncStatus, status, failedMessage)
1✔
57
        return nil
1✔
58
}
59

60
func (dn *NodeReconciler) shouldUpdateStatus(current, desiredNodeState *sriovnetworkv1.SriovNetworkNodeState) bool {
1✔
61
        // check number of interfaces are equal
1✔
62
        if len(current.Status.Interfaces) != len(desiredNodeState.Status.Interfaces) {
1✔
NEW
63
                return true
×
NEW
64
        }
×
65

66
        // check for bridges
67
        if !reflect.DeepEqual(current.Status.Bridges, desiredNodeState.Status.Bridges) {
1✔
NEW
68
                return true
×
NEW
69
        }
×
70

71
        // check for system
72
        if !reflect.DeepEqual(current.Status.System, desiredNodeState.Status.System) {
1✔
NEW
73
                return true
×
NEW
74
        }
×
75

76
        // check for interfaces
77
        // we can't use deep equal here because if we have a vf inside a pod is name will not be available for example
78
        // we use the index for both lists
79
        c := current.Status.DeepCopy().Interfaces
1✔
80
        d := desiredNodeState.Status.DeepCopy().Interfaces
1✔
81
        for idx := range d {
2✔
82
                // check if it's a new device
1✔
83
                if d[idx].PciAddress != c[idx].PciAddress {
1✔
NEW
84
                        return true
×
NEW
85
                }
×
86
                // remove all the vfs
87
                d[idx].VFs = nil
1✔
88
                c[idx].VFs = nil
1✔
89

1✔
90
                if !reflect.DeepEqual(d[idx], c[idx]) {
1✔
NEW
91
                        return true
×
NEW
92
                }
×
93
        }
94

95
        return false
1✔
96
}
97

98
func (dn *NodeReconciler) updateStatusFromHost(nodeState *sriovnetworkv1.SriovNetworkNodeState) error {
1✔
99
        log.Log.WithName("updateStatusFromHost").Info("Getting host network status")
1✔
100
        var ifaces []sriovnetworkv1.InterfaceExt
1✔
101
        var bridges sriovnetworkv1.Bridges
1✔
102
        var err error
1✔
103

1✔
104
        if vars.PlatformType == consts.VirtualOpenStack {
1✔
NEW
105
                ifaces, err = dn.platformHelpers.DiscoverSriovDevicesVirtual()
×
NEW
106
                if err != nil {
×
NEW
107
                        return err
×
NEW
108
                }
×
109
        } else {
1✔
110
                ifaces, err = dn.HostHelpers.DiscoverSriovDevices(dn.HostHelpers)
1✔
111
                if err != nil {
1✔
NEW
112
                        return err
×
NEW
113
                }
×
114
                if vars.ManageSoftwareBridges {
1✔
NEW
115
                        bridges, err = dn.HostHelpers.DiscoverBridges()
×
NEW
116
                        if err != nil {
×
NEW
117
                                return err
×
NEW
118
                        }
×
119
                }
120
        }
121

122
        nodeState.Status.Interfaces = ifaces
1✔
123
        nodeState.Status.Bridges = bridges
1✔
124
        nodeState.Status.System.RdmaMode, err = dn.HostHelpers.DiscoverRDMASubsystem()
1✔
125
        return err
1✔
126
}
127

128
func (dn *NodeReconciler) recordStatusChangeEvent(ctx context.Context, oldStatus, newStatus, lastError string) {
1✔
129
        if oldStatus != newStatus {
2✔
130
                if oldStatus == "" {
2✔
131
                        oldStatus = Unknown
1✔
132
                }
1✔
133
                if newStatus == "" {
1✔
NEW
134
                        newStatus = Unknown
×
NEW
135
                }
×
136
                eventMsg := fmt.Sprintf("Status changed from: %s to: %s", oldStatus, newStatus)
1✔
137
                if lastError != "" {
1✔
NEW
138
                        eventMsg = fmt.Sprintf("%s. Last Error: %s", eventMsg, lastError)
×
NEW
139
                }
×
140
                dn.eventRecorder.SendEvent(ctx, "SyncStatusChanged", eventMsg)
1✔
141
        }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc