feat: CNS writes SWIFT conflist (#2110)

* cns writes swift conflist

* gofumpt
ed

* var naming

* add swift scenario to switch
This commit is contained in:
Quang Nguyen 2023-08-23 16:04:05 -04:00 коммит произвёл GitHub
Родитель 06e3877cdf
Коммит e767b150cd
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 95 добавлений и 0 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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

Просмотреть файл

@ -21,3 +21,7 @@ func (v *OverlayGenerator) Generate() error {
func (v *CiliumGenerator) Generate() error {
return errNotImplemented
}
func (v *SWIFTGenerator) Generate() error {
return errNotImplemented
}

29
cns/cniconflist/testdata/fixtures/azure-linux-swift.conflist поставляемый Normal file
Просмотреть файл

@ -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
}
]
}

Просмотреть файл

@ -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)