Skip to content

Commit e767b15

Browse files
authored
feat: CNS writes SWIFT conflist (#2110)
* cns writes swift conflist * gofumpt ed * var naming * add swift scenario to switch
1 parent 06e3877 commit e767b15

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

cns/cniconflist/generator.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const (
1616
overlaycniName = "azure" //nolint:unused,deadcode,varcheck // used in linux
1717
overlaycniType = "azure-vnet" //nolint:unused,deadcode,varcheck // used in linux
1818
nodeLocalDNSIP = "169.254.20.10" //nolint:unused,deadcode,varcheck // used in linux
19+
azurecniVersion = "0.3.0" //nolint:unused,deadcode,varcheck // used in linux
20+
azureName = "azure" //nolint:unused,deadcode,varcheck // used in linux
21+
azureType = "azure-vnet" //nolint:unused,deadcode,varcheck // used in linux
1922
)
2023

2124
// cniConflist represents the containernetworking/cni/pkg/types.NetConfList
@@ -63,6 +66,11 @@ type CiliumGenerator struct {
6366
Writer io.WriteCloser
6467
}
6568

69+
// SWIFTGenerator generates the Azure CNI conflist for the SWIFT scenario
70+
type SWIFTGenerator struct {
71+
Writer io.WriteCloser
72+
}
73+
6674
func (v *V4OverlayGenerator) Close() error {
6775
if err := v.Writer.Close(); err != nil {
6876
return errors.Wrap(err, "error closing generator")
@@ -94,3 +102,11 @@ func (v *CiliumGenerator) Close() error {
94102

95103
return nil
96104
}
105+
106+
func (v *SWIFTGenerator) Close() error {
107+
if err := v.Writer.Close(); err != nil {
108+
return errors.Wrap(err, "error closing generator")
109+
}
110+
111+
return nil
112+
}

cns/cniconflist/generator_linux.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,31 @@ func (v *CiliumGenerator) Generate() error {
133133

134134
return nil
135135
}
136+
137+
// Generate writes the CNI conflist to the Generator's output stream
138+
func (v *SWIFTGenerator) Generate() error {
139+
conflist := cniConflist{
140+
CNIVersion: azurecniVersion,
141+
Name: azureName,
142+
Plugins: []any{
143+
cni.NetworkConfig{
144+
Type: azureType,
145+
Mode: cninet.OpModeTransparent,
146+
ExecutionMode: string(util.V4Swift),
147+
IPsToRouteViaHost: []string{nodeLocalDNSIP},
148+
IPAM: cni.IPAM{
149+
Type: network.AzureCNS,
150+
},
151+
},
152+
portmapConfig,
153+
},
154+
}
155+
156+
enc := json.NewEncoder(v.Writer)
157+
enc.SetIndent("", "\t")
158+
if err := enc.Encode(conflist); err != nil {
159+
return errors.Wrap(err, "error encoding conflist to json")
160+
}
161+
162+
return nil
163+
}

cns/cniconflist/generator_linux_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ func TestGenerateCiliumConflist(t *testing.T) {
7777
assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes()))
7878
}
7979

80+
func TestGenerateSWIFTConflist(t *testing.T) {
81+
fixture := "testdata/fixtures/azure-linux-swift.conflist"
82+
83+
buffer := new(bytes.Buffer)
84+
g := cniconflist.SWIFTGenerator{Writer: &bufferWriteCloser{buffer}}
85+
err := g.Generate()
86+
assert.NoError(t, err)
87+
88+
fixtureBytes, err := os.ReadFile(fixture)
89+
assert.NoError(t, err)
90+
91+
// remove newlines and carriage returns in case these UTs are running on Windows
92+
assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes()))
93+
}
94+
8095
// removeNewLines will remove the newlines and carriage returns from the byte slice
8196
func removeNewLines(b []byte) []byte {
8297
var bb []byte //nolint:prealloc // can't prealloc since we don't know how many bytes will get removed

cns/cniconflist/generator_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ func (v *OverlayGenerator) Generate() error {
2121
func (v *CiliumGenerator) Generate() error {
2222
return errNotImplemented
2323
}
24+
25+
func (v *SWIFTGenerator) Generate() error {
26+
return errNotImplemented
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"cniVersion": "0.3.0",
3+
"name": "azure",
4+
"plugins": [
5+
{
6+
"type": "azure-vnet",
7+
"mode": "transparent",
8+
"ipsToRouteViaHost": [
9+
"169.254.20.10"
10+
],
11+
"executionMode": "v4swift",
12+
"ipam": {
13+
"type": "azure-cns"
14+
},
15+
"dns": {},
16+
"runtimeConfig": {
17+
"dns": {}
18+
},
19+
"windowsSettings": {}
20+
},
21+
{
22+
"type": "portmap",
23+
"capabilities": {
24+
"portMappings": true
25+
},
26+
"snat": true
27+
}
28+
]
29+
}

cns/service/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const (
9696
scenarioDualStackOverlay cniConflistScenario = "dualStackOverlay"
9797
scenarioOverlay cniConflistScenario = "overlay"
9898
scenarioCilium cniConflistScenario = "cilium"
99+
scenarioSWIFT cniConflistScenario = "swift"
99100
)
100101

101102
var (
@@ -550,6 +551,8 @@ func main() {
550551
conflistGenerator = &cniconflist.OverlayGenerator{Writer: writer}
551552
case scenarioCilium:
552553
conflistGenerator = &cniconflist.CiliumGenerator{Writer: writer}
554+
case scenarioSWIFT:
555+
conflistGenerator = &cniconflist.SWIFTGenerator{Writer: writer}
553556
default:
554557
logger.Errorf("unable to generate cni conflist for unknown scenario: %s", scenario)
555558
os.Exit(1)

0 commit comments

Comments
 (0)