Skip to content

Commit

Permalink
Refactor egress-v4-cni plugin to support unit testing (#2353)
Browse files Browse the repository at this point in the history
* Refactor egress-v4-cni-plugin so that code can be unit-tested
  • Loading branch information
wanyufe authored Apr 26, 2023
1 parent ec4715a commit 62dbe0d
Show file tree
Hide file tree
Showing 28 changed files with 1,996 additions and 668 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ coverage.txt
core-plugins/
build/
vendor
egress-v4-cni
egress-cni
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ LDFLAGS = -X pkg/version/info.Version=$(VERSION) -X pkg/awsutils/awssession.vers
# ALLPKGS is the set of packages provided in source.
ALLPKGS = $(shell go list $(VENDOR_OVERRIDE_FLAG) ./... | grep -v cmd/packet-verifier)
# BINS is the set of built command executables.
BINS = aws-k8s-agent aws-cni grpc-health-probe cni-metrics-helper aws-vpc-cni aws-vpc-cni-init egress-v4-cni
BINS = aws-k8s-agent aws-cni grpc-health-probe cni-metrics-helper aws-vpc-cni aws-vpc-cni-init egress-cni
# CORE_PLUGIN_DIR is the directory containing upstream containernetworking plugins
CORE_PLUGIN_DIR = $(MAKEFILE_PATH)/core-plugins/

Expand Down Expand Up @@ -134,7 +134,7 @@ build-linux: ## Build the VPC CNI plugin agent using the host's Go toolchain.
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o aws-k8s-agent ./cmd/aws-k8s-agent
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o aws-cni ./cmd/routed-eni-cni-plugin
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o grpc-health-probe ./cmd/grpc-health-probe
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o egress-v4-cni ./cmd/egress-v4-cni-plugin
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o egress-cni ./cmd/egress-cni-plugin

# Build VPC CNI init container entrypoint
build-aws-vpc-cni-init: BUILD_FLAGS = $(BUILD_MODE) -ldflags '-s -w $(LDFLAGS)'
Expand Down
20 changes: 13 additions & 7 deletions cmd/aws-vpc-cni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"os"
"os/exec"
Expand All @@ -48,13 +47,17 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/containernetworking/cni/pkg/types"

"github.com/aws/amazon-vpc-cni-k8s/utils"
"github.com/aws/amazon-vpc-cni-k8s/utils/cp"
"github.com/aws/amazon-vpc-cni-k8s/utils/imds"
"github.com/containernetworking/cni/pkg/types"
)

const (
egressPluginIpamSubnetV4 = "169.254.172.0/22"
egressPluginIpamDstV4 = "0.0.0.0/0"
egressPluginIpamDataDirV4 = "/run/cni/v6pd/egress-v4-ipam"
defaultHostCNIBinPath = "/host/opt/cni/bin"
defaultHostCNIConfDirPath = "/host/etc/cni/net.d"
defaultAWSconflistFile = "/app/10-aws.conflist"
Expand Down Expand Up @@ -214,7 +217,7 @@ func isValidJSON(inFile string) error {
}

func generateJSON(jsonFile string, outFile string, nodeIP string) error {
byteValue, err := ioutil.ReadFile(jsonFile)
byteValue, err := os.ReadFile(jsonFile)
if err != nil {
return err
}
Expand All @@ -234,8 +237,11 @@ func generateJSON(jsonFile string, outFile string, nodeIP string) error {
netconf = strings.Replace(netconf, "__PODSGENFORCINGMODE__", podSGEnforcingMode, -1)
netconf = strings.Replace(netconf, "__PLUGINLOGFILE__", pluginLogFile, -1)
netconf = strings.Replace(netconf, "__PLUGINLOGLEVEL__", pluginLogLevel, -1)
netconf = strings.Replace(netconf, "__EGRESSV4PLUGINLOGFILE__", egressV4pluginLogFile, -1)
netconf = strings.Replace(netconf, "__EGRESSV4PLUGINENABLED__", enabledIPv6, -1)
netconf = strings.Replace(netconf, "__EGRESSPLUGINLOGFILE__", egressV4pluginLogFile, -1)
netconf = strings.Replace(netconf, "__EGRESSPLUGINENABLED__", enabledIPv6, -1)
netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMSUBNET__", egressPluginIpamSubnetV4, -1)
netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMDST__", egressPluginIpamDstV4, -1)
netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMDATADIR__", egressPluginIpamDataDirV4, -1)
netconf = strings.Replace(netconf, "__RANDOMIZESNAT__", randomizeSNAT, -1)
netconf = strings.Replace(netconf, "__NODEIP__", nodeIP, -1)

Expand Down Expand Up @@ -265,7 +271,7 @@ func generateJSON(jsonFile string, outFile string, nodeIP string) error {
log.Fatalf("%s is not a valid json object, error: %s", netconf, err)
}

err = ioutil.WriteFile(outFile, byteValue, 0644)
err = os.WriteFile(outFile, byteValue, 0644)
return err
}

Expand Down Expand Up @@ -345,7 +351,7 @@ func _main() int {
log.WithError(err).Error("Failed to enable nftables")
}

pluginBins := []string{"aws-cni", "egress-v4-cni"}
pluginBins := []string{"aws-cni", "egress-cni"}
hostCNIBinPath := utils.GetEnv(envHostCniBinPath, defaultHostCNIBinPath)
err := cp.InstallBinaries(pluginBins, hostCNIBinPath)
if err != nil {
Expand Down
Loading

0 comments on commit 62dbe0d

Please sign in to comment.