-
Notifications
You must be signed in to change notification settings - Fork 180
feat: Add support for (custom) types #498
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
base: main
Are you sure you want to change the base?
Conversation
This fragment is repeated in the downstream functions. I guess it is a left-over from refactorings.
This adds the `Types` field to the ResourceGraphDefinition CRD. It contains a map of names to type definitions, where the definitions follow the simple schema. The defined types are then loaded into the transformer as predefined types. Appearances in the spec are then properly resolved. An example for usage would be: ``` yaml apiVersion: kro.run/v1alpha1 kind: ResourceGraphDefinition metadata: name: service-entry-dns spec: schema: apiVersion: v1alpha1 kind: ServiceEntryDNS spec: ports: "[]port | required=true" types: port: name: string protocol: string number: integer | required ``` Known issues: Currently there is no support for references of custom types within the definition of a custom type! Fixes kro-run#144
If a simple type was used with the required marker: ``` yaml types: myType: string | required=true description="my description" spec: myValue: myType ``` the required marker was lost, while the description was set properly. This happened as in the applyMarkers method of the transformer the required information is set to the parent. While loading the predefined types this parent is just a shell object, that is later read to fill the list of predefined types and then discarded. Now the information on required types is captured in the `predefinedType` type and then later used to set the parent object required fields when handling predefined types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gfrey! left two tiny comments
// A predefined type is a type that is predefined in the schema. | ||
// It is used to resolve references in the schema, while capturing the fact | ||
// whether the type has the required marker set (this information would | ||
// otherwise be lost in the parsing process). | ||
type predefinedType struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! initially i wasn't sure about this, but i just remembered that required fields always live parent-type-schema-definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that adding this broke the test for the predefined types. So, I fixed that.
pkg/simpleschema/transform_test.go
Outdated
if err != nil { | ||
t.Fatalf("Failed to load pre-defined types: %v", err) | ||
} | ||
func floatPtr(f float64) *float64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this function isn't used, shall we remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely!
This was never used but accidentally committed. So removing it.
This is reported when `gofmt` is run with the `-s` option.
This is the first implementation that was discussed in the slack channel. The other idea of having the
types
field in thespec
part of the schema, seems to be more complicated to implement.Fixes #144.