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

k8snetworkplumbingwg / sriov-network-operator / 9939865280

15 Jul 2024 01:00PM UTC coverage: 40.901% (-0.08%) from 40.981%
9939865280

push

github

web-flow
Merge pull request #734 from SchSeba/fix_mlx_plugin

Skip firmware reset on devices the operator doesn't control

16 of 53 new or added lines in 5 files covered. (30.19%)

5 existing lines in 1 file now uncovered.

5599 of 13689 relevant lines covered (40.9%)

0.45 hits per line

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

0.0
/pkg/host/store/store.go
1
package store
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "os"
7
        "path/filepath"
8

9
        "sigs.k8s.io/controller-runtime/pkg/log"
10

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

17
// Contains all the file storing on the host
18
//
19
//go:generate ../../../bin/mockgen -destination mock/mock_store.go -source store.go
20
type ManagerInterface interface {
21
        ClearPCIAddressFolder() error
22
        SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) error
23
        RemovePfAppliedStatus(pciAddress string) error
24
        LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interface, bool, error)
25

26
        GetCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error)
27
        WriteCheckpointFile(*sriovnetworkv1.SriovNetworkNodeState) error
28
}
29

30
type manager struct{}
31

32
// NewManager: create the initial folders needed to store the info about the PF
33
// and return a manager struct that implements the ManagerInterface interface
34
func NewManager() (ManagerInterface, error) {
×
35
        if err := createOperatorConfigFolderIfNeeded(); err != nil {
×
36
                return nil, err
×
37
        }
×
38

39
        return &manager{}, nil
×
40
}
41

42
// createOperatorConfigFolderIfNeeded: create the operator base folder on the host
43
// together with the pci folder to save the PF status objects as json files
44
func createOperatorConfigFolderIfNeeded() error {
×
45
        hostExtension := utils.GetHostExtension()
×
46
        SriovConfBasePathUse := filepath.Join(hostExtension, consts.SriovConfBasePath)
×
47
        _, err := os.Stat(SriovConfBasePathUse)
×
48
        if err != nil {
×
49
                if os.IsNotExist(err) {
×
50
                        err = os.MkdirAll(SriovConfBasePathUse, os.ModeDir)
×
51
                        if err != nil {
×
52
                                return fmt.Errorf("failed to create the sriov config folder on host in path %s: %v", SriovConfBasePathUse, err)
×
53
                        }
×
54
                } else {
×
55
                        return fmt.Errorf("failed to check if the sriov config folder on host in path %s exist: %v", SriovConfBasePathUse, err)
×
56
                }
×
57
        }
58

59
        PfAppliedConfigUse := filepath.Join(hostExtension, consts.PfAppliedConfig)
×
60
        _, err = os.Stat(PfAppliedConfigUse)
×
61
        if err != nil {
×
62
                if os.IsNotExist(err) {
×
63
                        err = os.MkdirAll(PfAppliedConfigUse, os.ModeDir)
×
64
                        if err != nil {
×
65
                                return fmt.Errorf("failed to create the pci folder on host in path %s: %v", PfAppliedConfigUse, err)
×
66
                        }
×
67
                } else {
×
68
                        return fmt.Errorf("failed to check if the pci folder on host in path %s exist: %v", PfAppliedConfigUse, err)
×
69
                }
×
70
        }
71

72
        return nil
×
73
}
74

75
// ClearPCIAddressFolder: removes all the PFs storage information
76
func (s *manager) ClearPCIAddressFolder() error {
×
77
        hostExtension := utils.GetHostExtension()
×
78
        PfAppliedConfigUse := filepath.Join(hostExtension, consts.PfAppliedConfig)
×
79
        _, err := os.Stat(PfAppliedConfigUse)
×
80
        if err != nil {
×
81
                if os.IsNotExist(err) {
×
82
                        return nil
×
83
                }
×
84
                return fmt.Errorf("failed to check the pci address folder path %s: %v", PfAppliedConfigUse, err)
×
85
        }
86

87
        err = os.RemoveAll(PfAppliedConfigUse)
×
88
        if err != nil {
×
89
                return fmt.Errorf("failed to remove the PCI address folder on path %s: %v", PfAppliedConfigUse, err)
×
90
        }
×
91

92
        err = os.Mkdir(PfAppliedConfigUse, os.ModeDir)
×
93
        if err != nil {
×
94
                return fmt.Errorf("failed to create the pci folder on host in path %s: %v", PfAppliedConfigUse, err)
×
95
        }
×
96

97
        return nil
×
98
}
99

100
// SaveLastPfAppliedStatus will save the PF object as a json into the /etc/sriov-operator/pci/<pci-address>
101
// this function must be called after running the chroot function
102
func (s *manager) SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) error {
×
103
        data, err := json.Marshal(PfInfo)
×
104
        if err != nil {
×
105
                log.Log.Error(err, "failed to marshal PF status", "status", *PfInfo)
×
106
                return err
×
107
        }
×
108

109
        hostExtension := utils.GetHostExtension()
×
110
        pathFile := filepath.Join(hostExtension, consts.PfAppliedConfig, PfInfo.PciAddress)
×
111
        err = os.WriteFile(pathFile, data, 0644)
×
112
        return err
×
113
}
114

NEW
115
func (s *manager) RemovePfAppliedStatus(pciAddress string) error {
×
NEW
116
        hostExtension := utils.GetHostExtension()
×
NEW
117
        pathFile := filepath.Join(hostExtension, consts.PfAppliedConfig, pciAddress)
×
NEW
118
        err := os.RemoveAll(pathFile)
×
NEW
119
        if err != nil {
×
NEW
120
                log.Log.Error(err, "failed to remove PF status", "pathFile", pathFile)
×
NEW
121
                return err
×
NEW
122
        }
×
NEW
123
        return nil
×
124
}
125

126
// LoadPfsStatus convert the /etc/sriov-operator/pci/<pci-address> json to pfstatus
127
// returns false if the file doesn't exist.
128
func (s *manager) LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interface, bool, error) {
×
129
        hostExtension := utils.GetHostExtension()
×
130
        pathFile := filepath.Join(hostExtension, consts.PfAppliedConfig, pciAddress)
×
131
        pfStatus := &sriovnetworkv1.Interface{}
×
132
        data, err := os.ReadFile(pathFile)
×
133
        if err != nil {
×
134
                if os.IsNotExist(err) {
×
135
                        return nil, false, nil
×
136
                }
×
137
                log.Log.Error(err, "failed to read PF status", "path", pathFile)
×
138
                return nil, false, err
×
139
        }
140

141
        err = json.Unmarshal(data, pfStatus)
×
142
        if err != nil {
×
143
                log.Log.Error(err, "failed to unmarshal PF status", "data", string(data))
×
144
                return nil, false, err
×
145
        }
×
146

147
        return pfStatus, true, nil
×
148
}
149

150
func (s *manager) GetCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error) {
×
151
        log.Log.Info("getCheckPointNodeState()")
×
152
        configdir := filepath.Join(vars.Destdir, consts.CheckpointFileName)
×
153
        file, err := os.OpenFile(configdir, os.O_RDONLY, 0644)
×
154
        if err != nil {
×
155
                if os.IsNotExist(err) {
×
156
                        return nil, nil
×
157
                }
×
158
                return nil, err
×
159
        }
160
        defer file.Close()
×
161
        if err = json.NewDecoder(file).Decode(&sriovnetworkv1.InitialState); err != nil {
×
162
                return nil, err
×
163
        }
×
164

165
        return &sriovnetworkv1.InitialState, nil
×
166
}
167

168
func (s *manager) WriteCheckpointFile(ns *sriovnetworkv1.SriovNetworkNodeState) error {
×
169
        configdir := filepath.Join(vars.Destdir, consts.CheckpointFileName)
×
170
        file, err := os.OpenFile(configdir, os.O_RDWR|os.O_CREATE, 0644)
×
171
        if err != nil {
×
172
                return err
×
173
        }
×
174
        defer file.Close()
×
175
        log.Log.Info("WriteCheckpointFile(): try to decode the checkpoint file")
×
176
        if err = json.NewDecoder(file).Decode(&sriovnetworkv1.InitialState); err != nil {
×
177
                log.Log.V(2).Error(err, "WriteCheckpointFile(): fail to decode, writing new file instead")
×
178
                log.Log.Info("WriteCheckpointFile(): write checkpoint file")
×
179
                if err = file.Truncate(0); err != nil {
×
180
                        return err
×
181
                }
×
182
                if _, err = file.Seek(0, 0); err != nil {
×
183
                        return err
×
184
                }
×
185
                if err = json.NewEncoder(file).Encode(*ns); err != nil {
×
186
                        return err
×
187
                }
×
188
                sriovnetworkv1.InitialState = *ns
×
189
        }
190
        return nil
×
191
}
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