Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ CNS_ARCHIVE_NAME = azure-cns-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
CNMS_ARCHIVE_NAME = azure-cnms-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
NPM_ARCHIVE_NAME = azure-npm-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
NPM_IMAGE_ARCHIVE_NAME = azure-npm-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
CNI_IMAGE_ARCHIVE_NAME = azure-cni-manager-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
CNMS_IMAGE_ARCHIVE_NAME = azure-cnms-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
TELEMETRY_IMAGE_ARCHIVE_NAME = azure-vnet-telemetry-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
CNS_IMAGE_ARCHIVE_NAME = azure-cns-$(GOOS)-$(GOARCH)-$(VERSION).$(ARCHIVE_EXT)
Expand Down Expand Up @@ -279,6 +280,7 @@ tools: acncli
.PHONY: tools-images
tools-images:
docker build --no-cache -f ./tools/acncli/Dockerfile --build-arg VERSION=$(VERSION) -t $(AZURE_CNI_IMAGE):$(VERSION) .
docker save $(AZURE_CNI_IMAGE):$(VERSION) | gzip -c > $(IMAGE_DIR)/$(CNI_IMAGE_ARCHIVE_NAME)

# Build the Azure CNM plugin image, installable with "docker plugin install".
.PHONY: azure-vnet-plugin-image
Expand Down
28 changes: 21 additions & 7 deletions cni/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/log"
semver "github.com/hashicorp/go-version"
utilexec "k8s.io/utils/exec"
)

Expand All @@ -24,13 +26,7 @@ type AzureCNIClient struct {
exec utilexec.Interface
}

func NewCNIClient(exec utilexec.Interface) *AzureCNIClient {
return &AzureCNIClient{
exec: exec,
}
}

func (c *AzureCNIClient) GetEndpointState() (api.CNIState, error) {
func (c *AzureCNIClient) GetEndpointState() (*api.AzureCNIState, error) {
cmd := c.exec.Command(azureVnetBinName)
cmd.SetDir(azureVnetBinDirectory)

Expand All @@ -52,3 +48,21 @@ func (c *AzureCNIClient) GetEndpointState() (api.CNIState, error) {

return state, nil
}

func (c *AzureCNIClient) GetVersion() (*semver.Version, error) {
cmd := c.exec.Command(azureVnetBinName, "-v")
cmd.SetDir(azureVnetBinDirectory)

output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to get Azure CNI version with err: [%w], output: [%s]", err, string(output))
}

res := strings.Fields(string(output))

if len(res) != 4 {
return nil, fmt.Errorf("Unexpected Azure CNI Version formatting: %v", output)
}

return semver.NewVersion(res[3])
}
60 changes: 46 additions & 14 deletions cni/client/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,62 @@ package client

import (
"io"
"log"
"os"
"testing"

"github.com/Azure/azure-container-networking/cni/api"
testutils "github.com/Azure/azure-container-networking/test/utils"
ver "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
"k8s.io/utils/exec"
)

// todo: enable this test in CI, requires built azure vnet
func TestGetStateFromAzureCNI(t *testing.T) {
testutils.RequireRootforTest(t)

func TestMain(m *testing.M) {
testutils.RequireRootforTestMain()
var err error
// copy test state file to /var/run/azure-vnet.json
in, err := os.Open("./testresources/azure-vnet-test.json")
require.NoError(t, err)

defer in.Close()
if err != nil {
return
}

out, err := os.Create("/var/run/azure-vnet.json")
require.NoError(t, err)
if err != nil {
return
}

exit := 0
defer func() {
out.Close()
if in != nil {
in.Close()
}

if out != nil {
out.Close()
}

err := os.Remove("/var/run/azure-vnet.json")
require.NoError(t, err)
if err != nil {
log.Print(err)
os.Exit(1)
}

os.Exit(exit)
}()

_, err = io.Copy(out, in)
require.NoError(t, err)
if err != nil {
return
}

exit = m.Run()
}

out.Close()
// todo: enable this test in CI, requires built azure vnet
func TestGetStateFromAzureCNI(t *testing.T) {

realexec := exec.New()
c := NewCNIClient(realexec)
c := AzureCNIClient{exec: exec.New()}
state, err := c.GetEndpointState()
require.NoError(t, err)

Expand All @@ -52,3 +73,14 @@ func TestGetStateFromAzureCNI(t *testing.T) {

require.Exactly(t, res, state)
}

func TestGetVersion(t *testing.T) {
c := &AzureCNIClient{exec: exec.New()}
version, err := c.GetVersion()
require.NoError(t, err)

expectedVersion, err := ver.NewVersion("v1.4.0-2-g984c5a5e-dirty")
require.NoError(t, err)

require.Equal(t, expectedVersion, version)
}
20 changes: 19 additions & 1 deletion cni/client/client_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-container-networking/cni/api"
testutils "github.com/Azure/azure-container-networking/test/utils"
ver "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
)

Expand All @@ -17,7 +18,7 @@ func TestGetState(t *testing.T) {

fakeexec, _ := testutils.GetFakeExecWithScripts(calls)

c := NewCNIClient(fakeexec)
c := &AzureCNIClient{exec: fakeexec}
state, err := c.GetEndpointState()
require.NoError(t, err)

Expand All @@ -30,3 +31,20 @@ func TestGetState(t *testing.T) {

require.Equal(t, res, state)
}

func TestGetVersion(t *testing.T) {
calls := []testutils.TestCmd{
{Cmd: []string{"./azure-vnet", "-v"}, Stdout: `Azure CNI Version v1.4.0-2-g984c5a5e-dirty`},
}

fakeexec, _ := testutils.GetFakeExecWithScripts(calls)

c := &AzureCNIClient{exec: fakeexec}
version, err := c.GetVersion()
require.NoError(t, err)

expectedVersion, err := ver.NewVersion("v1.4.0-2-g984c5a5e-dirty")
require.NoError(t, err)

require.Equal(t, expectedVersion, version)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/golang/mock v1.2.0
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/ishidawataru/sctp v0.0.0-20210226210310-f2269e66cdee // indirect
Expand Down Expand Up @@ -43,9 +44,11 @@ require (
google.golang.org/protobuf v1.25.0
gotest.tools/v3 v3.0.2 // indirect
k8s.io/api v0.18.19
k8s.io/apiextensions-apiserver v0.18.2
k8s.io/apimachinery v0.18.19
k8s.io/client-go v0.18.19
k8s.io/klog v1.0.0
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
sigs.k8s.io/controller-runtime v0.6.0
sigs.k8s.io/yaml v1.2.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:Fecb
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have Masterminds/semver vendored, but this package seems to be better supported and it's by hashicorp

github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
Expand Down
2 changes: 1 addition & 1 deletion test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func RequireRootforTest(t *testing.T) {
}
}

func RequireRootforTestMain(m *testing.M) {
func RequireRootforTestMain() {
if !isCurrentUserRoot() {
log.Printf("These tests require root!")
os.Exit(1)
Expand Down
25 changes: 25 additions & 0 deletions vendor/github.com/hashicorp/go-version/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading