-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add list and delete commands in policy recommendation CLI #56
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,149 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
sparkv1 "antrea.io/theia/third_party/sparkoperator/v1beta2" | ||
) | ||
|
||
// policyRecommendationDeleteCmd represents the policy-recommendation delete command | ||
var policyRecommendationDeleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete a policy recommendation Spark job", | ||
Long: `Delete a policy recommendation Spark job by ID.`, | ||
Aliases: []string{"del"}, | ||
Args: cobra.RangeArgs(0, 1), | ||
Example: ` | ||
Delete the policy recommendation job with ID e998433e-accb-4888-9fc8-06563f073e86 | ||
$ theia policy-recommendation delete e998433e-accb-4888-9fc8-06563f073e86 | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
recoID, err := cmd.Flags().GetString("id") | ||
if err != nil { | ||
return err | ||
} | ||
if recoID == "" && len(args) == 1 { | ||
recoID = args[0] | ||
} | ||
err = ParseRecommendationID(recoID) | ||
if err != nil { | ||
return err | ||
} | ||
kubeconfig, err := ResolveKubeConfig(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
endpoint, err := cmd.Flags().GetString("clickhouse-endpoint") | ||
if err != nil { | ||
return err | ||
} | ||
if endpoint != "" { | ||
err = ParseEndpoint(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
useClusterIP, err := cmd.Flags().GetBool("use-cluster-ip") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientset, err := CreateK8sClient(kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("couldn't create k8s client using given kubeconfig, %v", err) | ||
} | ||
|
||
idMap, err := getPolicyRecommendationIdMap(clientset, kubeconfig, endpoint, useClusterIP) | ||
if err != nil { | ||
return fmt.Errorf("err when getting policy recommendation ID map, %v", err) | ||
} | ||
|
||
if _, ok := idMap[recoID]; !ok { | ||
return fmt.Errorf("could not find the policy recommendation job with given ID") | ||
} | ||
|
||
clientset.CoreV1().RESTClient().Delete(). | ||
AbsPath("/apis/sparkoperator.k8s.io/v1beta2"). | ||
Namespace(flowVisibilityNS). | ||
Resource("sparkapplications"). | ||
Name("pr-" + recoID). | ||
Do(context.TODO()) | ||
|
||
err = deletePolicyRecommendationResult(clientset, kubeconfig, endpoint, useClusterIP, recoID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Successfully deleted policy recommendation job with ID %s\n", recoID) | ||
return nil | ||
}, | ||
} | ||
|
||
func getPolicyRecommendationIdMap(clientset kubernetes.Interface, kubeconfig string, endpoint string, useClusterIP bool) (idMap map[string]bool, err error) { | ||
idMap = make(map[string]bool) | ||
sparkApplicationList := &sparkv1.SparkApplicationList{} | ||
err = clientset.CoreV1().RESTClient().Get(). | ||
AbsPath("/apis/sparkoperator.k8s.io/v1beta2"). | ||
Namespace(flowVisibilityNS). | ||
Resource("sparkapplications"). | ||
Do(context.TODO()).Into(sparkApplicationList) | ||
if err != nil { | ||
return idMap, err | ||
} | ||
for _, sparkApplication := range sparkApplicationList.Items { | ||
id := sparkApplication.ObjectMeta.Name[3:] | ||
idMap[id] = true | ||
} | ||
completedPolicyRecommendationList, err := getCompletedPolicyRecommendationList(clientset, kubeconfig, endpoint, useClusterIP) | ||
if err != nil { | ||
return idMap, err | ||
} | ||
for _, completedPolicyRecommendation := range completedPolicyRecommendationList { | ||
idMap[completedPolicyRecommendation.id] = true | ||
} | ||
return idMap, nil | ||
} | ||
|
||
func deletePolicyRecommendationResult(clientset kubernetes.Interface, kubeconfig string, endpoint string, useClusterIP bool, recoID string) (err error) { | ||
connect, portForward, err := setupClickHouseConnection(clientset, kubeconfig, endpoint, useClusterIP) | ||
if portForward != nil { | ||
defer portForward.Stop() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
query := "ALTER TABLE recommendations DELETE WHERE id = (?);" | ||
_, err = connect.Exec(query, recoID) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete recommendation result with id %s: %v", recoID, err) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
policyRecommendationCmd.AddCommand(policyRecommendationDeleteCmd) | ||
policyRecommendationDeleteCmd.Flags().StringP( | ||
"id", | ||
"i", | ||
"", | ||
"ID of the policy recommendation Spark job.", | ||
) | ||
} |
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,150 @@ | ||
// Copyright 2022 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
sparkv1 "antrea.io/theia/third_party/sparkoperator/v1beta2" | ||
) | ||
|
||
type policyRecommendationRow struct { | ||
timeComplete time.Time | ||
id string | ||
} | ||
|
||
// policyRecommendationListCmd represents the policy-recommendation list command | ||
var policyRecommendationListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List all policy recommendation Spark jobs", | ||
Long: `List all policy recommendation Spark jobs with name, creation time and status.`, | ||
Aliases: []string{"ls"}, | ||
Example: ` | ||
List all policy recommendation Spark jobs | ||
$ theia policy-recommendation list | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
kubeconfig, err := ResolveKubeConfig(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
clientset, err := CreateK8sClient(kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("couldn't create k8s client using given kubeconfig, %v", err) | ||
} | ||
endpoint, err := cmd.Flags().GetString("clickhouse-endpoint") | ||
if err != nil { | ||
return err | ||
} | ||
if endpoint != "" { | ||
err = ParseEndpoint(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
useClusterIP, err := cmd.Flags().GetBool("use-cluster-ip") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = PolicyRecoPreCheck(clientset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sparkApplicationList := &sparkv1.SparkApplicationList{} | ||
err = clientset.CoreV1().RESTClient().Get(). | ||
AbsPath("/apis/sparkoperator.k8s.io/v1beta2"). | ||
Namespace(flowVisibilityNS). | ||
Resource("sparkapplications"). | ||
Do(context.TODO()).Into(sparkApplicationList) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
completedPolicyRecommendationList, err := getCompletedPolicyRecommendationList(clientset, kubeconfig, endpoint, useClusterIP) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
sparkApplicationTable := [][]string{ | ||
{"CreationTime", "CompletionTime", "ID", "Status"}, | ||
} | ||
idMap := make(map[string]bool) | ||
for _, sparkApplication := range sparkApplicationList.Items { | ||
id := sparkApplication.ObjectMeta.Name[3:] | ||
idMap[id] = true | ||
sparkApplicationTable = append(sparkApplicationTable, | ||
[]string{ | ||
FormatTimestamp(sparkApplication.ObjectMeta.CreationTimestamp.Time), | ||
FormatTimestamp(sparkApplication.Status.TerminationTime.Time), | ||
id, | ||
strings.TrimSpace(string(sparkApplication.Status.AppState.State)), | ||
}) | ||
} | ||
|
||
for _, completedPolicyRecommendation := range completedPolicyRecommendationList { | ||
if _, ok := idMap[completedPolicyRecommendation.id]; !ok { | ||
idMap[completedPolicyRecommendation.id] = true | ||
sparkApplicationTable = append(sparkApplicationTable, | ||
[]string{ | ||
"N/A", | ||
FormatTimestamp(completedPolicyRecommendation.timeComplete), | ||
completedPolicyRecommendation.id, | ||
"COMPLETED", | ||
}) | ||
} | ||
} | ||
|
||
TableOutput(sparkApplicationTable) | ||
return nil | ||
}, | ||
} | ||
|
||
func getCompletedPolicyRecommendationList(clientset kubernetes.Interface, kubeconfig string, endpoint string, useClusterIP bool) (completedPolicyRecommendationList []policyRecommendationRow, err error) { | ||
connect, portForward, err := setupClickHouseConnection(clientset, kubeconfig, endpoint, useClusterIP) | ||
if portForward != nil { | ||
defer portForward.Stop() | ||
} | ||
if err != nil { | ||
return completedPolicyRecommendationList, err | ||
} | ||
query := "SELECT timeCreated, id FROM recommendations;" | ||
rows, err := connect.Query(query) | ||
if err != nil { | ||
return completedPolicyRecommendationList, fmt.Errorf("failed to get recommendation jobs: %v", err) | ||
} | ||
defer rows.Close() | ||
for rows.Next() { | ||
var row policyRecommendationRow | ||
err := rows.Scan(&row.timeComplete, &row.id) | ||
if err != nil { | ||
return completedPolicyRecommendationList, fmt.Errorf("err when scanning recommendations row %v", err) | ||
} | ||
completedPolicyRecommendationList = append(completedPolicyRecommendationList, row) | ||
} | ||
return completedPolicyRecommendationList, nil | ||
} | ||
|
||
func init() { | ||
policyRecommendationCmd.AddCommand(policyRecommendationListCmd) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a clickhouse peculiarity that we need to use ALTER TABLE for deleting records?
WHERE not work for Clickhouse?Does DELETE FROM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Clickhouse doesn't have Update/Delete commands like Mysql database, reference doc: https://clickhouse.com/docs/en/sql-reference/statements/alter/delete/