Skip to content

Commit

Permalink
fix: get container ID from kube rather than docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Rudoi committed Apr 4, 2019
1 parent b388426 commit 827328e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 217 deletions.
25 changes: 0 additions & 25 deletions ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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 @@ -145,7 +144,6 @@ type IPAMContext struct {
dataStore *datastore.DataStore
k8sClient k8sapi.K8SAPIs
eniConfig eniconfig.ENIConfig
dockerClient docker.APIs
networkClient networkutils.NetworkAPIs

currentMaxAddrsPerENI int64
Expand Down Expand Up @@ -179,7 +177,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 @@ -313,28 +310,6 @@ func (c *IPAMContext) getLocalPodsWithRetry() ([]*k8sapi.K8SPodInfo, error) {
return nil, errors.New("unable to get local pods, giving up")
}

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
35 changes: 12 additions & 23 deletions ipamd/ipamd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import (
"github.com/aws/amazon-vpc-cni-k8s/ipamd/datastore"
"github.com/aws/amazon-vpc-cni-k8s/pkg/apis/crd/v1alpha1"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils"
"github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/docker"
"github.com/aws/amazon-vpc-cni-k8s/pkg/docker/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig/mocks"
mock_awsutils "github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils/mocks"
mock_eniconfig "github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi"
"github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi/mocks"
"github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils/mocks"
mock_k8sapi "github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi/mocks"
mock_networkutils "github.com/aws/amazon-vpc-cni-k8s/pkg/networkutils/mocks"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,26 +60,23 @@ 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{
awsClient: mockAWS,
k8sClient: mockK8S,
dockerClient: mockDocker,
networkClient: mockNetwork}

eni1 := awsutils.ENIMetadata{
Expand Down Expand Up @@ -143,14 +138,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 @@ -175,7 +164,7 @@ func TestIncreaseIPPoolCustomENI(t *testing.T) {

func TestIncreaseIPPoolCustomENINoCfg(t *testing.T) {
os.Setenv(envCustomNetworkCfg, "true")
ctrl, mockAWS, mockK8S, _, mockNetwork, mockENIConfig := setup(t)
ctrl, mockAWS, mockK8S, mockNetwork, mockENIConfig := setup(t)
defer ctrl.Finish()

mockContext := &IPAMContext{
Expand All @@ -196,7 +185,7 @@ func TestIncreaseIPPoolCustomENINoCfg(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 @@ -274,7 +263,7 @@ func testIncreaseIPPool(t *testing.T, useENIConfig bool) {
}

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 @@ -345,7 +334,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 @@ -362,7 +351,7 @@ func TestGetWarmENITarget(t *testing.T) {
}

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

// MaxENI 5 is less than lower bound of 10, so 5
Expand Down Expand Up @@ -397,7 +386,7 @@ func TestGetMaxENI(t *testing.T) {
}

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

mockContext := &IPAMContext{
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.

9 changes: 8 additions & 1 deletion pkg/k8sapi/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
clientset "k8s.io/client-go/kubernetes"

"github.com/operator-framework/operator-sdk/pkg/k8sclient"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -215,7 +215,14 @@ 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
}

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

0 comments on commit 827328e

Please sign in to comment.