Experimental code generators for Crossplane controllers.
Go to file
Jared Watts 3ad1f97263
Merge pull request #108 from jbw976/stop-overwrite
build: use a subfolder in the releases S3 bucket
2025-05-07 18:13:00 +01:00
.github Merge pull request #96 from crossplane/renovate/ubuntu-24.x 2025-04-21 15:03:56 -07:00
build@1ed19332b9 add helper functions; add k8s.io/utils/ptr dependency to helper functions 2025-03-25 16:03:56 -05:00
cmd fix(angryjet): rename helpers to convert 2025-04-23 11:32:36 +02:00
internal fix(angryjet): rename more helpers to convert 2025-04-24 14:56:17 +02:00
pkg/convert Rename pkg/helpers to pkg/convert 2025-04-21 14:19:11 -07:00
.gitignore types.traverser: rename TypeProcessor to NamedProcessor 2021-09-05 13:25:21 +03:00
.gitmodules Switch to crossplane/build 2024-05-15 17:55:24 -07:00
.golangci.yml Drop unsupported config option 2025-04-21 13:04:08 -07:00
LICENSE Initial commit 2019-09-01 15:51:43 -07:00
Makefile build: use a subfolder in the releases S3 bucket 2025-05-07 13:06:22 +01:00
OWNERS.md Make Alper an owner too 2024-05-17 11:52:07 -07:00
README.md Update link to Provider Development Guide 2023-03-14 16:50:31 -07:00
go.mod Bump k8s utils, cmp 2025-04-21 14:59:12 -07:00
go.sum Bump k8s utils, cmp 2025-04-21 14:59:12 -07:00
renovate.json Add renovate.json 2023-12-15 14:34:41 +00:00

README.md

crossplane-tools Godoc

Code generators for Crossplane controllers.

angryjet

angryjet will detect Go structs that appear to be capable of satisfying crossplane-runtime's interfaces (such as resource.Managed) and automatically generate the method set required to satisfy that interface. A struct is considered capable of satisfying crossplane-runtime's interfaces based on the heuristics described in the Provider Development Guide, for example a managed resource must:

  • Embed a ResourceStatus struct in their Status struct.
  • Embed a ResourceSpec struct in their Spec struct.
  • Embed a Parameters struct in their Spec struct.

Methods are not written if they are already defined outside of the file that would be generated. Use the //+crossplane:generate:methods=false comment marker to explicitly disable generation of any methods for a type. Use go generate to generate your Crossplane API types by adding a generate marker to the top level of your api/ directory, for example:

// Generate crossplane-runtime methodsets (resource.Claim, etc)
//go:generate go run ../vendor/github.com/crossplane/crossplane-tools/cmd/angryjet/main.go generate-methodsets ./...

Reference Resolvers

In addition to functions that satisfy resource.Managed, you can use angryjet to generate a ResolveReferences method as well. In order to generate a resolution call for given field, you need to add the following comment marker:

// +crossplane:generate:reference:type=<target type>

<target type> could either be just the type name of the CRD if it is in the same package or <package path>.<target type> if it is in a different package, such as github.com/crossplane/provider-aws/apis/ec2/v1beta1.VPC.

The generated resolver will use the external name annotation of the target resource to fetch the value and it assumes that reference field is named as FieldNameRef/FieldNameRefs if array and selector field is named as FieldNameSelector. You can override these defaults by adding the optional comment markers, see the following example:

type SomeParameters struct {
    // +crossplane:generate:reference:type=github.com/crossplane/provider-aws/apis/ec2/v1beta1.Subnet
    // +crossplane:generate:reference:extractor=github.com/crossplane/provider-aws/apis/ec2/v1beta1.SubnetARN()
    // +crossplane:generate:reference:refFieldName=SubnetIDRefs
    // +crossplane:generate:reference:selectorFieldName=SubnetIDSelector
    SubnetIDs []string `json:"subnetIds,omitempty"`
    
    SubnetIDRefs []xpv1.Reference `json:"subnetIdRefs,omitempty"`
    
    SubnetIDSelector *xpv1.Selector `json:"subnetIdSelector,omitempty"`
}

Note that it doesn't make any change to the CRD struct; authors still need to add FieldNameRef and FieldNameSelector fields on their own for the generated code to compile.

Usage

$ angryjet generate-methodsets --help
usage: angryjet generate-methodsets [<flags>] [<packages>]

Generate a Crossplane method sets.

Flags:
  --help                     Show context-sensitive help (also try --help-long and --help-man).
  --header-file=HEADER-FILE  The contents of this file will be added to the top of all generated files.
  --filename-managed="zz_generated.managed.go"
                             The filename of generated managed resource files.
  --filename-resolvers="zz_generated.resolvers.go"
                             The filename of generated reference resolver files.
  --filename-managed-list="zz_generated.managedlist.go"
                             The filename of generated managed list resource files.
  --filename-pc="zz_generated.pc.go"
                             The filename of generated provider config files.
  --filename-pcu="zz_generated.pcu.go"
                             The filename of generated provider config usage files.
  --filename-pcu-list="zz_generated.pculist.go"
                             The filename of generated provider config usage files.

Args:
  [<packages>]  Package(s) for which to generate methods, for example github.com/crossplane/crossplane/apis/...