-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Theiactl and policy recommendation CLI
Signed-off-by: Yongming Ding <dyongming@vmware.com>
- Loading branch information
Yongming Ding
committed
May 20, 2022
1 parent
0cf17c5
commit 3fdbccd
Showing
13 changed files
with
2,554 additions
and
3 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,89 @@ | ||
// 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" | ||
|
||
"github.com/google/uuid" | ||
"github.com/spf13/cobra" | ||
|
||
sparkv1 "antrea.io/theia/third_party/sparkoperator/v1beta2" | ||
) | ||
|
||
// checkCmd represents the check command | ||
var checkCmd = &cobra.Command{ | ||
Use: "check", | ||
Short: "Check the status of a policy recommendation Spark job", | ||
Long: `Check the current status of a policy recommendation Spark job by ID. | ||
It will return the status of this Spark application like SUBMITTED, RUNNING, COMPLETED, or FAILED.`, | ||
Example: ` | ||
Check the current status of job with ID e998433e-accb-4888-9fc8-06563f073e86 | ||
$ theiactl policyreco check --id e998433e-accb-4888-9fc8-06563f073e86 | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
recoID, err := cmd.Flags().GetString("id") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = uuid.Parse(recoID) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode input id %s into a UUID, err: %v", recoID, err) | ||
} | ||
|
||
kubeconfig, err := cmd.Flags().GetString("kubeconfig") | ||
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) | ||
} | ||
|
||
err = PolicyRecoPreCheck(clientset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sparkApplication := &sparkv1.SparkApplication{} | ||
err = clientset.CoreV1().RESTClient(). | ||
Get(). | ||
AbsPath("/apis/sparkoperator.k8s.io/v1beta2"). | ||
Namespace(flowVisibilityNS). | ||
Resource("sparkapplications"). | ||
Name("policy-reco-" + recoID). | ||
Do(context.TODO()). | ||
Into(sparkApplication) | ||
if err != nil { | ||
return err | ||
} | ||
state := strings.TrimSpace(string(sparkApplication.Status.AppState.State)) | ||
fmt.Printf("Status of this policy recommendation job is %s\n", state) | ||
return nil | ||
// TODO: add implementation of checking work progress through Spark Monitoring Service after port forwarder finished | ||
}, | ||
} | ||
|
||
func init() { | ||
policyrecoCmd.AddCommand(checkCmd) | ||
checkCmd.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,42 @@ | ||
// 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 ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// policyrecoCmd represents the policyreco command group | ||
var policyrecoCmd = &cobra.Command{ | ||
Use: "policyreco", | ||
Short: "Commands of Theia policy recommendation feature", | ||
Long: `Command group of Theia policy recommendation feature. | ||
Must specify a subcommand like start, check or result.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("Error: must also specify a subcommand like start, check or result") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(policyrecoCmd) | ||
rootCmd.PersistentFlags().StringP( | ||
"kubeconfig", | ||
"k", | ||
"~/.kube/config", | ||
"path to the k8s config file", | ||
) | ||
} |
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,41 @@ | ||
// 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 ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// resultCmd represents the result command | ||
var resultCmd = &cobra.Command{ | ||
Use: "result", | ||
Short: "Get the recommendation result of a policy recommendation Spark job", | ||
Long: `Get the recommendation result of a policy recommendation Spark job by ID. | ||
It will return the recommended network policies described in yaml.`, | ||
Example: ` | ||
Get the recommendation result with job ID e998433e-accb-4888-9fc8-06563f073e86 | ||
$ theiactl policyreco result --id e998433e-accb-4888-9fc8-06563f073e86 | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("result called") | ||
// TODO: add implementation of 'theiactl policyreco result' command here | ||
}, | ||
} | ||
|
||
func init() { | ||
policyrecoCmd.AddCommand(resultCmd) | ||
} |
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,41 @@ | ||
// 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 ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "theiactl", | ||
Short: "theiactl is the command line tool for Theia", | ||
Long: `theiactl is the command line tool for Theia that supports the | ||
policy recommendation feature`, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
} |
Oops, something went wrong.