Skip to content

Add regular expression (regex) pattern support #930

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

Open
chlunde opened this issue Sep 5, 2021 · 5 comments
Open

Add regular expression (regex) pattern support #930

chlunde opened this issue Sep 5, 2021 · 5 comments
Assignees
Labels
area/code-generation Issues or PRs as related to controllers or docs code generation good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. kind/enhancement Categorizes issue or PR as related to existing feature enhancements. priority/backlog Higher priority than priority/awaiting-more-evidence.

Comments

@chlunde
Copy link

chlunde commented Sep 5, 2021

Is your feature request related to a problem?
When creating a CR, only the documentation and types are available. Sometimes the type is string and the documentation is brief or non-existent, for example for a role of type string. The api-2.json files often contains a validation pattern for this case, but the author of a CR does not have that information available, and will have to create the resource in a cluster and observe the result to check if the format is correct.

Describe the solution you'd like
Consider adding kubebuilder Pattern annotations. In order to do this we would have to expose this information in aws-sdk-for-go v1, that seems trivial:

diff -ruwbp /home/chlunde/go/pkg/mod/github.com/aws/[email protected]/private/model/api/shape.go ./private/model/api/shape.go
--- /home/chlunde/go/pkg/mod/github.com/aws/[email protected]/private/model/api/shape.go	2021-09-05 13:33:33.430030539 +0200
+++ ./private/model/api/shape.go	2021-09-05 21:59:57.401057747 +0200
@@ -80,6 +81,7 @@ type Shape struct {
 	MemberRef        ShapeRef             `json:"member"` // List ref
 	KeyRef           ShapeRef             `json:"key"`    // map key ref
 	ValueRef         ShapeRef             `json:"value"`  // map value ref
+	Pattern          string               `json:"pattern"`
 	Required         []string
 	Payload          string
 	Type             string

Finally we can add it to the template, similar to

diff --git a/templates/crossplane/apis/type_def.go.tpl b/templates/crossplane/apis/type_def.go.tpl
index 6a0cbdc..4598d98 100644
--- a/templates/crossplane/apis/type_def.go.tpl
+++ b/templates/crossplane/apis/type_def.go.tpl
@@ -4,6 +4,9 @@ type {{ .Names.Camel }} struct {
        {{- if $attr.Shape }}
        {{ $attr.Shape.Documentation }}
        {{- end }}
+       {{- if $attr.Shape.Pattern -}}
+       // +kubebuilder:validation:Pattern:={{ $attr.Shape.Pattern }}
+       {{- end }}
        {{ $attr.Names.Camel }} {{ $attr.GoType }} `json:"{{ $attr.Names.CamelLower }},omitempty"`
 {{- end }}
 }

The result

  • richer validation
  • documentation
  • possibility of detecting errors using static checking early in gitops pipelines and development environments
  • detecting errors before sending to the AWS API (if there is no static checking set up/not gitops)

Example (I have not tested this in a cluster, so the syntax might not be exactly right):

+       // +kubebuilder:validation:Pattern:=arn:(aws[a-zA-Z-]*)?:iam::\d{12}:role/?[a-zA-Z_0-9+=,.@\-_/]+
        Role *string `json:"role,omitempty"`
+       // +kubebuilder:validation:Pattern:=arn:(aws[a-zA-Z0-9-]*):([a-zA-Z0-9\-])+:([a-z]{2}(-gov)?-[a-z]+-\d{1})?:(\d{12})?:(.*)
        SigningJobARN *string `json:"signingJobARN,omitempty"`
+       // +kubebuilder:validation:Pattern:=arn:(aws[a-zA-Z0-9-]*):([a-zA-Z0-9\-])+:([a-z]{2}(-gov)?-[a-z]+-\d{1})?:(\d{12})?:(.*)
        SigningProfileVersionARN *string `json:"signingProfileVersionARN,omitempty"`
+       // +kubebuilder:validation:Pattern:=(\$LATEST|[0-9]+)
        Version *string `json:"version,omitempty"`

Describe alternatives you've considered
Keeping it as it is? 🤔

Things to consider
We would also have to understand what possible differences between the regex language in the JSON files are, and what the API server validates, and possibly convert (or drop) some patterns. We should also consider if introducing validations will cause other problems, and possibly an escape hatch in generator-config.yaml to ignore the pattern for a specific attribute?

Related issue: crossplane-contrib/provider-aws#572

cc @muvaf

@chlunde chlunde added the kind/enhancement Categorizes issue or PR as related to existing feature enhancements. label Sep 5, 2021
@jaypipes jaypipes added the area/code-generation Issues or PRs as related to controllers or docs code generation label Sep 28, 2021
@jaypipes
Copy link
Collaborator

Hi @chlunde! This is a great feature request and something I think we should be able to accommodate fairly easily. I'll see what I can do about getting this into our next sprint or two.

@jaypipes jaypipes added the priority/backlog Higher priority than priority/awaiting-more-evidence. label Sep 28, 2021
@ack-bot
Copy link
Collaborator

ack-bot commented Dec 27, 2021

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
If this issue is safe to close now please do so with /close.
Provide feedback via https://github.com/aws-controllers-k8s/community.
/lifecycle stale

@ack-bot ack-bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 27, 2021
@a-hilaly
Copy link
Member

/lifecycle frozen

@ack-bot ack-bot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Dec 28, 2021
@RedbackThomson RedbackThomson added the good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. label Mar 10, 2022
@knottnt knottnt self-assigned this May 6, 2025
@knottnt
Copy link

knottnt commented May 6, 2025

One concern I have with this feature request is that with stricter validation of field patterns we lose flexibility when/if an underlying AWS API changes its pattern or a mismatch is introduced some other way. If an ACK CRD and underlying AWS API’s pattern became out of sync we’d need to re-generate the ACK controller and publish a new version before users could resolve the mismatch. Note, this doesn’t necessarily require a breaking change on the AWS API side as the new pattern could be a superset of the old pattern.

We could update our API Reference docs and CRD descriptions to include this information with less potential downside in the event of a mismatch. Would that help address some of your concerns?

@knottnt
Copy link

knottnt commented May 6, 2025

/remove-lifecycle frozen

@ack-prow ack-prow bot removed the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label May 6, 2025
ack-prow bot pushed a commit to aws-controllers-k8s/code-generator that referenced this issue May 22, 2025
Issue #, if available: [930](aws-controllers-k8s/community#930)

Description of changes:
- Read smithy.api#pattern from aws-sdk-go-v2 JSON models
- Add Regex pattern to documentation string if exists
- Add test to verify pattern string is added when present

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/code-generation Issues or PRs as related to controllers or docs code generation good first issue Denotes an issue ready for a new contributor, according to the "help wanted" guidelines. kind/enhancement Categorizes issue or PR as related to existing feature enhancements. priority/backlog Higher priority than priority/awaiting-more-evidence.
Projects
None yet
Development

No branches or pull requests

6 participants