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

(#21648)add kubectl set port command #51947

Closed
wants to merge 3 commits into from
Closed

Conversation

zjj2wry
Copy link
Contributor

@zjj2wry zjj2wry commented Sep 5, 2017

What this PR does / why we need it:
#21648
add kubectl set port command
Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Special notes for your reviewer:

Release note:

Add 'kubectl set port' command for setting port in resources embedding pod templates

@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 Sep 5, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @zjj2wry. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

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-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 5, 2017
@zjj2wry zjj2wry changed the title Set port (#21648)add kubectl set port command Sep 5, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zjj2wry
We suggest the following additional approver: smarterclayton

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

No associated issue. Update pull-request body to add a reference to an issue, or get approval with /approve no-issue

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

@k8s-github-robot k8s-github-robot added kind/old-docs release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Sep 5, 2017
@zjj2wry
Copy link
Contributor Author

zjj2wry commented Sep 5, 2017

cc @kubernetes/sig-cli-pr-reviews

@k8s-ci-robot k8s-ci-robot added the sig/cli Categorizes an issue or PR as relevant to SIG CLI. label Sep 5, 2017
@k8s-ci-robot
Copy link
Contributor

@zjj2wry: Reiterating the mentions to trigger a notification:
@kubernetes/sig-cli-pr-reviews.

In response to this:

cc @kubernetes/sig-cli-pr-reviews

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.

"fmt"
"io"
"strings"

Copy link
Member

Choose a reason for hiding this comment

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

remove this newline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree 👍


"strconv"

"github.com/spf13/cobra"
Copy link
Member

Choose a reason for hiding this comment

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

add a newline here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alse agree

cmd.Flags().BoolVar(&options.All, "all", false, "Select all resources in the namespace of the specified resource types")
cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().BoolVar(&options.Local, "local", false, "If true, set port will NOT contact api-server but run locally.")
cmd.Flags().Int("port", 80, i18n.T("Name or number for the port on the container that the service should direct traffic to. Optional."))
Copy link
Member

Choose a reason for hiding this comment

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

port should be marked as required? I think we'd better not set default value, especially 80. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, i will updated

}

cmd := &cobra.Command{
Use: "port (-f FILENAME | TYPE NAME)",
Copy link
Member

Choose a reason for hiding this comment

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

appending --port ?


cmd := &cobra.Command{
Use: "port (-f FILENAME | TYPE NAME)",
Short: i18n.T("Update port of a pod template"),
Copy link
Member

Choose a reason for hiding this comment

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

What about Update existing container port(s) of resources ?

if len(containers) == 0 {
fmt.Println(info.Mapping.Resource, info.Name, o.containerSelector)
_, err := fmt.Fprintf(o.Err, "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, o.containerSelector)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

what about

if _, err := xxx; err != nil {
    return err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

return err
}
for _, c := range containers {
if !o.overwrite {
Copy link
Member

Choose a reason for hiding this comment

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

L199-L205 seems have nothing to do with c. Can we move it to outer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thank you point it

if _, err := fmt.Fprintln(o.Out, "no resources changed"); err != nil {
return nil, err
}
return nil, err
Copy link
Member

Choose a reason for hiding this comment

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

return nil, err ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this should be return nil,nil

for _, patch := range patches {
info := patch.Info
if patch.Err != nil {
allErrs = append(allErrs, fmt.Errorf("error: %s/%s %v\n", info.Mapping.Resource, info.Name, patch.Err))
Copy link
Member

Choose a reason for hiding this comment

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

not sure for adding \n here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this should

}

// no changes
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

should have a better way of string(patch.Patch) == "{}".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this follow other set command

@zjj2wry
Copy link
Contributor Author

zjj2wry commented Sep 5, 2017

@dixudx thanks, i will fix this tomorrow

Copy link
Contributor Author

@zjj2wry zjj2wry left a comment

Choose a reason for hiding this comment

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

@dixudx thank your careful review work. i will fix as soon as possible

o.Output = cmdutil.GetFlagString(cmd, "output")
o.port = cmdutil.GetFlagInt(cmd, "port")
o.protocol = strings.ToUpper(cmdutil.GetFlagString(cmd, "protocol"))
o.containerSelector = cmdutil.GetFlagString(cmd, "containers")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i parse this in L184

o.Local = cmdutil.GetFlagBool(cmd, "local")
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.Output = cmdutil.GetFlagString(cmd, "output")
o.port = cmdutil.GetFlagInt(cmd, "port")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure, but i'd more like keep a style.

cmd.Flags().BoolVar(&options.All, "all", false, "Select all resources in the namespace of the specified resource types")
cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().BoolVar(&options.Local, "local", false, "If true, set port will NOT contact api-server but run locally.")
cmd.Flags().Int("port", 80, i18n.T("Name or number for the port on the container that the service should direct traffic to. Optional."))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, i will updated

if o.protocol != "TCP" && o.protocol != "UDP" {
errors = append(errors, fmt.Errorf("protocol must tcp or udp"))
}
if o.port < 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, i will follow server-side

if len(containers) == 0 {
fmt.Println(info.Mapping.Resource, info.Name, o.containerSelector)
_, err := fmt.Fprintf(o.Err, "warning: %s/%s does not have any containers matching %q\n", info.Mapping.Resource, info.Name, o.containerSelector)
if err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

return err
}
for _, c := range containers {
if !o.overwrite {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thank you point it

if _, err := fmt.Fprintln(o.Out, "no resources changed"); err != nil {
return nil, err
}
return nil, err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this should be return nil,nil

return nil
}

func (o *PortOptions) Validate() error {
Copy link
Member

Choose a reason for hiding this comment

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

port (-f FILENAME | TYPE NAME) [--port=PORT]

I think we'd better add validation on the type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this validation in f.UpdatePodSpecForObject(info.Object, func(spec *api.PodSpec) error

}

var (
portResources = `
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 add like this? This does not seems like k8s style.

Please refer below,

validResources = `Valid resource types include:
* all
* certificatesigningrequests (aka 'csr')
* clusterrolebindings
* clusterroles
* clusters (valid only for federation apiservers)
* componentstatuses (aka 'cs')
* configmaps (aka 'cm')
* controllerrevisions
* cronjobs
* customresourcedefinition (aka 'crd')
* 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 (aka 'netpol')
* nodes (aka 'no')
* persistentvolumeclaims (aka 'pvc')
* persistentvolumes (aka 'pv')
* poddisruptionbudgets (aka 'pdb')
* podpreset
* pods (aka 'po')
* podsecuritypolicies (aka 'psp')
* podtemplates
* replicasets (aka 'rs')
* replicationcontrollers (aka 'rc')
* resourcequotas (aka 'quota')
* rolebindings
* roles
* secrets
* serviceaccounts (aka 'sa')
* services (aka 'svc')
* statefulsets
* storageclasses
`

But we're trying to replace such hard-coded way, refer to #42873 and #51324.

@liggitt PTAL. For updating such container ports, can we use the old hard-coded strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this mean support update env's resources

@zjj2wry
Copy link
Contributor Author

zjj2wry commented Sep 24, 2017

/kind feature

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 24, 2017
@zjj2wry
Copy link
Contributor Author

zjj2wry commented Sep 25, 2017

/unassign @dshulyak
/assign @liggitt

@k8s-ci-robot k8s-ci-robot assigned liggitt and unassigned dshulyak Sep 25, 2017
@liggitt
Copy link
Member

liggitt commented Sep 25, 2017

/unassign
/assign @fabianofranz @pwittrock

@fabianofranz
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 6, 2017
@k8s-ci-robot
Copy link
Contributor

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

Test name Commit Details Rerun command
pull-kubernetes-e2e-gce-bazel f0f1bd0 link /test pull-kubernetes-e2e-gce-bazel
pull-kubernetes-e2e-gce-gpu f0f1bd0 link /test pull-kubernetes-e2e-gce-gpu
pull-kubernetes-bazel-build f0f1bd0 link /test pull-kubernetes-bazel-build
pull-kubernetes-kubemark-e2e-gce f0f1bd0 link /test pull-kubernetes-kubemark-e2e-gce
pull-kubernetes-e2e-kops-aws f0f1bd0 link /test pull-kubernetes-e2e-kops-aws
pull-kubernetes-bazel-test f0f1bd0 link /test pull-kubernetes-bazel-test
pull-kubernetes-unit f0f1bd0 link /test pull-kubernetes-unit
pull-kubernetes-e2e-gce-etcd3 f0f1bd0 link /test pull-kubernetes-e2e-gce-etcd3
pull-kubernetes-verify f0f1bd0 link /test pull-kubernetes-verify

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.

@pwittrock pwittrock assigned seans3 and unassigned pwittrock Oct 11, 2017
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/kubernetes/pkg/api"
Copy link
Member

Choose a reason for hiding this comment

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

We are not introducing new deps on k8s.io/kubernetes. How can this PR be done without this? Can we use the versioned objects.

Copy link
Member

Choose a reason for hiding this comment

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

we want to do this anyway, see the TODO in https://github.com/kubernetes/kubernetes/pull/53158/files#diff-466503ed5c81843c5ba705bc4c6fd4dc

// TODO: switch UpdatePodSpecForObject to work on v1.PodSpec, use info.VersionedObject, and avoid conversion completely

Copy link
Member

Choose a reason for hiding this comment

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

Why a TODO? Can we do this now before this PR goes in? We are actively trying to remove these dependencies from kubectl.

Copy link
Member

Choose a reason for hiding this comment

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

agree, was saying we could use this as an opportunity to resolve the TODO

Copy link
Member

Choose a reason for hiding this comment

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

oh perfect :)

Copy link
Contributor Author

@zjj2wry zjj2wry Oct 20, 2017

Choose a reason for hiding this comment

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

there is a issue created by wangshiyang. will help fix it priority then fix this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

open PR #54554 fix this

return nil, err
}
if transformed && err == nil {
return runtime.Encode(o.Encoder, info.Object)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

as is, this would fail intermittently on DaemonSet and ReplicaSet objects

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liggitt nice, thank you very much.

@liggitt
Copy link
Member

liggitt commented Oct 12, 2017

needs tests in test-cmd-util.sh on the various resources

@zjj2wry
Copy link
Contributor Author

zjj2wry commented Oct 12, 2017

will updated it quickly

@pwittrock
Copy link
Member

/assign @pwittrock

@pwittrock
Copy link
Member

/unassign @pwittrock

@k8s-github-robot
Copy link

This PR hasn't been active in 30 days. It will be closed in 59 days (Jan 23, 2018).

cc @fabianofranz @seans3 @soltysh @zjj2wry

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

@fabianofranz
Copy link
Contributor

/unassign

@zjj2wry
Copy link
Contributor Author

zjj2wry commented Feb 22, 2018

/close

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. kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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.