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

kubeovn / kube-ovn / 13075539137

31 Jan 2025 03:15PM UTC coverage: 22.333% (-0.004%) from 22.337%
13075539137

Pull #4954

github

web-flow
build(deps): bump github.com/brianvoe/gofakeit/v7 from 7.1.2 to 7.2.1

Bumps [github.com/brianvoe/gofakeit/v7](https://github.com/brianvoe/gofakeit) from 7.1.2 to 7.2.1.
- [Release notes](https://github.com/brianvoe/gofakeit/releases)
- [Commits](https://github.com/brianvoe/gofakeit/compare/v7.1.2...v7.2.1)

---
updated-dependencies:
- dependency-name: github.com/brianvoe/gofakeit/v7
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #4954: build(deps): bump github.com/brianvoe/gofakeit/v7 from 7.1.2 to 7.2.1

10374 of 46452 relevant lines covered (22.33%)

0.26 hits per line

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

82.53
/pkg/ovs/ovn-nb-port_group.go
1
package ovs
2

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

8
        "github.com/ovn-org/libovsdb/client"
9
        "github.com/ovn-org/libovsdb/model"
10
        "github.com/ovn-org/libovsdb/ovsdb"
11
        "github.com/scylladb/go-set/strset"
12
        "k8s.io/klog/v2"
13

14
        "github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
15
)
16

17
func (c *OVNNbClient) CreatePortGroup(pgName string, externalIDs map[string]string) error {
1✔
18
        exist, err := c.PortGroupExists(pgName)
1✔
19
        if err != nil {
2✔
20
                klog.Error(err)
1✔
21
                return err
1✔
22
        }
1✔
23

24
        // ignore
25
        if exist {
2✔
26
                return nil
1✔
27
        }
1✔
28

29
        pg := &ovnnb.PortGroup{
1✔
30
                Name:        pgName,
1✔
31
                ExternalIDs: externalIDs,
1✔
32
        }
1✔
33

1✔
34
        ops, err := c.ovsDbClient.Create(pg)
1✔
35
        if err != nil {
1✔
36
                klog.Error(err)
×
37
                return fmt.Errorf("generate operations for creating port group %s: %w", pgName, err)
×
38
        }
×
39

40
        if err = c.Transact("pg-add", ops); err != nil {
2✔
41
                klog.Error(err)
1✔
42
                return fmt.Errorf("create port group %s: %w", pgName, err)
1✔
43
        }
1✔
44

45
        return nil
1✔
46
}
47

48
// PortGroupAddPorts add ports to port group
49
func (c *OVNNbClient) PortGroupAddPorts(pgName string, lspNames ...string) error {
1✔
50
        return c.PortGroupUpdatePorts(pgName, ovsdb.MutateOperationInsert, lspNames...)
1✔
51
}
1✔
52

53
// PortGroupRemovePorts remove ports from port group
54
func (c *OVNNbClient) PortGroupRemovePorts(pgName string, lspNames ...string) error {
1✔
55
        return c.PortGroupUpdatePorts(pgName, ovsdb.MutateOperationDelete, lspNames...)
1✔
56
}
1✔
57

58
func (c *OVNNbClient) PortGroupSetPorts(pgName string, ports []string) error {
1✔
59
        if pgName == "" {
2✔
60
                return errors.New("port group name is empty")
1✔
61
        }
1✔
62

63
        pg, err := c.GetPortGroup(pgName, false)
1✔
64
        if err != nil {
2✔
65
                klog.Error(err)
1✔
66
                return fmt.Errorf("get port group %s: %w", pgName, err)
1✔
67
        }
1✔
68

69
        expected := strset.NewWithSize(len(ports))
1✔
70
        for _, port := range ports {
2✔
71
                lsp, err := c.GetLogicalSwitchPort(port, true)
1✔
72
                if err != nil {
1✔
73
                        klog.Error(err)
×
74
                        return err
×
75
                }
×
76
                if lsp != nil {
2✔
77
                        expected.Add(lsp.UUID)
1✔
78
                }
1✔
79
        }
80

81
        existing := strset.New(pg.Ports...)
1✔
82
        toAdd := strset.Difference(expected, existing).List()
1✔
83
        toDel := strset.Difference(existing, expected).List()
1✔
84

1✔
85
        insertOps, err := c.portGroupUpdatePortOp(pgName, toAdd, ovsdb.MutateOperationInsert)
1✔
86
        if err != nil {
1✔
87
                klog.Error(err)
×
88
                return fmt.Errorf("failed generate operations for adding ports %v to port group %s: %w", toAdd, pgName, err)
×
89
        }
×
90
        deleteOps, err := c.portGroupUpdatePortOp(pgName, toDel, ovsdb.MutateOperationDelete)
1✔
91
        if err != nil {
1✔
92
                klog.Error(err)
×
93
                return fmt.Errorf("failed generate operations for deleting ports %v from port group %s: %w", toDel, pgName, err)
×
94
        }
×
95

96
        if err = c.Transact("pg-ports-update", append(insertOps, deleteOps...)); err != nil {
1✔
97
                klog.Error(err)
×
98
                return fmt.Errorf("port group %s set ports %v: %w", pgName, ports, err)
×
99
        }
×
100

101
        return nil
1✔
102
}
103

104
// UpdatePortGroup update port group
105
func (c *OVNNbClient) UpdatePortGroup(pg *ovnnb.PortGroup, fields ...interface{}) error {
1✔
106
        op, err := c.Where(pg).Update(pg, fields...)
1✔
107
        if err != nil {
2✔
108
                klog.Error(err)
1✔
109
                return fmt.Errorf("generate operations for updating port group %s: %w", pg.Name, err)
1✔
110
        }
1✔
111

112
        if err = c.Transact("pg-update", op); err != nil {
1✔
113
                klog.Error(err)
×
114
                return fmt.Errorf("update port group %s: %w", pg.Name, err)
×
115
        }
×
116

117
        return nil
1✔
118
}
119

120
// PortGroupUpdatePorts add several ports to or from port group once
121
func (c *OVNNbClient) PortGroupUpdatePorts(pgName string, op ovsdb.Mutator, lspNames ...string) error {
1✔
122
        if len(lspNames) == 0 {
2✔
123
                return nil
1✔
124
        }
1✔
125

126
        lspUUIDs := make([]string, 0, len(lspNames))
1✔
127

1✔
128
        for _, lspName := range lspNames {
2✔
129
                lsp, err := c.GetLogicalSwitchPort(lspName, true)
1✔
130
                if err != nil {
1✔
131
                        klog.Error(err)
×
132
                        return err
×
133
                }
×
134

135
                // ignore non-existent object
136
                if lsp != nil {
2✔
137
                        lspUUIDs = append(lspUUIDs, lsp.UUID)
1✔
138
                }
1✔
139
        }
140

141
        ops, err := c.portGroupUpdatePortOp(pgName, lspUUIDs, op)
1✔
142
        if err != nil {
2✔
143
                klog.Error(err)
1✔
144
                return fmt.Errorf("generate operations for port group %s update ports %v: %w", pgName, lspNames, err)
1✔
145
        }
1✔
146

147
        if err := c.Transact("pg-ports-update", ops); err != nil {
1✔
148
                klog.Error(err)
×
149
                return fmt.Errorf("port group %s update ports %v: %w", pgName, lspNames, err)
×
150
        }
×
151

152
        return nil
1✔
153
}
154

155
func (c *OVNNbClient) DeletePortGroup(pgName ...string) error {
1✔
156
        delList := make([]*ovnnb.PortGroup, 0, len(pgName))
1✔
157
        for _, name := range pgName {
2✔
158
                // get port group
1✔
159
                pg, err := c.GetPortGroup(name, true)
1✔
160
                if err != nil {
1✔
161
                        return fmt.Errorf("get port group %s when delete: %w", name, err)
×
162
                }
×
163
                // not found, skip
164
                if pg == nil {
2✔
165
                        continue
1✔
166
                }
167
                delList = append(delList, pg)
1✔
168
        }
169
        if len(delList) == 0 {
2✔
170
                return nil
1✔
171
        }
1✔
172

173
        var modelList []model.Model = make([]model.Model, len(delList))
1✔
174
        for i, pg := range delList {
2✔
175
                modelList[i] = pg
1✔
176
        }
1✔
177
        op, err := c.Where(modelList...).Delete()
1✔
178
        if err != nil {
1✔
179
                return err
×
180
        }
×
181
        if err := c.Transact("pg-del", op); err != nil {
1✔
182
                klog.Error(err)
×
183
                return fmt.Errorf("delete port group %s: %w", pgName, err)
×
184
        }
×
185

186
        return nil
1✔
187
}
188

189
// GetPortGroup get port group by name
190
func (c *OVNNbClient) GetPortGroup(pgName string, ignoreNotFound bool) (*ovnnb.PortGroup, error) {
1✔
191
        if pgName == "" {
2✔
192
                return nil, errors.New("port group name is empty")
1✔
193
        }
1✔
194
        ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
1✔
195
        defer cancel()
1✔
196

1✔
197
        pg := &ovnnb.PortGroup{Name: pgName}
1✔
198
        if err := c.ovsDbClient.Get(ctx, pg); err != nil {
2✔
199
                if ignoreNotFound && errors.Is(err, client.ErrNotFound) {
2✔
200
                        return nil, nil
1✔
201
                }
1✔
202
                klog.Error(err)
1✔
203
                return nil, fmt.Errorf("get port group %s: %w", pgName, err)
1✔
204
        }
205

206
        return pg, nil
1✔
207
}
208

209
// ListPortGroups list port groups which match the given externalIDs,
210
// result should include all port groups when externalIDs is empty,
211
// result should include all port groups which externalIDs[key] is not empty when externalIDs[key] is ""
212
func (c *OVNNbClient) ListPortGroups(externalIDs map[string]string) ([]ovnnb.PortGroup, error) {
1✔
213
        ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
1✔
214
        defer cancel()
1✔
215

1✔
216
        pgs := make([]ovnnb.PortGroup, 0)
1✔
217

1✔
218
        if err := c.WhereCache(func(pg *ovnnb.PortGroup) bool {
2✔
219
                if len(pg.ExternalIDs) < len(externalIDs) {
2✔
220
                        return false
1✔
221
                }
1✔
222

223
                if len(pg.ExternalIDs) != 0 {
2✔
224
                        for k, v := range externalIDs {
2✔
225
                                // if only key exist but not value in externalIDs, we should include this pg,
1✔
226
                                // it's equal to shell command `ovn-nbctl --columns=xx find port_group external_ids:key!=\"\"`
1✔
227
                                if len(v) == 0 {
2✔
228
                                        if len(pg.ExternalIDs[k]) == 0 {
2✔
229
                                                return false
1✔
230
                                        }
1✔
231
                                } else {
1✔
232
                                        if pg.ExternalIDs[k] != v {
1✔
233
                                                return false
×
234
                                        }
×
235
                                }
236
                        }
237
                }
238

239
                return true
1✔
240
        }).List(ctx, &pgs); err != nil {
×
241
                klog.Errorf("list logical switch ports: %v", err)
×
242
                return nil, err
×
243
        }
×
244

245
        return pgs, nil
1✔
246
}
247

248
func (c *OVNNbClient) PortGroupExists(pgName string) (bool, error) {
1✔
249
        lsp, err := c.GetPortGroup(pgName, true)
1✔
250
        return lsp != nil, err
1✔
251
}
1✔
252

253
// portGroupUpdatePortOp create operations add port to or delete port from port group
254
func (c *OVNNbClient) portGroupUpdatePortOp(pgName string, lspUUIDs []string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
1✔
255
        if len(lspUUIDs) == 0 {
2✔
256
                return nil, nil
1✔
257
        }
1✔
258

259
        mutation := func(pg *ovnnb.PortGroup) *model.Mutation {
2✔
260
                mutation := &model.Mutation{
1✔
261
                        Field:   &pg.Ports,
1✔
262
                        Value:   lspUUIDs,
1✔
263
                        Mutator: op,
1✔
264
                }
1✔
265

1✔
266
                return mutation
1✔
267
        }
1✔
268

269
        return c.portGroupOp(pgName, mutation)
1✔
270
}
271

272
// portGroupUpdateACLOp create operations add acl to or delete acl from port group
273
func (c *OVNNbClient) portGroupUpdateACLOp(pgName string, aclUUIDs []string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
1✔
274
        if len(aclUUIDs) == 0 {
2✔
275
                return nil, nil
1✔
276
        }
1✔
277

278
        mutation := func(pg *ovnnb.PortGroup) *model.Mutation {
2✔
279
                mutation := &model.Mutation{
1✔
280
                        Field:   &pg.ACLs,
1✔
281
                        Value:   aclUUIDs,
1✔
282
                        Mutator: op,
1✔
283
                }
1✔
284

1✔
285
                return mutation
1✔
286
        }
1✔
287

288
        return c.portGroupOp(pgName, mutation)
1✔
289
}
290

291
// portGroupOp create operations about port group
292
func (c *OVNNbClient) portGroupOp(pgName string, mutationsFunc ...func(pg *ovnnb.PortGroup) *model.Mutation) ([]ovsdb.Operation, error) {
1✔
293
        pg, err := c.GetPortGroup(pgName, false)
1✔
294
        if err != nil {
2✔
295
                klog.Error(err)
1✔
296
                return nil, fmt.Errorf("get port group %s: %w", pgName, err)
1✔
297
        }
1✔
298

299
        if len(mutationsFunc) == 0 {
2✔
300
                return nil, nil
1✔
301
        }
1✔
302

303
        mutations := make([]model.Mutation, 0, len(mutationsFunc))
1✔
304

1✔
305
        for _, f := range mutationsFunc {
2✔
306
                mutation := f(pg)
1✔
307

1✔
308
                if mutation != nil {
2✔
309
                        mutations = append(mutations, *mutation)
1✔
310
                }
1✔
311
        }
312

313
        ops, err := c.ovsDbClient.Where(pg).Mutate(pg, mutations...)
1✔
314
        if err != nil {
1✔
315
                klog.Error(err)
×
316
                return nil, fmt.Errorf("generate operations for mutating port group %s: %w", pgName, err)
×
317
        }
×
318

319
        return ops, nil
1✔
320
}
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