Skip to content

Commit

Permalink
fix: get container ID from kube rather than docker (#371)
Browse files Browse the repository at this point in the history
* fix: get container ID from kube rather than docker
* chore: add log statement when containerID is found
* fix: log containerID after assignment
* fix: update unit tests
  • Loading branch information
Andrew Rudoi authored and Claes Mogren committed Jul 23, 2019
1 parent 02d2af8 commit 14de538
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 212 deletions.
24 changes: 0 additions & 24 deletions ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

"github.com/aws/amazon-vpc-cni-k8s/ipamd/datastore"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils"
"github.com/aws/amazon-vpc-cni-k8s/pkg/docker"
"github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
"github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils"
Expand Down Expand Up @@ -153,7 +152,6 @@ type IPAMContext struct {
k8sClient k8sapi.K8SAPIs
useCustomNetworking bool
eniConfig eniconfig.ENIConfig
dockerClient docker.APIs
networkClient networkutils.NetworkAPIs
maxIPsPerENI int
maxENI int
Expand Down Expand Up @@ -225,7 +223,6 @@ func New(k8sapiClient k8sapi.K8SAPIs, eniConfig *eniconfig.ENIConfigController)

c.k8sClient = k8sapiClient
c.networkClient = networkutils.New()
c.dockerClient = docker.New()
c.eniConfig = eniConfig

client, err := awsutils.New()
Expand Down Expand Up @@ -398,27 +395,6 @@ func (c *IPAMContext) getLocalPodsWithRetry() ([]*k8sapi.K8SPodInfo, error) {
return nil, nil
}

var containers map[string]*docker.ContainerInfo
for retry := 1; retry <= maxK8SRetries; retry++ {
containers, err = c.dockerClient.GetRunningContainers()
if err == nil {
break
}
log.Infof("Not able to get local containers yet (attempt %d/%d): %v", retry, maxK8SRetries, err)
time.Sleep(retryK8SInterval)
}

// TODO consider using map
for _, pod := range pods {
// needs to find the container ID
for _, container := range containers {
if container.K8SUID == pod.UID {
log.Debugf("Found pod(%v)'s container ID: %v ", container.Name, container.ID)
pod.Container = container.ID
break
}
}
}
return pods, nil
}

Expand Down
25 changes: 7 additions & 18 deletions ipamd/ipamd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils"
mock_awsutils "github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/docker"
mock_docker "github.com/aws/amazon-vpc-cni-k8s/pkg/docker/mocks"
mock_eniconfig "github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
mock_k8sapi "github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi/mocks"
Expand Down Expand Up @@ -58,20 +56,18 @@ const (
func setup(t *testing.T) (*gomock.Controller,
*mock_awsutils.MockAPIs,
*mock_k8sapi.MockK8SAPIs,
*mock_docker.MockAPIs,
*mock_networkutils.MockNetworkAPIs,
*mock_eniconfig.MockENIConfig) {
ctrl := gomock.NewController(t)
return ctrl,
mock_awsutils.NewMockAPIs(ctrl),
mock_k8sapi.NewMockK8SAPIs(ctrl),
mock_docker.NewMockAPIs(ctrl),
mock_networkutils.NewMockNetworkAPIs(ctrl),
mock_eniconfig.NewMockENIConfig(ctrl)
}

func TestNodeInit(t *testing.T) {
ctrl, mockAWS, mockK8S, mockDocker, mockNetwork, _ := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, _ := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand All @@ -81,7 +77,6 @@ func TestNodeInit(t *testing.T) {
maxENI: 4,
warmENITarget: 1,
warmIPTarget: 3,
dockerClient: mockDocker,
networkClient: mockNetwork}

eni1 := awsutils.ENIMetadata{
Expand Down Expand Up @@ -141,14 +136,8 @@ func TestNodeInit(t *testing.T) {
mockNetwork.EXPECT().SetupENINetwork(gomock.Any(), secMAC, secDevice, secSubnet)

mockAWS.EXPECT().GetLocalIPv4().Return(ipaddr01)
k8sName := "/k8s_POD_" + "pod1" + "_" + "default" + "_" + "pod-uid" + "_0"
mockK8S.EXPECT().K8SGetLocalPodIPs().Return([]*k8sapi.K8SPodInfo{{Name: "pod1",
Namespace: "default", UID: "pod-uid", IP: ipaddr02}}, nil)

var dockerList = make(map[string]*docker.ContainerInfo, 0)
dockerList["pod-uid"] = &docker.ContainerInfo{ID: "docker-id",
Name: k8sName, K8SUID: "pod-uid"}
mockDocker.EXPECT().GetRunningContainers().Return(dockerList, nil)
Namespace: "default", UID: "pod-uid", Container: "container-uid", IP: ipaddr02}}, nil)

var rules []netlink.Rule
mockNetwork.EXPECT().GetRuleList().Return(rules, nil)
Expand All @@ -172,7 +161,7 @@ func TestIncreaseIPPoolCustomENI(t *testing.T) {
}

func testIncreaseIPPool(t *testing.T, useENIConfig bool) {
ctrl, mockAWS, mockK8S, _, mockNetwork, mockENIConfig := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, mockENIConfig := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand Down Expand Up @@ -248,7 +237,7 @@ func testIncreaseIPPool(t *testing.T, useENIConfig bool) {

func TestTryAddIPToENI(t *testing.T) {
_ = os.Unsetenv(envCustomNetworkCfg)
ctrl, mockAWS, mockK8S, _, mockNetwork, mockENIConfig := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, mockENIConfig := setup(t)
defer ctrl.Finish()

warmIpTarget := 3
Expand Down Expand Up @@ -312,7 +301,7 @@ func TestTryAddIPToENI(t *testing.T) {
}

func TestNodeIPPoolReconcile(t *testing.T) {
ctrl, mockAWS, mockK8S, _, mockNetwork, _ := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, _ := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand Down Expand Up @@ -382,7 +371,7 @@ func TestNodeIPPoolReconcile(t *testing.T) {
}

func TestGetWarmENITarget(t *testing.T) {
ctrl, _, _, _, _, _ := setup(t)
ctrl, _, _, _, _ := setup(t)
defer ctrl.Finish()

_ = os.Setenv("WARM_IP_TARGET", "5")
Expand All @@ -399,7 +388,7 @@ func TestGetWarmENITarget(t *testing.T) {
}

func TestGetWarmIPTargetState(t *testing.T) {
ctrl, mockAWS, mockK8S, _, mockNetwork, _ := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, _ := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand Down
3 changes: 1 addition & 2 deletions ipamd/rpc_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestServer_AddNetwork(t *testing.T) {
ctrl, mockAWS, mockK8S, mockDocker, mockNetwork, _ := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, _ := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand All @@ -36,7 +36,6 @@ func TestServer_AddNetwork(t *testing.T) {
maxENI: 4,
warmENITarget: 1,
warmIPTarget: 3,
dockerClient: mockDocker,
networkClient: mockNetwork,
dataStore: datastore.NewDataStore(),
}
Expand Down
91 changes: 0 additions & 91 deletions pkg/docker/docker.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/docker/generate_mocks.go

This file was deleted.

61 changes: 0 additions & 61 deletions pkg/docker/mocks/docker_mocks.go

This file was deleted.

8 changes: 8 additions & 0 deletions pkg/k8sapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,15 @@ func (d *Controller) handlePodUpdate(key string) error {
d.workerPodsLock.Lock()
defer d.workerPodsLock.Unlock()

var containerID string

if len(pod.Status.ContainerStatuses) > 0 {
containerID = pod.Status.ContainerStatuses[0].ContainerID
log.Debugf("Found pod (%v)'s container ID: %v", podName, containerID)
}

d.workerPods[key] = &K8SPodInfo{
Container: containerID,
Name: podName,
Namespace: pod.GetNamespace(),
UID: string(pod.GetUID()),
Expand Down

0 comments on commit 14de538

Please sign in to comment.