Skip to content

Commit f687dee

Browse files
authored
'overlay' conflist generator for CNS (#2010)
1 parent 0bc72e4 commit f687dee

File tree

7 files changed

+95
-3
lines changed

7 files changed

+95
-3
lines changed

cns/cniconflist/generator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type DualStackOverlayGenerator struct {
5353
Writer io.WriteCloser
5454
}
5555

56+
// OverlayGenerator generates the Azure CNI conflist for all Overlay scenarios
57+
type OverlayGenerator struct {
58+
Writer io.WriteCloser
59+
}
60+
5661
// CiliumGenerator generates the Azure CNI conflist for the Cilium scenario
5762
type CiliumGenerator struct {
5863
Writer io.WriteCloser
@@ -74,6 +79,14 @@ func (v *DualStackOverlayGenerator) Close() error {
7479
return nil
7580
}
7681

82+
func (v *OverlayGenerator) Close() error {
83+
if err := v.Writer.Close(); err != nil {
84+
return errors.Wrap(err, "error closing generator")
85+
}
86+
87+
return nil
88+
}
89+
7790
func (v *CiliumGenerator) Close() error {
7891
if err := v.Writer.Close(); err != nil {
7992
return errors.Wrap(err, "error closing generator")

cns/cniconflist/generator_linux.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ func (v *DualStackOverlayGenerator) Generate() error {
8080
return nil
8181
}
8282

83+
// Generate writes the CNI conflist to the Generator's output stream
84+
func (v *OverlayGenerator) Generate() error {
85+
conflist := cniConflist{
86+
CNIVersion: overlaycniVersion,
87+
Name: overlaycniName,
88+
Plugins: []any{
89+
cni.NetworkConfig{
90+
Type: overlaycniType,
91+
Mode: cninet.OpModeTransparent,
92+
IPsToRouteViaHost: []string{nodeLocalDNSIP},
93+
IPAM: cni.IPAM{
94+
Type: network.AzureCNS,
95+
Mode: string(util.Overlay),
96+
},
97+
},
98+
portmapConfig,
99+
},
100+
}
101+
102+
enc := json.NewEncoder(v.Writer)
103+
enc.SetIndent("", "\t")
104+
if err := enc.Encode(conflist); err != nil {
105+
return errors.Wrap(err, "error encoding conflist to json")
106+
}
107+
108+
return nil
109+
}
110+
83111
// Generate writes the CNI conflist to the Generator's output stream
84112
func (v *CiliumGenerator) Generate() error {
85113
conflist := cniConflist{

cns/cniconflist/generator_linux_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (b *bufferWriteCloser) Close() error {
1818
}
1919

2020
func TestGenerateV4OverlayConflist(t *testing.T) {
21-
fixture := "testdata/fixtures/azure-linux-swift-overlay.conflist"
21+
fixture := "testdata/fixtures/azure-linux-swift-v4overlay.conflist"
2222

2323
buffer := new(bytes.Buffer)
2424
g := cniconflist.V4OverlayGenerator{Writer: &bufferWriteCloser{buffer}}
@@ -47,6 +47,21 @@ func TestGenerateDualStackOverlayConflist(t *testing.T) {
4747
assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes()))
4848
}
4949

50+
func TestGenerateOverlayConflist(t *testing.T) {
51+
fixture := "testdata/fixtures/azure-linux-swift-overlay.conflist"
52+
53+
buffer := new(bytes.Buffer)
54+
g := cniconflist.OverlayGenerator{Writer: &bufferWriteCloser{buffer}}
55+
err := g.Generate()
56+
assert.NoError(t, err)
57+
58+
fixtureBytes, err := os.ReadFile(fixture)
59+
assert.NoError(t, err)
60+
61+
// remove newlines and carriage returns in case these UTs are running on Windows
62+
assert.Equal(t, removeNewLines(fixtureBytes), removeNewLines(buffer.Bytes()))
63+
}
64+
5065
func TestGenerateCiliumConflist(t *testing.T) {
5166
fixture := "testdata/fixtures/cilium.conflist"
5267

cns/cniconflist/generator_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func (v *DualStackOverlayGenerator) Generate() error {
1414
return errNotImplemented
1515
}
1616

17+
func (v *OverlayGenerator) Generate() error {
18+
return errNotImplemented
19+
}
20+
1721
func (v *CiliumGenerator) Generate() error {
1822
return errNotImplemented
1923
}

cns/cniconflist/testdata/fixtures/azure-linux-swift-overlay.conflist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
"ipsToRouteViaHost": [
99
"169.254.20.10"
1010
],
11-
"executionMode": "v4swift",
1211
"ipam": {
13-
"mode": "v4overlay",
12+
"mode": "overlay",
1413
"type": "azure-cns"
1514
},
1615
"dns": {},
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
"mode": "v4overlay",
14+
"type": "azure-cns"
15+
},
16+
"dns": {},
17+
"runtimeConfig": {
18+
"dns": {}
19+
},
20+
"windowsSettings": {}
21+
},
22+
{
23+
"type": "portmap",
24+
"capabilities": {
25+
"portMappings": true
26+
},
27+
"snat": true
28+
}
29+
]
30+
}

cns/service/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type cniConflistScenario string
8888
const (
8989
scenarioV4Overlay cniConflistScenario = "v4overlay"
9090
scenarioDualStackOverlay cniConflistScenario = "dualStackOverlay"
91+
scenarioOverlay cniConflistScenario = "overlay"
9192
scenarioCilium cniConflistScenario = "cilium"
9293
)
9394

@@ -539,6 +540,8 @@ func main() {
539540
conflistGenerator = &cniconflist.V4OverlayGenerator{Writer: writer}
540541
case scenarioDualStackOverlay:
541542
conflistGenerator = &cniconflist.DualStackOverlayGenerator{Writer: writer}
543+
case scenarioOverlay:
544+
conflistGenerator = &cniconflist.OverlayGenerator{Writer: writer}
542545
case scenarioCilium:
543546
conflistGenerator = &cniconflist.CiliumGenerator{Writer: writer}
544547
default:

0 commit comments

Comments
 (0)