Skip to content

Commit

Permalink
Add policyreco result and check command to Theiactl
Browse files Browse the repository at this point in the history
Signed-off-by: Yanjun Zhou <zhouya@vmware.com>
  • Loading branch information
yanjunz97 committed May 27, 2022
1 parent 386a262 commit c58e8b9
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 18 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/upload_release_assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,48 @@ jobs:
asset_path: ./assets/flow-visibility.yml
asset_name: flow-visibility.yml
asset_content_type: application/octet-stream
- name: Upload theiactl-darwin-x86_64
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./assets/theiactl-darwin-x86_64
asset_name: theiactl-darwin-x86_64
asset_content_type: application/octet-stream
- name: Upload theiactl-linux-arm
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./assets/theiactl-linux-arm
asset_name: theiactl-linux-arm
asset_content_type: application/octet-stream
- name: Upload theiactl-linux-arm64
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./assets/theiactl-linux-arm64
asset_name: theiactl-linux-arm64
asset_content_type: application/octet-stream
- name: Upload theiactl-linux-x86_64
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./assets/theiactl-linux-x86_64
asset_name: theiactl-linux-x86_64
asset_content_type: application/octet-stream
- name: Upload theiactl-windows-x86_64.exe
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./assets/theiactl-windows-x86_64.exe
asset_name: theiactl-windows-x86_64.exe
asset_content_type: application/octet-stream
23 changes: 14 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
SHELL := /bin/bash
SHELL := /bin/bash
# go options
GO ?= go
LDFLAGS :=
GOFLAGS :=
BINDIR ?= $(CURDIR)/bin
GO_FILES := $(shell find . -type d -name '.cache' -prune -o -type f -name '*.go' -print)
GOPATH ?= $$($(GO) env GOPATH)
DOCKER_CACHE := $(CURDIR)/.cache
GO_VERSION := $(shell head -n 1 build/images/deps/go-version)
GO ?= go
LDFLAGS :=
GOFLAGS :=
BINDIR ?= $(CURDIR)/bin
GO_FILES := $(shell find . -type d -name '.cache' -prune -o -type f -name '*.go' -print)
GOPATH ?= $$($(GO) env GOPATH)
DOCKER_CACHE := $(CURDIR)/.cache
THEIACTL_BINARY_NAME ?= theiactl
GO_VERSION := $(shell head -n 1 build/images/deps/go-version)

DOCKER_BUILD_ARGS = --build-arg GO_VERSION=$(GO_VERSION)

Expand Down Expand Up @@ -168,3 +169,7 @@ theiactl:
theiactl-darwin:
@mkdir -p $(BINDIR)
GOOS=darwin $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/theia/pkg/theiactl

.PHONY: theiactl-release
theiactl-release:
@$(GO) build -o $(BINDIR)/$(THEIACTL_BINARY_NAME) $(GOFLAGS) -ldflags '-s -w $(LDFLAGS)' antrea.io/theia/pkg/theiactl
21 changes: 21 additions & 0 deletions hack/release/prepare-assets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ pushd $THIS_DIR/../.. > /dev/null
mkdir -p "$1"
OUTPUT_DIR=$(cd "$1" && pwd)

THEIACTL_BUILDS=(
"linux amd64 linux-x86_64"
"linux arm64 linux-arm64"
"linux arm linux-arm"
"windows amd64 windows-x86_64.exe"
"darwin amd64 darwin-x86_64"
)

for build in "${THEIACTL_BUILDS[@]}"; do
args=($build)
os="${args[0]}"
arch="${args[1]}"
suffix="${args[2]}"

# cgo is disabled by default when cross-compiling, but enabled by default
# for native builds. We ensure it is always disabled for portability since
# these binaries will be distributed as release assets.
GOOS=$os GOARCH=$arch CGO_ENABLED=0 THEIACTL_BINARY_NAME="theia-$suffix" BINDIR="$OUTPUT_DIR"/ make theiactl-release
done


export IMG_TAG=$VERSION

export IMG_NAME=projects.registry.vmware.com/antrea/theia-clickhouse-monitor
Expand Down
88 changes: 87 additions & 1 deletion pkg/theiactl/commands/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ package commands

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"strings"
"time"

"github.com/google/uuid"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/wait"

sparkv1 "antrea.io/theia/third_party/sparkoperator/v1beta2"
)
Expand Down Expand Up @@ -72,12 +78,92 @@ $ theiactl policyreco check --id e998433e-accb-4888-9fc8-06563f073e86
return err
}
state := strings.TrimSpace(string(sparkApplication.Status.AppState.State))
if state == "RUNNING" {
// Check the working progress of running recommendation job
// Forward the policy recommendation service port
portForwardCmd := exec.Command("kubectl", "port-forward", fmt.Sprintf("service/policy-reco-%s-ui-svc", recoID), "-n", flowVisibilityNS, "4040:4040")
if err := portForwardCmd.Start(); err != nil {
return fmt.Errorf("failed to forward port for policy recommendation service, %v", err)
}
defer portForwardCmd.Process.Kill()
stateProgress, err := getPolicyRecommendationProgress()
if err != nil {
return fmt.Errorf("failed to get the status of job with id %s: %v", recoID, err)
}
state += stateProgress
}
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 getPolicyRecommendationProgress() (string, error) {
// Get the id of current spark application
url := "http://localhost:4040/api/v1/applications"
response, err := getResponseFromSparkMonitoringSvc(url)
if err != nil {
return "", fmt.Errorf("failed to get response from Spark Monitoring service: %v", err)
}
var getAppsResult []map[string]interface{}
json.Unmarshal([]byte(response), &getAppsResult)
if len(getAppsResult) != 1 {
return "", fmt.Errorf("wrong number of Spark Application, expected 1, got %d", len(getAppsResult))
}
sparkAppID := getAppsResult[0]["id"]
// Check the percentage of completed stages
url = fmt.Sprintf("http://localhost:4040/api/v1/applications/%s/stages", sparkAppID)
response, err = getResponseFromSparkMonitoringSvc(url)
if err != nil {
return "", fmt.Errorf("failed to get response from Spark Monitoring service: %v", err)
}
var getStagesResult []map[string]interface{}
json.Unmarshal([]byte(response), &getStagesResult)
NumStageResult := len(getStagesResult)
if NumStageResult < 1 {
return "", fmt.Errorf("wrong number of Spark Application stages, expected at least 1, got %d", NumStageResult)
}
completedStages := 0
for _, stage := range getStagesResult {
if stage["status"] == "COMPLETE" || stage["status"] == "SKIPPED" {
completedStages++
}
}
return fmt.Sprintf(": %d/%d (%d%%) stages completed", completedStages, NumStageResult, completedStages*100/NumStageResult), nil
}

func getResponseFromSparkMonitoringSvc(url string) ([]byte, error) {
sparkMonitoringClient := http.Client{}
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
var res *http.Response
var getErr error
connRetryInterval := 1 * time.Second
connTimeout := 10 * time.Second
if err := wait.PollImmediate(connRetryInterval, connTimeout, func() (bool, error) {
res, err = sparkMonitoringClient.Do(request)
if err != nil {
getErr = err
return false, nil
}
return true, nil
}); err != nil {
return nil, getErr
}
if res == nil {
return nil, fmt.Errorf("response is nil")
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
return body, nil
}

func init() {
policyrecoCmd.AddCommand(checkCmd)
checkCmd.Flags().StringP(
Expand Down
Loading

0 comments on commit c58e8b9

Please sign in to comment.