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

Implementation of theia and policy recommendation CLI #25

Merged
merged 2 commits into from
Jun 7, 2022

Conversation

dreamtalen
Copy link

@dreamtalen dreamtalen commented May 20, 2022

In this commit, we add the implementation of theia, the command-line
tool for Theia based on cobra framework.
Policy recommendation CLI is also included. It consists of 3 commands
which let users could run a new policy recommendation job, check the
current status of a previous job, and retrieve the recommendation result of a
previous job respectively.

@dreamtalen dreamtalen marked this pull request as draft May 20, 2022 22:45
@dreamtalen dreamtalen changed the title Implementation of Theia CLI framework and policyreco start command Implementation of Theiactl and policy recommendation commands May 20, 2022
@dreamtalen dreamtalen changed the title Implementation of Theiactl and policy recommendation commands Implementation of Theiactl and policy recommendation CLI May 20, 2022
@dreamtalen dreamtalen marked this pull request as ready for review May 20, 2022 23:21
@dreamtalen dreamtalen force-pushed the cli branch 8 times, most recently from e9dc141 to c05ef81 Compare May 22, 2022 22:51
Copy link
Contributor

@salv-orlando salv-orlando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review

Makefile Outdated Show resolved Hide resolved
we can continue to deploy other necessary components of policy recommendation feature:

We need to install the [Kubernetes Operator for Apache Spark](https://github.com/GoogleCloudPlatform/spark-on-k8s-operator)
in the cluster.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yanjunz97 - Can you assess how difficult would it to add this to helm charts as well?
If not possible, we might want to document that the Theia helm chart does not yet suppport Spark Operator deployment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be difficult to add it. I would take a look at it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salv-orlando This support is added in PR #31

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should just document that way (assuming it is simpler). Again, consider create a separate PR just for documentation.

docs/network-policy-recommendation.md Outdated Show resolved Hide resolved
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`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the time being I'd keep "short" and "long" description the same or change it to

theiactl is the command line tool for Theia which provides access to some network flow visibility capabilities

I don't think we want to expand the long description for every feature we add.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

pkg/theiactl/commands/root.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/start.go Outdated Show resolved Hide resolved
'YYYY-MM-DD hh:mm:ss' format, for example: 2006-01-02 15:04:05`, err)
}
recoJobArgs = append(recoJobArgs, "--end_time", endTime)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a simple check to make sure that if both start and end time are specified start_time < end_time ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, added

err := json.Unmarshal([]byte(nsAllowList), &parsedNsAllowList)
if err != nil {
return fmt.Errorf(`parsing ns_allow_list: %v, ns_allow_list should
be a list of namespace string, for example: '["kube-system","flow-aggregator","flow-visibility"]'`, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be too difficult for the CLI to parse a list of comma-separated strings? The JSON notation is not super user-friendly

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Since in CLI we don't actually use the parsed list of namespaces. Here is just a pre-check for the format of the input is valid and pass the input string to the PySpark directly.

pkg/theiactl/commands/start.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/start.go Outdated Show resolved Hide resolved
Copy link
Contributor

@heanlan heanlan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits while looking through the doc

docs/network-policy-recommendation.md Outdated Show resolved Hide resolved
docs/network-policy-recommendation.md Outdated Show resolved Hide resolved
docs/network-policy-recommendation.md Outdated Show resolved Hide resolved
docs/network-policy-recommendation.md Outdated Show resolved Hide resolved
@yanjunz97
Copy link
Contributor

yanjunz97 commented May 26, 2022

I added a commit to implement the two CLI commands to

  • check the current status of a previous job
  • get the recommendation result of a previous job

Currently kubectl port-forward is used for port forwarding. I'm still working on the unit tests for these two commands. Will update the commit once they are finished.


Updated: Unit tests for the two commands are added.

Copy link
Contributor

@salv-orlando salv-orlando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments about check and result commands

pkg/theiactl/commands/check.go Outdated Show resolved Hide resolved
},
}

func getPolicyRecommendationProgress(id string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the 'id' passed to this function the same as the SparkAppId that would be retrieved from the monitoring service?
If so, do we actually need to pass it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id is different from SparkAppId. But it's true that we do not need this parameter in this function any more. Removed it now, thanks!

pkg/theiactl/commands/check.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/check.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/check.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/result.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/utils.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/utils.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/result.go Outdated Show resolved Hide resolved
pkg/theiactl/commands/result.go Outdated Show resolved Hide resolved
## Command-line Usage

Currently, the network policy recommendation feature only supports command line interaction through Theiactl.
Theiactl is the Theia command-line tool. To get more information about Theiactl, please refer to this [doc](./theiactl.md). We have 3 Theiactl commands for the policy recommendation feature:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following antctl docs convention, I think we should use lowercase for theiactl unless beginning of sentence?
https://github.com/antrea-io/antrea/blob/main/docs/antctl.md

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, addressed

@yanjunz97 yanjunz97 force-pushed the cli branch 3 times, most recently from c58e8b9 to daa942a Compare May 27, 2022 20:53
@yanjunz97
Copy link
Contributor

I notice that theiactl recopolicy start starts the recommendation job but does not clean it, which means the the policy-reco driver pod remained in Completed status when the job finished. Is that possible to delete the pod and related svc automatically somewhere?

@salv-orlando
Copy link
Contributor

/theia-test-e2e

@dreamtalen dreamtalen changed the title Implementation of Theiactl and policy recommendation CLI Implementation of theia and policy recommendation CLI Jun 3, 2022
@dreamtalen dreamtalen force-pushed the cli branch 3 times, most recently from 819f75c to 6a9fc60 Compare June 3, 2022 17:55
@salv-orlando salv-orlando added this to the 0.1 milestone Jun 3, 2022
@dreamtalen dreamtalen force-pushed the cli branch 3 times, most recently from 54f6ade to 9a8b26e Compare June 3, 2022 21:43

// policyRecommendationRetrieveCmd represents the policy-recommendation retrieve command
var policyRecommendationRetrieveCmd = &cobra.Command{
Use: "retrieve",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer "results" over "retrieve". But no strong opinion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Jianjun, I find sonobuoy have retrieve command to get results and another results command to inspect results. I prefer we could keep retrieve here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Let us if others have an opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have a strong opinion, both terms are good for me.

Use: "retrieve",
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.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetworkPolicies

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, addressed.

Use: "run",
Short: "Run a new policy recommendation Spark job",
Long: `Run a new policy recommendation Spark job.
Network policies will be recommended based on the flow records sent by Flow Aggregator.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NetworkPolicies

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we can remove this sentence (need not to talk about implementation in CLI help message).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed this sentence.

Long: `Run a new policy recommendation Spark job.
Network policies will be recommended based on the flow records sent by Flow Aggregator.
Must finish the deployment of Theia first, please follow the steps in
https://github.com/antrea-io/theia/blob/main/docs/network-policy-recommendation.md`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove "please follow the steps..." too. Needs not to be in CLI help messages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rigth. Also because the link on main branch might become invalid or the content might change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed this sentence.

pkg/theia/commands/policy_recommendation_run.go Outdated Show resolved Hide resolved
pkg/theia/commands/policy_recommendation_run.go Outdated Show resolved Hide resolved
"use-cluster-ip",
false,
`Enable this option will use ClusterIP instead of port forwarding when connecting to the ClickHouse Service.
It can only be used when running in cluster. (Only works when wait is enabled)`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "(Only works when wait is enabled)" mean?

Should we check "wait" is enabled and say: "It can only be used when running in cluster and when wait is enabled"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks addressed.

"file",
"f",
"",
"The file path where you want to save the results. (Only works when wait is enabled)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You used "result" in other places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, change to use result.

policyRecommendationRunCmd.Flags().String(
"clickhouse-endpoint",
"",
"The ClickHouse Service endpoint. (Only works when wait is enabled)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks addressed.

$ theia policy-recommendation status --id e998433e-accb-4888-9fc8-06563f073e86
Or
$ theia policy-recommendation status e998433e-accb-4888-9fc8-06563f073e86
Use Cluster IP when checking the current status of job with ID e998433e-accb-4888-9fc8-06563f073e86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClusterIP

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks addressed.

Copy link
Contributor

@salv-orlando salv-orlando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more minor comments

Long: `Run a new policy recommendation Spark job.
Network policies will be recommended based on the flow records sent by Flow Aggregator.
Must finish the deployment of Theia first, please follow the steps in
https://github.com/antrea-io/theia/blob/main/docs/network-policy-recommendation.md`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rigth. Also because the link on main branch might become invalid or the content might change

}
matchResult, err := regexp.MatchString(k8sQuantitiesReg, driverCoreRequest)
if err != nil || !matchResult {
return fmt.Errorf("driver-core-request should conform to the Kubernetes convention")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pedant comment: Kubernetes resource quantity convention

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks addressed.

Makefile Outdated Show resolved Hide resolved
Comment on lines 59 to 65
LabelSelector: "app.kubernetes.io/instance=policy-reco,app.kubernetes.io/name=spark-operator",
})
if err != nil {
return fmt.Errorf("error %v when finding the policy-reco-spark-operator Pod, please check the deployment of the Spark Operator", err)
}
if len(pods.Items) < 1 {
return fmt.Errorf("can't find the policy-reco-spark-operator Pod, please check the deployment of the Spark Operator")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also replace policy-reco- by policy-recommendation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will leave the Labels as it is for now before #31 is merged.

Copy link
Contributor

@salv-orlando salv-orlando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any additional comment. PR looks good to me.

Copy link
Contributor

@ziyouw ziyouw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For UT, we can consider to cover more cases later.

Comment on lines 40 to 56
username, password, err := getClickHouseSecret(fakeClientset)
assert.NoError(t, err)
assert.Equal(t, "clickhouse_operator", string(username))
assert.Equal(t, "clickhouse_operator_password", string(password))
}

func TestGetResultFromClickHouse(t *testing.T) {
recoID := "db2134ea-7169-46f8-b56d-d643d4751d1d"
expectedResult := "recommend-allow-acnp-kube-system-rpeal"
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
defer db.Close()
resultRow := sqlmock.NewRows([]string{"yamls"}).AddRow(expectedResult)
mock.ExpectQuery("SELECT yamls FROM recommendations WHERE id = (?);").WithArgs(recoID).WillReturnRows(resultRow)
result, err := getResultFromClickHouse(db, recoID)
assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For UT, only covering happy path may be not enough, we may also consider some negative cases later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some failure cases in the second commit, we could add more cases after this PR merged.

@dreamtalen dreamtalen force-pushed the cli branch 2 times, most recently from 6806eab to 96b630b Compare June 6, 2022 17:12
In this commit, we add the implementation of theia, the command-line
tool for Theia based on cobra framework.
Policy recommendation CLI is also included. It consists of 3 commands
which let users could run a new policy recommendation job, check the
current status of a previous job, and retrieve the recommendation
result of a previous job respectively.
In order to access theia services externally, theia can
make use of k8s port forwarder functionality. Port forwarder
provides tooling to start/stop port forwarding channel for
eather Pod+Port or Service+Port. The code is loosely based on
corresponding code in kubectl.
Document of theia and policy recommendation will be added in a seperate
commit later.

Signed-off-by: Yongming Ding <dyongming@vmware.com>
Co-authored-by: Yanjun Zhou <zhouya@vmware.com>
Co-authored-by: Anna Khmelnitsky <akhmelnitsky@vmware.com>
const (
flowVisibilityNS = "flow-visibility"
k8sQuantitiesReg = "^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$"
sparkImage = "antrea/theia-policy-recommendation:latest"
Copy link
Contributor

@heanlan heanlan Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to change to "projects.registry.vmware.com/antrea/theia-policy-recommendation:latest" before getting merged?

Signed-off-by: Yongming Ding <dyongming@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants