Skip to content

Commit

Permalink
feat: Make Action work without old summary path
Browse files Browse the repository at this point in the history
Signed-off-by: zhy76 <[email protected]>
  • Loading branch information
zhy76 committed Aug 23, 2023
1 parent 914315b commit 36785be
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
23 changes: 9 additions & 14 deletions cmd/visual/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ var networkCmd = &cobra.Command{
Short: "network subcommand is a command to visualization network connection behaviors differences.",
Example: "visual network --old [old json file name] --new [new json file name] -app [app name] -o [png file name]",
Run: func(cmd *cobra.Command, args []string) {
a := cmd.Flags().Changed("old")
a := cmd.Flags().Changed("new")
if a == false {
klog.Fatalf("Error: 'old' flag is not set")
}
b := cmd.Flags().Changed("new")
if b == false {
klog.Fatalf("Error: 'new' flag is not set")
}

fmt.Println("old file:", oldFile)
// Check is URL
var err error
if !utils.CheckIsURL(oldFile) {
oldFile, err = filepath.Abs(oldFile)
if err != nil {
klog.Fatalf("Error: getting absolute path of 'oldFile' flag: %v", err)
if oldFile != "" {
fmt.Println("old file:", oldFile)
// Check is URL
if !utils.CheckIsURL(oldFile) {
oldFile, err = filepath.Abs(oldFile)
if err != nil {
klog.Fatalf("Error: getting absolute path of 'oldFile' flag: %v", err)
}
}
}

Expand Down Expand Up @@ -63,9 +61,6 @@ func init() {
flags.StringVarP(&appName, "app", "", "", "filter app name, if you want to visualize specific app")
flags.StringVarP(&netOutput, "output", "o", "net.png", "output image file name")

if err := networkCmd.MarkPersistentFlagRequired("old"); err != nil {
klog.Fatalf("Error: marking 'old' flag as required: %v", err)
}
if err := networkCmd.MarkPersistentFlagRequired("new"); err != nil {
klog.Fatalf("Error: marking 'new' flag as required: %v", err)
}
Expand Down
48 changes: 42 additions & 6 deletions pkg/visualisation/visualisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kubearmor/kubearmor-action/utils"
exe "github.com/kubearmor/kubearmor-action/utils/exec"
osi "github.com/kubearmor/kubearmor-action/utils/os"
"github.com/sethvargo/go-githubactions"
"k8s.io/klog"
)

Expand Down Expand Up @@ -149,7 +150,7 @@ func ConvertVndToPlantUML(vnd *VisualNetworkData, appName string) error {
}
for _, ip := range ips {
color := "Lightblue"
if strings.Contains(ip, appName) {
if appName != "" && strings.Contains(ip, appName) {
color = "Orange"
}
_, err = file.WriteString(fmt.Sprintf("[%s] #%s\n", ip, color))
Expand Down Expand Up @@ -207,6 +208,38 @@ func ParseNetworkData(sdOlds, sdNews []*SummaryData, appName string) *VisualNetw

vn := &VisualNetworkData{}
vn.NsIps = make(map[string][]string)
// if sdOlds == nil || len(sdOlds) == 0
if sdOlds == nil || len(sdOlds) == 0 {
for _, sdNew := range sdNews {
// Get Namespace Labels
getNsIps(sdNew, nsipsNew)
// Get Different Network Connections
getDiffConnectionData(sdNew, cdsNew, appName)
}

// merge the connections
for k, v := range cdsNew {
// unchanged
if v.kind == 0 {
edge := fmt.Sprintf("[%s] -[#%s]-> [%s] : %s/%s\n", k.src, v.edgeColor, k.dst, k.protocol, k.port)
vn.Connections = append(vn.Connections, edge)
}
// add ips to the ips map, to filter nsips
ips[k.src] = true
ips[k.dst] = true
}

// filter nsips by ips
for ns, ipss := range nsipsNew {
for _, ip := range ipss {
if _, ok := ips[ip]; ok {
vn.NsIps[ns] = append(vn.NsIps[ns], ip)
}
}
}
return vn
}
// if sdOlds != nil && len(sdOlds) != 0
for _, sdOld := range sdOlds {
// Get Namespace Labels
getNsIps(sdOld, nsipsOld)
Expand Down Expand Up @@ -431,12 +464,15 @@ func ConvertNetworkJSONToImage(jsonFileOld string, jsonFileNew string, output st
}

// get old summary data from old json file
klog.Infoln("Parsing Old Summary Data...")
sdOlds := ParseSummaryData(jsonFileOld)
if sdOlds == nil {
return fmt.Errorf("Error: Old SummaryData is nil")
var sdOlds []*SummaryData
if jsonFileOld != "" {
klog.Infoln("Parsing Old Summary Data...")
sdOlds = ParseSummaryData(jsonFileOld)
// handle nil
if sdOlds == nil {
githubactions.Warningf("Warning: Old summary report file path is invalid!")
}
}

// get new summary data from new json file
klog.Infoln("Parsing New Summary Data...")
sdNews := ParseSummaryData(jsonFileNew)
Expand Down
8 changes: 8 additions & 0 deletions utils/urlfile/urlfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/url"
"time"

"k8s.io/klog"
)

// URLVisitor downloads the contents of a URL, and if successful, returns
Expand Down Expand Up @@ -82,6 +84,12 @@ func ReadJSONFromURL(url string) ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
// recover from panic if one occured.
if r := recover(); r != nil {
klog.Warningf("Recovered in ReadJSONFromURL: %v\n", r)
}
}()
defer body.Close()
data, err := io.ReadAll(body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func ReadFile(address string) ([]byte, error) {

// CheckIsURL checks if the given address is a url
func CheckIsURL(address string) bool {
// Check if the address is a valid url
_, err := url.ParseRequestURI(address)
if err != nil {
return false
}
// Parse the address as a url
u, err := url.Parse(address)
if err != nil {
Expand Down

0 comments on commit 36785be

Please sign in to comment.