Skip to content

Commit

Permalink
Allow setting custom-entries-file in formats
Browse files Browse the repository at this point in the history
This commit adds the the flag customEntriesFormat that sets the custom
entries file format (json/yaml/csv).

We also include 3 example files to help the user to create his own custom
file, and they'll be used in the integration test.

Signed-off-by: Lior Noy <[email protected]>
  • Loading branch information
liornoy committed Jun 16, 2024
1 parent d97ed0f commit ec1657b
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endif
generate: oc build
rm -rf $(DEST_DIR)/communication-matrix
mkdir -p $(DEST_DIR)/communication-matrix
./$(EXECUTABLE) -format=$(FORMAT) -env=$(CLUSTER_ENV) -destDir=$(DEST_DIR)/communication-matrix -deployment=$(DEPLOYMENT)
./$(EXECUTABLE) -format=$(FORMAT) -env=$(CLUSTER_ENV) -destDir=$(DEST_DIR)/communication-matrix -deployment=$(DEPLOYMENT) -customEntriesPath=$(CUSTOM_ENTRIES_PATH) -customEntriesFormat=$(CUSTOM_ENTRIES_FORMAT)

clean:
@rm -f $(EXECUTABLE)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ the `ss` command on each node, and converts the output into a corresponding ComD
### Communication Matrix Creation Guide

Use the `generate` Makefile target to create the matrix.
Add additional entires to the matrix via a custom file, using
the variables `CUSTOM_ENTRIES_PATH` and `CUSTOM_ENTRIES_FORMAT`.
Examples are available in the `example-custom-entries` files.

The following environment variables are used to configure:
```
FORMAT (csv/json/yaml)
CLUSTER_ENV (baremetal/aws)
DEST_DIR (path to the directory containing the artifacts)
DEPLOYMENT (mno/sno)
CUSTOM_ENTRIES_PATH (path to the file containing custom entries to add to the matrix)
CUSTOM_ENTRIES_FORMAT (the format of the custom entries file (json,yaml,csv))
```

The generated artifcats are:
Expand Down
22 changes: 14 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ import (

func main() {
var (
destDir string
format string
envStr string
deploymentStr string
customEntriesPath string
printFn func(m types.ComMatrix) ([]byte, error)
destDir string
format string
envStr string
deploymentStr string
customEntriesPath string
customEntriesFormat string
printFn func(m types.ComMatrix) ([]byte, error)
)

flag.StringVar(&destDir, "destDir", "communication-matrix", "Output files dir")
flag.StringVar(&format, "format", "csv", "Desired format (json,yaml,csv)")
flag.StringVar(&envStr, "env", "baremetal", "Cluster environment (baremetal/aws)")
flag.StringVar(&deploymentStr, "deployment", "mno", "Deployment type (mno/sno)")
flag.StringVar(&customEntriesPath, "customEntriesPath", "", "Add custom entries from a JSON file to the matrix")
flag.StringVar(&customEntriesPath, "customEntriesPath", "", "Add custom entries from a file to the matrix")
flag.StringVar(&customEntriesFormat, "customEntriesFormat", "", "Set the format of the custom entries file (json,yaml,csv)")

flag.Parse()

Expand Down Expand Up @@ -74,7 +76,11 @@ func main() {
panic(fmt.Sprintf("invalid deployment type: %s", deploymentStr))
}

mat, err := commatrix.New(kubeconfig, customEntriesPath, env, deployment)
if customEntriesPath != "" && customEntriesFormat == "" {
panic("error, variable customEntriesFormat is not set")
}

mat, err := commatrix.New(kubeconfig, customEntriesPath, customEntriesFormat, env, deployment)
if err != nil {
panic(fmt.Sprintf("failed to create the communication matrix: %s", err))
}
Expand Down
52 changes: 43 additions & 9 deletions commatrix/commatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"path/filepath"

"github.com/gocarina/gocsv"
"sigs.k8s.io/yaml"

"github.com/openshift-kni/commatrix/client"
"github.com/openshift-kni/commatrix/endpointslices"

Check failure on line 14 in commatrix/commatrix.go

View workflow job for this annotation

GitHub Actions / unit-test

could not import github.com/openshift-kni/commatrix/endpointslices (-: # github.com/openshift-kni/commatrix/endpointslices
"github.com/openshift-kni/commatrix/types"
Expand Down Expand Up @@ -34,7 +37,7 @@ const (
// Custom entries from a JSON file can be added to the matrix by setting `customEntriesPath`.
// Returns a pointer to ComMatrix and error. Entries include traffic direction, protocol,
// port number, namespace, service name, pod, container, node role, and flow optionality for OpenShift.
func New(kubeconfigPath string, customEntriesPath string, e Env, d Deployment) (*types.ComMatrix, error) {
func New(kubeconfigPath string, customEntriesPath string, customEntriesFormat string, e Env, d Deployment) (*types.ComMatrix, error) {
res := make([]types.ComDetails, 0)

cs, err := client.New(kubeconfigPath)
Expand All @@ -55,15 +58,19 @@ func New(kubeconfigPath string, customEntriesPath string, e Env, d Deployment) (

staticEntries, err := getStaticEntries(e, d)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed adding static entries: %s", err)
}

res = append(res, staticEntries...)

if customEntriesPath != "" {
customComDetails, err := addFromFile(customEntriesPath)
inputFormat, err := parseFormat(customEntriesFormat)
if err != nil {
return nil, fmt.Errorf("failed fetching custom entries from file %s err: %w", customEntriesPath, err)
return nil, fmt.Errorf("failed adding custom entries: %s", err)
}
customComDetails, err := addFromFile(customEntriesPath, inputFormat)
if err != nil {
return nil, fmt.Errorf("failed adding custom entries: %s", err)
}

res = append(res, customComDetails...)
Expand All @@ -74,7 +81,7 @@ func New(kubeconfigPath string, customEntriesPath string, e Env, d Deployment) (
return &types.ComMatrix{Matrix: cleanedComDetails}, nil
}

func addFromFile(fp string) ([]types.ComDetails, error) {
func addFromFile(fp string, format types.Format) ([]types.ComDetails, error) {
var res []types.ComDetails
f, err := os.Open(filepath.Clean(fp))
if err != nil {
Expand All @@ -85,10 +92,24 @@ func addFromFile(fp string) ([]types.ComDetails, error) {
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %v", fp, err)
}

err = json.Unmarshal(raw, &res)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal custom entries file: %v", err)
switch format {
case types.JSON:
err = json.Unmarshal(raw, &res)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal custom entries file: %v", err)
}
case types.YAML:
err = yaml.Unmarshal(raw, &res)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal custom entries file: %v", err)
}
case types.CSV:
err = gocsv.UnmarshalBytes(raw, &res)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal custom entries file: %v", err)
}
default:
return nil, fmt.Errorf("invalid value for format must be (json,yaml,csv)")
}

return res, nil
Expand Down Expand Up @@ -124,3 +145,16 @@ func getStaticEntries(e Env, d Deployment) ([]types.ComDetails, error) {

return comDetails, nil
}

func parseFormat(format string) (types.Format, error) {
switch format {
case types.FormatJSON:
return types.JSON, nil
case types.FormatYAML:
return types.YAML, nil
case types.FormatCSV:
return types.CSV, nil
}

return types.FormatErr, fmt.Errorf("failed to parse format: %s. options are: (json/yaml/csv)", format)
}
3 changes: 3 additions & 0 deletions commatrix/example-custom-entries.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Direction,Protocol,Port,Namespace,Service,Pod,Container,NodeRole,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
24 changes: 24 additions & 0 deletions commatrix/example-custom-entries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"direction": "ingress",
"protocol": "TCP",
"port": "9050",
"namespace": "example-namespace",
"service": "example-service",
"pod": "example-pod",
"container": "example-container",
"nodeRole": "master",
"optional": false
},
{
"direction": "ingress",
"protocol": "UDP",
"port": "9051",
"namespace": "example-namespace2",
"service": "example-service2",
"pod": "example-pod2",
"container": "example-container2",
"nodeRole": "worker",
"optional": false
}
]
18 changes: 18 additions & 0 deletions commatrix/example-custom-entries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- container: example-container
direction: ingress
namespace: example-namespace
nodeRole: master
optional: false
pod: example-pod
port: "9050"
protocol: TCP
service: example-service
- container: example-container2
direction: ingress
namespace: example-namespace2
nodeRole: worker
optional: false
pod: example-pod2
port: "9051"
protocol: UDP
service: example-service2
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/openshift-kni/commatrix
go 1.21

require (
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a
github.com/openshift/client-go v0.0.0-20240415214935-be70f772f157
github.com/sirupsen/logrus v1.9.3
k8s.io/api v0.29.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPDJseKLEN4BapJ42uSi9SZBQ2YyiA=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
35 changes: 25 additions & 10 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,35 @@ import (
"sigs.k8s.io/yaml"
)

type Format int

const (
FormatErr = -1
JSON Format = iota
YAML
CSV
)

const (
FormatJSON = "json"
FormatYAML = "yaml"
FormatCSV = "csv"
)

type ComMatrix struct {
Matrix []ComDetails
}

type ComDetails struct {
Direction string `json:"direction"`
Protocol string `json:"protocol"`
Port int `json:"port"`
Namespace string `json:"namespace"`
Service string `json:"service"`
Pod string `json:"pod"`
Container string `json:"container"`
NodeRole string `json:"nodeRole"`
Optional bool `json:"optional"`
Direction string `json:"direction" yaml:"direction"`
Protocol string `json:"protocol" yaml:"protocol"`
Port string `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"`
}

func ToCSV(m ComMatrix) ([]byte, error) {
Expand All @@ -52,7 +67,7 @@ func ToCSV(m ComMatrix) ([]byte, error) {
}

func ToJSON(m ComMatrix) ([]byte, error) {
out, err := json.Marshal(m.Matrix)
out, err := json.MarshalIndent(m.Matrix, "", " ")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ec1657b

Please sign in to comment.