Skip to content

Commit

Permalink
Merge pull request #418 from nckturner/renames
Browse files Browse the repository at this point in the history
Variable and function renames
  • Loading branch information
Claes Mogren authored Apr 26, 2019
2 parents 7c98b25 + 3d7ff88 commit 4f5d646
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 71 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
66 changes: 33 additions & 33 deletions ipamd/datastore/data_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var (
// ENIIPPool contains ENI/IP Pool information. Exported fields will be marshaled for introspection.
type ENIIPPool struct {
createTime time.Time
lastUnAssignedTime time.Time
lastUnassignedTime time.Time
// IsPrimary indicates whether ENI is a primary ENI
IsPrimary bool
ID string
Expand All @@ -99,9 +99,9 @@ type ENIIPPool struct {

// AddressInfo contains information about an IP, Exported fields will be marshaled for introspection.
type AddressInfo struct {
address string
Address string
Assigned bool // true if it is assigned to a pod
unAssignedTime time.Time
UnassignedTime time.Time
}

// PodKey is used to locate pod IP
Expand Down Expand Up @@ -180,8 +180,8 @@ func (ds *DataStore) AddENI(eniID string, deviceNumber int, isPrimary bool) erro
return nil
}

// AddENIIPv4Address add an IP of an ENI to data store
func (ds *DataStore) AddENIIPv4Address(eniID string, ipv4 string) error {
// AddIPv4AddressFromStore add an IP of an ENI to data store
func (ds *DataStore) AddIPv4AddressFromStore(eniID string, ipv4 string) error {
ds.lock.Lock()
defer ds.lock.Unlock()

Expand All @@ -202,13 +202,13 @@ func (ds *DataStore) AddENIIPv4Address(eniID string, ipv4 string) error {
// Prometheus gauge
totalIPs.Set(float64(ds.total))

curENI.IPv4Addresses[ipv4] = &AddressInfo{address: ipv4, Assigned: false}
curENI.IPv4Addresses[ipv4] = &AddressInfo{Address: ipv4, Assigned: false}
log.Infof("Added ENI(%s)'s IP %s to datastore", eniID, ipv4)
return nil
}

// DelENIIPv4Address delete an IP of ENI from datastore
func (ds *DataStore) DelENIIPv4Address(eniID string, ipv4 string) error {
// DelIPv4AddressFromStore delete an IP of ENI from datastore
func (ds *DataStore) DelIPv4AddressFromStore(eniID string, ipv4 string) error {
ds.lock.Lock()
defer ds.lock.Unlock()
log.Debugf("Deleting ENI(%s)'s IPv4 address %s from datastore", eniID, ipv4)
Expand Down Expand Up @@ -282,23 +282,23 @@ func (ds *DataStore) assignPodIPv4AddressUnsafe(k8sPod *k8sapi.K8SPodInfo) (stri
continue
}
for _, addr := range eni.IPv4Addresses {
if k8sPod.IP == addr.address {
if k8sPod.IP == addr.Address {
// After L-IPAM restart and built IP warm-pool, it needs to take the existing running pod IP out of the pool.
if !addr.Assigned {
incrementAssignedCount(ds, eni, addr)
}
log.Infof("AssignPodIPv4Address: Reassign IP %v to pod (name %s, namespace %s)",
addr.address, k8sPod.Name, k8sPod.Namespace)
ds.podsIP[podKey] = PodIPInfo{IP: addr.address, DeviceNumber: eni.DeviceNumber}
return addr.address, eni.DeviceNumber, nil
addr.Address, k8sPod.Name, k8sPod.Namespace)
ds.podsIP[podKey] = PodIPInfo{IP: addr.Address, DeviceNumber: eni.DeviceNumber}
return addr.Address, eni.DeviceNumber, nil
}
if !addr.Assigned && k8sPod.IP == "" && curTime.Sub(addr.unAssignedTime) > addressCoolingPeriod {
if !addr.Assigned && k8sPod.IP == "" && curTime.Sub(addr.UnassignedTime) > addressCoolingPeriod {
// This is triggered by a pod's Add Network command from CNI plugin
incrementAssignedCount(ds, eni, addr)
log.Infof("AssignPodIPv4Address: Assign IP %v to pod (name %s, namespace %s container %s)",
addr.address, k8sPod.Name, k8sPod.Namespace, k8sPod.Container)
ds.podsIP[podKey] = PodIPInfo{IP: addr.address, DeviceNumber: eni.DeviceNumber}
return addr.address, eni.DeviceNumber, nil
addr.Address, k8sPod.Name, k8sPod.Namespace, k8sPod.Container)
ds.podsIP[podKey] = PodIPInfo{IP: addr.Address, DeviceNumber: eni.DeviceNumber}
return addr.Address, eni.DeviceNumber, nil
}
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ func (ds *DataStore) getDeletableENI() *ENIIPPool {
continue
}

if time.Now().Sub(eni.lastUnAssignedTime) < addressENICoolingPeriod {
if time.Now().Sub(eni.lastUnassignedTime) < addressENICoolingPeriod {
continue
}

Expand Down Expand Up @@ -359,10 +359,10 @@ func (ds *DataStore) GetENINeedsIP(maxIPperENI int, skipPrimary bool) *ENIIPPool
return nil
}

// RemoveUnusedENIFromDataStore removes a deletable ENI from the data store.
// RemoveUnusedENIFromStore removes a deletable ENI from the data store.
// It returns the name of the ENI which has been removed from the data store and needs to be deleted,
// or empty string if no ENI could be removed.
func (ds *DataStore) RemoveUnusedENIFromDataStore() string {
func (ds *DataStore) RemoveUnusedENIFromStore() string {
ds.lock.Lock()
defer ds.lock.Unlock()

Expand All @@ -375,7 +375,7 @@ func (ds *DataStore) RemoveUnusedENIFromDataStore() string {
removableENI := deletableENI.ID
eniIPCount := len(ds.eniIPPools[removableENI].IPv4Addresses)
ds.total -= eniIPCount
log.Infof("RemoveUnusedENIFromDataStore %s: IP address pool stats: free %d addresses, total: %d, assigned: %d",
log.Infof("RemoveUnusedENIFromStore %s: IP address pool stats: free %d addresses, total: %d, assigned: %d",
removableENI, eniIPCount, ds.total, ds.assigned)
delete(ds.eniIPPools, removableENI)

Expand All @@ -385,8 +385,8 @@ func (ds *DataStore) RemoveUnusedENIFromDataStore() string {
return removableENI
}

// DeleteENI free a ENI.
func (ds *DataStore) DeleteENI(eni string) error {
// RemoveENIFromDataStore removes an ENI from the datastore. It return nil on success or an error.
func (ds *DataStore) RemoveENIFromDataStore(eni string) error {
ds.lock.Lock()
defer ds.lock.Unlock()

Expand All @@ -401,7 +401,7 @@ func (ds *DataStore) DeleteENI(eni string) error {
}

ds.total -= len(eniIPPool.IPv4Addresses)
log.Infof("DeleteENI %s: IP address pool stats: free %d addresses, total: %d, assigned: %d",
log.Infof("RemoveENIFromDataStore %s: IP address pool stats: free %d addresses, total: %d, assigned: %d",
eni, len(eniIPPool.IPv4Addresses), ds.total, ds.assigned)
delete(ds.eniIPPools, eni)

Expand All @@ -410,12 +410,12 @@ func (ds *DataStore) DeleteENI(eni string) error {
return nil
}

// UnAssignPodIPv4Address a) find out the IP address based on PodName and PodNameSpace
// UnassignPodIPv4Address a) find out the IP address based on PodName and PodNameSpace
// b) mark IP address as unassigned c) returns IP address, ENI's device number, error
func (ds *DataStore) UnAssignPodIPv4Address(k8sPod *k8sapi.K8SPodInfo) (string, int, error) {
func (ds *DataStore) UnassignPodIPv4Address(k8sPod *k8sapi.K8SPodInfo) (string, int, error) {
ds.lock.Lock()
defer ds.lock.Unlock()
log.Debugf("UnAssignPodIPv4Address: IP address pool stats: total:%d, assigned %d, pod(Name: %s, Namespace: %s, Container %s)",
log.Debugf("UnassignPodIPv4Address: IP address pool stats: total:%d, assigned %d, pod(Name: %s, Namespace: %s, Container %s)",
ds.total, ds.assigned, k8sPod.Name, k8sPod.Namespace, k8sPod.Container)

podKey := PodKey{
Expand All @@ -425,7 +425,7 @@ func (ds *DataStore) UnAssignPodIPv4Address(k8sPod *k8sapi.K8SPodInfo) (string,
}
ipAddr, ok := ds.podsIP[podKey]
if !ok {
log.Warnf("UnAssignPodIPv4Address: Failed to find pod %s namespace %s Container %s",
log.Warnf("UnassignPodIPv4Address: Failed to find pod %s namespace %s Container %s",
k8sPod.Name, k8sPod.Namespace, k8sPod.Container)
return "", 0, ErrUnknownPod
}
Expand All @@ -438,16 +438,16 @@ func (ds *DataStore) UnAssignPodIPv4Address(k8sPod *k8sapi.K8SPodInfo) (string,
assignedIPs.Set(float64(ds.assigned))
eni.AssignedIPv4Addresses--
curTime := time.Now()
ip.unAssignedTime = curTime
eni.lastUnAssignedTime = curTime
log.Infof("UnAssignPodIPv4Address: pod (Name: %s, NameSpace %s Container %s)'s ipAddr %s, DeviceNumber%d",
k8sPod.Name, k8sPod.Namespace, k8sPod.Container, ip.address, eni.DeviceNumber)
ip.UnassignedTime = curTime
eni.lastUnassignedTime = curTime
log.Infof("UnassignPodIPv4Address: pod (Name: %s, NameSpace %s Container %s)'s ipAddr %s, DeviceNumber%d",
k8sPod.Name, k8sPod.Namespace, k8sPod.Container, ip.Address, eni.DeviceNumber)
delete(ds.podsIP, podKey)
return ip.address, eni.DeviceNumber, nil
return ip.Address, eni.DeviceNumber, nil
}
}

log.Warnf("UnAssignPodIPv4Address: Failed to find pod %s namespace %s container %s using IP %s",
log.Warnf("UnassignPodIPv4Address: Failed to find pod %s namespace %s container %s using IP %s",
k8sPod.Name, k8sPod.Namespace, k8sPod.Container, ipAddr.IP)
return "", 0, ErrUnknownPodIP
}
Expand Down
48 changes: 24 additions & 24 deletions ipamd/datastore/data_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func TestDeleteENI(t *testing.T) {
eniInfos := ds.GetENIInfos()
assert.Equal(t, len(eniInfos.ENIIPPools), 3)

err = ds.DeleteENI("eni-2")
err = ds.RemoveENIFromDataStore("eni-2")
assert.NoError(t, err)

eniInfos = ds.GetENIInfos()
assert.Equal(t, len(eniInfos.ENIIPPools), 2)

err = ds.DeleteENI("unknown-eni")
err = ds.RemoveENIFromDataStore("unknown-eni")
assert.Error(t, err)

eniInfos = ds.GetENIInfos()
Expand All @@ -76,28 +76,28 @@ func TestAddENIIPv4Address(t *testing.T) {
err = ds.AddENI("eni-2", 2, false)
assert.NoError(t, err)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.1")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.1")
assert.NoError(t, err)
assert.Equal(t, ds.total, 1)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 1)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.1")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.1")
assert.Error(t, err)
assert.Equal(t, ds.total, 1)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 1)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.2")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 2)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)

err = ds.AddENIIPv4Address("eni-2", "1.1.2.2")
err = ds.AddIPv4AddressFromStore("eni-2", "1.1.2.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 3)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)
assert.Equal(t, len(ds.eniIPPools["eni-2"].IPv4Addresses), 1)

err = ds.AddENIIPv4Address("dummy-eni", "1.1.2.2")
err = ds.AddIPv4AddressFromStore("dummy-eni", "1.1.2.2")
assert.Error(t, err)
assert.Equal(t, ds.total, 3)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)
Expand All @@ -114,17 +114,17 @@ func TestGetENIIPPools(t *testing.T) {
err = ds.AddENI("eni-2", 2, false)
assert.NoError(t, err)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.1")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.1")
assert.NoError(t, err)
assert.Equal(t, ds.total, 1)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 1)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.2")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 2)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)

err = ds.AddENIIPv4Address("eni-2", "1.1.2.2")
err = ds.AddIPv4AddressFromStore("eni-2", "1.1.2.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 3)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)
Expand All @@ -143,28 +143,28 @@ func TestDelENIIPv4Address(t *testing.T) {
err := ds.AddENI("eni-1", 1, true)
assert.NoError(t, err)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.1")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.1")
assert.NoError(t, err)
assert.Equal(t, ds.total, 1)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 1)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.2")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 2)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)

err = ds.AddENIIPv4Address("eni-1", "1.1.1.3")
err = ds.AddIPv4AddressFromStore("eni-1", "1.1.1.3")
assert.NoError(t, err)
assert.Equal(t, ds.total, 3)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 3)

err = ds.DelENIIPv4Address("eni-1", "1.1.1.2")
err = ds.DelIPv4AddressFromStore("eni-1", "1.1.1.2")
assert.NoError(t, err)
assert.Equal(t, ds.total, 2)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)

// delete a unknown IP
err = ds.DelENIIPv4Address("eni-1", "10.10.10.10")
err = ds.DelIPv4AddressFromStore("eni-1", "10.10.10.10")
assert.Error(t, err)
assert.Equal(t, ds.total, 2)
assert.Equal(t, len(ds.eniIPPools["eni-1"].IPv4Addresses), 2)
Expand All @@ -177,11 +177,11 @@ func TestPodIPv4Address(t *testing.T) {

ds.AddENI("eni-2", 2, false)

ds.AddENIIPv4Address("eni-1", "1.1.1.1")
ds.AddIPv4AddressFromStore("eni-1", "1.1.1.1")

ds.AddENIIPv4Address("eni-1", "1.1.1.2")
ds.AddIPv4AddressFromStore("eni-1", "1.1.1.2")

ds.AddENIIPv4Address("eni-2", "1.1.2.2")
ds.AddIPv4AddressFromStore("eni-2", "1.1.2.2")

podInfo := k8sapi.K8SPodInfo{
Name: "pod-1",
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestPodIPv4Address(t *testing.T) {
_, _, err = ds.AssignPodIPv4Address(&podInfo)
assert.Error(t, err)
// Unassign unknown Pod
_, _, err = ds.UnAssignPodIPv4Address(&podInfo)
_, _, err = ds.UnassignPodIPv4Address(&podInfo)
assert.Error(t, err)

// Unassign pod which have same name/namespace, but different container
Expand All @@ -271,15 +271,15 @@ func TestPodIPv4Address(t *testing.T) {
Namespace: "ns-3",
Container: "container-2",
}
_, _, err = ds.UnAssignPodIPv4Address(&podInfo)
_, _, err = ds.UnassignPodIPv4Address(&podInfo)
assert.Error(t, err)

podInfo = k8sapi.K8SPodInfo{
Name: "pod-1",
Namespace: "ns-2",
}

_, deviceNum, err := ds.UnAssignPodIPv4Address(&podInfo)
_, deviceNum, err := ds.UnassignPodIPv4Address(&podInfo)
assert.NoError(t, err)
assert.Equal(t, ds.total, 3)
assert.Equal(t, ds.assigned, 2)
Expand All @@ -288,12 +288,12 @@ func TestPodIPv4Address(t *testing.T) {
assert.Equal(t, ds.eniIPPools["eni-2"].AssignedIPv4Addresses, 0)

// should not able to free this eni
eni := ds.RemoveUnusedENIFromDataStore()
eni := ds.RemoveUnusedENIFromStore()
assert.True(t, eni == "")

ds.eniIPPools["eni-2"].createTime = time.Time{}
ds.eniIPPools["eni-2"].lastUnAssignedTime = time.Time{}
eni = ds.RemoveUnusedENIFromDataStore()
ds.eniIPPools["eni-2"].lastUnassignedTime = time.Time{}
eni = ds.RemoveUnusedENIFromStore()
assert.Equal(t, eni, "eni-2")

assert.Equal(t, ds.total, 2)
Expand Down
Loading

0 comments on commit 4f5d646

Please sign in to comment.