diff --git a/docs/networkpolicy-recommendation.md b/docs/networkpolicy-recommendation.md index e641b5182..e191c847c 100644 --- a/docs/networkpolicy-recommendation.md +++ b/docs/networkpolicy-recommendation.md @@ -145,7 +145,7 @@ kubectl apply -f recommended_policies.yml The `theia policy-recommendation list` command lists all policy recommendation jobs. `CreateTime`, `CompleteTime`, `ID` and `Status` of each policy -recommendation job will be displayed in the form of a table. For example: +recommendation job will be displayed in table format. For example: ```bash > theia policy-recommendation list diff --git a/pkg/theia/commands/policy_recommendation_delete.go b/pkg/theia/commands/policy_recommendation_delete.go index 445365d2f..0b9241e2a 100644 --- a/pkg/theia/commands/policy_recommendation_delete.go +++ b/pkg/theia/commands/policy_recommendation_delete.go @@ -75,7 +75,7 @@ $ theia policy-recommendation delete e998433e-accb-4888-9fc8-06563f073e86 idMap, err := getPolicyRecommendationIdMap(clientset, kubeconfig, endpoint, useClusterIP) if err != nil { - return fmt.Errorf("err when get policy recommendation ID map, %v", err) + return fmt.Errorf("err when getting policy recommendation ID map, %v", err) } if _, ok := idMap[recoID]; !ok { @@ -125,41 +125,13 @@ func getPolicyRecommendationIdMap(clientset kubernetes.Interface, kubeconfig str } func deletePolicyRecommendationResult(clientset kubernetes.Interface, kubeconfig string, endpoint string, useClusterIP bool, recoID string) (err error) { - if endpoint == "" { - service := "clickhouse-clickhouse" - if useClusterIP { - serviceIP, servicePort, err := GetServiceAddr(clientset, service) - if err != nil { - return fmt.Errorf("error when getting the ClickHouse Service address: %v", err) - } - endpoint = fmt.Sprintf("tcp://%s:%d", serviceIP, servicePort) - } else { - listenAddress := "localhost" - listenPort := 9000 - _, servicePort, err := GetServiceAddr(clientset, service) - if err != nil { - return fmt.Errorf("error when getting the ClickHouse Service port: %v", err) - } - // Forward the ClickHouse service port - pf, err := StartPortForward(kubeconfig, service, servicePort, listenAddress, listenPort) - if err != nil { - return fmt.Errorf("error when forwarding port: %v", err) - } - defer pf.Stop() - endpoint = fmt.Sprintf("tcp://%s:%d", listenAddress, listenPort) - } + connect, portForward, err := setupClickHouseConnection(clientset, kubeconfig, endpoint, useClusterIP) + if portForward != nil { + defer portForward.Stop() } - - // Connect to ClickHouse and get the result - username, password, err := getClickHouseSecret(clientset) if err != nil { return err } - url := fmt.Sprintf("%s?debug=false&username=%s&password=%s", endpoint, username, password) - connect, err := connectClickHouse(clientset, url) - if err != nil { - return fmt.Errorf("error when connecting to ClickHouse, %v", err) - } query := "ALTER TABLE recommendations DELETE WHERE id = (?);" _, err = connect.Exec(query, recoID) if err != nil { diff --git a/pkg/theia/commands/policy_recommendation_list.go b/pkg/theia/commands/policy_recommendation_list.go index 3e73a9a20..36a3121d8 100644 --- a/pkg/theia/commands/policy_recommendation_list.go +++ b/pkg/theia/commands/policy_recommendation_list.go @@ -122,41 +122,13 @@ $ theia policy-recommendation list } func getCompletedPolicyRecommendationList(clientset kubernetes.Interface, kubeconfig string, endpoint string, useClusterIP bool) (completedPolicyRecommendationList []policyRecommendationRow, err error) { - if endpoint == "" { - service := "clickhouse-clickhouse" - if useClusterIP { - serviceIP, servicePort, err := GetServiceAddr(clientset, service) - if err != nil { - return completedPolicyRecommendationList, fmt.Errorf("error when getting the ClickHouse Service address: %v", err) - } - endpoint = fmt.Sprintf("tcp://%s:%d", serviceIP, servicePort) - } else { - listenAddress := "localhost" - listenPort := 9000 - _, servicePort, err := GetServiceAddr(clientset, service) - if err != nil { - return completedPolicyRecommendationList, fmt.Errorf("error when getting the ClickHouse Service port: %v", err) - } - // Forward the ClickHouse service port - pf, err := StartPortForward(kubeconfig, service, servicePort, listenAddress, listenPort) - if err != nil { - return completedPolicyRecommendationList, fmt.Errorf("error when forwarding port: %v", err) - } - defer pf.Stop() - endpoint = fmt.Sprintf("tcp://%s:%d", listenAddress, listenPort) - } + connect, portForward, err := setupClickHouseConnection(clientset, kubeconfig, endpoint, useClusterIP) + if portForward != nil { + defer portForward.Stop() } - - // Connect to ClickHouse and get the result - username, password, err := getClickHouseSecret(clientset) if err != nil { return completedPolicyRecommendationList, err } - url := fmt.Sprintf("%s?debug=false&username=%s&password=%s", endpoint, username, password) - connect, err := connectClickHouse(clientset, url) - if err != nil { - return completedPolicyRecommendationList, fmt.Errorf("error when connecting to ClickHouse, %v", err) - } query := "SELECT timeCreated, id FROM recommendations;" rows, err := connect.Query(query) if err != nil { @@ -167,7 +139,7 @@ func getCompletedPolicyRecommendationList(clientset kubernetes.Interface, kubeco var row policyRecommendationRow err := rows.Scan(&row.timeComplete, &row.id) if err != nil { - return completedPolicyRecommendationList, fmt.Errorf("err when scaning recommendations row %v", err) + return completedPolicyRecommendationList, fmt.Errorf("err when scanning recommendations row %v", err) } completedPolicyRecommendationList = append(completedPolicyRecommendationList, row) }