-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (109 loc) · 3.51 KB
/
main.go
File metadata and controls
144 lines (109 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
pdk "github.com/extism/go-pdk"
"github.com/helm/helm-plugin-gotemplate-renderer/pkg/engine"
"github.com/helm/helm-plugin-gotemplate-renderer/pkg/release"
chart "helm.sh/helm/v4/pkg/chart/v2"
)
type Input struct {
Chart *chart.Chart `json:"chart"`
ValuesJSON []byte `json:"values"`
}
type OutputManifest struct {
Filename string `json:"filename"`
Manifest []byte `json:"manifest"`
}
type Output struct {
Manifests []OutputManifest `json:"manifests"`
}
type ExtismHostFunctions struct {
}
func (e *ExtismHostFunctions) LookupKubernetesResource(apiVersion string, kind string, namespace string, name string) (map[string]interface{}, error) {
memApiVersion := pdk.AllocateString(apiVersion)
memKind := pdk.AllocateString(kind)
memNamespace := pdk.AllocateString(namespace)
memName := pdk.AllocateString(name)
resultPtr := extismKubernetesResourceLookup(
extismPointer(memApiVersion.Offset()),
extismPointer(memKind.Offset()),
extismPointer(memNamespace.Offset()),
extismPointer(memName.Offset()),
)
resultMem := pdk.FindMemory(uint64(resultPtr))
type lookupKubernetesResourceResult struct {
Error *string `json:"error,omitempty"`
Result map[string]any `json:"result"`
}
result := lookupKubernetesResourceResult{}
if err := json.Unmarshal(resultMem.ReadBytes(), &result); err != nil {
return nil, fmt.Errorf("failed to deserialize LookupKubernetesResource return json: %w", err)
}
if result.Error != nil {
return nil, fmt.Errorf("host error: %s", *result.Error)
}
return result.Result, nil
}
func (e *ExtismHostFunctions) ResolveHostname(hostname string) string {
memHostname := pdk.AllocateString(hostname)
resultPtr := extismResolveHostname(
extismPointer(memHostname.Offset()),
)
resultMem := pdk.FindMemory(uint64(resultPtr))
return string(resultMem.ReadBytes())
}
func RenderChartTemplates(input Input) (*Output, error) {
hostFunctions := ExtismHostFunctions{}
//e, err := renderer.NewEngine(&hostFunctions, renderer.WithDNS(true))
e, err := engine.NewEngine(&hostFunctions)
if err != nil {
return nil, fmt.Errorf("failed to create gotemplate engine: %w", err)
}
var vals map[string]any
if err := json.Unmarshal(input.ValuesJSON, &vals); err != nil {
return nil, fmt.Errorf("failed to parse input values json: %w", err)
}
chrt := input.Chart
if err := release.ProcessDependencies(chrt, vals); err != nil {
return nil, fmt.Errorf("chart dependencies processing failed: %w", err)
}
renderedManifests, err := e.RenderAllChartTemplates(chrt, vals)
if err != nil {
return nil, fmt.Errorf("failed to render chart templates: %w", err)
}
result := Output{}
for filename, data := range renderedManifests {
result.Manifests = append(result.Manifests, OutputManifest{
Filename: filename,
Manifest: []byte(data),
})
}
return &result, nil
}
func RunPlugin() error {
var input Input
if err := pdk.InputJSON(&input); err != nil {
return fmt.Errorf("failed to parse input json: %w", err)
}
output, err := RenderChartTemplates(input)
if err != nil {
pdk.Log(pdk.LogError, fmt.Sprintf("failed: %s", err.Error()))
return err
}
if err := pdk.OutputJSON(output); err != nil {
return fmt.Errorf("failed to write output json: %w", err)
}
return nil
}
//go:wasmexport helm_chart_renderer
func HelmChartRenderer() uint64 {
pdk.Log(pdk.LogDebug, "running gotemplate-renderer plugin")
if err := RunPlugin(); err != nil {
pdk.Log(pdk.LogError, err.Error())
pdk.SetError(err)
return 1
}
return 0
}
func main() {}