Skip to content

Commit 85436ab

Browse files
Enable crd variables and controller functions and hooks within hook templates (#91)
Issue #, aws-controllers-k8s/community#838 Description of changes: This PR enables support for crd variables and controller functions and hooks within hook templates. The hook templates insides the service controllers can be similar to the templates that are inside code-generator. For example, following hook template refers `.CRD` variable, invokes controller function `GoCodeSetCreateOutput` and has its own extension hook `sdk_addon_set_output_post_populate`: and it can be used as hook inside generator.yaml for a resource: ``` {{ $outputShape := .CRD.GetOutputShape .CRD.Ops.Create }} // This method copies the data from given {{ $outputShape.ShapeName }} by populating it // into copy of supplied resource and returns that. func (rm *resourceManager) set{{ $outputShape.ShapeName }}Output ( r *resource, obj *svcsdk.{{ $outputShape.ShapeName }}, ) (*resource, error) { if obj == nil || r == nil || r.ko == nil { return nil, nil } resp := &svcsdk.{{ .CRD.Ops.Create.OutputRef.Shape.ShapeName }}{ {{ $outputShape.ShapeName }}:obj } // Merge in the information we read from the API call above to the copy of // the original Kubernetes object we passed to the function ko := r.ko.DeepCopy() {{ $createCode := GoCodeSetCreateOutput .CRD "resp" "ko" 1 true }} {{ $createCode }} rm.setStatusDefaults(ko) {{- if $hookCode := Hook .CRD "sdk_addon_set_output_post_populate" }} {{ $hookCode }} {{- end }} return &resource{ko}, nil } ``` Testing: `make test` passed for `code-generator` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent d579746 commit 85436ab

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

pkg/generate/ack/controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,16 @@ func Controller(
123123
return nil, err
124124
}
125125

126+
metaVars := g.MetaVars()
127+
126128
// Hook code can reference a template path, and we can look up the template
127129
// in any of our base paths...
128130
controllerFuncMap["Hook"] = func(r *ackmodel.CRD, hookID string) string {
129-
code, err := ResourceHookCode(templateBasePaths, r, hookID)
131+
crdVars := &templateCRDVars{
132+
metaVars,
133+
r,
134+
}
135+
code, err := ResourceHookCode(templateBasePaths, r, hookID, crdVars, controllerFuncMap)
130136
if err != nil {
131137
// It's a compile-time error, so just panic...
132138
panic(err)
@@ -141,8 +147,6 @@ func Controller(
141147
controllerFuncMap,
142148
)
143149

144-
metaVars := g.MetaVars()
145-
146150
// First add all the CRD pkg/resource templates
147151
targets := []string{
148152
"delta.go.tpl",

pkg/generate/ack/hook.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func ResourceHookCode(
105105
templateBasePaths []string,
106106
r *ackmodel.CRD,
107107
hookID string,
108+
vars interface{},
109+
funcMap ttpl.FuncMap,
108110
) (string, error) {
109111
resourceName := r.Names.Original
110112
if resourceName == "" || hookID == "" {
@@ -146,6 +148,7 @@ func ResourceHookCode(
146148
return "", err
147149
}
148150
t := ttpl.New(tplPath)
151+
t = t.Funcs(funcMap)
149152
if t, err = t.Parse(string(tplContents)); err != nil {
150153
err := fmt.Errorf(
151154
"resource %s hook config for %s is invalid: error parsing %s: %s",
@@ -156,7 +159,7 @@ func ResourceHookCode(
156159
var b bytes.Buffer
157160
// TODO(jaypipes): Instead of nil for template vars here, maybe pass in
158161
// a struct of variables?
159-
if err := t.Execute(&b, nil); err != nil {
162+
if err := t.Execute(&b, vars); err != nil {
160163
err := fmt.Errorf(
161164
"resource %s hook config for %s is invalid: error executing %s: %s",
162165
resourceName, hookID, tplPath, err,

pkg/generate/ack/hook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestResourceHookCodeInline(t *testing.T) {
3838

3939
// The Broker's update operation has a special hook callback configured
4040
expected := `if err := rm.requeueIfNotRunning(latest); err != nil { return nil, err }`
41-
got, err := ack.ResourceHookCode(basePaths, crd, hookID)
41+
got, err := ack.ResourceHookCode(basePaths, crd, hookID, nil, nil)
4242
assert.Nil(err)
4343
assert.Equal(expected, got)
4444
}
@@ -59,7 +59,7 @@ func TestResourceHookCodeTemplatePath(t *testing.T) {
5959

6060
// The Broker's delete operation has a special hook configured to point to a template.
6161
expected := "// this is my template.\n"
62-
got, err := ack.ResourceHookCode(basePaths, crd, hookID)
62+
got, err := ack.ResourceHookCode(basePaths, crd, hookID, nil, nil)
6363
assert.Nil(err)
6464
assert.Equal(expected, got)
6565
}

0 commit comments

Comments
 (0)