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

[WIP] Fix hardcoded string, add fetch validResource from APIServer #51324

Closed
wants to merge 4 commits into from

Conversation

shiywang
Copy link
Contributor

@shiywang shiywang commented Aug 25, 2017

Fixes #51023

if DiscoveryClient failed, return previous hard coded string.
this change may make kubectl get/annotate.. -h a little slow first time, @deads2k do you think it's acceptable ?

kubectl get -h  0.13s user 0.02s system 128% cpu 0.117 total
./kubectl get -h  0.13s user 0.02s system 5% cpu 2.600 total

before:

shiywang:amd64 root [dynamicResource] $ kubectl get -h
Display one or many resources. 

Valid resource types include: 

  * all  
  * certificatesigningrequests (aka 'csr')  
  * clusters (valid only for federation apiservers)  
  * clusterrolebindings  
  * clusterroles  
  * componentstatuses (aka 'cs')  
  * configmaps (aka 'cm')  
  * daemonsets (aka 'ds')  
  * deployments (aka 'deploy')  
  * endpoints (aka 'ep')  
  * events (aka 'ev')  
  * horizontalpodautoscalers (aka 'hpa')  
  * ingresses (aka 'ing')  
  * jobs  
  * limitranges (aka 'limits')  
  * namespaces (aka 'ns')  
  * networkpolicies  
  * nodes (aka 'no')  
  * persistentvolumeclaims (aka 'pvc')  
  * persistentvolumes (aka 'pv')  
  * pods (aka 'po')  
  * poddisruptionbudgets (aka 'pdb')  
  * podsecuritypolicies (aka 'psp')  
  * podtemplates  
  * replicasets (aka 'rs')  
  * replicationcontrollers (aka 'rc')  
  * resourcequotas (aka 'quota')  
  * rolebindings  
  * roles  
  * secrets  
  * serviceaccounts (aka 'sa')  
  * services (aka 'svc')  
  * statefulsets  
  * storageclasses  
  * thirdpartyresources  

This command will hide resources that have completed, such as pods that are in the Succeeded or Failed phases. You can
see the full results for any resource by providing the '--show-all' flag. 

after:

shiywang:amd64 root [dynamicResource] $ ./kubectl get -h
Display one or many resources. 

Valid resource types include:

  * all
  * bindings 
  * certificatesigningrequests  (aka 'csr')
  * clusterrolebindings 
  * clusterroles 
  * clusters (valid only for federation apiservers)
  * componentstatuses  (aka 'cs')
  * configmaps  (aka 'cm')
  * controllerrevisions 
  * customresourcedefinitions  (aka 'crd')
  * daemonsets  (aka 'ds')
  * deployments  (aka 'deploy')
  * endpoints  (aka 'ep')
  * events  (aka 'ev')
  * externaladmissionhookconfigurations 
  * horizontalpodautoscalers  (aka 'hpa')
  * ingresses  (aka 'ing')
  * initializerconfigurations 
  * jobs 
  * limitranges  (aka 'limits')
  * localsubjectaccessreviews 
  * namespaces  (aka 'ns')
  * networkpolicies  (aka 'netpol')
  * nodes  (aka 'no')
  * persistentvolumeclaims  (aka 'pvc')
  * persistentvolumes  (aka 'pv')
  * poddisruptionbudgets  (aka 'pdb')
  * podpresets 
  * pods  (aka 'po')
  * podsecuritypolicies  (aka 'psp')
  * podtemplates 
  * replicasets  (aka 'rs')
  * replicationcontrollers 
  * resourcequotas  (aka 'quota')
  * rolebindings 
  * roles 
  * secrets 
  * selfsubjectaccessreviews 
  * serviceaccounts  (aka 'sa')
  * services  (aka 'svc')
  * statefulsets  (aka 'sts')
  * storageclasses  (aka 'sc')
  * subjectaccessreviews 
  * tokenreviews 
 

This command will hide resources that have completed, such as pods that are in the Succeeded or Failed phases. You can
see the full results for any resource by providing the '--show-all' flag. 

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 25, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: shiywang
We suggest the following additional approver: deads2k

Assign the PR to them by writing /assign @deads2k in a comment when ready.

Associated issue: 51023

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

const validResources = `Valid resource types include:

* all
* certificatesigningrequests (aka 'csr')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

some of are not belong to core/legacy api group, remove them ?

Copy link
Contributor

Choose a reason for hiding this comment

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

some of are not belong to core/legacy api group, remove them ?

I would

@shiywang
Copy link
Contributor Author

/assign @deads2k

@shiywang shiywang force-pushed the dynamicResource branch 3 times, most recently from db78046 to 913a5f4 Compare August 25, 2017 09:19
@shiywang shiywang changed the title Fix hardcoded string, add fetch validResource from APIServer [WIP] Fix hardcoded string, add fetch validResource from APIServer Aug 25, 2017
resourceMap := make(map[string]metav1.APIResource)
var keys []string
discoveryClient, err := f.clientAccessFactory.DiscoveryClient()
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

invert the if to reduce nesting

discoveryClient, err := f.clientAccessFactory.DiscoveryClient()
if err == nil {
apiResList, err := discoveryClient.ServerResources()
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

invert if to reduce nesting

if err == nil {
for _, apiResources := range apiResList {
for _, apiRes := range apiResources.APIResources {
if !strings.Contains(apiRes.Name, "/") && apiRes.Name != "apiservices" {
Copy link
Contributor

Choose a reason for hiding this comment

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

apiservices are resources too! :)

@@ -489,3 +490,95 @@ func (f *ring1Factory) OpenAPISchema(cacheDir string) (openapi.Resources, error)
// Delegate to the OpenAPIGetter
return f.openAPIGetter.getter.Get()
}

func (f *ring1Factory) ValidResourcesFromDiscoveryClient() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method ought to return []string of just the resources strings

Copy link
Contributor Author

@shiywang shiywang Aug 28, 2017

Choose a reason for hiding this comment

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

resources strings only contain resource name ? or we can directly return sorted []APIResources then write a wrapper/helper function to concatenate those APIResource.Name and APIREsource.ShortNames to string output ?

row := "Valid resource types include:\n\n * all\n"
for _, k := range keys {
if k == "clusters" {
row = row + " * clusters (valid only for federation apiservers)\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd do this purely on discovery. If they're in the discovery list, they don't need qualification

@deads2k
Copy link
Contributor

deads2k commented Aug 25, 2017

@kubernetes/sig-cli-misc

@k8s-ci-robot k8s-ci-robot added the sig/cli Categorizes an issue or PR as relevant to SIG CLI. label Aug 25, 2017
@shiywang shiywang force-pushed the dynamicResource branch 2 times, most recently from bd1c7c2 to f098b7a Compare August 29, 2017 05:43
@k8s-github-robot
Copy link

Adding do-not-merge/release-note-label-needed because the release note process has not been followed.
One of the following labels is required "release-note", "release-note-action-required", "release-note-experimental" or "release-note-none".
Please see: https://github.com/kubernetes/community/blob/master/contributors/devel/pull-requests.md#write-release-notes-if-needed.

@k8s-github-robot k8s-github-robot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Aug 30, 2017
@k8s-ci-robot
Copy link
Contributor

@shiywang: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-bazel 585d4e8 link /test pull-kubernetes-bazel
pull-kubernetes-e2e-gce-bazel 36b8fab link /test pull-kubernetes-e2e-gce-bazel
pull-kubernetes-unit 36b8fab link /test pull-kubernetes-unit
pull-kubernetes-kubemark-e2e-gce 36b8fab link /test pull-kubernetes-kubemark-e2e-gce
pull-kubernetes-e2e-gce-etcd3 36b8fab link /test pull-kubernetes-e2e-gce-etcd3
pull-kubernetes-e2e-kops-aws 36b8fab link /test pull-kubernetes-e2e-kops-aws

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 1, 2017
@k8s-github-robot
Copy link

@shiywang PR needs rebase

@@ -113,7 +113,7 @@ func NewCmdAnnotate(f cmdutil.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "annotate [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]",
Short: i18n.T("Update the annotations on a resource"),
Long: annotateLong,
Long: templates.LongDesc(fmt.Sprintf(annotateLong, f.ValidResourcesFromDiscoveryClient())),
Copy link
Member

@liggitt liggitt Sep 2, 2017

Choose a reason for hiding this comment

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

I don't think we should ever talk to the server to display help. I'd rather see a dedicated command for displaying available resources, and reference that command in this help string

Copy link
Contributor Author

@shiywang shiywang Sep 4, 2017

Choose a reason for hiding this comment

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

I wrote a proposal for this feature, ptal kubernetes/community#1017

@k8s-github-robot
Copy link

This PR hasn't been active in 90 days. Closing this PR. Please reopen if you would like to work towards merging this change, if/when the PR is ready for the next round of review.

cc @deads2k @fabianofranz @ghodss @shiywang

You can add 'keep-open' label to prevent this from happening again, or add a comment to keep it open another 90 days

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. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants