Skip to content

Conversation

@fionera
Copy link
Contributor

@fionera fionera commented Nov 22, 2025

Kubernetes types with either +optional or omitempty, are expected to not have a null disjunction. By checking for their annotations we can switch to a kubernetes semantic mode and not generate them.

@fionera fionera requested a review from cueckoo as a code owner November 22, 2025 00:44
Copy link
Member

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

SGTM overall, but I think this implementation isn't quite complete - it should support the "optional" and "nullable" field comment attributes that the k8s openapi tool supports. Note that we already support +optional (and I'm not sure if that's already compatible with what k8s does), but we don't support +nullable at all.

@mvdan
Copy link
Member

mvdan commented Nov 22, 2025

Also, could you split this in two commits as follows:

  1. Add the testscript file, showing what the current implementation generates for k8s types
  2. Add the code changes, showing how it affects the generated types from the first commit, effectively showing a diff removing null | etc

@mvdan mvdan self-assigned this Nov 22, 2025
@fionera fionera force-pushed the kubernetes-semantics branch 2 times, most recently from da2c98e to 289db25 Compare November 24, 2025 02:19
Copy link
Member

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

LGTM - just a few last nits and I'm happy to import into Gerrit and merge

We are currently generating "wrong" schemas from K8s source when
comparing against their OpenAPI spec.

Change-Id: Ieb30f98ba2bdc921ab48c81aedbdc89c685b4bd9
Signed-off-by: Tim Windelschmidt <[email protected]>
Kubernetes types with either +optional or omitempty are expected to not
have null disjunctions. Fields with +nullable are required and have a
valid null value. By checking for the k8s openapi annotations we can
switch to a kubernetes semantic mode and generate the schema correctly.

Change-Id: I747de2ddda57f8bc079afe9180a8179fd5b2c778
Signed-off-by: Tim Windelschmidt <[email protected]>
@fionera fionera force-pushed the kubernetes-semantics branch from 289db25 to d7f763b Compare November 24, 2025 16:18
Copy link
Member

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM with some last nits.

When you're ready, could you please send these for a final review and test/merge on Gerrit? The way we import PRs to Gerrit doesn't really support multi-commit PRs unless we squash them, and I don't want to squash here.

// +optional
OptionalCommentPointer *string `json:"optionalCommentPointer"`
// +nullable
NullableCommentPointer *string `json:"nullableCommentPointer"`
Copy link
Member

Choose a reason for hiding this comment

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

I just realised - we need to include a field which is both optional and nullable. it should generate as...

foo?: null | string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out that should be the default behavior:

Optional fields have the following properties:

They have the +optional comment tag in Go.
They are a pointer type in the Go definition (e.g. AwesomeFlag *SomeFlag) or have a built-in nil value (e.g. maps and slices).
They are marked with the omitempty json struct tag in the Go definition.
The API server should allow POSTing and PUTing a resource with this field unset.

return regular
case "+optional":
return optional
}
Copy link
Member

Choose a reason for hiding this comment

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

I think that the way this loops over the doc lines, and returns at the first hit, will not work well when a field is marked as BOTH optional and nullable. You might need to make fieldKind a bit set, so that we can track that a field was marked as either or both optional/nullable?

If this is not common and you want to leave this tweak for a future PR, that's fine, but then we'd need a TODO.

Copy link
Contributor Author

@fionera fionera Nov 29, 2025

Choose a reason for hiding this comment

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

I actually changed the whole PR 😅 I am gonna close this one and push it to gerrit as its probably a better review platform

@fionera fionera closed this Nov 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants