Skip to content
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

Add structType support #2

Merged
merged 3 commits into from
Dec 17, 2019
Merged

Add structType support #2

merged 3 commits into from
Dec 17, 2019

Conversation

mariantalla
Copy link

Similarly to #1, this PR adds a +structType marker, that maps to x-kubernetes-map-type in kube-openapi.

cc @apelisse

@mariantalla mariantalla changed the base branch from master to add-list-type December 16, 2019 16:16
@mariantalla
Copy link
Author

mariantalla commented Dec 16, 2019

(Oops sorry, I'll rebase edit: done)

Putting WIP for now because I see test failures due to this validation:

if schema.Type != "object" {
    return fmt.Errorf("must apply structType to an object")
}

Test failures show that schema.Type is empty, which is a bit surprising 🤔 Looking into this next.

It maps to x-kubernetes-map-type in the openapi schema, similar to the
mapType marker.
@@ -29,6 +29,7 @@ var TopologyMarkers = []*definitionWithHelp{
must(markers.MakeDefinition("listType", markers.DescribesField, ListType(""))),
must(markers.MakeDefinition("listMapKey", markers.DescribesField, ListMapKey(""))),
must(markers.MakeDefinition("mapType", markers.DescribesField, MapType(""))),
must(markers.MakeDefinition("structType", markers.DescribesField, StructType(""))),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping them ordered, sweet

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listType and listMapKey are not ordered :-/ Not your fault ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listType and listMapKey are not ordered

Shall I change that as part of this PR? It's a tiny bit of scope creep, but especially if we'll squash commits as part of kubernetes-sigs#347, it's probably not a huge deal.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll re-order when I rebase/squash! :-)

return nil
}

func (s StructType) ApplyFirst() {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly driven by this: https://github.com/kubernetes-sigs/controller-tools/blob/4752ed2de7d2fc1b6b18398bf26cf2ce6b53cd94/pkg/crd/markers/doc.go#L27-L29. If I got it right, it suggests that "top-level" types (e.g. lists, as opposed to list items) can implement this method to ensure that their schemas will be applied before types that don't implement it.

The comment also says it's discouraged and a hack so if all this is not quite right, I'm happy to remove it. No tests fail if I remove ApplyFirst for StructType, for what it's worth 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would only use it if there is an actual dependency between two types (like we do for ListType). But I don't think that's the case here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 5d66dad

func (m MapType) ApplyFirst() {}

func (s StructType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "object" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect "object" is the default and is thus not required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, makes sense. I went for "object" || "" just to be on the strict side. Let me know what you think, happy to remove the check completely if we think it doesn't add much.

@apelisse
Copy link
Owner

One minor nit, but looks good otherwise.

@mariantalla mariantalla changed the title [WIP] Add structType support Add structType support Dec 17, 2019
@apelisse
Copy link
Owner

Thanks!

@apelisse apelisse merged commit 1cbd921 into apelisse:add-list-type Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants