-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22a936b
commit 697fa74
Showing
2,443 changed files
with
756,153 additions
and
3,923 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func GetResources(kubeClient kubernetes.Interface, resourceType string) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
params := r.URL.Query() | ||
|
||
rt := resourceType | ||
if len(rt) == 0 { | ||
rt = params.Get("resourceType") | ||
} | ||
|
||
// TODO: remove all logs | ||
hlog.Infof("GetResources resourceType : %s query params : %s\n", rt, params) | ||
|
||
result := []string{} | ||
switch rt { | ||
case "namespaces": | ||
// list all namespaces | ||
namespaceList, err := kubeClient.CoreV1().Namespaces().List(r.Context(), metav1.ListOptions{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("found %d namespaces\n", len(namespaceList.Items)) | ||
for i := range namespaceList.Items { | ||
result = append(result, namespaceList.Items[i].Name) | ||
} | ||
case "pods": | ||
namespace := params.Get("namespace") | ||
|
||
if len(namespace) == 0 { | ||
writeError(w, http.StatusServiceUnavailable, "namespace cannot be empty") | ||
return | ||
} | ||
|
||
// list all Pods in namespace | ||
podList, err := kubeClient.CoreV1().Pods(namespace).List(r.Context(), metav1.ListOptions{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("found %d pods in namespace %s\n", len(podList.Items), namespace) | ||
for i := range podList.Items { | ||
result = append(result, podList.Items[i].Name) | ||
} | ||
default: | ||
writeError(w, http.StatusServiceUnavailable, | ||
fmt.Sprintf("unknown resourceType: %s", rt)) | ||
return | ||
} | ||
|
||
resp, err := json.Marshal(result) | ||
if err != nil { | ||
writeError(w, http.StatusServiceUnavailable, | ||
fmt.Sprintf("cannot marshal %v", params)) | ||
} else { | ||
writeRawJSON(w, http.StatusOK, resp) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/netobserv/network-observability-console-plugin/pkg/model" | ||
) | ||
|
||
func writeRawJSON(w http.ResponseWriter, code int, payload []byte) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
_, err := w.Write(payload) | ||
if err != nil { | ||
hlog.Errorf("Error while responding raw JSON: %v", err) | ||
} | ||
} | ||
|
||
func writeCSV(w http.ResponseWriter, code int, payload []byte, columns []string) { | ||
var qr model.QueryResponse | ||
err := json.Unmarshal(payload, &qr) | ||
if err != nil { | ||
writeError(w, http.StatusServiceUnavailable, fmt.Sprintf("Unknown error from Loki - cannot unmarshal (code: %d resp: %s)", code, payload)) | ||
return | ||
} | ||
|
||
datas, err := getCSVDatas(&qr, columns) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
t := time.Now() | ||
//output file would be 'export-stdLongYear-stdZeroMonth-stdZeroDay-stdHour-stdZeroMinute.csv' | ||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=export-%s.csv", t.Format("2006-01-02-15-04"))) | ||
w.Header().Set("Content-Type", "text/csv") | ||
w.Header().Set("Transfer-Encoding", "chunked") | ||
w.WriteHeader(code) | ||
writer := csv.NewWriter(w) | ||
for _, row := range datas { | ||
//write csv row | ||
err := writer.Write(row) | ||
if err != nil { | ||
writeError(w, http.StatusInternalServerError, fmt.Sprintf("Cannot write row %s", row)) | ||
return | ||
} | ||
} | ||
writer.Flush() | ||
} | ||
|
||
type errorResponse struct{ Message string } | ||
|
||
func writeError(w http.ResponseWriter, code int, message string) { | ||
response, err := json.Marshal(errorResponse{Message: message}) | ||
if err != nil { | ||
hlog.Errorf("Marshalling error while responding an error: %v (message was: %s)", err, message) | ||
code = http.StatusInternalServerError | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
_, err = w.Write(response) | ||
if err != nil { | ||
hlog.Errorf("Error while responding an error: %v (message was: %s)", err, message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.