feat: support JS and Go source files in fission package loader

This commit is contained in:
Naeel
2026-04-14 23:37:32 +03:00
parent d40c5c6e07
commit f9dfb882f0
5 changed files with 83 additions and 5 deletions
@@ -69,11 +69,11 @@ func (r *PackageResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
"source_dir": schema.StringAttribute{
Optional: true,
Description: "Путь к директории с кодом, которая будет упакована в zip.",
Description: "Путь к директории с кодом (поддерживаются main.py, main.js, main.go).",
},
"code_path": schema.StringAttribute{
Optional: true,
Description: "Путь к готовому zip-архиву с кодом.",
Description: "Путь к готовому файлу исходника для literal deployment.",
},
"code_hash": schema.StringAttribute{
Optional: true,
@@ -271,10 +271,14 @@ func resolveNamespace(resourceNamespace types.String, providerNamespace string)
// loadPackageLiteral читает bytes для spec.deployment.literal.
func loadPackageLiteral(sourceDir, codePath string) ([]byte, error) {
if sourceDir != "" {
mainFilePath := filepath.Join(sourceDir, "main.py")
mainFilePath, err := resolveMainSourceFile(sourceDir)
if err != nil {
return nil, err
}
mainFileBytes, err := os.ReadFile(mainFilePath)
if err != nil {
return nil, fmt.Errorf("read source_dir main.py %q: %w", mainFilePath, err)
return nil, fmt.Errorf("read source_dir source file %q: %w", mainFilePath, err)
}
return mainFileBytes, nil
@@ -288,6 +292,20 @@ func loadPackageLiteral(sourceDir, codePath string) ([]byte, error) {
return literalBytes, nil
}
// resolveMainSourceFile выбирает основной файл исходника из source_dir.
func resolveMainSourceFile(sourceDir string) (string, error) {
candidates := []string{"main.py", "main.js", "main.go"}
for _, candidate := range candidates {
candidatePath := filepath.Join(sourceDir, candidate)
fileInfo, err := os.Stat(candidatePath)
if err == nil && !fileInfo.IsDir() {
return candidatePath, nil
}
}
return "", fmt.Errorf("source_dir %q must contain one of: main.py, main.js, main.go", sourceDir)
}
// packageToUnstructured преобразует Terraform model в Kubernetes CRD payload.
func packageToUnstructured(model packageResourceModel, namespace string, literalBytes []byte) *unstructured.Unstructured {
literalSource := base64.StdEncoding.EncodeToString(literalBytes)
@@ -49,6 +49,40 @@ func TestLoadPackageLiteralFromSourceDir(t *testing.T) {
}
}
func TestLoadPackageLiteralFromSourceDirJS(t *testing.T) {
tempDir := t.TempDir()
mainPath := filepath.Join(tempDir, "main.js")
content := []byte("module.exports = async function() { return 'ok'; }\n")
if err := os.WriteFile(mainPath, content, 0o600); err != nil {
t.Fatalf("write temp main.js: %v", err)
}
loaded, err := loadPackageLiteral(tempDir, "")
if err != nil {
t.Fatalf("loadPackageLiteral returned error: %v", err)
}
if string(loaded) != string(content) {
t.Fatalf("unexpected loaded content")
}
}
func TestLoadPackageLiteralFromSourceDirGo(t *testing.T) {
tempDir := t.TempDir()
mainPath := filepath.Join(tempDir, "main.go")
content := []byte("package main\nfunc main() {}\n")
if err := os.WriteFile(mainPath, content, 0o600); err != nil {
t.Fatalf("write temp main.go: %v", err)
}
loaded, err := loadPackageLiteral(tempDir, "")
if err != nil {
t.Fatalf("loadPackageLiteral returned error: %v", err)
}
if string(loaded) != string(content) {
t.Fatalf("unexpected loaded content")
}
}
func TestPackageToUnstructured(t *testing.T) {
input := packageResourceModel{
Name: types.StringValue("pkg-a"),