Skip to content

Commit

Permalink
Merge pull request #141 from pehlicd/feat-add-port-flag
Browse files Browse the repository at this point in the history
feat: add port flag to dash command
  • Loading branch information
deggja committed May 12, 2024
2 parents 630d9e2 + 504ad3f commit 4cb07a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ Launch the dashboard:
netfetch dash
```

You may also specify a port for the dashboard to run on (default is 8080).

```sh
netfetch dash --port 8081
```

While in the dashboard, you have a couple of options.

You can use the `Scan cluster` button, which is the equivalent to the CLI `netfetch scan` command. This will populate the table view with all pods not targeted by a network policy.
Expand Down
11 changes: 6 additions & 5 deletions backend/cmd/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ var dashCmd = &cobra.Command{
Use: "dash",
Short: "Launch the Netfetch interactive dashboard",
Run: func(cmd *cobra.Command, args []string) {
startDashboardServer()
port, _ := cmd.Flags().GetString("port")
startDashboardServer(port)
},
}

func init() {
rootCmd.AddCommand(dashCmd)

dashCmd.Flags().StringP("port", "p", "8080", "Port for the interactive dashboard")
}

func setNoCacheHeaders(w http.ResponseWriter) {
Expand All @@ -36,7 +39,7 @@ func setNoCacheHeaders(w http.ResponseWriter) {
w.Header().Set("Expires", "0")
}

func startDashboardServer() {
func startDashboardServer(port string) {
// Verify connection to cluster or throw error
clientset, err := k8s.GetClientset()
if err != nil {
Expand All @@ -54,7 +57,7 @@ func startDashboardServer() {
AllowOriginRequestFunc: func(r *http.Request, origin string) bool {
// Implement your dynamic origin check here
host := r.Host // Extract the host from the request
allowedOrigins := []string{"http://localhost:8081", "https://" + host}
allowedOrigins := []string{"http://localhost:" + port, "https://" + host}
for _, allowedOrigin := range allowedOrigins {
if origin == allowedOrigin {
return true
Expand Down Expand Up @@ -84,7 +87,6 @@ func startDashboardServer() {
handler := c.Handler(http.DefaultServeMux)

// Start the server
port := "8080"
serverURL := fmt.Sprintf("http://localhost:%s", port)
startupMessage := HeaderStyle.Render(fmt.Sprintf("Starting dashboard server on %s", serverURL))
fmt.Println(startupMessage)
Expand Down Expand Up @@ -194,7 +196,6 @@ func handleClusterVisualizationRequest(w http.ResponseWriter, r *http.Request) {
return
}


clientset, err := k8s.GetClientset()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 4cb07a3

Please sign in to comment.