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

fix: update image UI arg parsing #2419

Merged
merged 12 commits into from
May 2, 2024
16 changes: 8 additions & 8 deletions core/server/api_container/server/api_container_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func NewApiContainerService(
metricsClient metrics_client.MetricsClient,
githubAuthProvider *git_package_content_provider.GitHubPackageAuthProvider,
) (*ApiContainerService, error) {
var emptyInitialSerializedParams *string
var emptyInitialSerializedParams string
emptyInitialSerializedParamsPtr := &emptyInitialSerializedParams
*emptyInitialSerializedParamsPtr = ""
service := &ApiContainerService{
filesArtifactStore: filesArtifactStore,
serviceNetwork: serviceNetwork,
Expand All @@ -132,7 +134,7 @@ func NewApiContainerService(
MainFunctionName: "",
ExperimentalFeatures: []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{},
RestartPolicy: kurtosis_core_rpc_api_bindings.RestartPolicy_NEVER,
InitialSerializedParams: emptyInitialSerializedParams,
InitialSerializedParams: emptyInitialSerializedParamsPtr,
},
metricsClient: metricsClient,
githubAuthProvider: githubAuthProvider,
Expand Down Expand Up @@ -175,9 +177,8 @@ func (apicService *ApiContainerService) RunStarlarkScript(args *kurtosis_core_rp
args.GetExperimentalFeatures(),
stream)

var initialSerializedParams *string
if apicService.starlarkRun.InitialSerializedParams == nil || *apicService.starlarkRun.InitialSerializedParams == "" {
initialSerializedParams = &apicService.starlarkRun.SerializedParams
apicService.starlarkRun.InitialSerializedParams = &serializedParams
}
apicService.starlarkRun = &kurtosis_core_rpc_api_bindings.GetStarlarkRunResponse{
PackageId: apicService.starlarkRun.PackageId,
Expand All @@ -188,7 +189,7 @@ func (apicService *ApiContainerService) RunStarlarkScript(args *kurtosis_core_rp
MainFunctionName: mainFuncName,
ExperimentalFeatures: experimentalFeatures,
RestartPolicy: apicService.restartPolicy,
InitialSerializedParams: initialSerializedParams,
InitialSerializedParams: apicService.starlarkRun.InitialSerializedParams,
}

return nil
Expand Down Expand Up @@ -323,9 +324,8 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r
serializedParams)
apicService.runStarlark(parallelism, dryRun, detectedPackageId, detectedPackageReplaceOptions, mainFuncName, actualRelativePathToMainFile, scriptWithRunFunction, serializedParams, downloadMode, nonBlockingMode, args.ExperimentalFeatures, stream)

var initialSerializedParams *string
if apicService.starlarkRun.InitialSerializedParams == nil || *apicService.starlarkRun.InitialSerializedParams == "" {
initialSerializedParams = &apicService.starlarkRun.SerializedParams
apicService.starlarkRun.InitialSerializedParams = &serializedParams
}
apicService.starlarkRun = &kurtosis_core_rpc_api_bindings.GetStarlarkRunResponse{
PackageId: packageIdFromArgs,
Expand All @@ -336,7 +336,7 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r
MainFunctionName: mainFuncName,
ExperimentalFeatures: args.ExperimentalFeatures,
RestartPolicy: apicService.restartPolicy,
InitialSerializedParams: initialSerializedParams,
InitialSerializedParams: apicService.starlarkRun.InitialSerializedParams,
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function generateStarlarkFromGraph(
o.forEach((arrayValue) => {
result += `${objectToStarlark(arrayValue, indent + 4)},\n`;
});
result += `${padLeft}],\n`;
result += `${padLeft}]\n`;
return result;
}
if (typeof o === "number") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
ModalOverlay,
} from "@chakra-ui/react";
import { isDefined, RemoveFunctions } from "kurtosis-ui-components";
import { useMemo, useState } from "react";
import { useCallback, useMemo, useState } from "react";
import { IoLogoDocker } from "react-icons/io5";
import { useNavigate } from "react-router-dom";
import { useKurtosisPackageIndexerClient } from "../../../../client/packageIndexer/KurtosisPackageIndexerClientContext";
import { useEnclavesContext } from "../../EnclavesContext";
import { EnclaveFullInfo } from "../../types";

Expand All @@ -41,48 +42,32 @@ function objectToStarlark(o: any, indent: number) {
o.forEach((arrayValue) => {
result += `${objectToStarlark(arrayValue, indent + 4)},\n`;
});
result += `${padLeft}],\n`;
result += `${padLeft}]\n`;
return result;
}
if (typeof o === "number") {
return `${o}`;
}
if (typeof o === "string") {
return `${o}`;
return `"${o}"`;
}
if (typeof o === "boolean") {
return o ? "True" : "False";
}
if (typeof o === "object") {
let result = "{";
Object.entries(o).forEach(([key, value]) => {
result += `\n${padLeft}"${key}": "${objectToStarlark(value, indent + 4)}",`;
result += `\n${padLeft}"${key}": ${objectToStarlark(value, indent + 4)},`;
});
result += `${padLeft}}`;
return result;
}
}

function deserializeParams(serializedParams: string): Record<string, string> {
try {
const parsedParams = JSON.parse(serializedParams);
if (typeof parsedParams === "object" && parsedParams !== null) {
const deserialized: Record<string, string> = {};
for (const key in parsedParams) {
if (typeof parsedParams[key] === "string") {
deserialized[key] = parsedParams[key];
} else {
throw new Error("Value is not a string.");
}
}
return deserialized;
} else {
throw new Error("Invalid JSON format.");
}
} catch (error) {
console.error("Error deserializing params:", error);
return {};
}
function wrapWithArgs(args: Record<string, any>) {
return {
args: args,
};
}

export type SetImageModalProps = {
Expand All @@ -97,8 +82,16 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl
const { runStarlarkScript } = useEnclavesContext(); // Assuming this is defined elsewhere
const [error, setError] = useState<string | null>(null);
const [newImage, setNewImage] = useState("");
const packageIndexerClient = useKurtosisPackageIndexerClient();
const navigator = useNavigate();

const getPackageInfo = useCallback(
async (packageName: string) => {
return await packageIndexerClient.readPackage(packageName);
},
[packageIndexerClient],
);

const handleSetImageSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

Expand All @@ -109,17 +102,36 @@ export const SetImageModel = ({ isOpen, onClose, currentImage, serviceName, encl
}

const packageId = starlarkRun.value.packageId;
const packageInfoResponse = await getPackageInfo(packageId);
if (packageInfoResponse.isErr) {
setError(`Error occurred getting info about ${packageId} from indexer.`);
return;
}
if (!packageInfoResponse.value.package) {
setError(`Could not find package ${packageId}`);
return;
}
const packageArgs = packageInfoResponse.value.package.args;

console.log(`initial serialized params: ${starlarkRun.value.initialSerializedParams}`);
if (!starlarkRun.value.initialSerializedParams) {
setError(`Error occurred getting initial params used to start package.`);
return;
}
const argsRecord = JSON.parse(starlarkRun.value.initialSerializedParams);
if (typeof argsRecord !== "object" || argsRecord === null) {
setError("Error: deserializing initial params of starlark package run failed.");
return;
}

let argsRecord;
if (starlarkRun.value.initialSerializedParams) {
console.log(`initial serialized params: ${starlarkRun.value.initialSerializedParams}`);
argsRecord = deserializeParams(starlarkRun.value.initialSerializedParams);
let args;
console.log(packageArgs);
if (packageArgs.length === 2 && packageArgs[1].name === "args") {
args = objectToStarlark(wrapWithArgs(argsRecord), 4);
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.log(`serialized params: ${starlarkRun.value.serializedParams}`);
argsRecord = deserializeParams(starlarkRun.value.serializedParams);
args = objectToStarlark(argsRecord, 4);
}
const args = objectToStarlark(argsRecord, 8);
console.log(`initial args used to start package:\n${args}`);
console.log(`args used to start package:\n${args}`);

const updateImageStarlarkScript = `
package = import_module("${packageId}/main.star")
Expand Down
Loading