-
Notifications
You must be signed in to change notification settings - Fork 447
🌱 crd gen: handle type any #1252
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the generator errors for this type, do we still expect it to spit out a yaml file? Not sure I was expecting to see a yaml output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("unsupported AST kind %T", expr), rawType))
// NB(directxman12): we explicitly don't handle interfaces
return &apiext.JSONSchemaProps{} I think reassessing the failure behaviour should be left out of scope of this pull request, in favour of more holistically revisiting that in another pull request. 2 It also looks like
3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: (devel) | ||
name: kindwithifaces.iface.example.com | ||
spec: | ||
group: iface.example.com | ||
names: | ||
kind: KindWithIFace | ||
listKind: KindWithIFaceList | ||
plural: kindwithifaces | ||
singular: kindwithiface | ||
scope: Namespaced | ||
versions: | ||
- name: iface | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
description: |- | ||
APIVersion defines the versioned schema of this representation of an object. | ||
Servers should convert recognized schemas to the latest internal value, and | ||
may reject unrecognized values. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources | ||
type: string | ||
kind: | ||
description: |- | ||
Kind is a string value representing the REST resource this object represents. | ||
Servers may infer this from the endpoint the client submits requests to. | ||
Cannot be updated. | ||
In CamelCase. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
properties: | ||
bar: {} | ||
type: object | ||
required: | ||
- metadata | ||
type: object | ||
served: true | ||
storage: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
//go:generate ../../../../../.run-controller-gen.sh crd:crdVersions=v1 paths=. output:dir=. | ||
|
||
// +groupName=iface.example.com | ||
package iface | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type KindWithIFace struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata"` | ||
Spec KindWithIFaceSpec `json:"spec,omitempty"` | ||
} | ||
|
||
type KindWithIFaceSpec struct { | ||
Bar any `json:"bar,omitempty"` | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Does types.Interface only capture any? Or do we actually want to block all interface types even if they would work?
What about changing the logic below to simply not panic on the type cast?
Then we can still handle various cases below that
Uh oh!
There was an error while loading. Please reload this page.
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.
It captures all interface types (that are named).
The intent here is to error on all interface types.
This aligns with what the existing code is doing here:
This PR is mainly about avoiding a panic.
We could do that.
I would prefer the style of a type switch here, since there is also an earlier if:
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
.So it would be something like this:
I did it this way because existing code was already doing
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
, so it aligns with that, and I don't want to make the diff bigger and scarier (by indenting/reordering existing code).