Skip to content

Commit

Permalink
save and detect future references on service
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Feb 29, 2024
1 parent 31a1be6 commit 56d9331
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 58 deletions.
33 changes: 10 additions & 23 deletions core/server/api_container/server/startosis_engine/plan.yml
@@ -1,26 +1,13 @@
packageId: github.com/kurtosis-tech/plan-yaml-prac
filesArtifacts:
services:
- uuid: 1
name: bye-file
files:
- bye.txt
- uuid: 3
name: hi-file
files:
- /hi.txt
tasks:
name: database
image:
name: postgres:latest
- uuid: 2
taskType: sh
command: echo $(cat /root/bye.txt) > hi.txt
image: badouralix/curl-jq
files:
- mountPath: /root
filesArtifacts:
- uuid: 1
name: bye-file
store:
- uuid: 3
name: hi-file
envVar:
- key: HELLO
value: Hello!
name: tedi
image:
name: ubuntu:latest
envVars:
- key: DB_URL
value: '{{ kurtosis.1.ip_address }}'
@@ -1,6 +1,7 @@
package startosis_engine

import (
"fmt"
"github.com/go-yaml/yaml"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/instructions_plan"
Expand All @@ -18,7 +19,9 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/recipe"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_packages"
"github.com/kurtosis-tech/stacktrace"
"go.starlark.net/starlark"
"strings"
)

// We need the package id and the args, the args need to be filled in
Expand Down Expand Up @@ -95,6 +98,8 @@ type PlanYamlGeneratorImpl struct {
planYaml *PlanYaml

uuidGenerator int

futureReferenceIndex map[string]string
}

func NewPlanYamlGenerator(
Expand All @@ -114,9 +119,12 @@ func NewPlanYamlGenerator(
FilesArtifacts: []*FilesArtifact{},
Tasks: []*Task{},
},
filesArtifactIndex: map[string]*FilesArtifact{},
serviceIndex: map[string]*Service{},
taskIndex: map[string]*Task{}}
filesArtifactIndex: map[string]*FilesArtifact{},
serviceIndex: map[string]*Service{},
taskIndex: map[string]*Task{},
uuidGenerator: 0,
futureReferenceIndex: map[string]string{},
}
}

func (pyg *PlanYamlGeneratorImpl) GenerateYaml() ([]byte, error) {
Expand Down Expand Up @@ -165,17 +173,42 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromAddService(addServiceInstruc

// start building Service Yaml object
service := &Service{} //nolint:exhaustruct
uuid := pyg.generateUuid()
service.Uuid = uuid

service.Uuid = pyg.generateUuid()

serviceName, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, add_service.ServiceNameArgName)
// store future references of this service
returnValue := addServiceInstruction.GetReturnedValue()
returnedService, ok := returnValue.(*kurtosis_types.Service)
if !ok {
return stacktrace.NewError("Cast to service didn't work")
}
//futureRefServiceName, err := returnedService.GetName()
//if err != nil {
// return err
//}
//pyg.futureReferenceIndex[string(futureRefServiceName)] = fmt.Sprintf("{{ kurtosis.%v.service_name }}", uuid)
futureRefIPAddress, err := returnedService.GetIpAddress()
if err != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", add_service.ServiceNameArgName)
return err
}
service.Name = string(serviceName)

starlarkServiceConfig, err := builtin_argument.ExtractArgumentValue[*service_config.ServiceConfig](arguments, add_service.ServiceConfigArgName)
pyg.futureReferenceIndex[futureRefIPAddress] = fmt.Sprintf("{{ kurtosis.%v.ip_address }}", uuid)
futureRefHostName, err := returnedService.GetHostname()
if err != nil {
return err
}
pyg.futureReferenceIndex[futureRefHostName] = fmt.Sprintf("{{ kurtosis.%v.hostname }}", uuid)

service.Uuid = uuid

var regErr error
serviceName, regErr := builtin_argument.ExtractArgumentValue[starlark.String](arguments, add_service.ServiceNameArgName)
if regErr != nil {
return startosis_errors.WrapWithInterpretationError(regErr, "Unable to extract value for '%s' argument", add_service.ServiceNameArgName)
}
service.Name = pyg.swapFutureReference(serviceName.GoString()) // swap future references in the strings

starlarkServiceConfig, regErr := builtin_argument.ExtractArgumentValue[*service_config.ServiceConfig](arguments, add_service.ServiceConfigArgName)
if regErr != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", add_service.ServiceConfigArgName)
}
serviceConfig, serviceConfigErr := starlarkServiceConfig.ToKurtosisType( // is this an expensive call? // TODO: add this error back in
Expand Down Expand Up @@ -214,8 +247,20 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromAddService(addServiceInstruc
}
service.Image = image

service.Cmd = serviceConfig.GetCmdArgs()
service.Entrypoint = serviceConfig.GetEntrypointArgs()
// detect future references
cmdArgs := []string{}
for _, cmdArg := range serviceConfig.GetCmdArgs() {
realCmdArg := pyg.swapFutureReference(cmdArg)
cmdArgs = append(cmdArgs, realCmdArg)
}
service.Cmd = cmdArgs

entryArgs := []string{}
for _, entryArg := range serviceConfig.GetEntrypointArgs() {
realEntryArg := pyg.swapFutureReference(entryArg)
entryArgs = append(entryArgs, realEntryArg)
}
service.Entrypoint = entryArgs

// ports
service.Ports = []*Port{}
Expand All @@ -236,9 +281,11 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromAddService(addServiceInstruc
// env vars
service.EnvVars = []*EnvironmentVariable{}
for key, val := range serviceConfig.GetEnvVars() {
// detect and future references
value := pyg.swapFutureReference(val)
envVar := &EnvironmentVariable{
Key: key,
Value: val,
Value: value,
}
service.EnvVars = append(service.EnvVars, envVar)
}
Expand Down Expand Up @@ -759,3 +806,15 @@ func (pyg *PlanYamlGeneratorImpl) generateUuid() int {
pyg.uuidGenerator++
return pyg.uuidGenerator
}

// if the string is a future reference, it swaps it out with what it should be
// else the string s is the same
func (pyg *PlanYamlGeneratorImpl) swapFutureReference(s string) string {
for futureRef, swappedValue := range pyg.futureReferenceIndex {
if strings.Contains(s, futureRef) { // TODO: handle case where s contains multiple future references
newString := strings.Replace(s, futureRef, swappedValue, -1)
return newString
}
}
return s
}
Expand Up @@ -94,9 +94,9 @@ func (suite *PlanYamlGeneratorTestSuite) SetupTest() {
suite.runner = NewStartosisRunner(suite.interpreter, suite.validator, suite.executor)
}

func TestRunPlanYamlGeneratorTestSuite(t *testing.T) {
suite.Run(t, new(PlanYamlGeneratorTestSuite))
}
//func TestRunPlanYamlGeneratorTestSuite(t *testing.T) {
// suite.Run(t, new(PlanYamlGeneratorTestSuite))
//}

func (suite *PlanYamlGeneratorTestSuite) TearDownTest() {
suite.packageContentProvider.RemoveAll()
Expand Down Expand Up @@ -137,28 +137,23 @@ CMD ["node", "app.js"]
relativePathToMainFile := "main.star"

serializedScript := `def run(plan, args):
bye_files_artifact = plan.render_templates(
name="bye-file",
config={
"bye.txt": struct(
template="Bye bye!",
data={}
)
}
database = plan.add_service(
name="database",
config=ServiceConfig(
image="postgres:latest",
)
)
plan.run_sh(
run="echo $(cat /root/bye.txt) > hi.txt",
env_vars = {
"HELLO": "Hello!"
},
files = {
"/root": bye_files_artifact,
},
store=[
StoreSpec(src="/hi.txt", name="hi-file")
]
plan.add_service(
name="tedi",
config=ServiceConfig(
image="ubuntu:latest",
env_vars={
"DB_URL": database.ip_address
},
)
)
`
serializedJsonParams := "{}"
_, instructionsPlan, interpretationError := suite.interpreter.Interpret(context.Background(), packageId, mainFunctionName, noPackageReplaceOptions, relativePathToMainFile, serializedScript, serializedJsonParams, defaultNonBlockingMode, emptyEnclaveComponents, emptyInstructionsPlanMask)
Expand Down

0 comments on commit 56d9331

Please sign in to comment.