Skip to content

Commit

Permalink
Implement the antctl framework
Browse files Browse the repository at this point in the history
Signed-off-by: Weiqiang TANG <weiqiangt@vmware.com>
  • Loading branch information
weiqiangt committed Dec 17, 2019
1 parent 03f875e commit b31ecd1
Show file tree
Hide file tree
Showing 34 changed files with 1,817 additions and 36 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.cache
antctl-bin
16 changes: 16 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ jobs:
- name: Build Antrea binaries
run: make bin

antctl:
name: Build antctl for macOS, Linux and Windows
runs-on: [ubuntu-18.04]
steps:

- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13

- name: Check-out code
uses: actions/checkout@v1

- name: Build antctl binaries
run: make antctl


mocks:
name: Check mock generation
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin
antctl-bin
.mockgen
.cache
.golangci-bin
Expand Down
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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)
GOPATH ?= $$($(GO) env GOPATH)
DOCKER_CACHE := $(CURDIR)/.cache

.PHONY: all
Expand Down Expand Up @@ -75,6 +76,19 @@ docker-tidy: $(DOCKER_CACHE)
.linux-bin:
GOBIN=$(BINDIR) $(GO) install $(GOFLAGS) -ldflags '$(LDFLAGS)' github.com/vmware-tanzu/antrea/cmd/...

# TODO: strip binary when building realses
ANTCTL_BINARIES := antctl-darwin antctl-linux antctl-windows
$(ANTCTL_BINARIES): antctl-%:
@GOOS=$* $(GO) build -o $(BINDIR)/$@ $(GOFLAGS) -ldflags '$(LDFLAGS)' github.com/vmware-tanzu/antrea/cmd/antctl
@if [[ $@ != *windows ]]; then \
chmod 0755 $(BINDIR)/$@; \
else \
mv $(BINDIR)/$@ $(BINDIR)/$@.exe; \
fi

.PHONY: antctl
antctl: $(ANTCTL_BINARIES)

.PHONY: .linux-test-unit
.linux-test-unit:
@echo
Expand Down Expand Up @@ -121,7 +135,7 @@ golangci: .golangci-bin
.linter:
@if ! PATH=$$PATH:$(GOPATH)/bin command -v golint > /dev/null; then \
echo "===> Installing Golint <==="; \
go get -u golang.org/x/lint/golint; \
$(GO) get -u golang.org/x/lint/golint; \
fi

.PHONY: lint
Expand Down
21 changes: 21 additions & 0 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ rules:
- subjectaccessreviews
verbs:
- create
- nonResourceURLs:
- /apis/antctl.antrea.tanzu.vmware.com/v1
- /apis/antctl.antrea.tanzu.vmware.com/v1/*
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down Expand Up @@ -323,6 +328,22 @@ spec:
name: antrea-config-48gttf992h
name: antrea-config
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
app: antrea
name: v1.antctl.antrea.tanzu.vmware.com
spec:
group: antctl.antrea.tanzu.vmware.com
groupPriorityMinimum: 1000
insecureSkipTLSVerify: true
service:
name: antrea
namespace: kube-system
version: v1
versionPriority: 15
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
Expand Down
32 changes: 32 additions & 0 deletions build/yamls/base/antctl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: antrea-agent
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: antctl
rules:
- nonResourceURLs:
- /apis/antctl.antrea.tanzu.vmware.com/v1
- /apis/antctl.antrea.tanzu.vmware.com/v1/*
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
app: antrea
name: antctl
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: antctl
subjects:
- kind: ServiceAccount
name: antctl
namespace: kube-system
5 changes: 5 additions & 0 deletions build/yamls/base/controller-rbac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ rules:
- subjectaccessreviews
verbs:
- create
- nonResourceURLs:
- /apis/antctl.antrea.tanzu.vmware.com/v1
- /apis/antctl.antrea.tanzu.vmware.com/v1/*
verbs:
- get
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
Expand Down
15 changes: 15 additions & 0 deletions build/yamls/base/controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ spec:
selector:
component: antrea-controller
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1.antctl.antrea.tanzu.vmware.com
spec:
insecureSkipTLSVerify: true
group: antctl.antrea.tanzu.vmware.com
groupPriorityMinimum: 1000
versionPriority: 15
service:
name: antrea
namespace: kube-system
version: v1
---

apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down
58 changes: 58 additions & 0 deletions cmd/antctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 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 main

import (
"flag"
"fmt"
"math"
"os"
"path"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/component-base/logs"

"github.com/vmware-tanzu/antrea/pkg/antctl"
)

var commandName = path.Base(os.Args[0])

var rootCmd = &cobra.Command{
Use: commandName,
Short: commandName + " is the command line tool for Antrea",
Long: commandName + " is the command line tool for Antrea that supports showing runtime information of ${component}",
}

func init() {
// prevent any unexpected output at first
pflag.CommandLine.MarkHidden("log-flush-frequency")
flag.Set("logtostderr", "true")
flag.Set("alsologtostderr", "true")
flag.Set("v", fmt.Sprint(math.MaxUint32))

}

func main() {
logs.InitLogs()
defer logs.FlushLogs()
antctl.Init(rootCmd)

err := rootCmd.Execute()
if err != nil {
logs.FlushLogs()
os.Exit(1)
}
}
11 changes: 11 additions & 0 deletions cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/vmware-tanzu/antrea/pkg/agent/controller/noderoute"
"github.com/vmware-tanzu/antrea/pkg/agent/interfacestore"
"github.com/vmware-tanzu/antrea/pkg/agent/openflow"
"github.com/vmware-tanzu/antrea/pkg/antctl"
"github.com/vmware-tanzu/antrea/pkg/k8s"
"github.com/vmware-tanzu/antrea/pkg/monitor"
"github.com/vmware-tanzu/antrea/pkg/ovs/ovsconfig"
Expand Down Expand Up @@ -88,6 +89,11 @@ func run(o *Options) error {
}
nodeConfig := agentInitializer.GetNodeConfig()

antctlServerOpt, err := antctl.NewLocalServerOption()
if err != nil {
return fmt.Errorf("error create local antctl server options: %w", err)
}

nodeRouteController := noderoute.NewNodeRouteController(k8sClient,
informerFactory,
ofClient,
Expand Down Expand Up @@ -127,7 +133,12 @@ func run(o *Options) error {

go agentMonitor.Run(stopCh)

serverStoppedCh := antctl.StartServer(antctlServerOpt, agentMonitor, nil, stopCh)

<-stopCh

klog.Info("Stopping Antrea agent")
<-serverStoppedCh // check if the antctl server stopped

return nil
}
15 changes: 14 additions & 1 deletion cmd/antrea-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/client-go/informers"
"k8s.io/klog"

"github.com/vmware-tanzu/antrea/pkg/antctl"
"github.com/vmware-tanzu/antrea/pkg/apiserver"
"github.com/vmware-tanzu/antrea/pkg/apiserver/storage"
"github.com/vmware-tanzu/antrea/pkg/controller/networkpolicy"
Expand Down Expand Up @@ -77,6 +78,11 @@ func run(o *Options) error {
return fmt.Errorf("error creating API server: %v", err)
}

antctlServerOpt, err := antctl.NewLocalServerOption()
if err != nil {
return fmt.Errorf("error create local antctl server options: %w", err)
}

// set up signal capture: the first SIGTERM / SIGINT signal is handled gracefully and will
// cause the stopCh channel to be closed; if another signal is received before the program
// exits, we will force exit.
Expand All @@ -89,10 +95,17 @@ func run(o *Options) error {

go networkPolicyController.Run(stopCh)

go apiServer.GenericAPIServer.PrepareRun().Run(stopCh)
readyAPIServer := apiServer.GenericAPIServer.PrepareRun()
antctl.Definition.ApplyToAPIServer(readyAPIServer.GenericAPIServer, controllerMonitor)
go readyAPIServer.Run(stopCh)

antctlStoppedCh := antctl.StartServer(antctlServerOpt, nil, controllerMonitor, stopCh)

<-stopCh

klog.Info("Stopping Antrea controller")
<-antctlStoppedCh // check if the antctl server stopped

return nil
}

Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/coreos/go-iptables v0.4.1
github.com/elazarl/goproxy v0.0.0-20190911111923-ecfe977594f1 // indirect
github.com/evanphx/json-patch v4.5.0+incompatible // indirect
github.com/fatih/structtag v1.1.0
github.com/gogo/protobuf v1.2.1
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.3.2
Expand All @@ -26,19 +27,19 @@ require (
github.com/j-keck/arping v1.0.0
github.com/json-iterator/go v1.1.6 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd
github.com/pkg/errors v0.8.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/streamrail/concurrent-map v0.0.0-20160823150647-8bf1e9bacbf6 // indirect
github.com/stretchr/testify v1.3.0
github.com/vishvananda/netlink v1.0.0
github.com/vmware/octant v0.8.0
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1
github.com/vmware/octant v0.0.0-00010101000000-000000000000
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/exp v0.0.0-20190121172915-509febef88a4
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9
golang.org/x/sys v0.0.0-20191008105621-543471e840be
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.22.0
gopkg.in/yaml.v2 v2.2.2
Expand Down
17 changes: 9 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ github.com/evanphx/json-patch v0.0.0-20190203023257-5858425f7550/go.mod h1:50XU6
github.com/evanphx/json-patch v4.1.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.5.0+incompatible h1:ouOWdg56aJriqS0huScTkVXPC5IcNrDCXZ6OoTAWu7M=
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/structtag v1.1.0 h1:6j4mUV/ES2duvnAzKMFkN6/A5mCaNYPD3xfbAkLLOF8=
github.com/fatih/structtag v1.1.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v0.0.0-20180820084758-c7ce16629ff4 h1:bRzFpEzvausOAt4va+I/22BZ1vXDtERngp0BNYDKej0=
Expand Down Expand Up @@ -350,8 +352,8 @@ golang.org/x/crypto v0.0.0-20181009213950-7c1a557ab941/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181025213731-e84da0312774/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1 h1:anGSYQpPhQwXlwsu5wmfq0nWkCNaMEMUwAv13Y92hd8=
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4 h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -372,8 +374,8 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 h1:e6HwijUxhDe+hPNjZQQn9bA5PW3vNmnN64U2ZW759Lk=
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -383,9 +385,8 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -398,8 +399,8 @@ golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down
Loading

0 comments on commit b31ecd1

Please sign in to comment.