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

✨ Warn about things that we strongly recommend avoiding #443

Closed
wants to merge 3 commits into from
Closed

Conversation

ksaritek
Copy link

I created a guestbook kubebuilder init project and added a *ast.StarExpr to guestbook struct

type Env struct {
A int json:"a"
B string json:"b"
}

// Guestbook is the Schema for the guestbooks API
type Guestbook struct {
metav1.TypeMeta json:",inline"
metav1.ObjectMeta json:"metadata,omitempty"

Spec   GuestbookSpec   `json:"spec,omitempty"`
Status GuestbookStatus `json:"status,omitempty"`
Envs   map[string]*Env `json:"envs,omitempty"`

}
my patch:

./controller-gen crd object:headerFile="hack/boilerplate.go.txt" paths="./..." output:crd:stdout


apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: guestbooks.webapp.my.domain
spec:
group: webapp.my.domain
names:
kind: Guestbook
listKind: GuestbookList
plural: guestbooks
singular: guestbook
scope: Namespaced
validation:
openAPIV3Schema:
description: Guestbook is the Schema for the guestbooks API
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
envs:
additionalProperties:
properties:
a:
type: integer
b:
type: string
required:
- a
- b
type: object
type: object
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:
description: GuestbookSpec defines the desired state of Guestbook
properties:
foo:
description: Foo is an example field of Guestbook. Edit Guestbook_types.go
to remove/update
type: string
type: object
status:
description: GuestbookStatus defines the observed state of Guestbook
type: object
type: object
version: v1
versions:

  • name: v1
    served: true
    storage: true
    status:
    acceptedNames:
    kind: ""
    plural: ""
    conditions: null
    storedVersions: null
    /go/src/example/api/v1/guestbook_types.go:55:20: map values should be a named type, not *ast.StarExpr
    controller-gen prints warning at the end like /go/src/example/api/v1/guestbook_types.go:55:20: map values should be a named type, not *ast.StarExpr.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 21, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @ksaritek!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ksaritek
To complete the pull request process, please assign directxman12
You can assign the PR to them by writing /assign @directxman12 in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 21, 2020
@ksaritek ksaritek changed the title (:sparkles:, minor) fix: Warn about things that we strongly recommend avoiding #434 ✨ fix: Warn about things that we strongly recommend avoiding #434 May 21, 2020
@ksaritek ksaritek changed the title ✨ fix: Warn about things that we strongly recommend avoiding #434 ✨ Warn about things that we strongly recommend avoiding #434 May 21, 2020
@ksaritek ksaritek changed the title ✨ Warn about things that we strongly recommend avoiding #434 (:sparkles:,minor) Warn about things that we strongly recommend avoiding #434 May 21, 2020
@ksaritek
Copy link
Author

/assign @DirectXMan12

@ksaritek ksaritek changed the title (:sparkles:,minor) Warn about things that we strongly recommend avoiding #434 ✨ Warn about things that we strongly recommend avoiding May 21, 2020
@ksaritek ksaritek changed the title ✨ Warn about things that we strongly recommend avoiding ✨ Warn about things that we strongly recommend avoiding May 21, 2020
@DirectXMan12
Copy link
Contributor

First comment: the beginning of the commit should generally say "what" and "why". Having examples is fine, but it's generally go to snip to the relevant parts, and to used fenced code blocks. I'd expect something like:

We have a set of patterns that we reccomend avoiding (since they're not in
line with the Kubernetes API conventions/best practices), but for legacy
reasons can't make a hard error.  This introduces a mechanism to warn about
such patterns when they're detected.

It functions similarly to the way errors (an `AddWarning` on `Packaged` with
special handling for `PositionedError`), except that such warnings are not
considered fatal at the end of processing.


This commit only introduces one such warning: warn about map values being
pointers.

For example:

```go
type Env struct {
    A int json:"a"
    B string json:"b"
}

type Container struct {
    Envs   map[string]*Env `json:"envs,omitempty"`
}
```

yields

```
/path/to/types.go:7:20: map values should be a named type, not *ast.StarExpr
```

Copy link
Contributor

@DirectXMan12 DirectXMan12 left a comment

Choose a reason for hiding this comment

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

comments inline. Please remove the merge commits. Usually we prefer one "logical chunk" per commit, so in this case I'd expect to either see 1 commit for the whole thing, or one for the warning functionality and one to update the type error to a warning.

Comment on lines +40 to +42
// WarningRecorder knows how to record errors. It wraps the part of
// pkg/loader.Package that we need to record errors in places were it might not
// make sense to have a loader.Package
Copy link
Contributor

Choose a reason for hiding this comment

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

the second sentence here seems like it was copy-pasted verbatim, and doesn't make much sense any more.
I'd go with

Suggested change
// WarningRecorder knows how to record errors. It wraps the part of
// pkg/loader.Package that we need to record errors in places were it might not
// make sense to have a loader.Package
// WarningRecorder knows how to record warnings, which are effectively
// non-fatal errors. It wraps the part of pkg/loader.Package that we need to
// record warnings in places were it might not make sense to have a
// loader.Package.

// pkg/loader.Package that we need to record errors in places were it might not
// make sense to have a loader.Package
type WarningRecorder interface {
// AddWarning records that the given error occurred.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// AddWarning records that the given error occurred.
// AddWarning records that the given warning (non-fatal error) occurred.

@@ -304,6 +304,7 @@ func mapToSchema(ctx *schemaContext, mapType *ast.MapType) *apiext.JSONSchemaPro
return &apiext.JSONSchemaProps{}
}
case *ast.StarExpr:
ctx.pkg.AddWarning(loader.ErrFromNode(fmt.Errorf("map values should be a named type, not %T", mapType.Value), mapType.Value))
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe tweak the message here a bit so it's obvious it's a warning. I'd do something like

as per the Kubernetes API conventions, map values *should* generally be primitives or named types that alias primitives, not %T.  Consider using the "list as map" pattern here, except in cases where map is used for compatiblity/legacy purposes.

@@ -61,6 +59,52 @@ func PrintErrors(pkgs []*Package, filterKinds ...packages.ErrorKind) bool {
return hadErrors
}

// PrintWarnings print errors associated with all packages
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// PrintWarnings print errors associated with all packages
// PrintWarnings print warnings associated with all packages

// package's dependencies have been visited (postorder).
// The boolean result of pre(pkg) determines whether
// the imports of package pkg are visited.
func visit(pkgs []*Package, pre func(*Package) bool, post func(*Package)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we ever use pre? If not, we can get rid of it.

Comment on lines +117 to +118
// Warnings contains any errors encountered querying the metadata
// of the package, or while parsing or type-checking its files.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Warnings contains any errors encountered querying the metadata
// of the package, or while parsing or type-checking its files.
// Warnings contains any warnings ("non-fatal" errors) encountered while type-checking or
// generating output (OpenAPI, etc) from the given package.

@@ -175,6 +223,25 @@ func (p *Package) AddError(err error) {
}
}

// AddWarning adds an error to the warningss associated with the given package.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// AddWarning adds an error to the warningss associated with the given package.
// AddWarning adds a warning (represented as an error) to the warnings associated with the given package.
// It functions nearly-identically to `AddError`, except that it adds to `Package.Warnings`.

@fejta-bot
Copy link

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.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 25, 2020
@DirectXMan12
Copy link
Contributor

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 26, 2020
@fejta-bot
Copy link

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.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 25, 2020
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Dec 25, 2020
@fejta-bot
Copy link

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@k8s-ci-robot
Copy link
Contributor

@fejta-bot: Closed this PR.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants