Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: K8s tree error #1509

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/httpd/userwebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,16 @@ func (userCon *UserWebsocket) readMessageLoop() error {
logger.Debugf("Ws[%s] receive %s message", userCon.Uuid, msg.Type)
continue
case TerminalK8STree:
data := userCon.k8sClient.GetTreeData()
data, err := userCon.k8sClient.GetTreeData()
responseMsg := Message{
Id: userCon.Uuid,
Type: TerminalK8STree,
Data: data,
KubernetesId: msg.KubernetesId,
}
if err != nil {
responseMsg.Err = err.Error()
}

userCon.SendMessage(&responseMsg)
continue
Expand Down
29 changes: 17 additions & 12 deletions pkg/proxy/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,41 @@
getPodContainersLine = "kubectl get pods --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,POD:.metadata.name,CONTAINER:.spec.containers[*].name --no-headers"
)

func (kc *KubernetesClient) GetTreeData() string {
func (kc *KubernetesClient) GetTreeData() (string, error) {
env := append(os.Environ(), "KUBECONFIG="+kc.configName)

namespacesCmd := exec.Command("bash", "-c", getNamespacesLine)
namespacesCmd.Env = env
namespacesOutput, err := namespacesCmd.CombinedOutput()
if err != nil {
logger.Debugf("Error executing kubectl get namespaces: %v", err)
return "{}"
return "{}", err
}

namespaceLines := strings.Split(strings.TrimSpace(string(namespacesOutput)), "\n")

namespaces := make(map[string]*Namespace)
for _, nsName := range namespaceLines {
if strings.HasPrefix(nsName, "error:") {
nsName = strings.TrimPrefix(nsName, "error: ")
return "{}", fmt.Errorf(nsName)

Check failure on line 141 in pkg/proxy/k8s.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}

if nsName = strings.TrimSpace(nsName); nsName != "" {
namespaces[nsName] = &Namespace{Name: nsName, Type: "namespace"}
}
}

podsCmd := exec.Command("bash", "-c", getPodContainersLine)
podsCmd.Env = env
podsOutput, err := podsCmd.CombinedOutput()
if err != nil {
logger.Debugf("Error executing kubectl get pods: %v", err)
return "{}"
return "{}", err
}

podLines := strings.Split(strings.TrimSpace(string(podsOutput)), "\n")

namespaces := make(map[string]*Namespace)
for _, nsName := range namespaceLines {
if nsName = strings.TrimSpace(nsName); nsName != "" {
namespaces[nsName] = &Namespace{Name: nsName, Type: "namespace"}
}
}

for _, line := range podLines {
record := strings.Fields(line)
if len(record) < 3 {
Expand Down Expand Up @@ -189,10 +194,10 @@
jsonData, err := json.Marshal(namespaces)
if err != nil {
logger.Errorf("Error marshalling JSON: %v", err)
return "{}"
return "{}", err
}

return string(jsonData)
return string(jsonData), nil
}

func (kc *KubernetesClient) Close() {
Expand Down
Loading