Skip to content

Commit

Permalink
Updating the correct abstraction - KurtosisBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Oct 7, 2023
1 parent 8002def commit 4ae76ff
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package docker_kurtosis_backend

import (
"context"
"github.com/sirupsen/logrus"
"io"
"sync"

"github.com/sirupsen/logrus"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/logs_aggregator_functions"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/logs_aggregator_functions/implementations/vector"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/engine"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/exec_result"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
Expand Down Expand Up @@ -78,11 +80,23 @@ func NewDockerKurtosisBackend(
}
}

func (backend *DockerKurtosisBackend) FetchImage(ctx context.Context, image string) (bool, error) {
pulledFromRemote, err := backend.dockerManager.FetchImage(ctx, image)
func (backend *DockerKurtosisBackend) FetchImage(ctx context.Context, image string, download_mode image_download_mode.ImageDownloadMode) (bool, error) {
var err error
var pulledFromRemote bool = false

switch image_pulling := download_mode; image_pulling {
case image_download_mode.Always:
err = backend.dockerManager.FetchLatestImage(ctx, image)
case image_download_mode.Missing:
pulledFromRemote, err = backend.dockerManager.FetchImageMissing(ctx, image)
case image_download_mode.Never:
return false, stacktrace.NewError("Undefined image pulling mode: '%v'", image_pulling)
}

if err != nil {
return false, stacktrace.Propagate(err, "An error occurred fetching image from kurtosis backend")
}

return pulledFromRemote, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (backend *DockerKurtosisBackend) CreateAPIContainer(
labelStrs,
).WithRestartPolicy(docker_manager.RestartOnFailure).Build()

if _, err = backend.dockerManager.FetchImage(ctx, image); err != nil {
if _, err = backend.dockerManager.FetchImageMissing(ctx, image); err != nil {
logrus.Warnf("Failed to pull the latest version of API container image '%v'; you may be running an out-of-date version", image)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func (manager *DockerManager) CreateAndStartContainer(
case Always:
err = manager.FetchLatestImage(ctx, dockerImage)
case Missing:
_, err = manager.FetchImage(ctx, dockerImage)
_, err = manager.FetchImageMissing(ctx, dockerImage)
case Never:
return "", nil, stacktrace.NewError("Undefined image pulling mode: '%v'", image_pulling)
}
Expand Down Expand Up @@ -1189,10 +1189,10 @@ func (manager *DockerManager) GetContainersByLabels(ctx context.Context, labels
return result, nil
}

// [FetchImage] uses the local [dockerImage] if it's available.
// [FetchImageMissing] uses the local [dockerImage] if it's available.
// If unavailable, will attempt to fetch the latest image.
// Returns error if local [dockerImage] is unavailable and pulling image fails.
func (manager *DockerManager) FetchImage(ctx context.Context, dockerImage string) (bool, error) {
func (manager *DockerManager) FetchImageMissing(ctx context.Context, dockerImage string) (bool, error) {
// if the image name doesn't have version information we concatenate `:latest`
// this behavior is similar to CreateAndStartContainer above
// this allows us to be deterministic in our behaviour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kubernetes_kurtosis_backend

import (
"context"
"io"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/kubernetes/kubernetes_kurtosis_backend/engine_functions"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/kubernetes/kubernetes_kurtosis_backend/shared_helpers"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/kubernetes/kubernetes_kurtosis_backend/user_services_functions"
Expand All @@ -13,12 +15,12 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/engine"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/exec_result"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"io"
)

const (
Expand Down Expand Up @@ -117,7 +119,7 @@ func NewKubernetesKurtosisBackend(
}
}

func (backend *KubernetesKurtosisBackend) FetchImage(ctx context.Context, image string) (bool, error) {
func (backend *KubernetesKurtosisBackend) FetchImage(ctx context.Context, image string, download_mode image_download_mode.ImageDownloadMode) (bool, error) {
logrus.Warnf("FetchImage isn't implemented for Kubernetes yet")
return false, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package metrics_reporting

import (
"context"
"io"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/api_container"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/compute_resources"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/engine"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/exec_result"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/stacktrace"
"io"
"time"
)

// TODO CALL THE METRICS LIBRARY EVENT-REGISTRATION FUNCTIONS HERE!!!!
Expand All @@ -25,8 +27,8 @@ func NewMetricsReportingKurtosisBackend(underlying backend_interface.KurtosisBac
return &MetricsReportingKurtosisBackend{underlying: underlying}
}

func (backend *MetricsReportingKurtosisBackend) FetchImage(ctx context.Context, image string) (bool, error) {
pulledFromRemote, err := backend.underlying.FetchImage(ctx, image)
func (backend *MetricsReportingKurtosisBackend) FetchImage(ctx context.Context, image string, download_mode image_download_mode.ImageDownloadMode) (bool, error) {
pulledFromRemote, err := backend.underlying.FetchImage(ctx, image, download_mode)
if err != nil {
return false, stacktrace.Propagate(err, "An error occurred pulling image '%v'", image)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package backend_interface

import (
"context"
"io"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/api_container"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/compute_resources"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/engine"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/exec_result"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"io"
"time"
)

// TODO This mega-backend should really have its individual functionalities split up into
Expand All @@ -29,7 +31,7 @@ type KurtosisBackend interface {
// FetchImage always attempts to retrieve the latest image.
// If retrieving the latest [dockerImage] fails, the local image will be used.
// Returns True is it was retrieved from cloud or False if it's a local image
FetchImage(ctx context.Context, image string) (bool, error)
FetchImage(ctx context.Context, image string, download_mode image_download_mode.ImageDownloadMode) (bool, error)

PruneUnusedImages(ctx context.Context) ([]string, error)

Expand Down
42 changes: 22 additions & 20 deletions container-engine-lib/lib/backend_interface/mock_kurtosis_backend.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2021 - present Kurtosis Technologies Inc.
* All Rights Reserved.
*/

package image_download_mode

const (
Never ImageDownloadMode = "never"
Always = "always"
Missing = "missing"
)

type ImageDownloadMode string
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package startosis_validator

import (
"context"
"sync"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors"
"github.com/sirupsen/logrus"
"sync"
)

const maxNumberOfConcurrentDownloads = int64(4)
Expand Down Expand Up @@ -61,7 +63,7 @@ func fetchImageFromBackend(ctx context.Context, wg *sync.WaitGroup, imageCurrent
}()

logrus.Debugf("Starting the download of image: '%s'", imageName)
imagePulledFromRemote, err := (*backend).FetchImage(ctx, imageName)
imagePulledFromRemote, err := (*backend).FetchImage(ctx, imageName, image_download_mode.Missing)
if err != nil {
logrus.Warnf("Container image '%s' download failed. Error was: '%s'", imageName, err.Error())
pullErrors <- startosis_errors.WrapWithValidationError(err, "Failed fetching the required image '%v'.", imageName)
Expand Down

0 comments on commit 4ae76ff

Please sign in to comment.