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

transport/cache: statically assert that tlsCacheKey is comparable #112680

Merged
merged 1 commit into from
Sep 26, 2022

Conversation

enj
Copy link
Member

@enj enj commented Sep 22, 2022

Signed-off-by: Monis Khan mok@microsoft.com

/kind cleanup
/triage accepted
/priority backlog
/assign @liggitt @aojea

NONE

@k8s-ci-robot k8s-ci-robot added the release-note-none Denotes a PR that doesn't merit a release note. label Sep 22, 2022
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. triage/accepted Indicates an issue or PR is ready to be actively worked on. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. priority/backlog Higher priority than priority/awaiting-more-evidence. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 22, 2022
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Sep 22, 2022
@@ -0,0 +1,22 @@
//go:build go1.18
Copy link
Member Author

Choose a reason for hiding this comment

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

Made this a new file with a build constraint in case someone was using an older go version.

Copy link
Member

Choose a reason for hiding this comment

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

should we force the go version too?

Copy link
Member Author

Choose a reason for hiding this comment

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

This felt like a "nicer" approach since this check is meant for the Kube devs to keep this stuff from drifting. I am sure over time we will pick up features from go 1.18 and thus naturally force library consumers to newer Go versions.

package transport

// isComparable asserts at compile time that tlsCacheKey is comparable in a way that will never panic at runtime.
func isComparable[T comparable]() { isComparable[tlsCacheKey]() }
Copy link
Member Author

Choose a reason for hiding this comment

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

If you add an interface field to tlsCacheKey, this will no longer compile.

Copy link
Member

Choose a reason for hiding this comment

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

could this be in a _test file and be equally effective?

also, is there a way to construct this that doesn't make a function that infinitely recursively calls itself, like

func isComparable[T comparable]() bool { return true }

var _ = isComparable[tlsCacheKey]()

Copy link
Member Author

@enj enj Sep 23, 2022

Choose a reason for hiding this comment

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

could this be in a _test file and be equally effective?

We don't generally put interface assertions in test files because you only get a failure if you actually compile/run the test. Having it in the regular file makes it impossible to miss.

also, is there a way to construct this that doesn't make a function that infinitely recursively calls itself

Yeah, that is basically what I had originally and I got too clever with my desire to fit in in one line :) (also our CI linter doesn't like that the function is unused so I have to fix it anyway)

Copy link
Member

Choose a reason for hiding this comment

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

We don't generally put interface assertions in test files

git grep "var _ = " | grep _test.go disagrees :)

because you only get a failure if you actually compile/run the test.

Which we do, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't generally put interface assertions in test files

git grep "var _ = " | grep _test.go disagrees :)

Hence I used generally. When we really want to make sure stuff does not drift we do it in the regular code like:

var (
_ rest.CategoriesProvider = &REST{}
_ rest.ShortNamesProvider = &REST{}
_ rest.StorageVersionProvider = &REST{}
_ rest.ResetFieldsStrategy = &REST{}
_ rest.Redirector = &REST{}
)

because you only get a failure if you actually compile/run the test.

Which we do, right?

Why push an assertion down to a later time?

Signed-off-by: Monis Khan <mok@microsoft.com>
@enj enj force-pushed the enj/i/tls_cache_key_comparable branch from 5afe3af to a6d0f48 Compare September 23, 2022 14:43
@enj
Copy link
Member Author

enj commented Sep 23, 2022

/retest

@aojea
Copy link
Member

aojea commented Sep 24, 2022

/lgtm

TIL you can force this check at build time

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 24, 2022
@liggitt
Copy link
Member

liggitt commented Sep 26, 2022

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: enj, liggitt

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

The pull request process is described 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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 26, 2022
@k8s-ci-robot k8s-ci-robot merged commit a024252 into kubernetes:master Sep 26, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.26 milestone Sep 26, 2022
@liggitt
Copy link
Member

liggitt commented Dec 22, 2022

I can't tell if the go1.20 changes make it possible for this to fail at runtime:

from https://tip.golang.org/doc/go1.20#language

Comparable types (such as ordinary interfaces) may now satisfy comparable constraints, even if the type arguments are not strictly comparable (comparison may panic at runtime). This makes it possible to instantiate a type parameter constrained by comparable (e.g., a type parameter for a user-defined generic map key) with a non-strictly comparable type argument such as an interface type, or a composite type containing an interface type.

@enj
Copy link
Member Author

enj commented Dec 22, 2022

I can't tell if the go1.20 changes make it possible for this to fail at runtime:

from https://tip.golang.org/doc/go1.20#language

Comparable types (such as ordinary interfaces) may now satisfy comparable constraints, even if the type arguments are not strictly comparable (comparison may panic at runtime). This makes it possible to instantiate a type parameter constrained by comparable (e.g., a type parameter for a user-defined generic map key) with a non-strictly comparable type argument such as an interface type, or a composite type containing an interface type.

We need to tweak our code to keep the static check: golang/go#56548 (comment)

@liggitt
Copy link
Member

liggitt commented Dec 22, 2022

We need to tweak our code to keep the static check: golang/go#56548 (comment)

I assume that's something we can do now in master ahead of go1.20?

@enj
Copy link
Member Author

enj commented Dec 22, 2022

We need to tweak our code to keep the static check: golang/go#56548 (comment)

I assume that's something we can do now in master ahead of go1.20?

Yeah I just had not gotten around to it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants