Skip to content

Commit

Permalink
Merge pull request #21 from shirmoran/fix-generate-commatrix-from-file
Browse files Browse the repository at this point in the history
Allow generating commatrix from csv and fix samples
  • Loading branch information
sabinaaledort authored Aug 4, 2024
2 parents 8cf81e2 + e9fdb5f commit 20db006
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 30 deletions.
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ func main() {
panic(fmt.Sprintf("Error while writing ss matrix to file :%v", err))
}
// generate the diff matrix between the enpointslice and the ss matrix
diff := commatrix.GenerateMatrixDiff(*mat, *ssMat)
diff, err := commatrix.GenerateMatrixDiff(*mat, *ssMat)
if err != nil {
panic(fmt.Sprintf("Error while generating matrix diff :%v", err))
}

// write the diff matrix between the enpointslice and the ss matrix to file
err = os.WriteFile(filepath.Join(destDir, "matrix-diff-ss"), []byte(diff), 0644)
if err != nil {
panic(fmt.Sprintf("Error writing the diff matrix :%v", err))
panic(fmt.Sprintf("Error writing the diff matrix file: %v", err))
}
}
33 changes: 30 additions & 3 deletions commatrix/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"strings"
"sync"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -164,14 +166,22 @@ func writeMatrixToFile(matrix types.ComMatrix, fileName, format string, printFn
return os.WriteFile(comMatrixFileName, res, 0644)
}

func GenerateMatrixDiff(mat1 types.ComMatrix, mat2 types.ComMatrix) string {
diff := consts.CSVHeaders + "\n"
// GenerateMatrixDiff generates the diff between mat1 to mat2.
func GenerateMatrixDiff(mat1, mat2 types.ComMatrix) (string, error) {
colNames, err := getComMatrixHeadersByFormat(types.FormatCSV)
if err != nil {
return "", fmt.Errorf("error getting commatrix CSV tags: %v", err)
}

diff := colNames + "\n"

for _, cd := range mat1.Matrix {
if mat2.Contains(cd) {
diff += fmt.Sprintf("%s\n", cd)
continue
}

// add "+" before cd's mat1 contains but mat2 doesn't
diff += fmt.Sprintf("+ %s\n", cd)
}

Expand All @@ -183,11 +193,28 @@ func GenerateMatrixDiff(mat1 types.ComMatrix, mat2 types.ComMatrix) string {
}

if !mat1.Contains(cd) {
// add "-" before cd's mat1 doesn't contain but mat2 does
diff += fmt.Sprintf("- %s\n", cd)
}
}

return diff
return diff, nil
}

func getComMatrixHeadersByFormat(format string) (string, error) {
typ := reflect.TypeOf(types.ComDetails{})

var tagsList []string
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get(format)
if tag == "" {
return "", fmt.Errorf("field %v has no tag of format %s", field, format)
}
tagsList = append(tagsList, tag)
}

return strings.Join(tagsList, ","), nil
}

func separateMatrixByRole(matrix types.ComMatrix) (types.ComMatrix, types.ComMatrix) {
Expand Down
1 change: 0 additions & 1 deletion consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ const (
RoleLabel = "node-role.kubernetes.io/"
DefaultDebugNamespace = "openshift-commatrix-debug"
DefaultDebugPodImage = "quay.io/openshift-release-dev/ocp-release:4.15.12-multi"
CSVHeaders = "Direction,Protocol,Port,Namespace,Service,Pod,Container,Node Role,Optional"
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Direction,Protocol,Port,Namespace,Service,Pod,Container,NodeRole,Optional
Direction,Protocol,Port,Namespace,Service,Pod,Container,Node Role,Optional
ingress,TCP,9050,example-namespace,example-service,example-pod,example-container,master,false
ingress,UDP,9051,example-namespace2,example-service2,example-pod2,example-container2,woker,false
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"direction": "ingress",
"protocol": "TCP",
"port": "9050",
"port": 9050,
"namespace": "example-namespace",
"service": "example-service",
"pod": "example-pod",
Expand All @@ -13,7 +13,7 @@
{
"direction": "ingress",
"protocol": "UDP",
"port": "9051",
"port": 9051,
"namespace": "example-namespace2",
"service": "example-service2",
"pod": "example-pod2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
nodeRole: master
optional: false
pod: example-pod
port: "9050"
port: 9050
protocol: TCP
service: example-service
- container: example-container2
Expand All @@ -13,6 +13,6 @@
nodeRole: worker
optional: false
pod: example-pod2
port: "9051"
port: 9051
protocol: UDP
service: example-service2
31 changes: 12 additions & 19 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"slices"
"strings"

"github.com/openshift-kni/commatrix/consts"
"github.com/gocarina/gocsv"
"sigs.k8s.io/yaml"
)

Expand All @@ -34,34 +34,27 @@ type ComMatrix struct {
}

type ComDetails struct {
Direction string `json:"direction" yaml:"direction"`
Protocol string `json:"protocol" yaml:"protocol"`
Port int `json:"port" yaml:"port"`
Namespace string `json:"namespace" yaml:"namespace"`
Service string `json:"service" yaml:"service"`
Pod string `json:"pod" yaml:"pod"`
Container string `json:"container" yaml:"container"`
NodeRole string `json:"nodeRole" yaml:"nodeRole"`
Optional bool `json:"optional" yaml:"optional"`
Direction string `json:"direction" yaml:"direction" csv:"Direction"`
Protocol string `json:"protocol" yaml:"protocol" csv:"Protocol"`
Port int `json:"port" yaml:"port" csv:"Port"`
Namespace string `json:"namespace" yaml:"namespace" csv:"Namespace"`
Service string `json:"service" yaml:"service" csv:"Service"`
Pod string `json:"pod" yaml:"pod" csv:"Pod"`
Container string `json:"container" yaml:"container" csv:"Container"`
NodeRole string `json:"nodeRole" yaml:"nodeRole" csv:"Node Role"`
Optional bool `json:"optional" yaml:"optional" csv:"Optional"`
}

func ToCSV(m ComMatrix) ([]byte, error) {
out := make([]byte, 0)
w := bytes.NewBuffer(out)
csvwriter := csv.NewWriter(w)

err := csvwriter.Write(strings.Split(consts.CSVHeaders, ","))
err := gocsv.MarshalCSV(&m.Matrix, csvwriter)
if err != nil {
return nil, fmt.Errorf("failed to write to CSV: %w", err)
return nil, err
}

for _, cd := range m.Matrix {
record := strings.Split(cd.String(), ",")
err := csvwriter.Write(record)
if err != nil {
return nil, fmt.Errorf("failed to convert to CSV foramt: %w", err)
}
}
csvwriter.Flush()

return w.Bytes(), nil
Expand Down

0 comments on commit 20db006

Please sign in to comment.