Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add starlark.Value serializer/deserializer for enclave persiste…
…nce (#1229) ## Description: For idempotent runs, we need to store for each instruction that was run the result object of this instruction, which can be any starlark.Value. This serde mechanism relies the Starlark code itself to serialize the value, and runs a mini-interpretation step to deserialize it. ## Is this change user facing? NO <!-- 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. -->
- Loading branch information
Guillaume Bouvignies
committed
Sep 5, 2023
1 parent
94a4b8b
commit 45b9330
Showing
10 changed files
with
328 additions
and
214 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
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
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
59 changes: 59 additions & 0 deletions
59
...server/startosis_engine/kurtosis_starlark_framework/test_engine/service_framework_test.go
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,59 @@ | ||
package test_engine | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/builtin_argument" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
const ( | ||
testServiceIpAddress = "192.168.0.43" | ||
testServiceHostname = "test-service-hostname" | ||
testServicePorts = "{}" | ||
) | ||
|
||
type serviceTestCase struct { | ||
*testing.T | ||
} | ||
|
||
func newServiceTestCase(t *testing.T) *serviceTestCase { | ||
return &serviceTestCase{ | ||
T: t, | ||
} | ||
} | ||
|
||
func (t serviceTestCase) GetId() string { | ||
return kurtosis_types.ServiceTypeName | ||
} | ||
|
||
func (t serviceTestCase) GetStarlarkCode() string { | ||
return fmt.Sprintf("%s(%s=%q, %s=%q, %s=%q, %s=%s)", | ||
kurtosis_types.ServiceTypeName, | ||
kurtosis_types.ServiceNameAttr, TestServiceName, | ||
kurtosis_types.HostnameAttr, testServiceHostname, | ||
kurtosis_types.IpAddressAttr, testServiceIpAddress, | ||
kurtosis_types.PortsAttr, testServicePorts) | ||
} | ||
|
||
func (t serviceTestCase) Assert(typeValue builtin_argument.KurtosisValueType) { | ||
serviceStarlark, ok := typeValue.(*kurtosis_types.Service) | ||
require.True(t, ok) | ||
|
||
resultServiceName, interpretationErr := serviceStarlark.GetName() | ||
require.Nil(t, interpretationErr) | ||
require.Equal(t, TestServiceName, resultServiceName) | ||
|
||
resultServiceHostname, interpretationErr := serviceStarlark.GetHostname() | ||
require.Nil(t, interpretationErr) | ||
require.Equal(t, testServiceHostname, resultServiceHostname) | ||
|
||
resultServiceIpAddress, interpretationErr := serviceStarlark.GetIpAddress() | ||
require.Nil(t, interpretationErr) | ||
require.Equal(t, testServiceIpAddress, resultServiceIpAddress) | ||
|
||
resultPorts, interpretationErr := serviceStarlark.GetPorts() | ||
require.Nil(t, interpretationErr) | ||
require.Equal(t, 0, resultPorts.Len()) | ||
} |
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
151 changes: 122 additions & 29 deletions
151
core/server/api_container/server/startosis_engine/kurtosis_types/service.go
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 |
---|---|---|
@@ -1,50 +1,143 @@ | ||
package kurtosis_types | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/builtin_argument" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_type_constructor" | ||
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors" | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
"strings" | ||
) | ||
|
||
const ( | ||
serviceTypeName = "Service" | ||
ServiceTypeName = "Service" | ||
|
||
hostnameAttr = "hostname" | ||
ipAddressAttr = "ip_address" | ||
portsAttr = "ports" | ||
serviceNameAttr = "name" | ||
HostnameAttr = "hostname" | ||
IpAddressAttr = "ip_address" | ||
PortsAttr = "ports" | ||
ServiceNameAttr = "name" | ||
) | ||
|
||
// Service is just a wrapper around a regular starlarkstruct.Struct | ||
// It naturally inherits all its function making it a valid starlark.Value | ||
func NewServiceType() *kurtosis_type_constructor.KurtosisTypeConstructor { | ||
return &kurtosis_type_constructor.KurtosisTypeConstructor{ | ||
KurtosisBaseBuiltin: &kurtosis_starlark_framework.KurtosisBaseBuiltin{ | ||
Name: ServiceTypeName, | ||
|
||
Arguments: []*builtin_argument.BuiltinArgument{ | ||
{ | ||
Name: ServiceNameAttr, | ||
IsOptional: false, | ||
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String], | ||
Validator: func(value starlark.Value) *startosis_errors.InterpretationError { | ||
return builtin_argument.NonEmptyString(value, ServiceNameAttr) | ||
}, | ||
}, | ||
{ | ||
Name: HostnameAttr, | ||
IsOptional: false, | ||
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String], | ||
Validator: func(value starlark.Value) *startosis_errors.InterpretationError { | ||
return builtin_argument.NonEmptyString(value, HostnameAttr) | ||
}, | ||
}, | ||
{ | ||
Name: IpAddressAttr, | ||
IsOptional: false, | ||
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String], | ||
Validator: func(value starlark.Value) *startosis_errors.InterpretationError { | ||
return builtin_argument.NonEmptyString(value, IpAddressAttr) | ||
}, | ||
}, | ||
{ | ||
Name: PortsAttr, | ||
IsOptional: false, | ||
ZeroValueProvider: builtin_argument.ZeroValueProvider[*starlark.Dict], | ||
Validator: func(value starlark.Value) *startosis_errors.InterpretationError { | ||
return nil | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
Instantiate: instantiate, | ||
} | ||
} | ||
|
||
func instantiate(arguments *builtin_argument.ArgumentValuesSet) (builtin_argument.KurtosisValueType, *startosis_errors.InterpretationError) { | ||
kurtosisValueType, interpretationErr := kurtosis_type_constructor.CreateKurtosisStarlarkTypeDefault(ServiceTypeName, arguments) | ||
if interpretationErr != nil { | ||
return nil, interpretationErr | ||
} | ||
return &Service{ | ||
KurtosisValueTypeDefault: kurtosisValueType, | ||
}, nil | ||
} | ||
|
||
type Service struct { | ||
*starlarkstruct.Struct | ||
*kurtosis_type_constructor.KurtosisValueTypeDefault | ||
} | ||
|
||
func CreateService(serviceName starlark.String, hostname starlark.String, ipAddress starlark.String, ports *starlark.Dict) (*Service, *startosis_errors.InterpretationError) { | ||
args := []starlark.Value{ | ||
serviceName, | ||
hostname, | ||
ipAddress, | ||
ports, | ||
} | ||
|
||
argumentDefinitions := NewServiceType().KurtosisBaseBuiltin.Arguments | ||
argumentValuesSet := builtin_argument.NewArgumentValuesSet(argumentDefinitions, args) | ||
kurtosisDefaultValue, interpretationErr := kurtosis_type_constructor.CreateKurtosisStarlarkTypeDefault(ServiceTypeName, argumentValuesSet) | ||
if interpretationErr != nil { | ||
return nil, interpretationErr | ||
} | ||
return &Service{ | ||
KurtosisValueTypeDefault: kurtosisDefaultValue, | ||
}, nil | ||
} | ||
|
||
func NewService(serviceName starlark.String, hostname starlark.String, ipAddress starlark.String, ports *starlark.Dict) *Service { | ||
structDict := starlark.StringDict{ | ||
serviceNameAttr: serviceName, | ||
hostnameAttr: hostname, | ||
ipAddressAttr: ipAddress, | ||
portsAttr: ports, | ||
func (serviceObj *Service) Copy() (builtin_argument.KurtosisValueType, error) { | ||
copiedValueType, err := serviceObj.KurtosisValueTypeDefault.Copy() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Service{ | ||
Struct: starlarkstruct.FromStringDict(starlark.String(serviceTypeName), structDict), | ||
KurtosisValueTypeDefault: copiedValueType, | ||
}, nil | ||
} | ||
|
||
func (serviceObj *Service) GetName() (service.ServiceName, *startosis_errors.InterpretationError) { | ||
serviceName, _, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String]( | ||
serviceObj.KurtosisValueTypeDefault, ServiceNameAttr) | ||
if interpretationErr != nil { | ||
return "", interpretationErr | ||
} | ||
return service.ServiceName(serviceName.GoString()), nil | ||
} | ||
|
||
// String manually overrides the default starlarkstruct.Struct String() function because it is wrong when | ||
// we provide a custom constructor, which we do here | ||
// | ||
// See https://github.com/google/starlark-go/issues/448 for more details | ||
func (service *Service) String() string { | ||
oldInvalid := fmt.Sprintf("\"%s\"(", serviceTypeName) | ||
newValid := fmt.Sprintf("%s(", serviceTypeName) | ||
return strings.Replace(service.Struct.String(), oldInvalid, newValid, 1) | ||
func (serviceObj *Service) GetHostname() (string, *startosis_errors.InterpretationError) { | ||
hostname, _, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String]( | ||
serviceObj.KurtosisValueTypeDefault, HostnameAttr) | ||
if interpretationErr != nil { | ||
return "", interpretationErr | ||
} | ||
return hostname.GoString(), nil | ||
} | ||
|
||
// Type Needs to be overridden as the default for starlarkstruct.Struct always return "struct", which is dumb | ||
func (service *Service) Type() string { | ||
return serviceTypeName | ||
func (serviceObj *Service) GetIpAddress() (string, *startosis_errors.InterpretationError) { | ||
ipAddress, _, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[starlark.String]( | ||
serviceObj.KurtosisValueTypeDefault, IpAddressAttr) | ||
if interpretationErr != nil { | ||
return "", interpretationErr | ||
} | ||
return ipAddress.GoString(), nil | ||
} | ||
|
||
func (serviceObj *Service) GetPorts() (*starlark.Dict, *startosis_errors.InterpretationError) { | ||
ports, _, interpretationErr := kurtosis_type_constructor.ExtractAttrValue[*starlark.Dict]( | ||
serviceObj.KurtosisValueTypeDefault, PortsAttr) | ||
if interpretationErr != nil { | ||
return nil, interpretationErr | ||
} | ||
return ports, nil | ||
} |
Oops, something went wrong.