Skip to content

Commit

Permalink
Add ServiceConnect image to clean-up exclusion list
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyic committed Dec 15, 2022
1 parent fc99baf commit d4e2174
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 39 deletions.
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())
}

// 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
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()

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

0 comments on commit d4e2174

Please sign in to comment.