From 7857a4fea9cb41974496bc61ba6d0144583208fd Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 23 Mar 2024 05:58:01 +0530 Subject: [PATCH] enhancement: Use pointer fields for `FibreChannel*` Allow fields to be `nil`-checked. Fixes: #612 Signed-off-by: Pranshu Srivastava --- go.mod | 1 + go.sum | 2 + sysfs/class_fibrechannel.go | 110 +++++++++++++++---------------- sysfs/class_fibrechannel_test.go | 89 ++++++++++++------------- 4 files changed, 103 insertions(+), 99 deletions(-) diff --git a/go.mod b/go.mod index 02a379eb..844df2c2 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,5 @@ require ( github.com/google/go-cmp v0.6.0 golang.org/x/sync v0.6.0 golang.org/x/sys v0.18.0 + k8s.io/utils v0.0.0-20240310230437-4693a0247e57 ) diff --git a/go.sum b/go.sum index 037f53a6..fd27ad43 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,5 @@ golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY= +k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= diff --git a/sysfs/class_fibrechannel.go b/sysfs/class_fibrechannel.go index ded07a88..4b583f71 100644 --- a/sysfs/class_fibrechannel.go +++ b/sysfs/class_fibrechannel.go @@ -28,36 +28,36 @@ import ( const fibrechannelClassPath = "class/fc_host" type FibreChannelCounters struct { - DumpedFrames uint64 // /sys/class/fc_host//statistics/dumped_frames - ErrorFrames uint64 // /sys/class/fc_host//statistics/error_frames - InvalidCRCCount uint64 // /sys/class/fc_host//statistics/invalid_crc_count - RXFrames uint64 // /sys/class/fc_host//statistics/rx_frames - RXWords uint64 // /sys/class/fc_host//statistics/rx_words - TXFrames uint64 // /sys/class/fc_host//statistics/tx_frames - TXWords uint64 // /sys/class/fc_host//statistics/tx_words - SecondsSinceLastReset uint64 // /sys/class/fc_host//statistics/seconds_since_last_reset - InvalidTXWordCount uint64 // /sys/class/fc_host//statistics/invalid_tx_word_count - LinkFailureCount uint64 // /sys/class/fc_host//statistics/link_failure_count - LossOfSyncCount uint64 // /sys/class/fc_host//statistics/loss_of_sync_count - LossOfSignalCount uint64 // /sys/class/fc_host//statistics/loss_of_signal_count - NosCount uint64 // /sys/class/fc_host//statistics/nos_count - FCPPacketAborts uint64 // / sys/class/fc_host//statistics/fcp_packet_aborts + DumpedFrames *uint64 // /sys/class/fc_host//statistics/dumped_frames + ErrorFrames *uint64 // /sys/class/fc_host//statistics/error_frames + InvalidCRCCount *uint64 // /sys/class/fc_host//statistics/invalid_crc_count + RXFrames *uint64 // /sys/class/fc_host//statistics/rx_frames + RXWords *uint64 // /sys/class/fc_host//statistics/rx_words + TXFrames *uint64 // /sys/class/fc_host//statistics/tx_frames + TXWords *uint64 // /sys/class/fc_host//statistics/tx_words + SecondsSinceLastReset *uint64 // /sys/class/fc_host//statistics/seconds_since_last_reset + InvalidTXWordCount *uint64 // /sys/class/fc_host//statistics/invalid_tx_word_count + LinkFailureCount *uint64 // /sys/class/fc_host//statistics/link_failure_count + LossOfSyncCount *uint64 // /sys/class/fc_host//statistics/loss_of_sync_count + LossOfSignalCount *uint64 // /sys/class/fc_host//statistics/loss_of_signal_count + NosCount *uint64 // /sys/class/fc_host//statistics/nos_count + FCPPacketAborts *uint64 // /sys/class/fc_host//statistics/fcp_packet_aborts } type FibreChannelHost struct { - Name string // /sys/class/fc_host/ - Speed string // /sys/class/fc_host//speed - PortState string // /sys/class/fc_host//port_state - PortType string // /sys/class/fc_host//port_type - SymbolicName string // /sys/class/fc_host//symbolic_name - NodeName string // /sys/class/fc_host//node_name - PortID string // /sys/class/fc_host//port_id - PortName string // /sys/class/fc_host//port_name - FabricName string // /sys/class/fc_host//fabric_name - DevLossTMO string // /sys/class/fc_host//dev_loss_tmo - SupportedClasses string // /sys/class/fc_host//supported_classes - SupportedSpeeds string // /sys/class/fc_host//supported_speeds - Counters FibreChannelCounters // /sys/class/fc_host//statistics/* + Name *string // /sys/class/fc_host/ + Speed *string // /sys/class/fc_host//speed + PortState *string // /sys/class/fc_host//port_state + PortType *string // /sys/class/fc_host//port_type + SymbolicName *string // /sys/class/fc_host//symbolic_name + NodeName *string // /sys/class/fc_host//node_name + PortID *string // /sys/class/fc_host//port_id + PortName *string // /sys/class/fc_host//port_name + FabricName *string // /sys/class/fc_host//fabric_name + DevLossTMO *string // /sys/class/fc_host//dev_loss_tmo + SupportedClasses *string // /sys/class/fc_host//supported_classes + SupportedSpeeds *string // /sys/class/fc_host//supported_speeds + Counters *FibreChannelCounters // /sys/class/fc_host//statistics/* } type FibreChannelClass map[string]FibreChannelHost @@ -78,7 +78,7 @@ func (fs FS) FibreChannelClass() (FibreChannelClass, error) { return nil, err } - fcc[host.Name] = *host + fcc[*host.Name] = *host } return fcc, nil @@ -87,7 +87,7 @@ func (fs FS) FibreChannelClass() (FibreChannelClass, error) { // Parse a single FC host. func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) { path := fs.sys.Path(fibrechannelClassPath, name) - host := FibreChannelHost{Name: name} + host := FibreChannelHost{Name: &name} for _, f := range [...]string{"speed", "port_state", "port_type", "node_name", "port_id", "port_name", "fabric_name", "dev_loss_tmo", "symbolic_name", "supported_classes", "supported_speeds"} { name := filepath.Join(path, f) @@ -103,39 +103,39 @@ func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) { switch f { case "speed": - host.Speed = value + host.Speed = &value case "port_state": - host.PortState = value + host.PortState = &value case "port_type": - host.PortType = value + host.PortType = &value case "node_name": if len(value) > 2 { value = value[2:] } - host.NodeName = value + host.NodeName = &value case "port_id": if len(value) > 2 { value = value[2:] } - host.PortID = value + host.PortID = &value case "port_name": if len(value) > 2 { value = value[2:] } - host.PortName = value + host.PortName = &value case "fabric_name": if len(value) > 2 { value = value[2:] } - host.FabricName = value + host.FabricName = &value case "dev_loss_tmo": - host.DevLossTMO = value + host.DevLossTMO = &value case "supported_classes": - host.SupportedClasses = value + host.SupportedClasses = &value case "supported_speeds": - host.SupportedSpeeds = value + host.SupportedSpeeds = &value case "symbolic_name": - host.SymbolicName = value + host.SymbolicName = &value } } @@ -143,7 +143,7 @@ func (fs FS) parseFibreChannelHost(name string) (*FibreChannelHost, error) { if err != nil { return nil, err } - host.Counters = *counters + host.Counters = counters return &host, nil } @@ -178,9 +178,9 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error) // Below switch was automatically generated. Don't need everything in there yet, so the unwanted bits are commented out. switch f.Name() { case "dumped_frames": - counters.DumpedFrames = *vp.PUInt64() + counters.DumpedFrames = vp.PUInt64() case "error_frames": - counters.ErrorFrames = *vp.PUInt64() + counters.ErrorFrames = vp.PUInt64() /* case "fc_no_free_exch": counters.FcNoFreeExch = *vp.PUInt64() @@ -208,41 +208,41 @@ func parseFibreChannelStatistics(hostPath string) (*FibreChannelCounters, error) counters.FcpOutputRequests = *vp.PUInt64() */ case "fcp_packet_aborts": - counters.FCPPacketAborts = *vp.PUInt64() + counters.FCPPacketAborts = vp.PUInt64() /* case "fcp_packet_alloc_failures": counters.FcpPacketAllocFailures = *vp.PUInt64() */ case "invalid_tx_word_count": - counters.InvalidTXWordCount = *vp.PUInt64() + counters.InvalidTXWordCount = vp.PUInt64() case "invalid_crc_count": - counters.InvalidCRCCount = *vp.PUInt64() + counters.InvalidCRCCount = vp.PUInt64() case "link_failure_count": - counters.LinkFailureCount = *vp.PUInt64() + counters.LinkFailureCount = vp.PUInt64() /* case "lip_count": counters.LipCount = *vp.PUInt64() */ case "loss_of_signal_count": - counters.LossOfSignalCount = *vp.PUInt64() + counters.LossOfSignalCount = vp.PUInt64() case "loss_of_sync_count": - counters.LossOfSyncCount = *vp.PUInt64() + counters.LossOfSyncCount = vp.PUInt64() case "nos_count": - counters.NosCount = *vp.PUInt64() + counters.NosCount = vp.PUInt64() /* case "prim_seq_protocol_err_count": counters.PrimSeqProtocolErrCount = *vp.PUInt64() */ case "rx_frames": - counters.RXFrames = *vp.PUInt64() + counters.RXFrames = vp.PUInt64() case "rx_words": - counters.RXWords = *vp.PUInt64() + counters.RXWords = vp.PUInt64() case "seconds_since_last_reset": - counters.SecondsSinceLastReset = *vp.PUInt64() + counters.SecondsSinceLastReset = vp.PUInt64() case "tx_frames": - counters.TXFrames = *vp.PUInt64() + counters.TXFrames = vp.PUInt64() case "tx_words": - counters.TXWords = *vp.PUInt64() + counters.TXWords = vp.PUInt64() } if err := vp.Err(); err != nil { diff --git a/sysfs/class_fibrechannel_test.go b/sysfs/class_fibrechannel_test.go index 55f728c2..edc7b3f5 100644 --- a/sysfs/class_fibrechannel_test.go +++ b/sysfs/class_fibrechannel_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "k8s.io/utils/ptr" ) func TestFibreChannelClass(t *testing.T) { @@ -35,53 +36,53 @@ func TestFibreChannelClass(t *testing.T) { want := FibreChannelClass{ "host0": FibreChannelHost{ - Name: "host0", - Speed: "16 Gbit", - PortState: "Online", - PortType: "Point-To-Point (direct nport connection)", - PortName: "1000e0071bce95f2", - SymbolicName: "Emulex SN1100E2P FV12.4.270.3 DV12.4.0.0. HN:gotest. OS:Linux", - NodeName: "2000e0071bce95f2", - PortID: "000002", - FabricName: "0", - DevLossTMO: "30", - SupportedClasses: "Class 3", - SupportedSpeeds: "4 Gbit, 8 Gbit, 16 Gbit", - Counters: FibreChannelCounters{ - DumpedFrames: ^uint64(0), - ErrorFrames: 0, - InvalidCRCCount: 0x2, - RXFrames: 0x3, - RXWords: 0x4, - TXFrames: 0x5, - TXWords: 0x6, - SecondsSinceLastReset: 0x7, - InvalidTXWordCount: 0x8, - LinkFailureCount: 0x9, - LossOfSyncCount: 0x10, - LossOfSignalCount: 0x11, - NosCount: 0x12, - FCPPacketAborts: 0x13, + Name: ptr.To("host0"), + Speed: ptr.To("16 Gbit"), + PortState: ptr.To("Online"), + PortType: ptr.To("Point-To-Point (direct nport connection)"), + PortName: ptr.To("1000e0071bce95f2"), + SymbolicName: ptr.To("Emulex SN1100E2P FV12.4.270.3 DV12.4.0.0. HN:gotest. OS:Linux"), + NodeName: ptr.To("2000e0071bce95f2"), + PortID: ptr.To("000002"), + FabricName: ptr.To("0"), + DevLossTMO: ptr.To("30"), + SupportedClasses: ptr.To("Class 3"), + SupportedSpeeds: ptr.To("4 Gbit, 8 Gbit, 16 Gbit"), + Counters: &FibreChannelCounters{ + DumpedFrames: ptr.To(^uint64(0)), + ErrorFrames: ptr.To(uint64(0)), + InvalidCRCCount: ptr.To(uint64(0x2)), + RXFrames: ptr.To(uint64(0x3)), + RXWords: ptr.To(uint64(0x4)), + TXFrames: ptr.To(uint64(0x5)), + TXWords: ptr.To(uint64(0x6)), + SecondsSinceLastReset: ptr.To(uint64(0x7)), + InvalidTXWordCount: ptr.To(uint64(0x8)), + LinkFailureCount: ptr.To(uint64(0x9)), + LossOfSyncCount: ptr.To(uint64(0x10)), + LossOfSignalCount: ptr.To(uint64(0x11)), + NosCount: ptr.To(uint64(0x12)), + FCPPacketAborts: ptr.To(uint64(0x13)), }, }, "host1": FibreChannelHost{ - Name: "host1", - PortState: "Online", - Counters: FibreChannelCounters{ - DumpedFrames: 0, - ErrorFrames: ^uint64(0), - InvalidCRCCount: 0x20, - RXFrames: 0x30, - RXWords: 0x40, - TXFrames: 0x50, - TXWords: 0x60, - SecondsSinceLastReset: 0x70, - InvalidTXWordCount: 0x80, - LinkFailureCount: 0x90, - LossOfSyncCount: 0x100, - LossOfSignalCount: 0x110, - NosCount: 0x120, - FCPPacketAborts: 0x130, + Name: ptr.To("host1"), + PortState: ptr.To("Online"), + Counters: &FibreChannelCounters{ + DumpedFrames: ptr.To(uint64(0)), + ErrorFrames: ptr.To(^uint64(0)), + InvalidCRCCount: ptr.To(uint64(0x20)), + RXFrames: ptr.To(uint64(0x30)), + RXWords: ptr.To(uint64(0x40)), + TXFrames: ptr.To(uint64(0x50)), + TXWords: ptr.To(uint64(0x60)), + SecondsSinceLastReset: ptr.To(uint64(0x70)), + InvalidTXWordCount: ptr.To(uint64(0x80)), + LinkFailureCount: ptr.To(uint64(0x90)), + LossOfSyncCount: ptr.To(uint64(0x100)), + LossOfSignalCount: ptr.To(uint64(0x110)), + NosCount: ptr.To(uint64(0x120)), + FCPPacketAborts: ptr.To(uint64(0x130)), }, }, }