From e767b150cdcd43577832342e69a8d60c526d930f Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 23 Aug 2023 16:04:05 -0400 Subject: [PATCH] feat: CNS writes SWIFT conflist (#2110) * cns writes swift conflist * gofumpt ed * var naming * add swift scenario to switch --- cns/cniconflist/generator.go | 16 ++++++++++ cns/cniconflist/generator_linux.go | 28 ++++++++++++++++++ cns/cniconflist/generator_linux_test.go | 15 ++++++++++ cns/cniconflist/generator_windows.go | 4 +++ .../fixtures/azure-linux-swift.conflist | 29 +++++++++++++++++++ cns/service/main.go | 3 ++ 6 files changed, 95 insertions(+) create mode 100644 cns/cniconflist/testdata/fixtures/azure-linux-swift.conflist diff --git a/cns/cniconflist/generator.go b/cns/cniconflist/generator.go index 240f4f5b7..1e154dd80 100644 --- a/cns/cniconflist/generator.go +++ b/cns/cniconflist/generator.go @@ -16,6 +16,9 @@ const ( overlaycniName = "azure" //nolint:unused,deadcode,varcheck // used in linux overlaycniType = "azure-vnet" //nolint:unused,deadcode,varcheck // used in linux nodeLocalDNSIP = "169.254.20.10" //nolint:unused,deadcode,varcheck // used in linux + azurecniVersion = "0.3.0" //nolint:unused,deadcode,varcheck // used in linux + azureName = "azure" //nolint:unused,deadcode,varcheck // used in linux + azureType = "azure-vnet" //nolint:unused,deadcode,varcheck // used in linux ) // cniConflist represents the containernetworking/cni/pkg/types.NetConfList @@ -63,6 +66,11 @@ type CiliumGenerator struct { Writer io.WriteCloser } +// SWIFTGenerator generates the Azure CNI conflist for the SWIFT scenario +type SWIFTGenerator struct { + Writer io.WriteCloser +} + func (v *V4OverlayGenerator) Close() error { if err := v.Writer.Close(); err != nil { return errors.Wrap(err, "error closing generator") @@ -94,3 +102,11 @@ func (v *CiliumGenerator) Close() error { return nil } + +func (v *SWIFTGenerator) Close() error { + if err := v.Writer.Close(); err != nil { + return errors.Wrap(err, "error closing generator") + } + + return nil +} diff --git a/cns/cniconflist/generator_linux.go b/cns/cniconflist/generator_linux.go index 64d299370..1c65cdbff 100644 --- a/cns/cniconflist/generator_linux.go +++ b/cns/cniconflist/generator_linux.go @@ -133,3 +133,31 @@ func (v *CiliumGenerator) Generate() error { return nil } + +// Generate writes the CNI conflist to the Generator's output stream +func (v *SWIFTGenerator) Generate() error { + conflist := cniConflist{ + CNIVersion: azurecniVersion, + Name: azureName, + Plugins: []any{ + cni.NetworkConfig{ + Type: azureType, + Mode: cninet.OpModeTransparent, + ExecutionMode: string(util.V4Swift), + IPsToRouteViaHost: []string{nodeLocalDNSIP}, + IPAM: cni.IPAM{ + Type: network.AzureCNS, + }, + }, + portmapConfig, + }, + } + + enc := json.NewEncoder(v.Writer) + enc.SetIndent("", "\t") + if err := enc.Encode(conflist); err != nil { + return errors.Wrap(err, "error encoding conflist to json") + } + + return nil +} diff --git a/cns/cniconflist/generator_linux_test.go b/cns/cniconflist/generator_linux_test.go index 3f202907b..982cabcee 100644 --- a/cns/cniconflist/generator_linux_test.go +++ b/cns/cniconflist/generator_linux_test.go @@ -77,6 +77,21 @@ func TestGenerateCiliumConflist(t *testing.T) { assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes())) } +func TestGenerateSWIFTConflist(t *testing.T) { + fixture := "testdata/fixtures/azure-linux-swift.conflist" + + buffer := new(bytes.Buffer) + g := cniconflist.SWIFTGenerator{Writer: &bufferWriteCloser{buffer}} + err := g.Generate() + assert.NoError(t, err) + + fixtureBytes, err := os.ReadFile(fixture) + assert.NoError(t, err) + + // remove newlines and carriage returns in case these UTs are running on Windows + assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes())) +} + // removeNewLines will remove the newlines and carriage returns from the byte slice func removeNewLines(b []byte) []byte { var bb []byte //nolint:prealloc // can't prealloc since we don't know how many bytes will get removed diff --git a/cns/cniconflist/generator_windows.go b/cns/cniconflist/generator_windows.go index 027cde81d..31551f4e5 100644 --- a/cns/cniconflist/generator_windows.go +++ b/cns/cniconflist/generator_windows.go @@ -21,3 +21,7 @@ func (v *OverlayGenerator) Generate() error { func (v *CiliumGenerator) Generate() error { return errNotImplemented } + +func (v *SWIFTGenerator) Generate() error { + return errNotImplemented +} diff --git a/cns/cniconflist/testdata/fixtures/azure-linux-swift.conflist b/cns/cniconflist/testdata/fixtures/azure-linux-swift.conflist new file mode 100644 index 000000000..eb8ca134a --- /dev/null +++ b/cns/cniconflist/testdata/fixtures/azure-linux-swift.conflist @@ -0,0 +1,29 @@ +{ + "cniVersion": "0.3.0", + "name": "azure", + "plugins": [ + { + "type": "azure-vnet", + "mode": "transparent", + "ipsToRouteViaHost": [ + "169.254.20.10" + ], + "executionMode": "v4swift", + "ipam": { + "type": "azure-cns" + }, + "dns": {}, + "runtimeConfig": { + "dns": {} + }, + "windowsSettings": {} + }, + { + "type": "portmap", + "capabilities": { + "portMappings": true + }, + "snat": true + } + ] +} diff --git a/cns/service/main.go b/cns/service/main.go index f160b5e13..b7d9def43 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -96,6 +96,7 @@ const ( scenarioDualStackOverlay cniConflistScenario = "dualStackOverlay" scenarioOverlay cniConflistScenario = "overlay" scenarioCilium cniConflistScenario = "cilium" + scenarioSWIFT cniConflistScenario = "swift" ) var ( @@ -550,6 +551,8 @@ func main() { conflistGenerator = &cniconflist.OverlayGenerator{Writer: writer} case scenarioCilium: conflistGenerator = &cniconflist.CiliumGenerator{Writer: writer} + case scenarioSWIFT: + conflistGenerator = &cniconflist.SWIFTGenerator{Writer: writer} default: logger.Errorf("unable to generate cni conflist for unknown scenario: %s", scenario) os.Exit(1)