Skip to content

Commit c2665f3

Browse files
adds environment flag fileIPAM (Azure#482)
* adds environment flag fileIPAM * "creating fileIpamSource once" * Update fileIpam.go well spaced if-1 * Update fileIpam_test.go well spaced if-2 * comment improved * smarter code structure
1 parent 47a35e6 commit c2665f3

File tree

6 files changed

+63
-30
lines changed

6 files changed

+63
-30
lines changed

cnm/plugin/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var args = common.ArgumentList{
3434
Type: "string",
3535
DefaultValue: common.OptEnvironmentAzure,
3636
ValueMap: map[string]interface{}{
37-
common.OptEnvironmentAzure: 0,
38-
common.OptEnvironmentMAS: 0,
37+
common.OptEnvironmentAzure: 0,
38+
common.OptEnvironmentMAS: 0,
39+
common.OptEnvironmentFileIpam: 0,
3940
},
4041
},
4142
{

cns/service/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ var args = acn.ArgumentList{
4949
Type: "string",
5050
DefaultValue: acn.OptEnvironmentAzure,
5151
ValueMap: map[string]interface{}{
52-
acn.OptEnvironmentAzure: 0,
53-
acn.OptEnvironmentMAS: 0,
52+
acn.OptEnvironmentAzure: 0,
53+
acn.OptEnvironmentMAS: 0,
54+
acn.OptEnvironmentFileIpam: 0,
5455
},
5556
},
5657

common/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ package common
66
// Command line options.
77
const (
88
// Operating environment.
9-
OptEnvironment = "environment"
10-
OptEnvironmentAlias = "e"
11-
OptEnvironmentAzure = "azure"
12-
OptEnvironmentMAS = "mas"
9+
OptEnvironment = "environment"
10+
OptEnvironmentAlias = "e"
11+
OptEnvironmentAzure = "azure"
12+
OptEnvironmentMAS = "mas"
13+
OptEnvironmentFileIpam = "fileIpam"
1314

1415
// API server URL.
1516
OptAPIServerURL = "api-url"

ipam/mas.go renamed to ipam/fileIpam.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
"runtime"
1212
"strings"
1313

14+
"github.com/Azure/azure-container-networking/common"
1415
"github.com/Azure/azure-container-networking/log"
1516
)
1617

1718
const (
1819
defaultLinuxFilePath = "/etc/kubernetes/interfaces.json"
1920
defaultWindowsFilePath = `c:\k\interfaces.json`
2021
windows = "windows"
21-
name = "MAS"
2222
)
2323

2424
// Microsoft Azure Stack IPAM configuration source.
25-
type masSource struct {
25+
type fileIpamSource struct {
2626
name string
2727
sink addressConfigSink
2828
fileLoaded bool
@@ -50,36 +50,39 @@ type IPAddress struct {
5050
IsPrimary bool
5151
}
5252

53-
// Creates the MAS source.
54-
func newMasSource(options map[string]interface{}) (*masSource, error) {
53+
// Creates the MAS/fileIpam source.
54+
func newFileIpamSource(options map[string]interface{}) (*fileIpamSource, error) {
5555
var filePath string
56+
var name string
57+
5658
if runtime.GOOS == windows {
5759
filePath = defaultWindowsFilePath
5860
} else {
5961
filePath = defaultLinuxFilePath
6062
}
6163

62-
return &masSource{
63-
name: name,
64+
name = options[common.OptEnvironment].(string)
65+
return &fileIpamSource{
66+
name: name,
6467
filePath: filePath,
6568
}, nil
6669
}
6770

6871
// Starts the MAS source.
69-
func (source *masSource) start(sink addressConfigSink) error {
72+
func (source *fileIpamSource) start(sink addressConfigSink) error {
7073
source.sink = sink
7174
return nil
7275
}
7376

7477
// Stops the MAS source.
75-
func (source *masSource) stop() {
78+
func (source *fileIpamSource) stop() {
7679
source.sink = nil
7780
}
7881

7982
// Refreshes configuration.
80-
func (source *masSource) refresh() error {
83+
func (source *fileIpamSource) refresh() error {
8184
if source == nil {
82-
return errors.New("masSource is nil")
85+
return errors.New("fileIpamSource is nil")
8386
}
8487

8588
if source.fileLoaded {

ipam/mas_test.go renamed to ipam/fileIpam_test.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"reflect"
66
"runtime"
77
"testing"
8+
9+
"github.com/Azure/azure-container-networking/common"
810
)
911

1012
func TestNewMasSource(t *testing.T) {
1113
options := make(map[string]interface{})
12-
mas, _ := newMasSource(options)
14+
options[common.OptEnvironment] = common.OptEnvironmentMAS
15+
mas, _ := newFileIpamSource(options)
1316

1417
if runtime.GOOS == windows {
1518
if mas.filePath != defaultWindowsFilePath {
@@ -20,14 +23,35 @@ func TestNewMasSource(t *testing.T) {
2023
t.Fatalf("default file path set incorrectly")
2124
}
2225
}
23-
if mas.name != "MAS" {
26+
27+
if mas.name != "mas" {
2428
t.Fatalf("mas source Name incorrect")
2529
}
2630
}
2731

32+
func TestNewFileIpamSource(t *testing.T) {
33+
options := make(map[string]interface{})
34+
options[common.OptEnvironment] = common.OptEnvironmentFileIPAM
35+
fileIpam, _ := newFileIpamSource(options)
36+
37+
if runtime.GOOS == windows {
38+
if fileIpam.filePath != defaultWindowsFilePath {
39+
t.Fatalf("default file path set incorrectly")
40+
}
41+
} else {
42+
if fileIpam.filePath != defaultLinuxFilePath {
43+
t.Fatalf("default file path set incorrectly")
44+
}
45+
}
46+
47+
if fileIpam.name != "fileIpam" {
48+
t.Fatalf("fileIpam source Name incorrect")
49+
}
50+
}
51+
2852
func TestGetSDNInterfaces(t *testing.T) {
2953
const validFileName = "testfiles/masInterfaceConfig.json"
30-
const invalidFileName = "mas_test.go"
54+
const invalidFileName = "fileIpam_test.go"
3155
const nonexistentFileName = "bad"
3256

3357
interfaces, err := getSDNInterfaces(validFileName)
@@ -166,11 +190,11 @@ func TestPopulateAddressSpaceMultipleSDNInterfaces(t *testing.T) {
166190
IsPrimary: true,
167191
IPSubnets: []IPSubnet{
168192
{
169-
Prefix: "0.0.0.0/24",
193+
Prefix: "0.0.0.0/24",
170194
IPAddresses: []IPAddress{},
171195
},
172196
{
173-
Prefix: "0.1.0.0/24",
197+
Prefix: "0.1.0.0/24",
174198
IPAddresses: []IPAddress{},
175199
},
176200
{
@@ -183,22 +207,22 @@ func TestPopulateAddressSpaceMultipleSDNInterfaces(t *testing.T) {
183207
},
184208
{
185209
MacAddress: "111111111111",
186-
IsPrimary: false,
210+
IsPrimary: false,
187211
IPSubnets: []IPSubnet{
188212
{
189-
Prefix: "1.0.0.0/24",
213+
Prefix: "1.0.0.0/24",
190214
IPAddresses: []IPAddress{},
191215
},
192216
{
193-
Prefix: "1.1.0.0/24",
217+
Prefix: "1.1.0.0/24",
194218
IPAddresses: []IPAddress{},
195219
},
196220
},
197221
},
198222
{
199223
MacAddress: "222222222222",
200-
IsPrimary: false,
201-
IPSubnets: []IPSubnet{},
224+
IsPrimary: false,
225+
IPSubnets: []IPSubnet{},
202226
},
203227
},
204228
}
@@ -263,4 +287,4 @@ func TestPopulateAddressSpaceMultipleSDNInterfaces(t *testing.T) {
263287
if pool.Priority != 1 {
264288
t.Fatalf("Incorrect interface priority. expected: %d, actual %d", 1, pool.Priority)
265289
}
266-
}
290+
}

ipam/manager.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ func (am *addressManager) StartSource(options map[string]interface{}) error {
190190
am.source, err = newAzureSource(options)
191191

192192
case common.OptEnvironmentMAS:
193-
am.source, err = newMasSource(options)
193+
am.source, err = newFileIpamSource(options)
194+
195+
case common.OptEnvironmentFileIpam:
196+
am.source, err = newFileIpamSource(options)
194197

195198
case "null":
196199
am.source, err = newNullSource()

0 commit comments

Comments
 (0)