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: set service v0 #2372

Merged
merged 27 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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 @@ -29,9 +29,12 @@ type privateServiceConfig struct {

// 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
// Mutually exclusive from ImageBuildSpec, ContainerImageName, NixBuildSpec
ImageRegistrySpec *image_registry_spec.ImageRegistrySpec

// Configuration for container engine to using Nix
// If nil, we will use the ContainerImageName and not use any Nix
// Mutually exclusive from ImageBuildSpec, ContainerImageName, ImageRegistrySpec
NixBuildSpec *nix_build_spec.NixBuildSpec

PrivatePorts map[string]*port_spec.PortSpec
Expand Down Expand Up @@ -88,7 +91,7 @@ func CreateServiceConfig(
cpuAllocationMillicpus uint64,
memoryAllocationMegabytes uint64,
privateIPAddrPlaceholder string,
minCpuMilliCores uint64,
minCpuMilliCpus uint64,
minMemoryMegaBytes uint64,
labels map[string]string,
user *service_user.ServiceUser,
Expand All @@ -104,7 +107,7 @@ func CreateServiceConfig(
internalServiceConfig := &privateServiceConfig{
ContainerImageName: containerImageName,
ImageBuildSpec: imageBuildSpec,
ImagerRegistrySpec: imageRegistrySpec,
ImageRegistrySpec: imageRegistrySpec,
NixBuildSpec: nixBuildSpec,
PrivatePorts: privatePorts,
PublicPorts: publicPorts,
Expand All @@ -117,7 +120,7 @@ func CreateServiceConfig(
MemoryAllocationMegabytes: memoryAllocationMegabytes,
PrivateIPAddrPlaceholder: privateIPAddrPlaceholder,
// The minimum resources specification is only available for kubernetes
MinCpuAllocationMilliCpus: minCpuMilliCores,
MinCpuAllocationMilliCpus: minCpuMilliCpus,
MinMemoryAllocationMegabytes: minMemoryMegaBytes,
Labels: labels,
User: user,
Expand All @@ -133,18 +136,34 @@ func (serviceConfig *ServiceConfig) GetContainerImageName() string {
return serviceConfig.privateServiceConfig.ContainerImageName
}

func (serviceConfig *ServiceConfig) SetContainerImageName(containerImage string) {
serviceConfig.privateServiceConfig.ContainerImageName = containerImage
}

func (serviceConfig *ServiceConfig) GetImageBuildSpec() *image_build_spec.ImageBuildSpec {
return serviceConfig.privateServiceConfig.ImageBuildSpec
}

func (serviceConfig *ServiceConfig) SetImageBuildSpec(imageBuildSpec *image_build_spec.ImageBuildSpec) {
serviceConfig.privateServiceConfig.ImageBuildSpec = imageBuildSpec
}

func (serviceConfig *ServiceConfig) GetImageRegistrySpec() *image_registry_spec.ImageRegistrySpec {
return serviceConfig.privateServiceConfig.ImagerRegistrySpec
return serviceConfig.privateServiceConfig.ImageRegistrySpec
}

func (serviceConfig *ServiceConfig) SetImageRegistrySpec(imageRegistrySpec *image_registry_spec.ImageRegistrySpec) {
serviceConfig.privateServiceConfig.ImageRegistrySpec = imageRegistrySpec
}

func (serviceConfig *ServiceConfig) GetNixBuildSpec() *nix_build_spec.NixBuildSpec {
return serviceConfig.privateServiceConfig.NixBuildSpec
}

func (serviceConfig *ServiceConfig) SetNixBuildSpec(nixBuildSpec *nix_build_spec.NixBuildSpec) {
serviceConfig.privateServiceConfig.NixBuildSpec = nixBuildSpec
}

func (serviceConfig *ServiceConfig) GetPrivatePorts() map[string]*port_spec.PortSpec {
return serviceConfig.privateServiceConfig.PrivatePorts
}
Expand Down Expand Up @@ -177,10 +196,18 @@ func (serviceConfig *ServiceConfig) GetCPUAllocationMillicpus() uint64 {
return serviceConfig.privateServiceConfig.CpuAllocationMillicpus
}

func (serviceConfig *ServiceConfig) SetCPUAllocationMillicpus(cpuAllocation uint64) {
serviceConfig.privateServiceConfig.CpuAllocationMillicpus = cpuAllocation
}

func (serviceConfig *ServiceConfig) GetMemoryAllocationMegabytes() uint64 {
return serviceConfig.privateServiceConfig.MemoryAllocationMegabytes
}

func (serviceConfig *ServiceConfig) SetMemoryAllocationMegabytes(memoryAllocation uint64) {
serviceConfig.privateServiceConfig.MemoryAllocationMegabytes = memoryAllocation
}

func (serviceConfig *ServiceConfig) GetPrivateIPAddrPlaceholder() string {
return serviceConfig.privateServiceConfig.PrivateIPAddrPlaceholder
}
Expand All @@ -190,27 +217,51 @@ func (serviceConfig *ServiceConfig) GetMinCPUAllocationMillicpus() uint64 {
return serviceConfig.privateServiceConfig.MinCpuAllocationMilliCpus
}

func (serviceConfig *ServiceConfig) SetMinCPUAllocationMillicpus(cpuAllocation uint64) {
serviceConfig.privateServiceConfig.MinCpuAllocationMilliCpus = cpuAllocation
}

// only available for Kubernetes
func (serviceConfig *ServiceConfig) GetMinMemoryAllocationMegabytes() uint64 {
return serviceConfig.privateServiceConfig.MinMemoryAllocationMegabytes
}

func (serviceConfig *ServiceConfig) SetMinMemoryAllocationMegabytes(memoryAllocation uint64) {
serviceConfig.privateServiceConfig.MemoryAllocationMegabytes = memoryAllocation
}

func (serviceConfig *ServiceConfig) GetUser() *service_user.ServiceUser {
return serviceConfig.privateServiceConfig.User
}

func (serviceConfig *ServiceConfig) SetUser(user *service_user.ServiceUser) {
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
serviceConfig.privateServiceConfig.User = user
}

func (serviceConfig *ServiceConfig) GetLabels() map[string]string {
return serviceConfig.privateServiceConfig.Labels
}

func (serviceConfig *ServiceConfig) SetLabels(labels map[string]string) {
serviceConfig.privateServiceConfig.Labels = labels
}

func (serviceConfig *ServiceConfig) GetTolerations() []v1.Toleration {
return serviceConfig.privateServiceConfig.Tolerations
}

func (serviceConfig *ServiceConfig) SetTolerations(tolerations []v1.Toleration) {
serviceConfig.privateServiceConfig.Tolerations = tolerations
}

func (serviceConfig *ServiceConfig) GetImageDownloadMode() image_download_mode.ImageDownloadMode {
return serviceConfig.privateServiceConfig.ImageDownloadMode
}

func (serviceConfig *ServiceConfig) SetImageDownloadMode(mode image_download_mode.ImageDownloadMode) {
serviceConfig.privateServiceConfig.ImageDownloadMode = mode
}

func (serviceConfig *ServiceConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(serviceConfig.privateServiceConfig)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import (
)

type InterpretationTimeValueStore struct {
serviceValues *serviceInterpretationValueRepository
serde *kurtosis_types.StarlarkValueSerde
serviceConfigValues map[service.ServiceName]*service.ServiceConfig
setServiceConfigValues map[service.ServiceName]*service.ServiceConfig
serviceValues *serviceInterpretationValueRepository
serde *kurtosis_types.StarlarkValueSerde
}

func CreateInterpretationTimeValueStore(enclaveDb *enclave_db.EnclaveDB, serde *kurtosis_types.StarlarkValueSerde) (*InterpretationTimeValueStore, error) {
serviceValuesRepository, err := getOrCreateNewServiceInterpretationTimeValueRepository(enclaveDb, serde)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating interpretation time value store")
}
return &InterpretationTimeValueStore{serviceValues: serviceValuesRepository, serde: serde}, nil
return &InterpretationTimeValueStore{
serviceConfigValues: map[service.ServiceName]*service.ServiceConfig{},
setServiceConfigValues: map[service.ServiceName]*service.ServiceConfig{},
serviceValues: serviceValuesRepository,
serde: serde}, nil
}

func (itvs *InterpretationTimeValueStore) PutService(name service.ServiceName, service *kurtosis_types.Service) error {
Expand Down Expand Up @@ -50,3 +56,32 @@ func (itvs *InterpretationTimeValueStore) RemoveService(name service.ServiceName
}
return nil
}

func (itvs *InterpretationTimeValueStore) PutServiceConfig(name service.ServiceName, serviceConfig *service.ServiceConfig) {
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
itvs.serviceConfigValues[name] = serviceConfig
}

func (itvs *InterpretationTimeValueStore) GetServiceConfig(name service.ServiceName) (*service.ServiceConfig, error) {
serviceConfig, ok := itvs.serviceConfigValues[name]
if !ok {
return nil, stacktrace.NewError("Did not find new service config for '%v' in interpretation time value store.", name)
}
return serviceConfig, nil
}

func (itvs *InterpretationTimeValueStore) SetServiceConfig(name service.ServiceName, serviceConfig *service.ServiceConfig) {
itvs.setServiceConfigValues[name] = serviceConfig
}

func (itvs *InterpretationTimeValueStore) ExistsNewServiceConfigForService(name service.ServiceName) bool {
_, doesConfigFromSetServiceInstructionExists := itvs.setServiceConfigValues[name]
return doesConfigFromSetServiceInstructionExists
}

func (itvs *InterpretationTimeValueStore) GetNewServiceConfig(name service.ServiceName) (*service.ServiceConfig, error) {
newServiceConfig, ok := itvs.setServiceConfigValues[name]
if !ok {
return nil, stacktrace.NewError("Did not find new service config for '%v' in interpretation time value store.", name)
}
return newServiceConfig, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/remove_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/render_templates"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/request"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/set_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/start_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/stop_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/store_service_files"
Expand Down Expand Up @@ -73,6 +74,7 @@ func KurtosisPlanInstructions(
add_service.NewAddServices(serviceNetwork, runtimeValueStore, packageId, packageContentProvider, packageReplaceOptions, interpretationTimeValueStore, imageDownloadMode),
get_service.NewGetService(interpretationTimeValueStore),
get_services.NewGetServices(interpretationTimeValueStore),
set_service.NewSetService(serviceNetwork, interpretationTimeValueStore, packageId, packageContentProvider, packageReplaceOptions, imageDownloadMode),
get_files_artifact.NewGetFilesArtifact(),
verify.NewVerify(runtimeValueStore),
exec.NewExec(serviceNetwork, runtimeValueStore),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (builtin *AddServiceCapabilities) Interpret(locatorOfModuleInWhichThisBuilt
return nil, startosis_errors.NewInterpretationError("Unable to extract image attribute off of service config.")
}
builtin.imageVal = rawImageVal
apiServiceConfig, readyCondition, interpretationErr := validateAndConvertConfigAndReadyCondition(
apiServiceConfig, readyCondition, interpretationErr := ValidateAndConvertConfigAndReadyCondition(
builtin.serviceNetwork,
serviceConfig,
locatorOfModuleInWhichThisBuiltInIsBeingCalled,
Expand Down Expand Up @@ -172,6 +172,8 @@ func (builtin *AddServiceCapabilities) Interpret(locatorOfModuleInWhichThisBuilt
if err != nil {
return nil, startosis_errors.WrapWithInterpretationError(err, "An error occurred while persisting return value for service '%v'", serviceName)
}
builtin.interpretationTimeValueStore.PutServiceConfig(builtin.serviceName, builtin.serviceConfig)
tedim52 marked this conversation as resolved.
Show resolved Hide resolved

return builtin.returnValue, nil
}

Expand All @@ -183,6 +185,14 @@ func (builtin *AddServiceCapabilities) Validate(_ *builtin_argument.ArgumentValu
}

func (builtin *AddServiceCapabilities) Execute(ctx context.Context, _ *builtin_argument.ArgumentValuesSet) (string, error) {
// update service config to use new service config set by a set_service instruction, if one exists
if builtin.interpretationTimeValueStore.ExistsNewServiceConfigForService(builtin.serviceName) {
newServiceConfig, err := builtin.interpretationTimeValueStore.GetNewServiceConfig(builtin.serviceName)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred retrieving a new service config '%s'.", builtin.serviceName)
}
builtin.serviceConfig = newServiceConfig
}
replacedServiceName, replacedServiceConfig, err := replaceMagicStrings(builtin.runtimeValueStore, builtin.serviceName, builtin.serviceConfig)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred replace a magic string in '%s' instruction arguments for service '%s'. Execution cannot proceed", AddServiceBuiltinName, builtin.serviceName)
Expand Down Expand Up @@ -257,6 +267,12 @@ func (builtin *AddServiceCapabilities) TryResolveWith(instructionsAreEqual bool,
}
}

// We check if service config was changed by a set_service instruction. If that's the case, it should be rerun
if builtin.interpretationTimeValueStore.ExistsNewServiceConfigForService(builtin.serviceName) {
enclaveComponents.AddService(builtin.serviceName, enclave_structure.ComponentIsUpdated)
return enclave_structure.InstructionIsUpdate
}

enclaveComponents.AddService(builtin.serviceName, enclave_structure.ComponentWasLeftIntact)
return enclave_structure.InstructionIsEqual
}
Expand Down Expand Up @@ -308,7 +324,7 @@ func (builtin *AddServiceCapabilities) Description() string {
return builtin.description
}

func validateAndConvertConfigAndReadyCondition(
func ValidateAndConvertConfigAndReadyCondition(
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
serviceNetwork service_network.ServiceNetwork,
rawConfig starlark.Value,
locatorOfModuleInWhichThisBuiltInIsBeingCalled string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (builtin *GetServicesCapabilities) TryResolveWith(instructionsAreEqual bool
if instructionsAreEqual {
return enclave_structure.InstructionIsEqual
}

return enclave_structure.InstructionIsUnknown
}

Expand Down
Loading
Loading