Skip to content

Commit 6349bf2

Browse files
authored
Move image pull/check/build out of runtime interface (#775)
Since these operations do not make sense in k8s, and since they are at a different level of abstraction to the other methods in the runtime interface, move them into a separate interface. We may have to expand image management into a more general container management interface, this is TBD. A noop image manager implementation is provided for the parts of the code shared between k8s and the CLI which attempt to pull images. This can probably be avoided if we separate the k8s logic more clearly from the CLI workflow.
1 parent da7a604 commit 6349bf2

File tree

12 files changed

+426
-377
lines changed

12 files changed

+426
-377
lines changed

cmd/thv/app/inspector.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111

1212
"github.com/stacklok/toolhive/pkg/container"
13+
"github.com/stacklok/toolhive/pkg/container/images"
1314
"github.com/stacklok/toolhive/pkg/container/runtime"
1415
"github.com/stacklok/toolhive/pkg/labels"
1516
"github.com/stacklok/toolhive/pkg/logger"
@@ -65,13 +66,8 @@ func inspectorCmdFunc(cmd *cobra.Command, args []string) error {
6566
return fmt.Errorf("failed to find server: %v", err)
6667
}
6768

68-
// Create container runtime
69-
rt, err := container.NewFactory().Create(ctx)
70-
if err != nil {
71-
return fmt.Errorf("failed to create container runtime: %v", err)
72-
}
73-
74-
processedImage, err := runner.HandleProtocolScheme(ctx, rt, inspectorImage, "")
69+
imageManager := images.NewImageManager(ctx)
70+
processedImage, err := runner.HandleProtocolScheme(ctx, imageManager, inspectorImage, "")
7571
if err != nil {
7672
return fmt.Errorf("failed to handle protocol scheme: %v", err)
7773
}
@@ -102,6 +98,12 @@ func inspectorCmdFunc(cmd *cobra.Command, args []string) error {
10298
AttachStdio: false,
10399
}
104100

101+
// Create container runtime
102+
rt, err := container.NewFactory().Create(ctx)
103+
if err != nil {
104+
return fmt.Errorf("failed to create container runtime: %v", err)
105+
}
106+
105107
labelsMap := map[string]string{}
106108
labels.AddStandardLabels(labelsMap, "inspector", "inspector", string(types.TransportTypeInspector), inspectorUIPort)
107109
_, err = rt.DeployWorkload(

cmd/thv/app/run.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"github.com/stacklok/toolhive/pkg/config"
1616
"github.com/stacklok/toolhive/pkg/container"
17-
"github.com/stacklok/toolhive/pkg/container/runtime"
17+
"github.com/stacklok/toolhive/pkg/container/images"
1818
"github.com/stacklok/toolhive/pkg/container/verifier"
1919
"github.com/stacklok/toolhive/pkg/logger"
2020
"github.com/stacklok/toolhive/pkg/permissions"
@@ -268,12 +268,13 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
268268

269269
var server *registry.Server
270270

271+
imageManager := images.NewImageManager(ctx)
271272
// Check if the serverOrImage is a protocol scheme, e.g., uvx://, npx://, or go://
272273
if runner.IsImageProtocolScheme(serverOrImage) {
273274
logger.Debugf("Detected protocol scheme: %s", serverOrImage)
274275
// Process the protocol scheme and build the image
275276
caCertPath := resolveCACertPath(runCACertPath)
276-
generatedImage, err := runner.HandleProtocolScheme(ctx, rt, serverOrImage, caCertPath)
277+
generatedImage, err := runner.HandleProtocolScheme(ctx, imageManager, serverOrImage, caCertPath)
277278
if err != nil {
278279
return fmt.Errorf("failed to process protocol scheme: %v", err)
279280
}
@@ -302,7 +303,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
302303
}
303304

304305
// Pull the image if necessary
305-
if err := pullImage(ctx, runConfig.Image, rt); err != nil {
306+
if err := pullImage(ctx, runConfig.Image, imageManager); err != nil {
306307
return fmt.Errorf("failed to retrieve or pull image: %v", err)
307308
}
308309

@@ -320,18 +321,18 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
320321
// If the image has the latest tag, it will be pulled to ensure we have the most recent version.
321322
// however, if there is a failure in pulling the "latest" tag, it will check if the image exists locally
322323
// as it is possible that the image was locally built.
323-
func pullImage(ctx context.Context, image string, rt runtime.Runtime) error {
324+
func pullImage(ctx context.Context, image string, imageManager images.ImageManager) error {
324325
// Check if the image has the "latest" tag
325326
isLatestTag := hasLatestTag(image)
326327

327328
if isLatestTag {
328329
// For "latest" tag, try to pull first
329330
logger.Infof("Image %s has 'latest' tag, pulling to ensure we have the most recent version...", image)
330-
err := rt.PullImage(ctx, image)
331+
err := imageManager.PullImage(ctx, image)
331332
if err != nil {
332333
// Pull failed, check if it exists locally
333334
logger.Infof("Pull failed, checking if image exists locally: %s", image)
334-
imageExists, checkErr := rt.ImageExists(ctx, image)
335+
imageExists, checkErr := imageManager.ImageExists(ctx, image)
335336
if checkErr != nil {
336337
return fmt.Errorf("failed to check if image exists: %v", checkErr)
337338
}
@@ -347,7 +348,7 @@ func pullImage(ctx context.Context, image string, rt runtime.Runtime) error {
347348
} else {
348349
// For non-latest tags, check locally first
349350
logger.Debugf("Checking if image exists locally: %s", image)
350-
imageExists, err := rt.ImageExists(ctx, image)
351+
imageExists, err := imageManager.ImageExists(ctx, image)
351352
logger.Debugf("ImageExists locally: %t", imageExists)
352353
if err != nil {
353354
return fmt.Errorf("failed to check if image exists locally: %v", err)
@@ -358,7 +359,7 @@ func pullImage(ctx context.Context, image string, rt runtime.Runtime) error {
358359
} else {
359360
// Image doesn't exist locally, try to pull
360361
logger.Infof("Image %s not found locally, pulling...", image)
361-
if err := rt.PullImage(ctx, image); err != nil {
362+
if err := imageManager.PullImage(ctx, image); err != nil {
362363
return fmt.Errorf("failed to pull image: %v", err)
363364
}
364365
logger.Infof("Successfully pulled image: %s", image)

0 commit comments

Comments
 (0)