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

feat: image load #2102

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package docker_kurtosis_backend

import (
"context"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"io"
"sync"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"

"github.com/sirupsen/logrus"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/engine_functions"
Expand Down Expand Up @@ -536,6 +537,10 @@ func (backend *DockerKurtosisBackend) GetAvailableCPUAndMemory(ctx context.Conte
return availableMemory, availableCpu, isResourceInformationComplete, nil
}

func (backend *DockerKurtosisBackend) LoadImage(ctx context.Context, image io.ReadCloser) error {
return backend.dockerManager.LoadImage(ctx, image)
}

func (backend *DockerKurtosisBackend) BuildImage(ctx context.Context, imageName string, imageBuildSpec *image_build_spec.ImageBuildSpec) (string, error) {
return backend.dockerManager.BuildImage(ctx, imageName, imageBuildSpec)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
"context"
"encoding/json"
"fmt"
"github.com/docker/docker/api/types/registry"
"github.com/docker/go-units"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/uuid_generator"
"github.com/kurtosis-tech/kurtosis/utils"
"io"
"io/ioutil"

Check failure on line 15 in container-engine-lib/lib/backend_impls/docker/docker_manager/docker_manager.go

View workflow job for this annotation

GitHub Actions / golang-lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"math"
"net"
"regexp"
"strings"
"sync"
"time"

"github.com/docker/docker/api/types/registry"
"github.com/docker/go-units"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/uuid_generator"
"github.com/kurtosis-tech/kurtosis/utils"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
Expand Down Expand Up @@ -1312,6 +1314,16 @@
return pulledFromRemote, imageArchitecture, nil
}

func (manager *DockerManager) LoadImage(ctx context.Context, image io.ReadCloser) error {
response, err := manager.dockerClient.ImageLoad(ctx, image, true)
if err != nil {
return stacktrace.Propagate(err, "Fail to load docker image")
}
responseBody, err := ioutil.ReadAll(response.Body)

Check failure on line 1322 in container-engine-lib/lib/backend_impls/docker/docker_manager/docker_manager.go

View workflow job for this annotation

GitHub Actions / golang-lint

ineffectual assignment to err (ineffassign)
logrus.Infof("Docker image loaded: %s", responseBody)
return nil
}

func (manager *DockerManager) BuildImage(ctx context.Context, imageName string, imageBuildSpec *image_build_spec.ImageBuildSpec) (string, error) {
buildContextDirPath := imageBuildSpec.GetBuildContextDir()
buildContextTarReader, err := getBuildContextReader(buildContextDirPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package kubernetes_kurtosis_backend

import (
"context"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"io"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
apiv1 "k8s.io/api/core/v1"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/kubernetes/kubernetes_kurtosis_backend/engine_functions"
Expand Down Expand Up @@ -483,6 +485,11 @@ func (backend *KubernetesKurtosisBackend) BuildImage(ctx context.Context, imageN
return "", stacktrace.NewError("Building images isn't yet implemented in Kubernetes.")
}

func (backend *KubernetesKurtosisBackend) LoadImage(ctx context.Context, image io.ReadCloser) error {
// TODO IMPLEMENT
return stacktrace.NewError("Loading images isn't yet implemented in Kubernetes.")
}

// ====================================================================================================
//
// Private Helper Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package metrics_reporting

import (
"context"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"io"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"

"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"
Expand Down Expand Up @@ -464,3 +466,7 @@ func (backend *MetricsReportingKurtosisBackend) GetAvailableCPUAndMemory(ctx con
func (backend *MetricsReportingKurtosisBackend) BuildImage(ctx context.Context, imageName string, imageBuildSpec *image_build_spec.ImageBuildSpec) (string, error) {
return backend.underlying.BuildImage(ctx, imageName, imageBuildSpec)
}

func (backend *MetricsReportingKurtosisBackend) LoadImage(ctx context.Context, image io.ReadCloser) error {
return backend.underlying.LoadImage(ctx, image)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package backend_interface

import (
"context"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"io"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"

"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"
Expand Down Expand Up @@ -353,4 +355,7 @@ type KurtosisBackend interface {
// BuildImage builds a container image based on the [imageBuildSpec] with [imageName]
// Returns image architecture and if error occurred
BuildImage(ctx context.Context, imageName string, imageBuildSpec *image_build_spec.ImageBuildSpec) (string, error)

// Load docker image file (tar.gz format) into docker
LoadImage(ctx context.Context, image io.ReadCloser) error
}

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,48 @@
package image_load

import (
"encoding/json"

"github.com/kurtosis-tech/stacktrace"
)

type ImageLoad struct {
// we do this way in order to have exported fields which can be marshalled
// and an unexported type for encapsulation
privateImageLoad *privateImageLoad
}

// ImageLoad contains the information need for building a container image.
type privateImageLoad struct {
// Location of the container image to load (eg. tar.gz) on the machine
ContainerImageFilePath string
}

func NewImageLoad(containerImageFilePath string) *ImageLoad {
internalImageLoad := &privateImageLoad{
ContainerImageFilePath: containerImageFilePath,
}
return &ImageLoad{internalImageLoad}
}

func (imageLoad *ImageLoad) GetContainerImageFilePath() string {
return imageLoad.privateImageLoad.ContainerImageFilePath
}

func (imageLoad *ImageLoad) MarshalJSON() ([]byte, error) {
return json.Marshal(imageLoad.privateImageLoad)
}

func (imageLoad *ImageLoad) UnmarshalJSON(data []byte) error {

// Suppressing exhaustruct requirement because we want an object with zero values
// nolint: exhaustruct
unmarshalledPrivateStructPtr := &privateImageLoad{}

if err := json.Unmarshal(data, unmarshalledPrivateStructPtr); err != nil {
return stacktrace.Propagate(err, "An error occurred unmarshalling the private struct")
}

imageLoad.privateImageLoad = unmarshalledPrivateStructPtr
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import (
"encoding/json"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_load"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_directory"
Expand All @@ -28,7 +30,11 @@
// Configuration for container engine to pull an in a private registry behind authentication
// If nil, we will use the ContainerImageName and not use any auth
// Mutually exclusive from ImageBuildSpec, ContainerImageName

ImagerRegistrySpec *image_registry_spec.ImageRegistrySpec
// Reference to a image file that can be loaded into a backend image
// This is one of the options to reference a image and this value is optional (i.e. nil)
ImageLoad *image_load.ImageLoad
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a usecase for this? I'd imagine users would be either a) specifying an existing Docker image, or b) getting an image through indirect means (either ImageBuildSpec or NixBuildSpec)


PrivatePorts map[string]*port_spec.PortSpec

Expand Down Expand Up @@ -67,6 +73,7 @@
containerImageName string,
imageBuildSpec *image_build_spec.ImageBuildSpec,
imageRegistrySpec *image_registry_spec.ImageRegistrySpec,
imageLoad *image_load.ImageLoad,
privatePorts map[string]*port_spec.PortSpec,
publicPorts map[string]*port_spec.PortSpec,
entrypointArgs []string,
Expand All @@ -88,7 +95,7 @@
return nil, stacktrace.Propagate(err, "Invalid service config labels '%+v'", labels)
}

internalServiceConfig := &privateServiceConfig{

Check failure on line 98 in container-engine-lib/lib/backend_interface/objects/service/service_config.go

View workflow job for this annotation

GitHub Actions / golang-lint

ImageLoad is missing in privateServiceConfig (exhaustruct)
ContainerImageName: containerImageName,
ImageBuildSpec: imageBuildSpec,
ImagerRegistrySpec: imageRegistrySpec,
Expand Down Expand Up @@ -124,6 +131,10 @@
return serviceConfig.privateServiceConfig.ImagerRegistrySpec
}

func (serviceConfig *ServiceConfig) GetImageLoad() *image_load.ImageLoad {
return serviceConfig.privateServiceConfig.ImageLoad
}

func (serviceConfig *ServiceConfig) GetPrivatePorts() map[string]*port_spec.PortSpec {
return serviceConfig.privateServiceConfig.PrivatePorts
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package service

import (
"encoding/json"

"testing"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_build_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_load"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_registry_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_directory"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_user"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"testing"
"time"
)

func TestServiceConfigMarshallers(t *testing.T) {
Expand Down Expand Up @@ -64,6 +67,7 @@ func getServiceConfigForTest(t *testing.T, imageName string) *ServiceConfig {
imageName,
testImageBuildSpec(),
testImageRegistrySpec(),
testImageLoad(),
testPrivatePorts(t),
testPublicPorts(t),
[]string{"bin", "bash", "ls"},
Expand Down Expand Up @@ -200,3 +204,7 @@ func testToleration() []v1.Toleration {
TolerationSeconds: &tolerationSeconds,
}}
}

func testImageLoad() *image_load.ImageLoad {
return image_load.NewImageLoad("/path/to/image")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package service_registration

import (
"fmt"
"math/rand"
"net"
"os"
"testing"
"time"

"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/port_spec"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_directory"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db"
"github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
"math/rand"
"net"
"os"
"testing"
"time"
)

const (
Expand Down Expand Up @@ -306,6 +307,7 @@ func getServiceConfigForTest(t *testing.T, imageName string) *service.ServiceCon
imageName,
nil,
nil,
nil,
testPrivatePorts(t),
testPublicPorts(t),
[]string{"bin", "bash", "ls"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func KurtosisTypeConstructors() []*starlark.Builtin {
starlark.NewBuiltin(service_config.ReadyConditionTypeName, service_config.NewReadyConditionType().CreateBuiltin()),
starlark.NewBuiltin(service_config.ImageBuildSpecTypeName, service_config.NewImageBuildSpecType().CreateBuiltin()),
starlark.NewBuiltin(service_config.ImageRegistrySpecTypeName, service_config.NewImageRegistrySpec().CreateBuiltin()),
starlark.NewBuiltin(service_config.ImageLoadTypeName, service_config.NewImageLoadType().CreateBuiltin()),
starlark.NewBuiltin(service_config.UserTypeName, service_config.NewUserType().CreateBuiltin()),
starlark.NewBuiltin(service_config.TolerationTypeName, service_config.NewTolerationType().CreateBuiltin()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package add_service
import (
"context"
"fmt"
"time"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service_directory"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"go.starlark.net/starlark"
"time"
)

const (
Expand Down Expand Up @@ -110,6 +111,8 @@ func validateSingleService(validatorEnvironment *startosis_validator.ValidatorEn
validatorEnvironment.AppendRequiredImageBuild(serviceConfig.GetContainerImageName(), serviceConfig.GetImageBuildSpec())
} else if serviceConfig.GetImageRegistrySpec() != nil {
validatorEnvironment.AppendImageToPullWithAuth(serviceConfig.GetContainerImageName(), serviceConfig.GetImageRegistrySpec())
} else if serviceConfig.GetImageLoad() != nil {
validatorEnvironment.AppendRequiredImageLoad(serviceConfig.GetContainerImageName(), serviceConfig.GetImageLoad())
} else {
validatorEnvironment.AppendRequiredImagePull(serviceConfig.GetContainerImageName())
}
Expand Down Expand Up @@ -202,6 +205,7 @@ func replaceMagicStrings(
serviceConfig.GetContainerImageName(),
serviceConfig.GetImageBuildSpec(),
serviceConfig.GetImageRegistrySpec(),
serviceConfig.GetImageLoad(),
serviceConfig.GetPrivatePorts(),
serviceConfig.GetPublicPorts(),
entrypoints,
Expand Down
Loading
Loading