-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add create enclave utils to SDK (#1550)
## Description: <!-- Describe this change, how it works, and the motivation behind it. --> A very idiomatic pattern in Go is to use `defer` and closer functions to cleanup resources, instead of relying on memory to call the correct SDK calls when the test/setup is done. This is very common on our test suit that we have a util, that now is exposed to an external user. This PR also introduces a single SDK call to get from starlark to enclave, something that was not possible before. ## Is this change user facing? YES <!-- If yes, please add the "user facing" label to the PR --> <!-- If yes, don't forget to include docs changes where relevant --> ## References (if applicable): <!-- Add relevant Github Issues, Discord threads, or other helpful information. --> #1152
- Loading branch information
1 parent
ccff275
commit dbca010
Showing
2 changed files
with
78 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config" | ||
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context" | ||
"github.com/kurtosis-tech/stacktrace" | ||
) | ||
|
||
func CreateEmptyEnclave(ctx context.Context, enclaveName string) (*enclaves.EnclaveContext, func() error, func() error, error) { | ||
kurtosisCtx, err := kurtosis_context.NewKurtosisContextFromLocalEngine() | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to get kurtosis context from local engine. Is the engine running? Try running 'kurtosis engine start'") | ||
} | ||
enclaveCtx, err := kurtosisCtx.CreateEnclave(ctx, enclaveName) | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to create enclave.") | ||
} | ||
stopEnclaveFunc := func() error { | ||
return kurtosisCtx.StopEnclave(ctx, enclaveName) | ||
|
||
} | ||
destroyEnclaveFunc := func() error { | ||
return kurtosisCtx.DestroyEnclave(ctx, enclaveName) | ||
} | ||
|
||
return enclaveCtx, stopEnclaveFunc, destroyEnclaveFunc, nil | ||
} | ||
|
||
func combineErrors(starlarkResult *enclaves.StarlarkRunResult, err error) error { | ||
if err != nil { | ||
return stacktrace.Propagate(err, "Failed to run Starlark during setup") | ||
} | ||
if starlarkResult.InterpretationError != nil { | ||
return stacktrace.NewError("Failed to setup enclave using Starlark because of an interpretation error:\n%v", starlarkResult.InterpretationError) | ||
} | ||
if len(starlarkResult.ValidationErrors) > 0 { | ||
return stacktrace.NewError("Failed to setup enclave using Starlark because of an interpretation error:\n%v", starlarkResult.ValidationErrors) | ||
} | ||
if starlarkResult.ExecutionError != nil { | ||
return stacktrace.NewError("Failed to setup enclave using Starlark because of an execution error:\n%v", starlarkResult.ExecutionError) | ||
} | ||
return nil | ||
} | ||
|
||
func CreateEnclaveFromStarlarkScript(ctx context.Context, enclaveName string, serializedScript string, runConfig *starlark_run_config.StarlarkRunConfig) (*enclaves.EnclaveContext, func() error, func() error, error) { | ||
enclaveCtx, stopEnclaveFunc, destroyEnclaveFunc, err := CreateEmptyEnclave(ctx, enclaveName) | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to create empty enclave") | ||
} | ||
starlarkResult, err := enclaveCtx.RunStarlarkScriptBlocking(ctx, serializedScript, runConfig) | ||
err = combineErrors(starlarkResult, err) | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to setup enclave using Starlark") | ||
} | ||
return enclaveCtx, stopEnclaveFunc, destroyEnclaveFunc, nil | ||
} | ||
|
||
func CreateEnclaveFromStarlarkRemotePackage(ctx context.Context, enclaveName string, packageId string, runConfig *starlark_run_config.StarlarkRunConfig) (*enclaves.EnclaveContext, func() error, func() error, error) { | ||
enclaveCtx, stopEnclaveFunc, destroyEnclaveFunc, err := CreateEmptyEnclave(ctx, enclaveName) | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to create empty enclave") | ||
} | ||
starlarkResult, err := enclaveCtx.RunStarlarkRemotePackageBlocking(ctx, packageId, runConfig) | ||
err = combineErrors(starlarkResult, err) | ||
if err != nil { | ||
return nil, nil, nil, stacktrace.Propagate(err, "Failed to setup enclave using Starlark") | ||
} | ||
return enclaveCtx, stopEnclaveFunc, destroyEnclaveFunc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters