Skip to content

Commit

Permalink
specify image name
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Feb 7, 2024
1 parent e7b1922 commit e1e24ac
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 183 deletions.
168 changes: 0 additions & 168 deletions container-engine-lib/lib/backend_interface/mock_kurtosis_backend.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nix_build_spec

import (
"encoding/json"
"fmt"

"github.com/kurtosis-tech/stacktrace"
)
Expand All @@ -17,17 +18,23 @@ type privateNixBuildSpec struct {
NixFlakeDir string
ContextDirPath string
FlakeOutput string
ImageName string
}

func NewNixBuildSpec(contextDirPath string, nixFlakeDir string, flakeOutput string) *NixBuildSpec {
func NewNixBuildSpec(imageName string, contextDirPath string, nixFlakeDir string, flakeOutput string) *NixBuildSpec {
internalNixBuildSpec := &privateNixBuildSpec{
NixFlakeDir: nixFlakeDir,
ContextDirPath: contextDirPath,
FlakeOutput: flakeOutput,
ImageName: imageName,
}
return &NixBuildSpec{internalNixBuildSpec}
}

func (nixBuildSpec *NixBuildSpec) GetImageName() string {
return nixBuildSpec.privateNixBuildSpec.ImageName
}

func (nixBuildSpec *NixBuildSpec) GetNixFlakeDir() string {
return nixBuildSpec.privateNixBuildSpec.NixFlakeDir
}
Expand All @@ -40,6 +47,10 @@ func (nixBuildSpec *NixBuildSpec) GetFlakeOutput() string {
return nixBuildSpec.privateNixBuildSpec.FlakeOutput
}

func (nixBuildSpec *NixBuildSpec) GetFullFlakeReference() string {
return fmt.Sprintf("%s/.#%s", nixBuildSpec.privateNixBuildSpec.NixFlakeDir, nixBuildSpec.privateNixBuildSpec.FlakeOutput)
}

func (nixBuildSpec *NixBuildSpec) MarshalJSON() ([]byte, error) {
return json.Marshal(nixBuildSpec.privateNixBuildSpec)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func testImageRegistrySpec() *image_registry_spec.ImageRegistrySpec {
}

func testNixBuildSpec() *nix_build_spec.NixBuildSpec {
return nix_build_spec.NewNixBuildSpec("test-image", "path", "")
return nix_build_spec.NewNixBuildSpec("test-image", "path", "", "")
}

func testServiceUser() *service_user.ServiceUser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ func (suite *KurtosisTypeConstructorTestSuite) TestNixBuildSpecTest() {
}

func (t *nixBuildSpecTest) GetStarlarkCode() string {
return fmt.Sprintf("%s(%s=%q, %s=%q, %s=%q)",
return fmt.Sprintf("%s(%s=%q, %s=%q, %s=%q, %s=%q)",
service_config.NixBuildSpecTypeName,
service_config.FlakeLocationDir,
testNixFlakeLocationDir,
service_config.NixContextAttr,
testNixContextDir,
service_config.NixImageName,
testNixImageName,
service_config.FlakeOutputAttr,
testNixFlakeOutput)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
testOnDiskContainerImagePath = "kurtosis-data/test-package/Dockerfile"

testNixContextDir = "./"
testNixImageName = "test-image"
testNixFlakeOutput = "foo"
testNixFlakeLocationDir = "./server/app"
testOnDiskNixContextDirPath = "kurtosis-data/test-package"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service_config

import (
"fmt"
"path"
"path/filepath"

Expand All @@ -20,6 +21,7 @@ const (
FlakeLocationDir = "flake_location_dir"
FlakeOutputAttr = "flake_output"
NixContextAttr = "build_context_dir"
NixImageName = "image_name"

// Currently only supports container nixs named Dockerfile
defaultNixFlakeFile = "flake.nix"
Expand All @@ -46,6 +48,14 @@ func NewNixBuildSpecType() *kurtosis_type_constructor.KurtosisTypeConstructor {
return builtin_argument.NonEmptyString(value, NixContextAttr)
},
},
{
Name: NixImageName,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: func(value starlark.Value) *startosis_errors.InterpretationError {
return builtin_argument.NonEmptyString(value, NixImageName)
},
},
{
Name: FlakeOutputAttr,
IsOptional: true,
Expand Down Expand Up @@ -110,15 +120,39 @@ func (nixBuildSpec *NixBuildSpec) GetFlakeOutput() (string, *startosis_errors.In
return flakeOutput.GoString(), nil
}

func (nixBuildSpec *NixBuildSpec) GetImageName() (string, *startosis_errors.InterpretationError) {
imageName, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](nixBuildSpec.KurtosisValueTypeDefault, NixImageName)
if interpretationErr != nil {
return "", interpretationErr
}
if !found {
return "", nil
}
return imageName.GoString(), nil
}

func (nixBuildSpec *NixBuildSpec) GetFlakeLocationDir() (string, *startosis_errors.InterpretationError) {
flakeLocator, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](nixBuildSpec.KurtosisValueTypeDefault, FlakeLocationDir)
flakeDir, found, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String](nixBuildSpec.KurtosisValueTypeDefault, FlakeLocationDir)
if interpretationErr != nil {
return "", interpretationErr
}
if !found {
return "", nil
}
return flakeLocator.GoString(), nil
return flakeDir.GoString(), nil
}

func (nixBuildSpec *NixBuildSpec) GetFullFlakeReference() (string, *startosis_errors.InterpretationError) {
flakeDir, err := nixBuildSpec.GetFlakeLocationDir()
if err != nil {
return "", err
}
flakeAttr, err := nixBuildSpec.GetFlakeOutput()
if err != nil {
return "", err
}
fullLocator := fmt.Sprintf("%s/.#%s", flakeDir, flakeAttr)
return fullLocator, nil
}

func (nixBuildSpec *NixBuildSpec) ToKurtosisType(
Expand Down Expand Up @@ -148,12 +182,17 @@ func (nixBuildSpec *NixBuildSpec) ToKurtosisType(
return nil, interpretationErr
}

imageName, interpretationErr := nixBuildSpec.GetImageName()
if interpretationErr != nil {
return nil, interpretationErr
}

flakeOutputStr, interpretationErr := nixBuildSpec.GetFlakeOutput()
if interpretationErr != nil {
return nil, interpretationErr
}

return nix_build_spec.NewNixBuildSpec(buildContextDirPathOnDisk, flakeNixFilePathOnDisk, flakeOutputStr), nil
return nix_build_spec.NewNixBuildSpec(imageName, buildContextDirPathOnDisk, flakeNixFilePathOnDisk, flakeOutputStr), nil
}

// Returns the filepath of the build context directory and flake nix on APIC based on package info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func convertImage(
packageReplaceOptions map[string]string) (string, *image_build_spec.ImageBuildSpec, *image_registry_spec.ImageRegistrySpec, *nix_build_spec.NixBuildSpec, *startosis_errors.InterpretationError) {
imageBuildSpecStarlarkType, isImageBuildSpecStarlarkType := image.(*ImageBuildSpec)
imageRegistrySpecStarlarkType, isImageRegistrySpecStarlarkType := image.(*ImageRegistrySpec)
NixBuildSpecStarlarkType, isNixBuildSpecStarlarkType := image.(*NixBuildSpec)
nixBuildSpecStarlarkType, isNixBuildSpecStarlarkType := image.(*NixBuildSpec)
if isImageBuildSpecStarlarkType {
imageBuildSpec, interpretationErr := imageBuildSpecStarlarkType.ToKurtosisType(locatorOfModuleInWhichThisBuiltInIsBeingCalled, packageId, packageContentProvider, packageReplaceOptions)
if interpretationErr != nil {
Expand All @@ -675,11 +675,11 @@ func convertImage(
}
return imageRegistrySpec.GetImageName(), nil, imageRegistrySpec, nil, nil
} else if isNixBuildSpecStarlarkType {
nixBuildSpec, interpretationErr := NixBuildSpecStarlarkType.ToKurtosisType(locatorOfModuleInWhichThisBuiltInIsBeingCalled, packageId, packageContentProvider, packageReplaceOptions)
nixBuildSpec, interpretationErr := nixBuildSpecStarlarkType.ToKurtosisType(locatorOfModuleInWhichThisBuiltInIsBeingCalled, packageId, packageContentProvider, packageReplaceOptions)
if interpretationErr != nil {
return "", nil, nil, nil, interpretationErr
}
return nixBuildSpec.GetFlakeOutput(), nil, nil, nixBuildSpec, nil
return nixBuildSpec.GetImageName(), nil, nil, nixBuildSpec, nil
} else {
imageName, interpretationErr := kurtosis_types.SafeCastToString(image, ImageAttr)
if interpretationErr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,26 @@ func (validator *ImagesValidator) nixBuildUsingBackend(
buildErrors chan<- error,
nixBuildStarted chan<- string,
nixBuildFinished chan<- *ValidatedImage) {
imageRef := nixBuildSpec.GetFullFlakeReference()
logrus.Debugf("Requesting the build of image: '%s'", imageRef)
var imageName string
logrus.Debugf("Requesting the build of image: '%s'", imageName)
var imageArch string
imageBuiltLocally := true
imagePulledFromRemote := false
defer wg.Done()
imageCurrentlyBuilding <- true
nixBuildStarted <- imageName
nixBuildStarted <- imageRef
defer func() {
<-imageCurrentlyBuilding
nixBuildFinished <- NewValidatedImage(imageName, imagePulledFromRemote, imageBuiltLocally, imageArch)
}()

logrus.Debugf("Starting the build of image: '%s'", imageName)
logrus.Debugf("Starting the build of image: '%s'", imageRef)
imageName, err := (*backend).NixBuild(ctx, nixBuildSpec)
if err != nil {
logrus.Warnf("Container image '%s' build failed. Error was: '%s'", imageName, err.Error())
buildErrors <- startosis_errors.WrapWithValidationError(err, "Failed to build the required image '%v'.", imageName)
logrus.Warnf("Container image '%s' build failed. Error was: '%s'", imageRef, err.Error())
buildErrors <- startosis_errors.WrapWithValidationError(err, "Failed to build the required image '%v'.", imageRef)
return
}
logrus.Debugf("Container image '%s' successfully built", imageName)
logrus.Debugf("Container image '%s' successfully built from Nix definition %s", imageName, imageRef)
}

0 comments on commit e1e24ac

Please sign in to comment.