From 67b24bda878177cb1a42f914537e4fad2e63c12d Mon Sep 17 00:00:00 2001 From: Claes Mogren Date: Mon, 25 Jun 2018 14:17:41 -0700 Subject: [PATCH] Initial travis CI integration --- .gitignore | 3 + .travis.yml | 30 ++++++++ Makefile | 37 ++++++---- README.md | 2 +- plugins/routed-eni/cni.go | 5 +- rpc/rpc.proto | 2 +- scripts/install-aws.sh | 1 + verify-aws.go | 152 -------------------------------------- verify-network.go | 104 -------------------------- 9 files changed, 59 insertions(+), 277 deletions(-) create mode 100644 .travis.yml delete mode 100644 verify-aws.go delete mode 100644 verify-network.go diff --git a/.gitignore b/.gitignore index 31d180cd006..3f497b99fe2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ aws-cni verify-aws verify-network *~ +.idea/ +*.iml +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000000..32331137d92 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,30 @@ +# This is how to tell Travis to use the fast container-based test +# runner instead of the slow VM-based one. +sudo: false + +language: go + +# Only the last two Go releases are supported by the Go team with security updates. +# Any older versions are considered deprecated. +go: + - "1.10.x" + - "1.9.x" + +# Only clone the most recent commit. +git: + depth: 1 + +go_import_path: github.com/aws/amazon-vpc-cni-k8s + +# Install needed tools +install: + - go get -u github.com/golang/dep/cmd/dep + - go get -u golang.org/x/lint/golint + - dep ensure + +# Tests to run +script: + - make build-linux + - make lint + - make vet + - make unit-test diff --git a/Makefile b/Makefile index 35909f44f7d..254b2840fff 100644 --- a/Makefile +++ b/Makefile @@ -12,35 +12,36 @@ # language governing permissions and limitations under the License. # -# build binary -static: - go build -o aws-k8s-agent main.go - go build -o aws-cni plugins/routed-eni/cni.go - go build verify-aws.go - go build verify-network.go +.PHONY: build-linux clean docker docker-build lint unit-test vet + +# Default to build the Linux binary +build-linux: + GOOS=linux CGO_ENABLED=0 go build -o aws-k8s-agent + GOOS=linux CGO_ENABLED=0 go build -o aws-cni ./plugins/routed-eni/ docker-build: docker run -v $(shell pwd):/usr/src/app/src/github.com/aws/amazon-vpc-cni-k8s \ --workdir=/usr/src/app/src/github.com/aws/amazon-vpc-cni-k8s \ --env GOPATH=/usr/src/app \ - golang:1.10 make static + golang:1.10 make build-linux -# build docker image +# Build docker image docker: docker-build @docker build -f scripts/dockerfiles/Dockerfile.release -t "amazon/amazon-k8s-cni:latest" . @echo "Built Docker image \"amazon/amazon-k8s-cni:latest\"" # unit-test unit-test: - go test -v -cover -race -timeout 150s ./pkg/awsutils/... - go test -v -cover -race -timeout 10s ./plugins/routed-eni/... - go test -v -cover -race -timeout 10s ./plugins/routed-eni/driver - go test -v -cover -race -timeout 10s ./pkg/k8sapi/... - go test -v -cover -race -timeout 10s ./pkg/networkutils/... - go test -v -cover -race -timeout 10s ./ipamd/... - -#golint + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 150s ./pkg/awsutils/... + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 10s ./plugins/routed-eni/... + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 10s ./plugins/routed-eni/driver + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 10s ./pkg/k8sapi/... + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 10s ./pkg/networkutils/... + GOOS=linux CGO_ENABLED=1 go test -v -cover -race -timeout 10s ./ipamd/... + +# golint +# To install: go get -u golang.org/x/lint/golint lint: golint pkg/awsutils/*.go golint plugins/routed-eni/*.go @@ -56,3 +57,7 @@ vet: go tool vet ./plugins/routed-eni go tool vet ./pkg/k8sapi go tool vet ./pkg/networkutils + +clean: + rm -f aws-k8s-agent + rm -f aws-cni diff --git a/README.md b/README.md index f075a8e9013..bfb63591482 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ L-IPAM requires following [IAM policy](https://docs.aws.amazon.com/IAM/latest/Us ## Building -* `make static` builds the binary files +* `make` defaults to `make build-linux` that builds the Linux binaries. * `make docker-build` uses a docker container (golang:1.10) to build the binaries. * `make docker` will create a docker container using the docker-build with the finished binaries, with a tag of `amazon/amazon-k8s-cni:latest` * `unit-test`, `lint` and `vet` provide ways to run the respective tests/tools and should be run before submitting a PR. diff --git a/plugins/routed-eni/cni.go b/plugins/routed-eni/cni.go index 0de7fa2f4e4..ecf2a1b818f 100644 --- a/plugins/routed-eni/cni.go +++ b/plugins/routed-eni/cni.go @@ -44,12 +44,11 @@ import ( const ( ipamDAddress = "localhost:50051" defaultLogFilePath = "/var/log/aws-routed-eni/plugin.log" - maxVethNameLen = 10 ) -// NetConf stores the common network config for CNI plugin +// NetConf stores the common network config for the CNI plugin type NetConf struct { - // CNIVersion is the version pluging + // CNIVersion is the plugin version CNIVersion string `json:"cniVersion,omitempty"` // Name is the plugin name diff --git a/rpc/rpc.proto b/rpc/rpc.proto index f39bdecc482..e91e02dd0ed 100644 --- a/rpc/rpc.proto +++ b/rpc/rpc.proto @@ -4,7 +4,7 @@ option java_multiple_files = true; option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto"; -package rpc;; +package rpc; // The service definition. service CNIBackend { diff --git a/scripts/install-aws.sh b/scripts/install-aws.sh index 69edde713aa..11ed0d2fe38 100755 --- a/scripts/install-aws.sh +++ b/scripts/install-aws.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash echo "=====Starting installing AWS-CNI =========" sed -i s/__VETHPREFIX__/${AWS_VPC_K8S_CNI_VETHPREFIX:-"eni"}/g /app/aws.conf cp /app/aws-cni /host/opt/cni/bin/ diff --git a/verify-aws.go b/verify-aws.go deleted file mode 100644 index 89b072b97cd..00000000000 --- a/verify-aws.go +++ /dev/null @@ -1,152 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils" - "github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger" - - log "github.com/cihub/seelog" - "math/rand" -) - -var testEC2Instance *awsutils.EC2InstanceMetadataCache - -const ( - defaultLogFilePath = "/var/log/verify-aws.log" -) - -func main() { - var testOK = true - defer log.Flush() - logger.SetupLogger(logger.GetLogFileLocation(defaultLogFilePath)) - // verify API: GetInstanceMetadata - testEC2Instance, _ = awsutils.New() - - testENIs, _ := testEC2Instance.GetAttachedENIs() - log.Infof("Number of current attached interface is %d \n", len(testENIs)) - - // detach and delete all interfaces except primary interface - for _, eni := range testENIs { - - log.Infof("*eni %s, primary eni %s\n", eni.ENIID, testEC2Instance.GetPrimaryENI()) - if eni.ENIID == testEC2Instance.GetPrimaryENI() { - log.Infof("skip deleting primary ENI %s\n", eni.ENIID) - continue - } - testEC2Instance.FreeENI(eni.ENIID) - } - - // verify only primary interface left - testENIs, err := testEC2Instance.GetAttachedENIs() - - if err != nil { - fmt.Print(" err: GetAttachedENIs()", err.Error()) - testOK = false - } - - log.Infof("Number of current attached interface is %d \n", len(testENIs)) - if len(testENIs) != 1 { - fmt.Println("=== Failed: GetInstanceMetadata, expected only primary interface") - testOK = false - } else { - fmt.Println("=== Passed OK: GetInstanceMetadata") - } - - // verify allocate IP address - testEC2Instance.AllocAllIPAddress(testEC2Instance.GetPrimaryENI()) - - //verify allocate MAX of ENI - if verifyFreeENIAWSAllocENI() != true { - testOK = false - fmt.Println("==== Failed: verifyFreeENIAWSAllocENI") - } else { - fmt.Println("===== Passed OK: verifyFreeENIAWSAllocENI") - } - - log.Infof("DONE: verify-aws testOK %v", testOK) - -} - -func verifyFreeENIAWSAllocENI() bool { - var enis []string - var result = true - - //verify allocate MAX number of ENI - for { - result, eni := verifyAllocENI(false) - - if !result { - break - } - fmt.Println("eni", eni) - enis = append(enis, eni) - } - - if len(enis) > 0 { - //pick radom eni to delete - eniIdx := rand.Intn(len(enis)) - - log.Debugf("Verify free eniIdx %d, eni : %s", eniIdx, enis[eniIdx]) - testEC2Instance.FreeENI(enis[eniIdx]) - - result, _ = verifyAllocENI(true) - } - - return result -} - -func verifyAllocENI(printResult bool) (bool, string) { - - // verify API: AllocENI - eni, err := testEC2Instance.AllocENI() - - if err != nil { - log.Errorf("verifyAllocENI: error from AllocENI %s", err) - } - - // will retry maximum of 5 * 5 seconds and see if the newly created ENI showing up on the instance - result := false - if eni != "" && err == nil { - err = testEC2Instance.AllocAllIPAddress(eni) - if err != nil { - log.Error("error on AllocAllIPAddress", err) - } - retry := 0 - for { - retry++ - if retry > 5 { - log.Info("exceed retry limit on GetInstanceMetaData") - break - } - testENIs, _ := testEC2Instance.GetAttachedENIs() - - // verify eni is in the returned eni list - - for _, returnedENI := range testENIs { - if eni == returnedENI.ENIID { - addrs, _, _ := testEC2Instance.DescribeENI(eni) - log.Infof("Number of address %d on eni %s", len(addrs), eni) - result = true - break - } - } - - if result != true { - time.Sleep(5 * time.Second) - continue - } - break - } - } - - if printResult { - if result == true { - log.Info("=== Passed OK: AllocENI") - } else { - log.Info("=== Failed: AllocENI %v", err) - } - } - return result, eni -} diff --git a/verify-network.go b/verify-network.go deleted file mode 100644 index 9ed16e93a20..00000000000 --- a/verify-network.go +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// -// or in the "license" file accompanying this file. This file 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 ( - "fmt" - "github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils" - "github.com/aws/amazon-vpc-cni-k8s/plugins/routed-eni/driver" - "net" -) - -const ( - // verify SetupENINetwork - // manually verify ip route show table eni-2 - // have corret default route out eth2 - // && ip route show does NOT default gw over eth2 on main table - eniDeviceNum = 1 // eth1 - eniIP = "10.0.77.104" - eniMAC = "02:7e:b6:0e:75:70" - updatedENISubnet = "10.0.64.0/19" - eniSubnet = "10.0.1.0/24" - - // verify SetupNodeNetwork - // manually verify iptables-save have correct NAT rule - nodeIP = "10.0.93.145" - vpcCIDR = "10.0.0.0/16" -) - -type podSpec struct { - VethName string - EthName string - IP string - NS string - Table int -} - -func main() { - hdlr := networkutils.New() - drv := driver.New() - - hdlr.SetupENINetwork(eniIP, eniMAC, eniDeviceNum, eniSubnet) - - hdlr.SetupENINetwork(eniIP, eniMAC, eniDeviceNum, updatedENISubnet) - - ip := net.ParseIP(nodeIP) - - _, cidr, _ := net.ParseCIDR(vpcCIDR) - err := hdlr.SetupHostNetwork(cidr, &ip) - - fmt.Printf("SetupNodeNetwork %v", err) - - // verify SetupPodNetwork - // TODO: make sure manually ip rule add not to table main priority 1024 - // manually verify ping between - // - pod to pod on same ENI - // - pod to pod on different ENs - // - pod to vpc's address on different node - // - pod to www.yahoo.com - // - PodList := []podSpec{ - podSpec{ - VethName: "veth-0", - EthName: "eth0", - IP: "10.0.64.219", - NS: "/proc/7058/ns/net", - Table: 0, - }, - podSpec{ - VethName: "veth-1", - EthName: "eth0", - IP: "10.0.92.55", - NS: "/proc/7114/ns/net", - Table: 1, - }, - podSpec{ - VethName: "veth-2", - EthName: "eth0", - IP: "10.0.71.56", - NS: "/proc/7163/ns/net", - Table: 1, - }, - } - - for _, pod := range PodList { - addr := &net.IPNet{ - IP: net.ParseIP(pod.IP), - Mask: net.IPv4Mask(255, 255, 255, 255), - } - err = drv.SetupNS(pod.VethName, pod.EthName, pod.NS, addr, pod.Table) - fmt.Printf("SetupPodNetwork Veth %s err %v", pod.VethName, err) - } - -}