-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_reconciler_template.go
51 lines (43 loc) · 1.96 KB
/
resource_reconciler_template.go
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
package templates
import (
"text/template"
)
var ResourceReconcilerTemplate = template.Must(template.New("resource_client").Funcs(Funcs).Parse(`package {{ .Project.ProjectConfig.Version }}
import (
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/reconcile"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/solo-io/go-utils/contextutils"
)
// Option to copy anything from the original to the desired before writing. Return value of false means don't update
type Transition{{ .Name }}Func func(original, desired *{{ .Name }}) (bool, error)
type {{ .Name }}Reconciler interface {
Reconcile(namespace string, desiredResources {{ .Name }}List, transition Transition{{ .Name }}Func, opts clients.ListOpts) error
}
func {{ lower_camel .Name }}sToResources(list {{ .Name }}List) resources.ResourceList {
var resourceList resources.ResourceList
for _, {{ lower_camel .Name }} := range list {
resourceList = append(resourceList, {{ lower_camel .Name }})
}
return resourceList
}
func New{{ .Name }}Reconciler(client {{ .Name }}Client, statusSetter resources.StatusSetter) {{ .Name }}Reconciler {
return &{{ lower_camel .Name }}Reconciler{
base: reconcile.NewReconciler(client.BaseClient(), statusSetter),
}
}
type {{ lower_camel .Name }}Reconciler struct {
base reconcile.Reconciler
}
func (r *{{ lower_camel .Name }}Reconciler) Reconcile(namespace string, desiredResources {{ .Name }}List, transition Transition{{ .Name }}Func, opts clients.ListOpts) error {
opts = opts.WithDefaults()
opts.Ctx = contextutils.WithLogger(opts.Ctx, "{{ lower_camel .Name }}_reconciler")
var transitionResources reconcile.TransitionResourcesFunc
if transition != nil {
transitionResources = func(original, desired resources.Resource) (bool, error) {
return transition(original.(*{{ .Name }}), desired.(*{{ .Name }}))
}
}
return r.base.Reconcile(namespace, {{ lower_camel .Name }}sToResources(desiredResources), transitionResources, opts)
}
`))