Skip to content

Commit

Permalink
Implementation of Theiactl and policy recommendation CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Yongming Ding <dyongming@vmware.com>
  • Loading branch information
Yongming Ding committed May 20, 2022
1 parent 0cf17c5 commit 3fdbccd
Show file tree
Hide file tree
Showing 13 changed files with 2,554 additions and 3 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,14 @@ clickhouse-monitor:
clickhouse-monitor-plugin:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/theia/plugins/clickhouse-monitor


.PHONY: theiactl
theiactl:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/theia/pkg/theiactl

# Add the darwin version binary to help dev&test on Mac for now
.PHONY: theiactl-darwin
theiactl-darwin:
@mkdir -p $(BINDIR)
GOOS=darwin $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/theia/pkg/theiactl
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ require (
github.com/ClickHouse/clickhouse-go v1.5.4
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/containernetworking/plugins v0.8.7
github.com/google/uuid v1.1.2
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/vmware/go-ipfix v0.5.12
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
Expand Down Expand Up @@ -60,7 +62,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
Expand Down Expand Up @@ -88,7 +89,6 @@ require (
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/streamrail/concurrent-map v0.0.0-20160823150647-8bf1e9bacbf6 // indirect
github.com/vishvananda/netlink v1.1.1-0.20210510164352-d17758a128bf // indirect
Expand Down
89 changes: 89 additions & 0 deletions pkg/theiactl/commands/check.go
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",
)
}
42 changes: 42 additions & 0 deletions pkg/theiactl/commands/policyreco.go
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",
)
}
41 changes: 41 additions & 0 deletions pkg/theiactl/commands/result.go
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)
}
41 changes: 41 additions & 0 deletions pkg/theiactl/commands/root.go
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() {
}
Loading

0 comments on commit 3fdbccd

Please sign in to comment.