Skip to content

Commit 66f2265

Browse files
authored
Add custom find_operation support (#454)
Issue: aws-controllers-k8s/community#1827 Description of changes: This change allow to implement custom find function. The name of the function could be written in generator.yaml as 'find_operation: <name-of-function>' similar to update_operation. And generated code for sdkFind() would call this custom find function. The find function itself could be written in hooks.go. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 1f16813 commit 66f2265

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

pkg/config/resource.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ type ResourceConfig struct {
6363
// very little consistency to the APIs that we can use to instruct the code
6464
// generator :(
6565
UpdateOperation *UpdateOperationConfig `json:"update_operation,omitempty"`
66+
// ReadOperation contains instructions for the code generator to generate
67+
// Go code for the read operation for the resource. For some resources,
68+
// there is no describe/find/list apis. However, it is possible to write
69+
// `read` function for such resources using another resource's (say,
70+
// resource B) `read` function. Then, one could write custom code to find
71+
// the relevant resource from the response of resource B's read function.
72+
//
73+
// Additionally, if the `read` function for a resource is not available
74+
// then reconciler's progression is stalled with the error `read`
75+
// operation `notImplemented`. Custom read function allows the
76+
// reconciler's progression.
77+
ReadOperation *ReadOperationsConfig `json:"find_operation,omitempty"`
6678
// Reconcile describes options for controlling the reconciliation
6779
// logic for a particular resource.
6880
Reconcile *ReconcileConfig `json:"reconcile,omitempty"`
@@ -326,6 +338,15 @@ type UpdateOperationConfig struct {
326338
OmitUnchangedFields bool `json:"omit_unchanged_fields"`
327339
}
328340

341+
// ReadOperationsConfig contains instructions for the code generator to handle
342+
// custom read operations for service APIs that have resources that have
343+
// difficult-to-standardize read operations.
344+
type ReadOperationsConfig struct {
345+
// CustomMethodName is a string for the method name to replace the
346+
// sdkFind() method implementation for this resource
347+
CustomMethodName string `json:"custom_method_name"`
348+
}
349+
329350
// AdditionalColumnConfig can be used to specify additional printer columns to be included
330351
// in a Resource's output from kubectl.
331352
type AdditionalColumnConfig struct {
@@ -651,6 +672,19 @@ func (c *Config) GetCustomUpdateMethodName(resourceName string) string {
651672
return ""
652673
}
653674

675+
func (c *Config) GetCustomFindMethodName(resourceName string) string {
676+
if c == nil {
677+
return ""
678+
}
679+
rConfig, found := c.Resources[resourceName]
680+
if found {
681+
if rConfig.ReadOperation != nil {
682+
return rConfig.ReadOperation.CustomMethodName
683+
}
684+
}
685+
return ""
686+
}
687+
654688
// GetAllRenames returns all of the CRD's field renames observed in the generator config
655689
// for a given map of operations.
656690
func (c *Config) GetAllRenames(

pkg/generate/ack/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
controllerIncludePaths = []string{
4747
"boilerplate.go.tpl",
4848
"pkg/resource/references_read_referenced_resource.go.tpl",
49+
"pkg/resource/sdk_find_custom.go.tpl",
4950
"pkg/resource/sdk_find_read_one.go.tpl",
5051
"pkg/resource/sdk_find_get_attributes.go.tpl",
5152
"pkg/resource/sdk_find_read_many.go.tpl",

pkg/generate/code/set_resource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ func SetResourceIdentifiers(
962962
case r.Ops.ReadMany != nil:
963963
// If single lookups can only be done using ReadMany
964964
op = r.Ops.ReadMany
965+
default:
966+
return ""
965967
}
966968
}
967969
inputShape := op.InputRef.Shape

pkg/model/crd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ func (r *CRD) CustomUpdateMethodName() string {
720720
return r.cfg.GetCustomUpdateMethodName(r.Names.Original)
721721
}
722722

723+
func (r *CRD) CustomFindMethodName() string {
724+
return r.cfg.GetCustomFindMethodName(r.Names.Original)
725+
}
726+
723727
// ListOpMatchFieldNames returns a slice of strings representing the field
724728
// names in the List operation's Output shape's element Shape that we should
725729
// check a corresponding value in the target Spec exists.

templates/pkg/resource/sdk.go.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ var (
3939
)
4040

4141
// sdkFind returns SDK-specific information about a supplied resource
42-
{{ if .CRD.Ops.ReadOne }}
42+
{{ if .CRD.CustomFindMethodName }}
43+
{{- template "sdk_find_custom" . }}
44+
{{- else if .CRD.Ops.ReadOne }}
4345
{{- template "sdk_find_read_one" . }}
4446
{{- else if .CRD.Ops.GetAttributes }}
4547
{{- template "sdk_find_get_attributes" . }}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{- define "sdk_find_custom" -}}
2+
func (rm *resourceManager) sdkFind(
3+
ctx context.Context,
4+
r *resource,
5+
) (*resource, error) {
6+
return rm.{{ .CRD.CustomFindMethodName }}(ctx, r)
7+
}
8+
{{- end -}}

0 commit comments

Comments
 (0)