Skip to content

Commit 98f838e

Browse files
Write to intermediate file before moving to state file (#755)
* write to temp file and move to state file * fixed memleak and other issues * call windows replace function with MOVEFILE_WRITE_THROUGH flag * moved few functions to platform package * moved test files to correct dir * addressed comments
1 parent c3aa60f commit 98f838e

File tree

20 files changed

+289
-195
lines changed

20 files changed

+289
-195
lines changed

cni/ipam/ipam.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@ func (plugin *ipamPlugin) Delete(args *cniSkel.CmdArgs) error {
294294
return err
295295
}
296296

297-
// Release the pool.
298-
plugin.am.ReleasePool(nwCfg.Ipam.AddrSpace, nwCfg.Ipam.Subnet)
299-
300297
return nil
301298
}
302299

cni/network/invoker_azure.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (invoker *AzureIPAMInvoker) Add(nwCfg *cni.NetworkConfig, subnetPrefix *net
5858
}()
5959

6060
if nwCfg.IPV6Mode != "" {
61-
nwCfg6 := nwCfg
61+
nwCfg6 := *nwCfg
6262
nwCfg6.Ipam.Environment = common.OptEnvironmentIPv6NodeIpam
6363
nwCfg6.Ipam.Type = ipamV6
6464

@@ -67,7 +67,7 @@ func (invoker *AzureIPAMInvoker) Add(nwCfg *cni.NetworkConfig, subnetPrefix *net
6767
nwCfg6.Ipam.Subnet = invoker.nwInfo.Subnets[1].Prefix.String()
6868
}
6969

70-
resultV6, err = invoker.plugin.DelegateAdd(ipamV6, nwCfg6)
70+
resultV6, err = invoker.plugin.DelegateAdd(nwCfg6.Ipam.Type, &nwCfg6)
7171
if err != nil {
7272
err = invoker.plugin.Errorf("Failed to allocate v6 pool: %v", err)
7373
}
@@ -83,18 +83,18 @@ func (invoker *AzureIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkCo
8383

8484
if nwCfg == nil {
8585
return invoker.plugin.Errorf("nil nwCfg passed to CNI ADD, stack: %+v", string(debug.Stack()))
86-
} else if address == nil {
86+
}
87+
88+
if len(invoker.nwInfo.Subnets) > 0 {
89+
nwCfg.Ipam.Subnet = invoker.nwInfo.Subnets[0].Prefix.String()
90+
}
91+
92+
if address == nil {
8793
if err := invoker.plugin.DelegateDel(nwCfg.Ipam.Type, nwCfg); err != nil {
88-
return invoker.plugin.Errorf("Network not found, attempted to release address with error: %v", err)
94+
return invoker.plugin.Errorf("Attempted to release address with error: %v", err)
8995
}
9096
} else if len(address.IP.To4()) == 4 {
91-
92-
// cleanup pool
93-
if options[optReleasePool] == optValPool {
94-
nwCfg.Ipam.Address = ""
95-
}
96-
97-
nwCfg.Ipam.Subnet = invoker.nwInfo.Subnets[0].Prefix.String()
97+
nwCfg.Ipam.Address = address.IP.String()
9898
log.Printf("Releasing ipv4 address :%s pool: %s",
9999
nwCfg.Ipam.Address, nwCfg.Ipam.Subnet)
100100
if err := invoker.plugin.DelegateDel(nwCfg.Ipam.Type, nwCfg); err != nil {
@@ -105,6 +105,7 @@ func (invoker *AzureIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkCo
105105
nwCfgIpv6 := *nwCfg
106106
nwCfgIpv6.Ipam.Environment = common.OptEnvironmentIPv6NodeIpam
107107
nwCfgIpv6.Ipam.Type = ipamV6
108+
nwCfgIpv6.Ipam.Address = address.IP.String()
108109
if len(invoker.nwInfo.Subnets) > 1 {
109110
nwCfgIpv6.Ipam.Subnet = invoker.nwInfo.Subnets[1].Prefix.String()
110111
}

cni/network/network.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
// Supported IP version. Currently support only IPv4
3737
ipVersion = "4"
3838
ipamV6 = "azure-vnet-ipamv6"
39-
optReleasePool = "DeleteOnErr"
40-
optValPool = "pool"
4139
)
4240

4341
// CNI Operation Types
@@ -438,20 +436,17 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
438436
log.Printf("[cni-net] Creating network %v.", networkId)
439437

440438
if !nwCfg.MultiTenancy {
441-
442-
options[optReleasePool] = optValPool
443439
result, resultV6, err = plugin.ipamInvoker.Add(nwCfg, &subnetPrefix, options)
444440
if err != nil {
445441
return err
446442
}
447443

448444
defer func() {
449445
if err != nil {
450-
options[optReleasePool] = optValPool
451446
if result != nil && len(result.IPs) > 0 {
452447
plugin.ipamInvoker.Delete(&result.IPs[0].Address, nwCfg, options)
453448
}
454-
if resultV6 != nil && len(result.IPs) > 0 {
449+
if resultV6 != nil && len(resultV6.IPs) > 0 {
455450
plugin.ipamInvoker.Delete(&resultV6.IPs[0].Address, nwCfg, options)
456451
}
457452
}
@@ -460,6 +455,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
460455

461456
gateway := result.IPs[0].Gateway
462457
subnetPrefix.IP = subnetPrefix.IP.Mask(subnetPrefix.Mask)
458+
nwCfg.Ipam.Subnet = subnetPrefix.String()
463459
// Find the master interface.
464460
masterIfName := plugin.findMasterInterface(nwCfg, &subnetPrefix)
465461
if masterIfName == "" {
@@ -541,7 +537,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
541537
if !nwCfg.MultiTenancy {
542538
// Network already exists.
543539
log.Printf("[cni-net] Found network %v with subnet %v.", networkId, nwInfo.Subnets[0].Prefix.String())
544-
nwInfo.Options[optReleasePool] = ""
545540
result, resultV6, err = plugin.ipamInvoker.Add(nwCfg, &subnetPrefix, nwInfo.Options)
546541
if err != nil {
547542
return err
@@ -551,8 +546,12 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
551546

552547
defer func() {
553548
if err != nil {
554-
nwInfo.Options[optReleasePool] = ""
555-
plugin.ipamInvoker.Delete(&result.IPs[0].Address, nwCfg, nwInfo.Options)
549+
if result != nil && len(result.IPs) > 0 {
550+
plugin.ipamInvoker.Delete(&result.IPs[0].Address, nwCfg, nwInfo.Options)
551+
}
552+
if resultV6 != nil && len(resultV6.IPs) > 0 {
553+
plugin.ipamInvoker.Delete(&resultV6.IPs[0].Address, nwCfg, nwInfo.Options)
554+
}
556555
}
557556
}()
558557
}
@@ -847,6 +846,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
847846
if !nwCfg.MultiTenancy {
848847
// attempt to release address associated with this Endpoint id
849848
// This is to ensure clean up is done even in failure cases
849+
log.Printf("release ip ep not found")
850850
if err = plugin.ipamInvoker.Delete(nil, nwCfg, nwInfo.Options); err != nil {
851851
log.Printf("Endpoint not found, attempted to release address with error: %v", err)
852852
}
@@ -878,8 +878,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
878878
if !nwCfg.MultiTenancy {
879879
// Call into IPAM plugin to release the endpoint's addresses.
880880
for _, address := range epInfo.IPAddresses {
881-
nwCfg.Ipam.Address = address.IP.String()
882-
nwInfo.Options[optReleasePool] = ""
881+
log.Printf("release ip:%s", address.IP.String())
883882
err = plugin.ipamInvoker.Delete(&address, nwCfg, nwInfo.Options)
884883
if err != nil {
885884
err = plugin.Errorf("Failed to release address %v with error: %v", address, err)

cnm/plugin/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func main() {
152152
return
153153
}
154154

155-
err = common.CreateDirectory(storeFileLocation)
155+
err = platform.CreateDirectory(storeFileLocation)
156156
if err != nil {
157157
log.Errorf("Failed to create File Store directory %s, due to Error:%v", storeFileLocation, err.Error())
158158
return

cns/service/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ func main() {
439439
// Log platform information.
440440
logger.Printf("Running on %v", platform.GetOSInfo())
441441

442-
err = acn.CreateDirectory(storeFileLocation)
442+
err = platform.CreateDirectory(storeFileLocation)
443443
if err != nil {
444444
logger.Errorf("Failed to create File Store directory %s, due to Error:%v", storeFileLocation, err.Error())
445445
return

common/utils.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
package common
55

66
import (
7-
"bufio"
87
"encoding/binary"
98
"encoding/json"
109
"encoding/xml"
1110
"fmt"
12-
"io"
1311
"io/ioutil"
1412
"net"
1513
"net/http"
@@ -123,35 +121,6 @@ func LogNetworkInterfaces() {
123121
}
124122
}
125123

126-
func CheckIfFileExists(filepath string) (bool, error) {
127-
_, err := os.Stat(filepath)
128-
if err == nil {
129-
return true, nil
130-
}
131-
132-
if os.IsNotExist(err) {
133-
return false, nil
134-
}
135-
136-
return true, err
137-
}
138-
139-
func CreateDirectory(dirPath string) error {
140-
var err error
141-
142-
if dirPath == "" {
143-
log.Printf("dirPath is empty, nothing to create.")
144-
return nil
145-
}
146-
147-
isExist, _ := CheckIfFileExists(dirPath)
148-
if !isExist {
149-
err = os.Mkdir(dirPath, os.ModePerm)
150-
}
151-
152-
return err
153-
}
154-
155124
func IpToInt(ip net.IP) uint32 {
156125
if len(ip) == 16 {
157126
return binary.BigEndian.Uint32(ip[12:16])
@@ -200,38 +169,6 @@ func StartProcess(path string, args []string) error {
200169
return err
201170
}
202171

203-
// ReadFileByLines reads file line by line and return array of lines.
204-
func ReadFileByLines(filename string) ([]string, error) {
205-
var (
206-
lineStrArr []string
207-
)
208-
209-
f, err := os.Open(filename)
210-
if err != nil {
211-
return nil, fmt.Errorf("Error opening %s file error %v", filename, err)
212-
}
213-
214-
defer f.Close()
215-
216-
r := bufio.NewReader(f)
217-
218-
for {
219-
lineStr, err := r.ReadString('\n')
220-
if err != nil {
221-
if err != io.EOF {
222-
return nil, fmt.Errorf("Error reading %s file error %v", filename, err)
223-
}
224-
225-
lineStrArr = append(lineStrArr, lineStr)
226-
break
227-
}
228-
229-
lineStrArr = append(lineStrArr, lineStr)
230-
}
231-
232-
return lineStrArr, nil
233-
}
234-
235172
// GetHostMetadata - retrieve VM metadata from wireserver
236173
func GetHostMetadata(fileName string) (Metadata, error) {
237174
content, err := ioutil.ReadFile(fileName)

common/utils_test.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

ipam/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func (am *addressManager) StartSource(options map[string]interface{}) error {
187187
var isLoaded bool
188188
environment, _ := options[common.OptEnvironment].(string)
189189

190-
if am.AddrSpaces != nil && len(am.AddrSpaces) > 0 {
190+
if am.AddrSpaces != nil && len(am.AddrSpaces) > 0 &&
191+
am.AddrSpaces[LocalDefaultAddressSpaceId] != nil &&
192+
len(am.AddrSpaces[LocalDefaultAddressSpaceId].Pools) > 0 {
191193
isLoaded = true
192194
}
193195

0 commit comments

Comments
 (0)