Skip to content

Commit 6a0f9ff

Browse files
author
Yongli Chen
authored
Add Azure-npm to provide k8s network policy support (#173)
* address comments * make azure-npm versiona consistent with main version * Npm telemetry (#12) * azure-npm * set logging file * parameterize telemetry API * avoid null ptr derefence * add telemetry to npm * address comments * add descriptive comments * add copyright info * returns on err
1 parent 700a257 commit 6a0f9ff

25 files changed

+3747
-46
lines changed

Makefile

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ CNSFILES = \
3939
$(COREFILES) \
4040
$(CNMFILES)
4141

42+
NPMFILES = \
43+
$(wildcard npm/*.go) \
44+
$(wildcard npm/ipsm/*.go) \
45+
$(wildcard npm/iptm/*.go) \
46+
$(wildcard npm/util/*.go) \
47+
$(wildcard npm/plugin/*.go) \
48+
$(COREFILES)
49+
4250
# Build defaults.
4351
GOOS ?= linux
4452
GOARCH ?= amd64
@@ -48,12 +56,14 @@ CNM_DIR = cnm/plugin
4856
CNI_NET_DIR = cni/network/plugin
4957
CNI_IPAM_DIR = cni/ipam/plugin
5058
CNS_DIR = cns/service
59+
NPM_DIR = npm/plugin
5160
OUTPUT_DIR = output
5261
BUILD_DIR = $(OUTPUT_DIR)/$(GOOS)_$(GOARCH)
5362
CNM_BUILD_DIR = $(BUILD_DIR)/cnm
5463
CNI_BUILD_DIR = $(BUILD_DIR)/cni
5564
CNI_MULTITENANCY_BUILD_DIR = $(BUILD_DIR)/cni-multitenancy
5665
CNS_BUILD_DIR = $(BUILD_DIR)/cns
66+
NPM_BUILD_DIR = $(BUILD_DIR)/npm
5767

5868
# Containerized build parameters.
5969
BUILD_CONTAINER_IMAGE = acn-build
@@ -83,7 +93,11 @@ CNS_ARCHIVE_NAME = azure-cns-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
8393
CNM_PLUGIN_IMAGE ?= microsoft/azure-vnet-plugin
8494
CNM_PLUGIN_ROOTFS = azure-vnet-plugin-rootfs
8595

96+
# Azure network policy manager parameters.
97+
AZURE_NPM_IMAGE = containernetworking/azure-npm
98+
8699
VERSION ?= $(shell git describe --tags --always --dirty)
100+
AZURE_NPM_VERSION = VERSION
87101

88102
ENSURE_OUTPUT_DIR_EXISTS := $(shell mkdir -p $(OUTPUT_DIR))
89103

@@ -92,8 +106,10 @@ azure-cnm-plugin: $(CNM_BUILD_DIR)/azure-vnet-plugin$(EXE_EXT) cnm-archive
92106
azure-vnet: $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT)
93107
azure-vnet-ipam: $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT)
94108
azure-cni-plugin: azure-vnet azure-vnet-ipam cni-archive
95-
azure-cns: $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT) cns-archive
96-
all-binaries: azure-cnm-plugin azure-cni-plugin azure-cns
109+
azure-cns: $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT) cns-archive
110+
azure-npm: $(NPM_BUILD_DIR)/azure-npm
111+
112+
all-binaries: azure-cnm-plugin azure-cni-plugin azure-cns azure-npm
97113

98114
# Clean all build artifacts.
99115
.PHONY: clean
@@ -116,6 +132,10 @@ $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT): $(CNIFILES)
116132
$(CNS_BUILD_DIR)/azure-cns$(EXE_EXT): $(CNSFILES)
117133
go build -v -o $(CNS_BUILD_DIR)/azure-cns$(EXE_EXT) -ldflags "-X main.version=$(VERSION) -s -w" $(CNS_DIR)/*.go
118134

135+
# Build the Azure NPM plugin.
136+
$(NPM_BUILD_DIR)/azure-npm: $(NPMFILES)
137+
go build -v -o $(NPM_BUILD_DIR)/azure-npm -ldflags "-X main.version=$(VERSION) -s -w" $(NPM_DIR)/*.go
138+
119139
# Build all binaries in a container.
120140
.PHONY: all-binaries-containerized
121141
all-binaries-containerized:
@@ -171,6 +191,21 @@ azure-vnet-plugin-image: azure-cnm-plugin
171191
publish-azure-vnet-plugin-image:
172192
docker plugin push $(CNM_PLUGIN_IMAGE):$(VERSION)
173193

194+
# Build the Azure NPM image.
195+
.PHONY: azure-npm-image
196+
azure-npm-image: azure-npm
197+
# Build the plugin image.
198+
docker build \
199+
-f npm/Dockerfile \
200+
-t $(AZURE_NPM_IMAGE):$(AZURE_NPM_VERSION) \
201+
--build-arg NPM_BUILD_DIR=$(NPM_BUILD_DIR) \
202+
.
203+
204+
# Publish the Azure NPM image to a Docker registry
205+
.PHONY: publish-azure-npm-image
206+
publish-azure-npm-image:
207+
docker push $(AZURE_NPM_IMAGE):$(AZURE_NPM_VERSION)
208+
174209
# Create a CNI archive for the target platform.
175210
.PHONY: cni-archive
176211
cni-archive:

cni/network/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333
type netPlugin struct {
3434
*cni.Plugin
3535
nm network.NetworkManager
36-
reportManager *telemetry.ReportManager
36+
reportManager *telemetry.CNIReportManager
3737
}
3838

3939
// NewPlugin creates a new netPlugin object.
@@ -58,7 +58,7 @@ func NewPlugin(config *common.PluginConfig) (*netPlugin, error) {
5858
}, nil
5959
}
6060

61-
func (plugin *netPlugin) SetReportManager(reportManager *telemetry.ReportManager) {
61+
func (plugin *netPlugin) SetReportManager(reportManager *telemetry.CNIReportManager) {
6262
plugin.reportManager = reportManager
6363
}
6464

cni/network/plugin/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
var version string
2525

2626
// If report write succeeded, mark the report flag state to false.
27-
func markSendReport(reportManager *telemetry.ReportManager) {
27+
func markSendReport(reportManager *telemetry.CNIReportManager) {
2828
if err := reportManager.Report.SetReportState(); err != nil {
2929
log.Printf("SetReportState failed due to %v", err)
3030
reportManager.Report.ErrorMessage = err.Error()
@@ -36,7 +36,7 @@ func markSendReport(reportManager *telemetry.ReportManager) {
3636
}
3737

3838
// send error report to hostnetagent if CNI encounters any error.
39-
func reportPluginError(reportManager *telemetry.ReportManager, err error) {
39+
func reportPluginError(reportManager *telemetry.CNIReportManager, err error) {
4040
log.Printf("Report plugin error")
4141
reportManager.GetReport(pluginName, version)
4242
reportManager.Report.ErrorMessage = err.Error()
@@ -53,11 +53,13 @@ func main() {
5353
var config common.PluginConfig
5454
var err error
5555
config.Version = version
56-
reportManager := &telemetry.ReportManager{
57-
HostNetAgentURL: hostNetAgentURL,
58-
IpamQueryURL: ipamQueryURL,
59-
ReportType: reportType,
60-
Report: &telemetry.Report{},
56+
reportManager := &telemetry.CNIReportManager{
57+
ReportMangager: &telemetry.ReportManager{
58+
HostNetAgentURL: hostNetAgentURL,
59+
ReportType: reportType,
60+
},
61+
IpamQueryURL: ipamQueryURL,
62+
Report: &telemetry.CNIReport{},
6163
}
6264

6365
reportManager.GetReport(pluginName, config.Version)

npm/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Use a minimal image as a parent image
2+
FROM ubuntu:16.04
3+
ARG NPM_BUILD_DIR
4+
5+
# Install dependencies.
6+
RUN apt-get update
7+
RUN apt-get install -y iptables
8+
RUN apt-get install -y ipset
9+
10+
# Install plugin.
11+
COPY $NPM_BUILD_DIR/azure-npm /usr/bin
12+
WORKDIR /usr/bin
13+
14+
# Run the npm command by default when the container starts.
15+
ENTRYPOINT ["/usr/bin/azure-npm"]

0 commit comments

Comments
 (0)