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

error out empty hostname #64815

Merged
merged 1 commit into from
Aug 4, 2018
Merged

Conversation

dixudx
Copy link
Member

@dixudx dixudx commented Jun 6, 2018

What this PR does / why we need it:
For linux, the hostname is read from file /proc/sys/kernel/hostname directly, which can be overwritten with whitespaces.

Should error out such invalid hostnames.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes kubernetes/kubeadm#835

Special notes for your reviewer:
/cc luxas timothysc

Release note:

nodes: improve handling of erroneous host names

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Jun 6, 2018
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. area/kubeadm sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. labels Jun 6, 2018
@dixudx
Copy link
Member Author

dixudx commented Jun 6, 2018

os.Hostname is reading from file /proc/sys/kernel/hostname directly.

root@server-01:~# cat /proc/sys/kernel/hostname
server-01
root@server-01:~# vim /proc/sys/kernel/hostname 
root@server-01:~# echo "   " > /proc/sys/kernel/hostname 
root@server-01:~# cat /proc/sys/kernel/hostname
   

After overwritten, os.Hostname() will get hostname with whitespaces, which will be trimmed as empty string.

@dixudx
Copy link
Member Author

dixudx commented Jun 6, 2018

@seh FYI.

@seh
Copy link
Contributor

seh commented Jun 6, 2018

My hostnames don’t have any white space in them, but kubeadm still fails to detect them properly.

echo "|$(cat /proc/sys/kernel/hostname)|"
|ip-10-103-0-147|

Hence, this patch cannot fix kubernetes/kubeadm#835.

// For linux, the hostname is read from file /proc/sys/kernel/hostname directly
hostname = strings.TrimSpace(hostname)
if len(hostname) == 0 {
glog.Fatalf("Empty hostname is invalid")
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like the use of glog.Fatalf here. It calls os.Exit and I perceive this as a bad thing. It does not allow for any error handling or recovery here. A panic or returning an error might be better.

Copy link
Member

Choose a reason for hiding this comment

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

+1 we should Errorf and pass back the error like everything else.

Copy link
Member

Choose a reason for hiding this comment

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

@dixudx - re-ping are you going to fix this ^

// For linux, the hostname is read from file /proc/sys/kernel/hostname directly
hostname = strings.TrimSpace(hostname)
if len(hostname) == 0 {
glog.Fatalf("Empty hostname is invalid")
Copy link
Member

Choose a reason for hiding this comment

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

+1 we should Errorf and pass back the error like everything else.

@timothysc timothysc added the kind/bug Categorizes issue or PR as related to a bug. label Jun 6, 2018
luxas
luxas previously requested changes Jun 6, 2018
Copy link
Member

@luxas luxas left a comment

Choose a reason for hiding this comment

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

Just wanna mention I'm very hesitant to changing pkg/util/node for fixing a bug in kubeadm only. pkg/util/node is used in A LOT of other places. Can we solve @seh's issue in an other way?

@dixudx
Copy link
Member Author

dixudx commented Jun 7, 2018

pkg/util/node is used in A LOT of other places.

Right. So kubeadm may not be the only victim. We should fix this root bug.

fixing a bug in kubeadm only

We could surface the fix in kubeadm. But IMO that's not a real fix.

@luxas
Copy link
Member

luxas commented Jun 8, 2018

@dixudx at this point, the only thing we can do is to try to surface the fix in kubeadm. We can't go ahead and do a large potentially very disruptive change now. If we have some easy fix locally in the kubeadm code to ease this problem we can do it, otherwise not.

@dixudx
Copy link
Member Author

dixudx commented Jun 8, 2018

Hence, this patch cannot fix

@seh Have you tried to test locally with this fix.

@seh
Copy link
Contributor

seh commented Jun 8, 2018

No, I haven't tested it. The solution to kubernetes/kubeadm#835 is not have kubeadm fail more frequently; it's to have kubeadm detect the machine's hostname properly. The machines in question have a hostname, those hostnames do not contain spaces, and the /proc/sys/kernel/hostname files do not contain any whitespace. Hence, this fix is not pertinent to the problem I described in kubernetes/kubeadm#835.

I'm not saying that your change here isn't worthy of inclusion, but let's not claim that it fixes kubernetes/kubeadm#835.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 16, 2018
@luxas
Copy link
Member

luxas commented Jun 30, 2018

@dixudx should we close this?

@dixudx
Copy link
Member Author

dixudx commented Jul 2, 2018

@luxas Shall we make this as a common check, not only for kubeadm.

@luxas
Copy link
Member

luxas commented Jul 2, 2018

@luxas Shall we make this as a common check, not only for kubeadm.

@dixudx I'm not sure what you mean here

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 3, 2018
@dixudx
Copy link
Member Author

dixudx commented Jul 3, 2018

@luxas The hostname checking is necessary, not only for kubeadm. For users that deploying clusters using other scripts, tightening the checking will help avoid potential issues.

@dixudx
Copy link
Member Author

dixudx commented Jul 3, 2018

@smarterclayton PTAL. Thanks

@smarterclayton
Copy link
Contributor

The comment about returning an error is appropriate. Utility code should not invoke glog.Fatalf or panic.

Copy link
Member

@timothysc timothysc 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 Jul 26, 2018
@dixudx
Copy link
Member Author

dixudx commented Jul 27, 2018

/retest

1 similar comment
@dixudx
Copy link
Member Author

dixudx commented Jul 27, 2018

/retest

@timothysc
Copy link
Member

/test pull-kubernetes-local-e2e-containerized

poke @smarterclayton for approval.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 28, 2018
@dixudx
Copy link
Member Author

dixudx commented Jul 28, 2018

@timothysc @smarterclayton Rebased. PTAL. Thanks.

@dixudx
Copy link
Member Author

dixudx commented Jul 30, 2018

/retest

Copy link
Member

@timothysc timothysc left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 30, 2018
@neolit123
Copy link
Member

@dixudx suggesting release note:

nodes: improve handling of erroneous host names

@@ -784,7 +788,11 @@ func InitializeTLS(kf *options.KubeletFlags, kc *kubeletconfiginternal.KubeletCo
return nil, err
}
if !canReadCertAndKey {
cert, key, err := certutil.GenerateSelfSignedCertKey(nodeutil.GetHostname(kf.HostnameOverride), nil, nil)
nodeName, err := nodeutil.GetHostname(kf.HostnameOverride)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the wrong name for the variable.

@smarterclayton
Copy link
Contributor

smarterclayton commented Jul 31, 2018

Can this break existing clusters that have empty hostnames? If I have a 1.11 cluster, won't my nodes start? If so, when they upgrade to 1.12 with this PR, wouldn't it break those clusters?

@k8s-ci-robot
Copy link
Contributor

New changes are detected. LGTM label has been removed.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 1, 2018
@dixudx
Copy link
Member Author

dixudx commented Aug 1, 2018

Can this break existing clusters that have empty hostnames? If I have a 1.11 cluster, won't my nodes start? If so, when they upgrade to 1.12 with this PR, wouldn't it break those clusters?

@smarterclayton No, it won't break any existing clusters.

The reason is that, for running clusters, the node name won't be empty. Because k8s won't allow node with empty node name when registering, right? /api/v1/nodes/<nodeName> does not make sense for empty node name.

// ValidateNode tests if required fields in the node are set.
func ValidateNode(node *core.Node) field.ErrorList {
fldPath := field.NewPath("metadata")
allErrs := ValidateObjectMeta(&node.ObjectMeta, false, ValidateNodeName, fldPath)
allErrs = append(allErrs, ValidateNodeSpecificAnnotations(node.ObjectMeta.Annotations, fldPath.Child("annotations"))...)

This means for existing clusters with empty hostnames, the node name must be override already. And for those clusters with hostname override, this PR has no effect at all.

IMO, it's quite safe to merge this PR.

@smarterclayton
Copy link
Contributor

ok

/approve

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 1, 2018
@dixudx
Copy link
Member Author

dixudx commented Aug 2, 2018

ping @timothysc for lgtm. Thanks.

@timothysc
Copy link
Member

/lgtm .

Thx for your patience @dixudx !

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dixudx, smarterclayton, timothysc

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

@timothysc timothysc added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 3, 2018
@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit cb1ef9f into kubernetes:master Aug 4, 2018
@dixudx dixudx deleted the hostname_empty branch August 4, 2018 03:48
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/kubeadm 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. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. 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.

"kubeadm join" Hostname pre-flight check fails to recognize hostname
9 participants