From e2784c4e2db6043e575bf9918db7863e11041fa8 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Fri, 5 Jan 2024 14:42:34 +0000 Subject: [PATCH] ensure that persitent keys are unique --- .../add_service/add_service_shared.go | 5 ++++- .../startosis_validator/validator_environment.go | 14 ++++++++++++++ .../api-reference/starlark-reference/directory.md | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service_shared.go b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service_shared.go index 95238e0a79..79a0309b8f 100644 --- a/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service_shared.go +++ b/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service/add_service_shared.go @@ -71,12 +71,15 @@ func validateSingleService(validatorEnvironment *startosis_validator.ValidatorEn return startosis_errors.NewValidationError(invalidServiceNameErrorText(serviceName)) } - // TODO perhaps add AddPersistentKey DoesPersistentKeyExist methods to the validator environment checks for it here if persistentDirectories := serviceConfig.GetPersistentDirectories(); persistentDirectories != nil { for _, directory := range persistentDirectories.ServiceDirpathToPersistentDirectory { if !service_directory.IsPersistentKeyValid(directory.PersistentKey) { return startosis_errors.NewValidationError(invalidPersistentKeyErrorText(directory.PersistentKey)) } + if validatorEnvironment.DoesPersistentKeyExist(directory.PersistentKey) == startosis_validator.ComponentCreatedOrUpdatedDuringPackageRun { + return startosis_errors.NewValidationError("There was an error validating '%s' as persistent key '%s' already exists inside the enclave") + } + validatorEnvironment.AddPersistentKey(directory.PersistentKey) } } diff --git a/core/server/api_container/server/startosis_engine/startosis_validator/validator_environment.go b/core/server/api_container/server/startosis_engine/startosis_validator/validator_environment.go index 2d581810ad..89784491d5 100644 --- a/core/server/api_container/server/startosis_engine/startosis_validator/validator_environment.go +++ b/core/server/api_container/server/startosis_engine/startosis_validator/validator_environment.go @@ -5,6 +5,7 @@ import ( "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_download_mode" "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/startosis_engine/startosis_errors" "github.com/sirupsen/logrus" ) @@ -15,6 +16,7 @@ type ValidatorEnvironment struct { imagesToBuild map[string]*image_build_spec.ImageBuildSpec serviceNames map[service.ServiceName]ComponentExistence artifactNames map[string]ComponentExistence + persistentKeys map[service_directory.DirectoryPersistentKey]ComponentExistence serviceNameToPrivatePortIDs map[service.ServiceName][]string availableCpuInMilliCores compute_resources.CpuMilliCores availableMemoryInMegaBytes compute_resources.MemoryInMegaBytes @@ -160,3 +162,15 @@ func (environment *ValidatorEnvironment) HasEnoughMemory(memoryToConsume uint64, } return startosis_errors.NewValidationError("service '%v' requires '%v' megabytes of memory but based on our calculation we will only have '%v' megabytes available at the time we start the service", serviceNameForLogging, memoryToConsume, environment.availableMemoryInMegaBytes) } + +func (environment *ValidatorEnvironment) AddPersistentKey(persistentKey service_directory.DirectoryPersistentKey) { + environment.persistentKeys[persistentKey] = ComponentCreatedOrUpdatedDuringPackageRun +} + +func (environment *ValidatorEnvironment) DoesPersistentKeyExist(persistentKey service_directory.DirectoryPersistentKey) ComponentExistence { + persistentKeyExistence, found := environment.persistentKeys[persistentKey] + if !found { + return ComponentNotFound + } + return persistentKeyExistence +} diff --git a/docs/docs/api-reference/starlark-reference/directory.md b/docs/docs/api-reference/starlark-reference/directory.md index 716b94e680..d6a717e9f2 100644 --- a/docs/docs/api-reference/starlark-reference/directory.md +++ b/docs/docs/api-reference/starlark-reference/directory.md @@ -24,7 +24,7 @@ the above example, `files_artifact_1` is a files artifact name. (see [upload_fil on how to create file artifacts). A persistent directory, as its name indicates, persists over service updates and restarts. It is uniquely identified -by its `persistent_key` and the service ID on which it is being used (a persistent directory cannot be shared across +by its `persistent_key` (a persistent directory cannot be shared across multiple services). When it is first created, it will be empty. The service can write anything in it. When the service gets updated, the data in it persists. It is particularly useful for a service's data directory, logs directory, etc.