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

Automated cherry pick of #17973 #17985

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/api/validation/validation.go
Expand Up @@ -1115,6 +1115,12 @@ func ValidatePodSpec(spec *api.PodSpec) errs.ValidationErrorList {
}
}

if len(spec.NodeName) > 0 {
if ok, msg := ValidateNodeName(spec.NodeName, false); !ok {
allErrs = append(allErrs, errs.NewFieldInvalid("nodeName", spec.NodeName, msg))
}
}

if spec.ActiveDeadlineSeconds != nil {
if *spec.ActiveDeadlineSeconds <= 0 {
allErrs = append(allErrs, errs.NewFieldInvalid("activeDeadlineSeconds", spec.ActiveDeadlineSeconds, "activeDeadlineSeconds must be a positive integer greater than 0"))
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/validation/validation_test.go
Expand Up @@ -1341,6 +1341,13 @@ func TestValidatePodSpec(t *testing.T) {
DNSPolicy: api.DNSClusterFirst,
ActiveDeadlineSeconds: &activeDeadlineSeconds,
},
"bad nodeName": {
NodeName: "node name",
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
Containers: []api.Container{{Name: "ctr", Image: "image", ImagePullPolicy: "IfNotPresent"}},
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
},
}
for k, v := range failureCases {
if errs := ValidatePodSpec(&v); len(errs) == 0 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/client/unversioned/kubelet.go
Expand Up @@ -18,7 +18,10 @@ package unversioned

import (
"errors"
"fmt"
"net/http"

"k8s.io/kubernetes/pkg/api/validation"
)

// KubeletClient is an interface for all kubelet functionality
Expand Down Expand Up @@ -75,6 +78,9 @@ func NewKubeletClient(config *KubeletConfig) (KubeletClient, error) {
}

func (c *HTTPKubeletClient) GetConnectionInfo(host string) (string, uint, http.RoundTripper, error) {
if ok, msg := validation.ValidateNodeName(host, false); !ok {
return "", 0, nil, fmt.Errorf("invalid node name: %s", msg)
}
scheme := "http"
if c.Config.EnableHttps {
scheme = "https"
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/unversioned/kubelet_test.go
Expand Up @@ -82,6 +82,7 @@ func TestNewKubeletClientTLSInvalid(t *testing.T) {

func TestNewKubeletClientTLSValid(t *testing.T) {
config := &KubeletConfig{
Port: 1234,
EnableHttps: true,
TLSClientConfig: TLSClientConfig{
CertFile: "../testdata/mycertvalid.cer",
Expand All @@ -99,4 +100,27 @@ func TestNewKubeletClientTLSValid(t *testing.T) {
if client == nil {
t.Error("client should not be nil")
}

{
scheme, port, transport, err := client.GetConnectionInfo("foo")
if err != nil {
t.Errorf("Error getting info: %v", err)
}
if scheme != "https" {
t.Errorf("Expected https, got %s", scheme)
}
if port != 1234 {
t.Errorf("Expected 1234, got %d", port)
}
if transport == nil {
t.Errorf("Expected transport, got nil")
}
}

{
_, _, _, err := client.GetConnectionInfo("foo bar")
if err == nil {
t.Errorf("Expected error getting connection info for invalid node name, got none")
}
}
}