Skip to content

Commit

Permalink
Generate commatrix output files and fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sabinaaledort committed Apr 15, 2024
1 parent 063bcef commit c10c995
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 77 deletions.
19 changes: 19 additions & 0 deletions pkg/network/commatrix/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FORMAT ?= csv
CLUSTER_ENV ?= baremetal
DEST_DIR ?= .

GO_SRC := cmd/main.go

EXECUTABLE := commatrix

.DEFAULT_GOAL := run

build:
go build -o $(EXECUTABLE) $(GO_SRC)

generate: build
mkdir -p $(DEST_DIR)/communication-matrix
./$(EXECUTABLE) -format=$(FORMAT) -env=$(CLUSTER_ENV) -destDir=$(DEST_DIR)/communication-matrix

clean:
@rm -f $(EXECUTABLE)
17 changes: 0 additions & 17 deletions pkg/network/commatrix/cmd/Makefile

This file was deleted.

142 changes: 123 additions & 19 deletions pkg/network/commatrix/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,159 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"path"
"path/filepath"

"github.com/openshift/library-go/pkg/network/commatrix"
clientutil "github.com/openshift/library-go/pkg/network/commatrix/client"
"github.com/openshift/library-go/pkg/network/commatrix/ss"
"github.com/openshift/library-go/pkg/network/commatrix/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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

flag.StringVar(&output, "output", "CSV", "The desired output format (JSON,YAML,CSV)")
flag.StringVar(&envStr, "env", "baremetal", "The environment the cluster is on (baremetal/aws)")
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(&customEntriesPath, "customEntriesPath", "", "Add custom entries from a JSON file to the matrix")

flag.Parse()

switch format {
case "json":
printFn = types.ToJSON
case "csv":
printFn = types.ToCSV
case "yaml":
printFn = types.ToYAML
default:
panic(fmt.Sprintf("invalid format: %s. Please specify json, csv, or yaml.", format))
}

kubeconfig, ok := os.LookupEnv("KUBECONFIG")
if !ok {
panic("must set the KUBECONFIG environment variable")
}

env, exists := commatrix.EnvMap[envStr]
if !exists {
var env commatrix.Env
switch envStr {
case "baremetal":
env = commatrix.Baremetal
case "aws":
env = commatrix.AWS
default:
panic(fmt.Sprintf("invalid cluster environment: %s", envStr))
}

mat, err := commatrix.New(kubeconfig, "", env)
mat, err := commatrix.New(kubeconfig, customEntriesPath, env)
if err != nil {
panic(fmt.Sprintf("failed to create the communication matrix: %s", err))
}

switch output {
case "JSON":
printFn = mat.ToJSON
case "CSV":
printFn = mat.ToCSV
case "YAML":
printFn = mat.ToYAML
default:
panic(fmt.Sprintf("invalid output format: %s. Please specify JSON, CSV, or YAML.", output))
res, err := printFn(mat)
if err != nil {
panic(err)
}

comMatrixFileName := filepath.Join(destDir, fmt.Sprintf("communication-matrix.%s", format))
err = os.WriteFile(comMatrixFileName, []byte(string(res)), 0644)
if err != nil {
panic(err)
}

res, err := printFn()
cs, err := clientutil.New(kubeconfig)
if err != nil {
panic(err)
}

fmt.Println(string(res))
tcpFile, err := os.OpenFile(path.Join(destDir, "raw-ss-tcp"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer tcpFile.Close()

udpFile, err := os.OpenFile(path.Join(destDir, "raw-ss-udp"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer udpFile.Close()

nodesList, err := cs.Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err)
}

nodesComDetails := []types.ComDetails{}
for _, n := range nodesList.Items {
// TODO: can be improved with go routines
cds, err := ss.CreateComDetailsFromNode(cs, &n, tcpFile, udpFile)
if err != nil {
panic(err)
}

nodesComDetails = append(nodesComDetails, cds...)
}
cleanedComDetails := types.RemoveDups(nodesComDetails)
ssComMat := types.ComMatrix{Matrix: cleanedComDetails}

res, err = printFn(&ssComMat)
if err != nil {
panic(err)
}

ssMatrixFileName := filepath.Join(destDir, fmt.Sprintf("ss-generated-matrix.%s", format))
err = os.WriteFile(ssMatrixFileName, []byte(string(res)), 0644)
if err != nil {
panic(err)
}

diff := ""
for _, cd1 := range mat.Matrix {
found := false
for _, cd2 := range ssComMat.Matrix {
if cd1.Compare(cd2) {
found = true
break
}
}
if !found {
diff += fmt.Sprintf("+ %s\n", cd1)
continue
}
diff += fmt.Sprintf("%s\n", cd1)
}

for _, cd1 := range ssComMat.Matrix {
found := false
for _, cd2 := range mat.Matrix {
if cd1.Compare(cd2) {
found = true
break
}
}
if !found {
diff += fmt.Sprintf("- %s\n", cd1)
continue
}
diff += fmt.Sprintf("%s\n", cd1)
}

err = os.WriteFile(filepath.Join(destDir, "matrix-diff-ss"),
[]byte(diff),
0644)
if err != nil {
panic(err)
}
}
5 changes: 0 additions & 5 deletions pkg/network/commatrix/commatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ const (
AWS
)

var EnvMap = map[string]Env{
"baremetal": Baremetal,
"aws": AWS,
}

// New initializes a ComMatrix using Kubernetes cluster data.
// It takes kubeconfigPath for cluster access to fetch EndpointSlice objects,
// detailing open ports for ingress traffic.
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/commatrix/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ const (
OptionalTrue = "true"
RoleLabel = "node-role.kubernetes.io/"
DefaultDebugNamespace = "openshift-commatrix-debug"
DefaultDebugPodImage = "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:535ce24b5f1894d2a07bfa7eed7ad028ffde0659693f2a571ac4712a21cd028c"
// TODO: change the image
DefaultDebugPodImage = "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:535ce24b5f1894d2a07bfa7eed7ad028ffde0659693f2a571ac4712a21cd028c"
)
23 changes: 7 additions & 16 deletions pkg/network/commatrix/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ func waitPodPhase(cs *client.ClientSet, interval time.Duration, timeout time.Dur
}

func createPod(cs *client.ClientSet, node string, namespace string, image string) (*corev1.Pod, error) {
defaultDockerCfgServiceName, err := getSecret(cs, namespace, "default-dockercfg")
if err != nil {
return nil, err
}
podDef := getPodDefinition(node, namespace, defaultDockerCfgServiceName.Name, image)
podDef := getPodDefinition(node, namespace, image)
pod, err := cs.Pods(namespace).Create(context.TODO(), podDef, metav1.CreateOptions{})
if err != nil {
return nil, err
Expand All @@ -151,7 +147,7 @@ func createPod(cs *client.ClientSet, node string, namespace string, image string
return pod, nil
}

func getPodDefinition(node string, namespace string, secret string, image string) *corev1.Pod {
func getPodDefinition(node string, namespace string, image string) *corev1.Pod {
podName := fmt.Sprintf("%s-debug-", strings.ReplaceAll(node, ".", "-"))
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -196,16 +192,11 @@ func getPodDefinition(node string, namespace string, secret string, image string
},
},
},
DNSPolicy: corev1.DNSClusterFirst,
EnableServiceLinks: ptr.To[bool](true),
HostIPC: true,
HostNetwork: true,
HostPID: true,
ImagePullSecrets: []corev1.LocalObjectReference{
{
Name: secret,
},
},
DNSPolicy: corev1.DNSClusterFirst,
EnableServiceLinks: ptr.To[bool](true),
HostIPC: true,
HostNetwork: true,
HostPID: true,
NodeName: node,
PreemptionPolicy: ptr.To[corev1.PreemptionPolicy](corev1.PreemptLowerPriority),
Priority: ptr.To[int32](1000000000),
Expand Down
8 changes: 4 additions & 4 deletions pkg/network/commatrix/endpointslices/endpointslices.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

type EndpointSlicesInfo struct {
EndpointSlice discoveryv1.EndpointSlice
Serivce corev1.Service
Service corev1.Service
Pods []corev1.Pod
}

Expand Down Expand Up @@ -52,7 +52,7 @@ func GetIngressEndpointSlicesInfo(cs *client.ClientSet) ([]EndpointSlicesInfo, e
if err != nil {
return nil, fmt.Errorf("failed to bundle resources: %w", err)
}
log.Debug("length of the creaed epsliceInfos slice: ", len(epsliceInfos))
log.Debug("length of the created epsliceInfos slice: ", len(epsliceInfos))
res := FilterForIngressTraffic(epsliceInfos)

log.Debug("length of the slice after filter: ", len(res))
Expand Down Expand Up @@ -120,7 +120,7 @@ func createEPSliceInfos(epSlicesList *discoveryv1.EndpointSliceList, servicesLis
log.Debugf("Added a new endpointSliceInfo with pods len: %d", len(pods))
res = append(res, EndpointSlicesInfo{
EndpointSlice: epSlice,
Serivce: *service,
Service: *service,
Pods: pods,
})
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (epSliceinfo *EndpointSlicesInfo) toComDetails(nodes []corev1.Node) ([]type
res := make([]types.ComDetails, 0)

// Get the Namespace and Pod's name from the service.
namespace := epSliceinfo.Serivce.Namespace
namespace := epSliceinfo.Service.Namespace
name := epSliceinfo.EndpointSlice.OwnerReferences[0].Name

// Get the node roles of this endpointslice. (master or worker or both).
Expand Down
10 changes: 5 additions & 5 deletions pkg/network/commatrix/endpointslices/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func FilterForIngressTraffic(epslicesInfo []EndpointSlicesInfo) []EndpointSlices
// FilterHostNetwork checks if the pods behind the endpointSlice are host network.
func FilterHostNetwork(epInfo EndpointSlicesInfo) bool {
if len(epInfo.Pods) == 0 {
log.Debugf("EndpointSliceInfo %s, got no pods", epInfo.Serivce.Name)
log.Debugf("EndpointSliceInfo %s, got no pods", epInfo.Service.Name)
return false
}
// Assuming all pods in an EndpointSlice are uniformly on host network or not, we only check the first one.
if !epInfo.Pods[0].Spec.HostNetwork {
log.Debugf("EndpointSliceInfo %s, is not hostNetwork", epInfo.Serivce.Name)
log.Debugf("EndpointSliceInfo %s, is not hostNetwork", epInfo.Service.Name)
return false
}

Expand All @@ -59,9 +59,9 @@ func FilterHostNetwork(epInfo EndpointSlicesInfo) bool {

// FilterServiceTypes checks if the service behind the endpointSlice is of type LoadBalancer or NodePort.
func FilterServiceTypes(epInfo EndpointSlicesInfo) bool {
if epInfo.Serivce.Spec.Type != corev1.ServiceTypeLoadBalancer &&
epInfo.Serivce.Spec.Type != corev1.ServiceTypeNodePort {
log.Debugf("EndpointSliceInfo %s, is not Loadbalancer not NodePort ", epInfo.Serivce.Name)
if epInfo.Service.Spec.Type != corev1.ServiceTypeLoadBalancer &&
epInfo.Service.Spec.Type != corev1.ServiceTypeNodePort {
log.Debugf("EndpointSliceInfo %s, is not Loadbalancer not NodePort ", epInfo.Service.Name)
return false
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/network/commatrix/endpointslices/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestFilterHostNetwork(t *testing.T) {
epInfo := EndpointSlicesInfo{
EndpointSlice: discoveryv1.EndpointSlice{},
Pods: []corev1.Pod{{}},
Serivce: corev1.Service{},
Service: corev1.Service{},
}
tests := []struct {
desc string
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestFilterServiceTypes(t *testing.T) {
epInfo := EndpointSlicesInfo{
EndpointSlice: discoveryv1.EndpointSlice{},
Pods: []corev1.Pod{},
Serivce: corev1.Service{},
Service: corev1.Service{},
}
tests := []struct {
desc string
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestFilterServiceTypes(t *testing.T) {
},
}
for _, test := range tests {
epInfo.Serivce.Spec.Type = test.serviceType
epInfo.Service.Spec.Type = test.serviceType
res := FilterServiceTypes(epInfo)
if res != test.expected {
t.Fatalf("test %s failed. expected %v got %v", test.desc, test.expected, res)
Expand Down
Loading

0 comments on commit c10c995

Please sign in to comment.