Skip to content

Commit 39f5f71

Browse files
committed
Change ComMatrix struct Matrix field name to Ports as we will be adding dynamice ranges to the matrix, and we want to make it more clear that there are two parts of the matrix - ports and ranges, rather than the communication matrix and additional ranges.
1 parent 5d1902c commit 39f5f71

File tree

8 files changed

+51
-51
lines changed

8 files changed

+51
-51
lines changed

cmd/generate/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ var (
185185
func TestCommatrixGeneration(t *testing.T) {
186186
expectedComDetails := slices.Concat(testEpsComDetails, types.GeneralStaticEntriesMaster, types.GeneralStaticEntriesWorker, types.StandardStaticEntries)
187187

188-
expectedComMatrix := types.ComMatrix{Matrix: expectedComDetails}
188+
expectedComMatrix := types.ComMatrix{Ports: expectedComDetails}
189189
expectedComMatrix.SortAndRemoveDuplicates()
190190

191191
testCases := []struct {

pkg/commatrix-creator/commatrix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (cm *CommunicationMatrixCreator) CreateEndpointMatrix() (*types.ComMatrix,
8484
epSliceComDetails = append(epSliceComDetails, customComDetails...)
8585
}
8686

87-
commMatrix := &types.ComMatrix{Matrix: epSliceComDetails}
87+
commMatrix := &types.ComMatrix{Ports: epSliceComDetails}
8888
log.Debug("Sorting ComMatrix and removing duplicates")
8989
commMatrix.SortAndRemoveDuplicates()
9090
return commMatrix, nil

pkg/commatrix-creator/commatrix_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,15 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
416416
g.By("Add to wanted comDetails the example static entries")
417417
wantedComDetails = slices.Concat(wantedComDetails, exampleComDetailsList)
418418

419-
wantedComMatrix := types.ComMatrix{Matrix: wantedComDetails}
419+
wantedComMatrix := types.ComMatrix{Ports: wantedComDetails}
420420
wantedComMatrix.SortAndRemoveDuplicates()
421421

422422
g.By("Generate diff between created commatrix and eanted commatrix")
423423
diff := matrixdiff.Generate(&wantedComMatrix, commatrix)
424424

425425
g.By("Checking whether diff is empty")
426-
o.Expect(diff.GetUniquePrimary().Matrix).To(o.BeEmpty())
427-
o.Expect(diff.GetUniqueSecondary().Matrix).To(o.BeEmpty())
426+
o.Expect(diff.GetUniquePrimary().Ports).To(o.BeEmpty())
427+
o.Expect(diff.GetUniqueSecondary().Ports).To(o.BeEmpty())
428428
})
429429

430430
g.It("Should successfully create an endpoint matrix without custom entries", func() {
@@ -436,15 +436,15 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
436436

437437
g.By("Generating wanted comDetails")
438438
wantedComDetails := slices.Concat(testEpsComDetails, types.GeneralStaticEntriesMaster)
439-
wantedComMatrix := types.ComMatrix{Matrix: wantedComDetails}
439+
wantedComMatrix := types.ComMatrix{Ports: wantedComDetails}
440440
wantedComMatrix.SortAndRemoveDuplicates()
441441

442442
g.By("Generate diff between created commatrix and eanted commatrix")
443443
diff := matrixdiff.Generate(&wantedComMatrix, commatrix)
444444

445445
g.By("Checking whether diff is empty")
446-
o.Expect(diff.GetUniquePrimary().Matrix).To(o.BeEmpty())
447-
o.Expect(diff.GetUniqueSecondary().Matrix).To(o.BeEmpty())
446+
o.Expect(diff.GetUniquePrimary().Ports).To(o.BeEmpty())
447+
o.Expect(diff.GetUniqueSecondary().Ports).To(o.BeEmpty())
448448
})
449449

450450
g.It("Should include IPv6 static entries when ipv6Enabled is true on Standard", func() {
@@ -462,12 +462,12 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
462462
types.GeneralIPv6StaticEntriesMaster,
463463
types.GeneralIPv6StaticEntriesWorker,
464464
)
465-
wantedMatrix := types.ComMatrix{Matrix: wanted}
465+
wantedMatrix := types.ComMatrix{Ports: wanted}
466466
wantedMatrix.SortAndRemoveDuplicates()
467467

468468
diff := matrixdiff.Generate(&wantedMatrix, commatrix)
469-
o.Expect(diff.GetUniquePrimary().Matrix).To(o.BeEmpty())
470-
o.Expect(diff.GetUniqueSecondary().Matrix).To(o.BeEmpty())
469+
o.Expect(diff.GetUniquePrimary().Ports).To(o.BeEmpty())
470+
o.Expect(diff.GetUniqueSecondary().Ports).To(o.BeEmpty())
471471
})
472472

473473
g.It("Should filter out localhost-bound ports from endpoint matrix", func() {
@@ -502,7 +502,7 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
502502
o.Expect(err).ToNot(o.HaveOccurred())
503503

504504
g.By("Verifying that localhost-bound ports (8080 on 127.0.0.1, 3000 on ::1) are filtered out")
505-
for _, entry := range commatrix.Matrix {
505+
for _, entry := range commatrix.Ports {
506506
if entry.Service == "localhost-service" {
507507
o.Expect(entry.Port).ToNot(o.Equal(8080), "Port 8080 bound to 127.0.0.1 should be filtered out")
508508
o.Expect(entry.Port).ToNot(o.Equal(3000), "Port 3000 bound to ::1 should be filtered out")
@@ -511,7 +511,7 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
511511

512512
g.By("Verifying that non-localhost port (9090 on 0.0.0.0) is present")
513513
foundPort9090 := false
514-
for _, entry := range commatrix.Matrix {
514+
for _, entry := range commatrix.Ports {
515515
if entry.Service == "localhost-service" && entry.Port == 9090 {
516516
foundPort9090 = true
517517
break
@@ -607,11 +607,11 @@ var _ = g.Describe("Commatrix creator pkg tests", func() {
607607
}
608608

609609
// Compare ignoring order
610-
gotMatrix := types.ComMatrix{Matrix: out}
610+
gotMatrix := types.ComMatrix{Ports: out}
611611
gotMatrix.SortAndRemoveDuplicates()
612-
expectedMatrix := types.ComMatrix{Matrix: expected}
612+
expectedMatrix := types.ComMatrix{Ports: expected}
613613
expectedMatrix.SortAndRemoveDuplicates()
614-
o.Expect(gotMatrix.Matrix).To(o.Equal(expectedMatrix.Matrix))
614+
o.Expect(gotMatrix.Ports).To(o.Equal(expectedMatrix.Ports))
615615
})
616616
})
617617
})

pkg/listening-sockets/listening-sockets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (cc *ConnectionCheck) GenerateSS(namespace string) (*types.ComMatrix, []byt
107107
return nil, nil, nil, err
108108
}
109109

110-
ssComMat := types.ComMatrix{Matrix: nodesComDetails}
110+
ssComMat := types.ComMatrix{Ports: nodesComDetails}
111111
ssComMat.SortAndRemoveDuplicates()
112112

113113
return &ssComMat, ssOutTCP, ssOutUDP, nil

pkg/listening-sockets/listening_sockets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ var _ = Describe("GenerateSS", func() {
192192

193193
Expect(normalizeOutput(string(ssOutTCP))).To(Equal(normalizeOutput(expectedTCPOutput)))
194194
Expect(normalizeOutput(string(ssOutUDP))).To(Equal(normalizeOutput(expectedUDPOutput)))
195-
Expect(ssMat.Matrix).To(Equal(expectedSSMat))
195+
Expect(ssMat.Ports).To(Equal(expectedSSMat))
196196
})
197197
})
198198

pkg/matrix-diff/matrix-diff.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ func Generate(primary *types.ComMatrix, secondary *types.ComMatrix) MatrixDiff {
2929
matrix := types.ComMatrix{}
3030
epsStatus := map[string]status{}
3131

32-
for _, cd := range primary.Matrix {
33-
matrix.Matrix = append(matrix.Matrix, cd)
32+
for _, cd := range primary.Ports {
33+
matrix.Ports = append(matrix.Ports, cd)
3434
epsStatus[cd.String()] = both
3535

3636
if !secondary.Contains(cd) {
3737
epsStatus[cd.String()] = uniquePrimary
3838
}
3939
}
4040

41-
for _, cd := range secondary.Matrix {
42-
matrix.Matrix = append(matrix.Matrix, cd)
41+
for _, cd := range secondary.Ports {
42+
matrix.Ports = append(matrix.Ports, cd)
4343
epsStatus[cd.String()] = both
4444

4545
if !primary.Contains(cd) {
@@ -59,7 +59,7 @@ func (m *MatrixDiff) String() (string, error) {
5959
}
6060
diff := colNames + "\n"
6161

62-
for _, cd := range m.Matrix {
62+
for _, cd := range m.Ports {
6363
switch m.cdToStatus[cd.String()] {
6464
case both:
6565
diff += fmt.Sprintf("%s\n", cd)
@@ -79,9 +79,9 @@ func (m *MatrixDiff) String() (string, error) {
7979
func (m *MatrixDiff) GetUniquePrimary() *types.ComMatrix {
8080
matrix := types.ComMatrix{}
8181

82-
for _, cd := range m.Matrix {
82+
for _, cd := range m.Ports {
8383
if m.cdToStatus[cd.String()] == uniquePrimary {
84-
matrix.Matrix = append(matrix.Matrix, cd)
84+
matrix.Ports = append(matrix.Ports, cd)
8585
}
8686
}
8787

@@ -92,9 +92,9 @@ func (m *MatrixDiff) GetUniquePrimary() *types.ComMatrix {
9292
func (m *MatrixDiff) GetUniqueSecondary() *types.ComMatrix {
9393
matrix := types.ComMatrix{}
9494

95-
for _, cd := range m.Matrix {
95+
for _, cd := range m.Ports {
9696
if m.cdToStatus[cd.String()] == uniqueSecondary {
97-
matrix.Matrix = append(matrix.Matrix, cd)
97+
matrix.Ports = append(matrix.Ports, cd)
9898
}
9999
}
100100

@@ -105,9 +105,9 @@ func (m *MatrixDiff) GetUniqueSecondary() *types.ComMatrix {
105105
func (m *MatrixDiff) GetSharedEntries() *types.ComMatrix {
106106
matrix := types.ComMatrix{}
107107

108-
for _, cd := range m.Matrix {
108+
for _, cd := range m.Ports {
109109
if m.cdToStatus[cd.String()] == both {
110-
matrix.Matrix = append(matrix.Matrix, cd)
110+
matrix.Ports = append(matrix.Ports, cd)
111111
}
112112
}
113113

pkg/types/types.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const (
5959
)
6060

6161
type ComMatrix struct {
62-
Matrix []ComDetails
62+
Ports []ComDetails
6363
}
6464

6565
type ComDetails struct {
@@ -89,7 +89,7 @@ func (m *ComMatrix) ToCSV() ([]byte, error) {
8989
w := bytes.NewBuffer(out)
9090
csvwriter := csv.NewWriter(w)
9191

92-
err := gocsv.MarshalCSV(&m.Matrix, csvwriter)
92+
err := gocsv.MarshalCSV(&m.Ports, csvwriter)
9393
if err != nil {
9494
return nil, err
9595
}
@@ -100,7 +100,7 @@ func (m *ComMatrix) ToCSV() ([]byte, error) {
100100
}
101101

102102
func (m *ComMatrix) ToJSON() ([]byte, error) {
103-
out, err := json.MarshalIndent(m.Matrix, "", " ")
103+
out, err := json.MarshalIndent(m.Ports, "", " ")
104104
if err != nil {
105105
return nil, err
106106
}
@@ -119,7 +119,7 @@ func (m *ComMatrix) ToYAML() ([]byte, error) {
119119

120120
func (m *ComMatrix) String() string {
121121
var result strings.Builder
122-
for _, details := range m.Matrix {
122+
for _, details := range m.Ports {
123123
result.WriteString(details.String() + "\n")
124124
}
125125

@@ -130,7 +130,7 @@ func (m *ComMatrix) WriteMatrixToFileByType(utilsHelpers utils.UtilsInterface, f
130130
if format == FormatNFT {
131131
pools := m.SeparateMatrixByGroup()
132132
for poolName, mat := range pools {
133-
if len(mat.Matrix) == 0 {
133+
if len(mat.Ports) == 0 {
134134
continue
135135
}
136136
if err := mat.writeMatrixToFile(utilsHelpers, fileNamePrefix+"-"+poolName, format, destDir); err != nil {
@@ -165,13 +165,13 @@ func (m *ComMatrix) print(format string) ([]byte, error) {
165165
// SeparateMatrixByGroup groups matrix entries by their group name (stored in NodeGroup).
166166
func (m *ComMatrix) SeparateMatrixByGroup() map[string]ComMatrix {
167167
res := make(map[string]ComMatrix)
168-
for _, entry := range m.Matrix {
168+
for _, entry := range m.Ports {
169169
pool := entry.NodeGroup
170170
if pool == "" {
171171
continue
172172
}
173173
cm := res[pool]
174-
cm.Matrix = append(cm.Matrix, entry)
174+
cm.Ports = append(cm.Ports, entry)
175175
res[pool] = cm
176176
}
177177
return res
@@ -188,7 +188,7 @@ func (m *ComMatrix) writeMatrixToFile(utilsHelpers utils.UtilsInterface, fileNam
188188
}
189189

190190
func (m *ComMatrix) Contains(cd ComDetails) bool {
191-
for _, cd1 := range m.Matrix {
191+
for _, cd1 := range m.Ports {
192192
if cd1.Equals(cd) {
193193
return true
194194
}
@@ -200,7 +200,7 @@ func (m *ComMatrix) Contains(cd ComDetails) bool {
200200
func (m *ComMatrix) ToNFTables() ([]byte, error) {
201201
var tcpPorts []string
202202
var udpPorts []string
203-
for _, line := range m.Matrix {
203+
for _, line := range m.Ports {
204204
if line.Protocol == "TCP" {
205205
tcpPorts = append(tcpPorts, fmt.Sprint(line.Port))
206206
} else if line.Protocol == "UDP" {
@@ -243,16 +243,16 @@ func (m *ComMatrix) ToNFTables() ([]byte, error) {
243243
func (m *ComMatrix) SortAndRemoveDuplicates() {
244244
allKeys := make(map[string]bool)
245245
res := []ComDetails{}
246-
for _, item := range m.Matrix {
246+
for _, item := range m.Ports {
247247
str := fmt.Sprintf("%s-%d-%s", item.NodeGroup, item.Port, item.Protocol)
248248
if _, value := allKeys[str]; !value {
249249
allKeys[str] = true
250250
res = append(res, item)
251251
}
252252
}
253-
m.Matrix = res
253+
m.Ports = res
254254

255-
slices.SortFunc(m.Matrix, func(a, b ComDetails) int {
255+
slices.SortFunc(m.Ports, func(a, b ComDetails) int {
256256
res := cmp.Compare(a.NodeGroup, b.NodeGroup)
257257
if res != 0 {
258258
return res

test/e2e/validation_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var _ = Describe("Validation", func() {
6161

6262
ComDetailsMatrix, err := types.ParseToComDetailsList(commatrixFileContent, types.FormatCSV)
6363
Expect(err).ToNot(HaveOccurred(), "Failed to parse generated commatrix")
64-
commatrix = &types.ComMatrix{Matrix: ComDetailsMatrix}
64+
commatrix = &types.ComMatrix{Ports: ComDetailsMatrix}
6565
})
6666

6767
It("generated communication matrix should be equal to documented communication matrix", func() {
@@ -93,7 +93,7 @@ var _ = Describe("Validation", func() {
9393

9494
docComDetailsList, err := types.ParseToComDetailsList(docCommatrixFileContent, types.FormatCSV)
9595
Expect(err).ToNot(HaveOccurred(), "Failed to parse documented communication matrix")
96-
docComMatrix := &types.ComMatrix{Matrix: docComDetailsList}
96+
docComMatrix := &types.ComMatrix{Ports: docComDetailsList}
9797

9898
By("generating diff between matrices for testing purposes")
9999
endpointslicesDiffWithDocMat := matrixdiff.Generate(commatrix, docComMatrix)
@@ -106,7 +106,7 @@ var _ = Describe("Validation", func() {
106106
By("comparing new and documented commatrices")
107107
// Get ports that are in the documented commatrix but not in the generated commatrix.
108108
notUsedPortsMat := endpointslicesDiffWithDocMat.GetUniqueSecondary()
109-
if len(notUsedPortsMat.Matrix) > 0 {
109+
if len(notUsedPortsMat.Ports) > 0 {
110110
logrus.Warningf("the following ports are documented but are not used:\n%s", notUsedPortsMat)
111111
}
112112

@@ -124,15 +124,15 @@ var _ = Describe("Validation", func() {
124124

125125
portsToIgnoreComDetails, err := types.ParseToComDetailsList(portsToIgnoreFileContent, openPortsToIgnoreFormat)
126126
Expect(err).ToNot(HaveOccurred())
127-
portsToIgnoreMat = &types.ComMatrix{Matrix: portsToIgnoreComDetails}
127+
portsToIgnoreMat = &types.ComMatrix{Ports: portsToIgnoreComDetails}
128128

129129
// generate the diff matrix between the open ports to ignore matrix and the missing ports in the documented commatrix (based on the diff between the enpointslice and the doc matrix)
130130
nonDocumentedEndpointslicesMat := endpointslicesDiffWithDocMat.GetUniquePrimary()
131131
endpointslicesDiffWithIgnoredPorts := matrixdiff.Generate(nonDocumentedEndpointslicesMat, portsToIgnoreMat)
132132
missingPortsMat = endpointslicesDiffWithIgnoredPorts.GetUniquePrimary()
133133
}
134134

135-
if len(missingPortsMat.Matrix) > 0 {
135+
if len(missingPortsMat.Ports) > 0 {
136136
err := fmt.Errorf("the following ports are used but are not documented:\n%s", missingPortsMat)
137137
Expect(err).ToNot(HaveOccurred())
138138
}
@@ -150,13 +150,13 @@ var _ = Describe("Validation", func() {
150150
missingEPSMat, err := extractEPSMatByStatus(matDiffSSFileContent, Missing)
151151
Expect(err).ToNot(HaveOccurred(), "Failed to extract missing EPS Matrix")
152152

153-
if len(notUsedEPSMat.Matrix) > 0 {
153+
if len(notUsedEPSMat.Ports) > 0 {
154154
logrus.Warningf("the following ports are not used: \n %s", notUsedEPSMat)
155155
}
156156

157157
missingEPSMat, err = filterKnownPorts(missingEPSMat)
158158
Expect(err).ToNot(HaveOccurred(), "Failed to filter the known ports")
159-
if len(missingEPSMat.Matrix) > 0 {
159+
if len(missingEPSMat.Ports) > 0 {
160160
err := fmt.Errorf("the following ports are used but don't have an endpointslice: \n %s", missingEPSMat)
161161
Expect(err).ToNot(HaveOccurred())
162162
}
@@ -190,7 +190,7 @@ func filterKnownPorts(mat *types.ComMatrix) (*types.ComMatrix, error) {
190190
}
191191

192192
res := []types.ComDetails{}
193-
for _, cd := range mat.Matrix {
193+
for _, cd := range mat.Ports {
194194
// Skip "ovnkube" ports in the nodePort range, these are dynamic open ports on the node,
195195
// no need to mention them in the matrix diff
196196
if cd.Service == "ovnkube" && cd.Port >= nodePortMin && cd.Port <= nodePortMax {
@@ -219,7 +219,7 @@ func filterKnownPorts(mat *types.ComMatrix) (*types.ComMatrix, error) {
219219
res = append(res, cd)
220220
}
221221

222-
return &types.ComMatrix{Matrix: res}, nil
222+
return &types.ComMatrix{Ports: res}, nil
223223
}
224224

225225
// extractEPSMatByStatus extracts and returns ComMatrix by filtering lines of a CSV content based on a EPS status.
@@ -231,7 +231,7 @@ func extractEPSMatByStatus(csvContent []byte, status EPSStatus) (*types.ComMatri
231231
return nil, err
232232
}
233233

234-
return &types.ComMatrix{Matrix: prefixEPSMat}, nil
234+
return &types.ComMatrix{Ports: prefixEPSMat}, nil
235235
}
236236

237237
// extractDiffByStatus filter the lines of the csv content based on the EPS status

0 commit comments

Comments
 (0)