Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Fix plugin image pull #101

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pkg/daemon/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ type DockerManager interface {

// ImageRemove removes the given image.
ImageRemove(image string) error

// ImageExists checks if the given image exists.
ImageExist(image string) (bool, error)
}
18 changes: 13 additions & 5 deletions pkg/daemon/egn_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,19 @@ func (d *EgnDaemon) RunPlugin(instanceId string, pluginArgs []string, options Ru
}
network = networks[0]
}
// XXX: Pull is removed to support local images that are already pulled
// err = d.docker.Pull(instance.Plugin.Image)
// if err != nil {
// return err
// }

// Pull image if it does not exist
ok, err := d.docker.ImageExist(instance.Plugin.Image)
if err != nil {
return err
}
if !ok {
err = d.docker.Pull(instance.Plugin.Image)
if err != nil {
return err
}
}

if !options.NoDestroyImage {
defer func() {
if err := d.docker.ImageRemove(instance.Plugin.Image); err != nil {
Expand Down
100 changes: 100 additions & 0 deletions pkg/daemon/egn_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2891,6 +2891,7 @@ func TestRunPlugin(t *testing.T) {
},
}, nil),
d.dockerManager.EXPECT().ContainerNetworks("abc123").Return([]string{"network-el"}, nil),
d.dockerManager.EXPECT().ImageExist(common.PluginImage.FullImage()).Return(true, nil),
d.dockerManager.EXPECT().Run(common.PluginImage.FullImage(), docker.RunOptions{
Network: "network-el",
Args: []string{"arg1", "arg2"},
Expand All @@ -2911,6 +2912,104 @@ func TestRunPlugin(t *testing.T) {
)
},
},
{
name: `pull image if does not exist`,
instanceId: "mock-avs-default",
args: []string{"arg1", "arg2"},
options: RunPluginOptions{
Binds: map[string]string{
"/tmp": "/tmp",
},
Volumes: map[string]string{
"volume1": "/tmp/volume1",
},
},
mocker: func(t *testing.T, d *mockerData) {
initInstanceDir(t, d.fs, d.dataDir.Path(), "mock-avs-default", `{
"name": "mock-avs",
"tag": "default",
"version": "`+common.MockAvsPkg.Version()+`",
"profile": "option-returner",
"url": "`+common.MockAvsPkg.Repo()+`",
"plugin": {
"image": "`+common.PluginImage.FullImage()+`"
}
}`)
gomock.InOrder(
d.locker.EXPECT().New(filepath.Join(d.dataDir.Path(), "nodes", "mock-avs-default", ".lock")).Return(d.locker),
d.composeManager.EXPECT().PS(compose.DockerComposePsOptions{
FilterRunning: true,
Path: filepath.Join(d.dataDir.Path(), "nodes", "mock-avs-default", "docker-compose.yml"),
Format: "json",
}).Return([]compose.ComposeService{
{
Id: "abc123",
},
}, nil),
d.dockerManager.EXPECT().ContainerNetworks("abc123").Return([]string{"network-el"}, nil),
d.dockerManager.EXPECT().ImageExist(common.PluginImage.FullImage()).Return(false, nil),
d.dockerManager.EXPECT().Pull(common.PluginImage.FullImage()).Return(nil),
d.dockerManager.EXPECT().Run(common.PluginImage.FullImage(), docker.RunOptions{
Network: "network-el",
Args: []string{"arg1", "arg2"},
Mounts: []docker.Mount{
{
Type: docker.VolumeTypeBind,
Source: "/tmp",
Target: "/tmp",
},
{
Type: docker.VolumeTypeVolume,
Source: "volume1",
Target: "/tmp/volume1",
},
},
}),
d.dockerManager.EXPECT().ImageRemove(common.PluginImage.FullImage()).Return(nil),
)
},
},
{
name: `pull image error`,
instanceId: "mock-avs-default",
args: []string{"arg1", "arg2"},
options: RunPluginOptions{
Binds: map[string]string{
"/tmp": "/tmp",
},
Volumes: map[string]string{
"volume1": "/tmp/volume1",
},
},
wantErr: true,
mocker: func(t *testing.T, d *mockerData) {
initInstanceDir(t, d.fs, d.dataDir.Path(), "mock-avs-default", `{
"name": "mock-avs",
"tag": "default",
"version": "`+common.MockAvsPkg.Version()+`",
"profile": "option-returner",
"url": "`+common.MockAvsPkg.Repo()+`",
"plugin": {
"image": "`+common.PluginImage.FullImage()+`"
}
}`)
gomock.InOrder(
d.locker.EXPECT().New(filepath.Join(d.dataDir.Path(), "nodes", "mock-avs-default", ".lock")).Return(d.locker),
d.composeManager.EXPECT().PS(compose.DockerComposePsOptions{
FilterRunning: true,
Path: filepath.Join(d.dataDir.Path(), "nodes", "mock-avs-default", "docker-compose.yml"),
Format: "json",
}).Return([]compose.ComposeService{
{
Id: "abc123",
},
}, nil),
d.dockerManager.EXPECT().ContainerNetworks("abc123").Return([]string{"network-el"}, nil),
d.dockerManager.EXPECT().ImageExist(common.PluginImage.FullImage()).Return(false, nil),
d.dockerManager.EXPECT().Pull(common.PluginImage.FullImage()).Return(assert.AnError),
)
},
},
{
name: `run plugin from image and host network`,
instanceId: "mock-avs-default",
Expand All @@ -2937,6 +3036,7 @@ func TestRunPlugin(t *testing.T) {
}`)
gomock.InOrder(
d.locker.EXPECT().New(filepath.Join(d.dataDir.Path(), "nodes", "mock-avs-default", ".lock")).Return(d.locker),
d.dockerManager.EXPECT().ImageExist(common.PluginImage.FullImage()).Return(true, nil),
d.dockerManager.EXPECT().Run(common.PluginImage.FullImage(), docker.RunOptions{
Network: docker.NetworkHost,
Args: []string{"arg1", "arg2"},
Expand Down