Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ServiceConnect image to clean-up exclusion list #3521

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ func (agent *ecsAgent) doStart(containerChangeEventStream *eventstream.EventStre
}
return exitcodes.ExitTerminal
}
agent.serviceconnectManager.SetECSClient(client, agent.containerInstanceARN)
scManager := agent.serviceconnectManager
scManager.SetECSClient(client, agent.containerInstanceARN)
if loaded, _ := scManager.IsLoaded(agent.dockerClient); loaded {
imageManager.AddImageToCleanUpExclusionList(agent.serviceconnectManager.GetLoadedImageName())
ubhattacharjya marked this conversation as resolved.
Show resolved Hide resolved
}

// Add container instance ARN to metadata manager
if agent.cfg.ContainerMetadataEnabled.Enabled() {
Expand Down
2 changes: 2 additions & 0 deletions agent/app/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ func testDoStartHappyPathWithConditions(t *testing.T, blackholed bool, warmPools
mockServiceConnectManager.EXPECT().GetCapabilitiesForAppnetInterfaceVersion("").AnyTimes()
mockServiceConnectManager.EXPECT().SetECSClient(gomock.Any(), gomock.Any()).AnyTimes()
mockServiceConnectManager.EXPECT().GetAppnetContainerTarballDir().AnyTimes()
mockServiceConnectManager.EXPECT().GetLoadedImageName().Return("service_connect_agent:v1").AnyTimes()
imageManager.EXPECT().AddImageToCleanUpExclusionList(gomock.Eq("service_connect_agent:v1")).Times(1)
imageManager.EXPECT().StartImageCleanupProcess(gomock.Any()).MaxTimes(1)
dockerClient.EXPECT().ListContainers(gomock.Any(), gomock.Any(), gomock.Any()).Return(
dockerapi.ListContainersResponse{}).AnyTimes()
Expand Down
6 changes: 6 additions & 0 deletions agent/app/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func TestDoStartTaskENIHappyPath(t *testing.T) {
mockServiceConnectManager.EXPECT().GetCapabilitiesForAppnetInterfaceVersion("").AnyTimes()
mockServiceConnectManager.EXPECT().SetECSClient(gomock.Any(), gomock.Any()).AnyTimes()
mockServiceConnectManager.EXPECT().GetAppnetContainerTarballDir().AnyTimes()
mockServiceConnectManager.EXPECT().GetLoadedImageName().Return("service_connect_agent:v1").AnyTimes()
imageManager.EXPECT().AddImageToCleanUpExclusionList(gomock.Eq("service_connect_agent:v1")).Times(1)
mockUdevMonitor.EXPECT().Monitor(gomock.Any()).Return(monitoShutdownEvents).AnyTimes()

gomock.InOrder(
Expand Down Expand Up @@ -456,6 +458,8 @@ func TestDoStartCgroupInitHappyPath(t *testing.T) {
mockServiceConnectManager.EXPECT().GetCapabilitiesForAppnetInterfaceVersion("").AnyTimes()
mockServiceConnectManager.EXPECT().SetECSClient(gomock.Any(), gomock.Any()).AnyTimes()
mockServiceConnectManager.EXPECT().GetAppnetContainerTarballDir().AnyTimes()
mockServiceConnectManager.EXPECT().GetLoadedImageName().Return("service_connect_agent:v1").AnyTimes()
imageManager.EXPECT().AddImageToCleanUpExclusionList(gomock.Eq("service_connect_agent:v1")).Times(1)

gomock.InOrder(
mockControl.EXPECT().Init().Return(nil),
Expand Down Expand Up @@ -618,6 +622,8 @@ func TestDoStartGPUManagerHappyPath(t *testing.T) {
mockServiceConnectManager.EXPECT().GetCapabilitiesForAppnetInterfaceVersion("").AnyTimes()
mockServiceConnectManager.EXPECT().SetECSClient(gomock.Any(), gomock.Any()).AnyTimes()
mockServiceConnectManager.EXPECT().GetAppnetContainerTarballDir().AnyTimes()
mockServiceConnectManager.EXPECT().GetLoadedImageName().Return("service_connect_agent:v1").AnyTimes()
imageManager.EXPECT().AddImageToCleanUpExclusionList(gomock.Eq("service_connect_agent:v1")).Times(1)

gomock.InOrder(
mockGPUManager.EXPECT().Initialize().Return(nil),
Expand Down
8 changes: 8 additions & 0 deletions agent/engine/docker_image_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ImageManager interface {
GetImageStateFromImageName(containerImageName string) (*image.ImageState, bool)
StartImageCleanupProcess(ctx context.Context)
SetDataClient(dataClient data.Client)
AddImageToCleanUpExclusionList(image string)
}

// dockerImageManager accounts all the images and their states in the instance.
Expand Down Expand Up @@ -112,6 +113,13 @@ func buildImageCleanupExclusionList(cfg *config.Config) []string {
return excludedImages
}

func (imageManager *dockerImageManager) AddImageToCleanUpExclusionList(image string) {
imageManager.imageCleanupExclusionList = append(imageManager.imageCleanupExclusionList, image)
logger.Info("Image excluded from cleanup", logger.Fields{
field.Image: image,
})
}

func (imageManager *dockerImageManager) AddAllImageStates(imageStates []*image.ImageState) {
imageManager.updateLock.Lock()
defer imageManager.updateLock.Unlock()
Expand Down
12 changes: 12 additions & 0 deletions agent/engine/mocks/engine_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agent/engine/serviceconnect/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type Manager interface {
loader.Loader

GetLoadedImageName() (string, error)
GetLoadedImageName() string
AugmentTaskContainer(task *apitask.Task, container *apicontainer.Container, hostConfig *dockercontainer.HostConfig) error
CreateInstanceTask(config *config.Config) (*apitask.Task, error)
AugmentInstanceContainer(task *apitask.Task, container *apicontainer.Container, hostConfig *dockercontainer.HostConfig) error
Expand Down
16 changes: 6 additions & 10 deletions agent/engine/serviceconnect/manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (m *manager) augmentAgentContainer(task *apitask.Task, container *apicontai
config.DrainRequest = m.adminDrainRequest

task.PopulateServiceConnectRuntimeConfig(config)
container.Image, _ = m.GetLoadedImageName()
container.Image = m.GetLoadedImageName()
return nil
}

Expand Down Expand Up @@ -297,10 +297,7 @@ func (m *manager) AugmentTaskContainer(task *apitask.Task, container *apicontain
}

func (m *manager) CreateInstanceTask(cfg *config.Config) (*apitask.Task, error) {
imageName, err := m.GetLoadedImageName()
if err != nil {
return nil, err
}
imageName := m.GetLoadedImageName()
containerRunning := apicontainerstatus.ContainerRunning
dockerHostConfig := dockercontainer.HostConfig{
NetworkMode: apitask.HostNetworkMode,
Expand Down Expand Up @@ -404,7 +401,7 @@ func (agent *manager) LoadImage(ctx context.Context, _ *config.Config, dockerCli
continue
}
agent.setLoadedAppnetVerion(supportedAppnetInterfaceVersion)
imageName, _ := agent.GetLoadedImageName()
imageName := agent.GetLoadedImageName()
logger.Info(fmt.Sprintf("Successfully loaded Appnet agent container tarball: %s", agentContainerTarballPath),
logger.Fields{
field.Image: imageName,
Expand All @@ -415,13 +412,12 @@ func (agent *manager) LoadImage(ctx context.Context, _ *config.Config, dockerCli
}

func (agent *manager) IsLoaded(dockerClient dockerapi.DockerClient) (bool, error) {
imageName, _ := agent.GetLoadedImageName()
return loader.IsImageLoaded(imageName, dockerClient)
return loader.IsImageLoaded(agent.GetLoadedImageName(), dockerClient)
}

func (agent *manager) GetLoadedImageName() (string, error) {
func (agent *manager) GetLoadedImageName() string {
agent.agentContainerTag = fmt.Sprintf(defaultAgentContainerTagFormat, agent.appnetInterfaceVersion)
return fmt.Sprintf("%s:%s", agent.agentContainerImageName, agent.agentContainerTag), nil
return fmt.Sprintf("%s:%s", agent.agentContainerImageName, agent.agentContainerTag)
}

func (agent *manager) GetLoadedAppnetVersion() (string, error) {
Expand Down
6 changes: 2 additions & 4 deletions agent/engine/serviceconnect/manager_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func (*manager) IsLoaded(dockerClient dockerapi.DockerClient) (bool, error) {
func (m *manager) SetECSClient(api.ECSClient, string) {
}

func (*manager) GetLoadedImageName() (string, error) {
return "", loader.NewUnsupportedPlatformError(fmt.Errorf(
"appnetAgent container get image name: unsupported platform: %s/%s",
runtime.GOOS, runtime.GOARCH))
func (*manager) GetLoadedImageName() string {
return ""
}

func (*manager) GetLoadedAppnetVersion() (string, error) {
Expand Down
5 changes: 2 additions & 3 deletions agent/engine/serviceconnect/mock/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 22 additions & 20 deletions agent/stats/service_connect_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,33 @@ func TestRetrieveServiceConnectMetrics(t *testing.T) {
}

for _, test := range tests {
// Set up a mock http sever on the statsUrlpath
mockUDSPath := "/tmp/appnet_admin.sock"
r := mux.NewRouter()
r.HandleFunc("/get/them/stats", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v", test.stats)
}))
func() {
// Set up a mock http sever on the statsUrlpath
mockUDSPath := "/tmp/appnet_admin.sock"
r := mux.NewRouter()
r.HandleFunc("/get/them/stats", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v", test.stats)
}))

ts := httptest.NewUnstartedServer(r)
ts := httptest.NewUnstartedServer(r)
defer ts.Close()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I received this error when running unit test listen unix /tmp/appnet_admin.sock: bind: address already in use
Moving ts.Close() up in a defer statement should help us clean up always.


l, err := net.Listen("unix", mockUDSPath)
assert.NoError(t, err)
l, err := net.Listen("unix", mockUDSPath)
assert.NoError(t, err)

ts.Listener.Close()
ts.Listener = l
ts.Start()
ts.Listener.Close()
ts.Listener = l
ts.Start()

serviceConnectStats := &ServiceConnectStats{
appnetClient: appnet.Client(),
}
serviceConnectStats.retrieveServiceConnectStats(t1)
serviceConnectStats := &ServiceConnectStats{
appnetClient: appnet.Client(),
}
serviceConnectStats.retrieveServiceConnectStats(t1)

sortMetrics(serviceConnectStats.GetStats())
sortMetrics(test.expectedStats)
assert.Equal(t, test.expectedStats, serviceConnectStats.GetStats())
ts.Close()
sortMetrics(serviceConnectStats.GetStats())
sortMetrics(test.expectedStats)
assert.Equal(t, test.expectedStats, serviceConnectStats.GetStats())
}()
}
}

Expand Down