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

Set singular names for core types to pass to discovery #113542

Merged
merged 10 commits into from Jan 3, 2023

Conversation

ardaguclu
Copy link
Member

@ardaguclu ardaguclu commented Nov 2, 2022

What type of PR is this?

/kind bug

What this PR does / why we need it:

Core types don't set their singular names for discovery endpoint(as opposed to CRDs).
This results in nondeterministic behavior when there is a CRD whose shortname is
one of core types singular name format. Thereby, when user requests resource via this short name,
it becomes unpredictable(because it is determined by the order of which one is requested first).

This PR adds singular names for all core types. As a result, core types will
have always higher precedence when their singular name format is requested
regardless of request order.

Which issue(s) this PR fixes:

Fixes #113227

Special notes for your reviewer:

In terms of backwards compatibility, this change has negative impact only on the use cases where people currently are expecting CRDs via singular form of core types. Isn't this already extreme case and incorrect expectation?

Does this PR introduce a user-facing change?

None

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver area/test sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Nov 2, 2022
@ardaguclu ardaguclu changed the title WIP: Fix shortname disperancy WIP: Set singular names for core types to pass discovery Nov 2, 2022
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Nov 2, 2022
@ardaguclu ardaguclu changed the title WIP: Set singular names for core types to pass discovery WIP: Set singular names for core types to pass to discovery Nov 2, 2022
set +o errexit
}

run_assert_singular_name_tests() {
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 test is not failing on master. I added it to just prevent future regressions.

kube::test::if_has_string "${output_message}" "test-crd-example"

# test that get pod returns v1/pod instead crd
output_message=$(kubectl get pod)
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 test should fail on master without this patch.

@ardaguclu
Copy link
Member Author

current failing tests don't seem to be related to the changes;
/retest

just to be sure rerun integration tests;
/test pull-kubernetes-integration

@ardaguclu ardaguclu changed the title WIP: Set singular names for core types to pass to discovery Set singular names for core types to pass to discovery Nov 2, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 2, 2022
@ardaguclu
Copy link
Member Author

/retest

@ardaguclu
Copy link
Member Author

@deads2k @liggitt could you please take a look at this?. Thanks.

@liggitt
Copy link
Member

liggitt commented Nov 3, 2022

~all storage implementations already set a DefaultQualifiedResource field to the plural version of their resource... I think a peer field SingularQualifiedResource would be a clearer place to set this, making the SingularResource getter be implemented by the Store type

that field could also be validated in Store#CompleteWithOptions

  • must not be empty
  • group must match DefaultQualifiedResource

@ardaguclu ardaguclu changed the title WIP: Set singular names for core types to pass to discovery Set singular names for core types to pass to discovery Nov 18, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 18, 2022
Copy link
Member

@liggitt liggitt left a comment

Choose a reason for hiding this comment

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

I think we also want an integration test that enables all built-in resources and ensures we set singular names for them which match lower(kind)

Something like this:

diff --git a/test/integration/apiserver/discovery/discovery_test.go b/test/integration/apiserver/discovery/discovery_test.go
index c6b7caaa477..1906f9aa552 100644
--- a/test/integration/apiserver/discovery/discovery_test.go
+++ b/test/integration/apiserver/discovery/discovery_test.go
@@ -27,6 +27,7 @@ import (
 
 	"github.com/google/go-cmp/cmp"
 	"github.com/stretchr/testify/require"
+
 	apidiscoveryv2beta1 "k8s.io/api/apidiscovery/v2beta1"
 	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 	apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@@ -690,6 +691,34 @@ func TestGroupPriorty(t *testing.T) {
 	})
 }
 
+func TestSingularNames(t *testing.T) {
+	server := kubeapiservertesting.StartTestServerOrDie(t, nil, []string{"--runtime-config=api/all=true"}, framework.SharedEtcd())
+	t.Cleanup(server.TearDownFn)
+
+	kubeClientSet, err := kubernetes.NewForConfig(server.ClientConfig)
+	require.NoError(t, err)
+
+	_, resources, err := kubeClientSet.Discovery().ServerGroupsAndResources()
+	require.NoError(t, err)
+
+	for _, rr := range resources {
+		for _, r := range rr.APIResources {
+			if strings.Contains(r.Name, "/") {
+				continue
+			}
+			if r.SingularName == "" {
+				t.Errorf("missing singularName for resource %q in %q", r.Name, rr.GroupVersion)
+				continue
+			}
+			if r.SingularName != strings.ToLower(r.Kind) {
+				t.Errorf("expected singularName for resource %q in %q to be %q, got %q", r.Name, rr.GroupVersion, strings.ToLower(r.Kind), r.SingularName)
+				continue
+			}
+		}
+	}
+
+}
+
 func makeCRDSpec(group string, kind string, namespaced bool, versions []string, categories ...string) apiextensionsv1.CustomResourceDefinitionSpec {
 	scope := apiextensionsv1.NamespaceScoped
 	if !namespaced {

pkg/registry/core/pod/storage/storage.go Outdated Show resolved Hide resolved
@liggitt
Copy link
Member

liggitt commented Nov 18, 2022

I think we also want an integration test that enables all built-in resources and ensures we set singular names for them which match lower(kind)

@deads2k does that restriction for built-in types match your expectation?

@ardaguclu
Copy link
Member Author

Reminder to myself: assure that aggregated discovery populates singular names.

@ardaguclu
Copy link
Member Author

Failure seems unrelated to this change;

/test pull-kubernetes-e2e-kind-ipv6

@liggitt
Copy link
Member

liggitt commented Nov 21, 2022

the registry and integration test changes lgtm, would like a second from @deads2k

will defer to @soltysh or @seans3 on the test-cmd test

@ardaguclu
Copy link
Member Author

@deads2k would you mind taking a look at this one?. Thanks.

@sftim
Copy link
Contributor

sftim commented Dec 20, 2022

Does this PR introduce a user-facing change?

I think it does, even though you can only directly observe it via the API and not via tools. The user visible change is that the nondeterministic behavior:

This results in nondeterministic behavior when there is a CRD whose shortname is
one of core types singular name format. Thereby, when user requests resource via this short name,
it becomes unpredictable(because it is determined by the order of which one is requested first).

becomes deterministic.

@@ -94,6 +95,12 @@ func (r *REST) NamespaceScoped() bool {
return r.store.NamespaceScoped()
}

var _ rest.SingularNameProvider = &REST{}

func (r *REST) GetSingularName() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

this stands out. Why does this one need it?

Copy link
Member

@liggitt liggitt Dec 20, 2022

Choose a reason for hiding this comment

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

because it doesn't inline r.store and inherit the default implementation (see all other standard rest.Storage implementations forwarding to r.store in this file)

Copy link
Member Author

Choose a reason for hiding this comment

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

it does not inherit default store implementation as said ^

@@ -1320,6 +1325,12 @@ func (e *Store) CompleteWithOptions(options *generic.StoreOptions) error {
if e.DefaultQualifiedResource.Empty() {
return fmt.Errorf("store %#v must have a non-empty qualified resource", e)
}
if e.SingularQualifiedResource.Empty() {
Copy link
Contributor

Choose a reason for hiding this comment

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

great.

@deads2k
Copy link
Contributor

deads2k commented Dec 20, 2022

namespace storage stood out. lgtm once I know why that needed a special case.

@deads2k
Copy link
Contributor

deads2k commented Jan 3, 2023

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 3, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 3edda9b27f6880059cbb9d46d884a8d3f6744d6b

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ardaguclu, deads2k

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 Jan 3, 2023
@k8s-ci-robot k8s-ci-robot merged commit 12c71fd into kubernetes:master Jan 3, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.27 milestone Jan 3, 2023
@ardaguclu ardaguclu deleted the fix-shortname-disperancy branch January 3, 2023 17:38
var _ rest.SingularNameProvider = &REST{}

func (r *REST) GetSingularName() string {
return "selfsubjectrulesreview"
Copy link
Member

Choose a reason for hiding this comment

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

@ardaguclu Hello, it seems like a bug, but I don't know why the tests are passing. I will fix this on promoting selfsubjectreviews to beta, jfyi.

Copy link
Member

Choose a reason for hiding this comment

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

The name is selfsubjectreview.

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. area/apiserver area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. 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. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
SIG Auth Old
Needs Triage
Development

Successfully merging this pull request may close these issues.

Kubectl restmapping cache causes inconsistent behaviour in case of resource name conflict
6 participants