Skip to content

Add custom find_operation support #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ type ResourceConfig struct {
// very little consistency to the APIs that we can use to instruct the code
// generator :(
UpdateOperation *UpdateOperationConfig `json:"update_operation,omitempty"`
// ReadOperation contains instructions for the code generator to generate
// Go code for the read operation for the resource. For some resources,
// there is no describe/find/list apis. However, it is possible to write
// `read` function for such resources using another resource's (say,
// resource B) `read` function. Then, one could write custom code to find
// the relevant resource from the response of resource B's read function.
//
// Additionally, if the `read` function for a resource is not available
// then reconciler's progression is stalled with the error `read`
// operation `notImplemented`. Custom read function allows the
// reconciler's progression.
ReadOperation *ReadOperationsConfig `json:"find_operation,omitempty"`
// Reconcile describes options for controlling the reconciliation
// logic for a particular resource.
Reconcile *ReconcileConfig `json:"reconcile,omitempty"`
Expand Down Expand Up @@ -326,6 +338,15 @@ type UpdateOperationConfig struct {
OmitUnchangedFields bool `json:"omit_unchanged_fields"`
}

// ReadOperationsConfig contains instructions for the code generator to handle
// custom read operations for service APIs that have resources that have
// difficult-to-standardize read operations.
type ReadOperationsConfig struct {
// CustomMethodName is a string for the method name to replace the
// sdkFind() method implementation for this resource
CustomMethodName string `json:"custom_method_name"`
}

// AdditionalColumnConfig can be used to specify additional printer columns to be included
// in a Resource's output from kubectl.
type AdditionalColumnConfig struct {
Expand Down Expand Up @@ -651,6 +672,19 @@ func (c *Config) GetCustomUpdateMethodName(resourceName string) string {
return ""
}

func (c *Config) GetCustomFindMethodName(resourceName string) string {
if c == nil {
return ""
}
rConfig, found := c.Resources[resourceName]
if found {
if rConfig.ReadOperation != nil {
return rConfig.ReadOperation.CustomMethodName
}
}
return ""
}

// GetAllRenames returns all of the CRD's field renames observed in the generator config
// for a given map of operations.
func (c *Config) GetAllRenames(
Expand Down
1 change: 1 addition & 0 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
controllerIncludePaths = []string{
"boilerplate.go.tpl",
"pkg/resource/references_read_referenced_resource.go.tpl",
"pkg/resource/sdk_find_custom.go.tpl",
"pkg/resource/sdk_find_read_one.go.tpl",
"pkg/resource/sdk_find_get_attributes.go.tpl",
"pkg/resource/sdk_find_read_many.go.tpl",
Expand Down
2 changes: 2 additions & 0 deletions pkg/generate/code/set_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,8 @@ func SetResourceIdentifiers(
case r.Ops.ReadMany != nil:
// If single lookups can only be done using ReadMany
op = r.Ops.ReadMany
default:
return ""
}
}
inputShape := op.InputRef.Shape
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ func (r *CRD) CustomUpdateMethodName() string {
return r.cfg.GetCustomUpdateMethodName(r.Names.Original)
}

func (r *CRD) CustomFindMethodName() string {
return r.cfg.GetCustomFindMethodName(r.Names.Original)
}

// ListOpMatchFieldNames returns a slice of strings representing the field
// names in the List operation's Output shape's element Shape that we should
// check a corresponding value in the target Spec exists.
Expand Down
4 changes: 3 additions & 1 deletion templates/pkg/resource/sdk.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var (
)

// sdkFind returns SDK-specific information about a supplied resource
{{ if .CRD.Ops.ReadOne }}
{{ if .CRD.CustomFindMethodName }}
{{- template "sdk_find_custom" . }}
{{- else if .CRD.Ops.ReadOne }}
{{- template "sdk_find_read_one" . }}
{{- else if .CRD.Ops.GetAttributes }}
{{- template "sdk_find_get_attributes" . }}
Expand Down
8 changes: 8 additions & 0 deletions templates/pkg/resource/sdk_find_custom.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- define "sdk_find_custom" -}}
func (rm *resourceManager) sdkFind(
ctx context.Context,
r *resource,
) (*resource, error) {
return rm.{{ .CRD.CustomFindMethodName }}(ctx, r)
}
{{- end -}}