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

fix kubectl create deployment image name #86636

Conversation

zhouya0
Copy link
Contributor

@zhouya0 zhouya0 commented Dec 26, 2019

What type of PR is this?

/kind bug

What this PR does / why we need it:
Kubectl create deployment command will use image's name as container's name. When image's name have "_" or "."(which is valide in docker), it will be failed because the name doesn't meet the DNS1123 rules. So it PR will delete these two symbol.
Which issue(s) this PR fixes:

Fixes #kubernetes/kubectl#789

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

fix kubectl create deployment image name

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Dec 26, 2019
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 26, 2019
@zhouya0
Copy link
Contributor Author

zhouya0 commented Dec 26, 2019

/test pull-kubernetes-e2e-gce

@thockin
Copy link
Member

thockin commented Dec 27, 2019

Hi! This is a start, but I don't think it is sufficient. kubectl create deployment supports multiple images, and I could legally say foo_bar and foobar and foo.bar in the same pod, which would collide. I am not saying it's a great idea, but it is valid and needs to be handled.

It gets more complicated - you can't simply escape characters. E.g. if I turned _ into -underscore- a) it could still collide and b) it could overflow.

Also I am not 100% confident that those are the only 2 such characters that we need to handle. Maybe, but I'd like to see a comment citing a spec or something authoritative.

To do this "properly" you need to look at the complete set of container images and their sanitized-generated names. If none of them collide, no problem. If any of them collide either a) throw an error and fail (see below) or b) further randomize them by adding a unique suffix (see below).

Option a is terrible because the user has no recourse. If that is ACTUALLY what they want to do, they simply can't. To fix this, we'd need a way for them to manually remap container names, which actually sounds OK to me. For example --image=bar-qux=foo/bar_qux:123. If we detect an = sign, one half becomes the container name ("bar-qux") and the other half becomes the image. In fact, if we do this, we don't need to sanitize at all, maybe?

Option b) is terrible because you need to stay less than max-length, so you end up potentially eating the last few characters to replace them with suffix, which demands further conflict checking.

@thockin thockin self-assigned this Dec 27, 2019
@thockin
Copy link
Member

thockin commented Dec 27, 2019

Assigning to myself just so I see notifications

@zhouya0
Copy link
Contributor Author

zhouya0 commented Jan 13, 2020

Thanks for your reply. But by far i haven't thought out solutions, but the image naming rules are referred by https://github.com/moby/moby/blob/be97c66708c24727836a22247319ff2943d91a03/daemon/names/names.go

@thockin
Copy link
Member

thockin commented Feb 17, 2020

Revisiting.

What about normalizing the names and then uniquifiying? That should be such a corner-case it won't matter.

E.g.

names = []
for each value in --image {
    name = sanitize(value)
    if name in names {
        name = uniquify(name, names)
    }
    // process container with name
}

Where sanitize() converts anything not supported into '-' and uniquify() truncates the input and adds a counter to the end. For example inputs of [ "foo_bar", "foo-bar" ] produce [ "foo-bar", "foo-bar-2" ]. Very long names will bump against the limit so "a-very-long-name" might become "a-very-long-na-2". Adding a 5-character hash of the image string might be simpler, still has to truncate.

Last corner case is what to do when the sanitized name is exactly colliding with a natural name, as above: inputs of [ "foo_bar", "foo-bar" ] produce [ "foo-bar", "foo-bar-2" ], but it might be better to produce [ "foo-bar-2", "foo-bar" ]. Probably not worth the hassle.

@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 18, 2020

The root cause of this is the different naming rules with docker and k8s. And actually this barely happens(naming images names with "_" or "." ), I just think add two more functions like sanitize and uniquify in this special case is not a good idea.

@thockin
Copy link
Member

thockin commented Feb 18, 2020 via email

@soltysh soltysh self-assigned this Feb 20, 2020
@soltysh
Copy link
Contributor

soltysh commented Feb 20, 2020

@zhouya0 how fast can you address Tim's comment, which is valid and I'd love to see happen asap, since this is something that will need to be fixed in other generators as well.

@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 21, 2020

@zhouya0 how fast can you address Tim's comment, which is valid and I'd love to see happen asap, since this is something that will need to be fixed in other generators as well.

Ok, I'll try to do it.

@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 21, 2020

@thockin I opened a new PR #88403.
The solution exactly follows your suggestion. But I didn't check the string limit. I have some questions:

  1. What is the max length of container's name?
  2. If the max length is set, where to put? Because is't duplicate of this part of code :
    const (
    // TODO: make this flexible for non-core resources with alternate naming rules.
    maxNameLength = 63
    randomLength = 5
    maxGeneratedNameLength = maxNameLength - randomLength
    )

@soltysh
Copy link
Contributor

soltysh commented Feb 21, 2020

@zhouya0 pls don't open new PRs, but apply the necessary fixes here. Also that other PR only addressed the sanitization part, but did not cover uniqueness Tim mentioned before. Please follow the advices given from reviewers.

@zhouya0 zhouya0 force-pushed the fix_kubectl_create_deployment_image_name branch from cc09f94 to 00d2cdb Compare February 22, 2020 11:50
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 22, 2020
@zhouya0 zhouya0 force-pushed the fix_kubectl_create_deployment_image_name branch from 00d2cdb to 5db45e1 Compare February 22, 2020 12:24
@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 22, 2020

/test pull-kubernetes-e2e-kind

@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 22, 2020

/assign @soltysh
Thanks for your @soltysh advice. I move my code to this PR. But the thing with uniqueness is done by this code isn't it?

name = fmt.Sprintf("%s-%s", name, utilrand.String(5))

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

/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 Feb 27, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: soltysh, zhouya0

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 Feb 27, 2020
@zhouya0
Copy link
Contributor Author

zhouya0 commented Feb 28, 2020

/kind bug

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Feb 28, 2020
@k8s-ci-robot k8s-ci-robot merged commit c7d7cf7 into kubernetes:master Feb 28, 2020
@k8s-ci-robot k8s-ci-robot added this to the v1.18 milestone Feb 28, 2020
wking pushed a commit to wking/kubernetes that referenced this pull request Jul 21, 2020
…eployment_image_name

fix kubectl create deployment image name

Kubernetes-commit: c7d7cf7
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/kubectl 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. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. 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/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants