diff --git a/pkg/kubelet/lifecycle/handlers.go b/pkg/kubelet/lifecycle/handlers.go index da14ba3d1b4..9ffbf4db742 100644 --- a/pkg/kubelet/lifecycle/handlers.go +++ b/pkg/kubelet/lifecycle/handlers.go @@ -23,13 +23,11 @@ import ( "io" "net/http" "net/url" - "strconv" "strings" "time" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/intstr" utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/tools/record" "k8s.io/klog/v2" @@ -106,29 +104,6 @@ func (hr *handlerRunner) Run(ctx context.Context, containerID kubecontainer.Cont } } -// resolvePort attempts to turn an IntOrString port reference into a concrete port number. -// If portReference has an int value, it is treated as a literal, and simply returns that value. -// If portReference is a string, an attempt is first made to parse it as an integer. If that fails, -// an attempt is made to find a port with the same name in the container spec. -// If a port with the same name is found, it's ContainerPort value is returned. If no matching -// port is found, an error is returned. -func resolvePort(portReference intstr.IntOrString, container *v1.Container) (int, error) { - if portReference.Type == intstr.Int { - return portReference.IntValue(), nil - } - portName := portReference.StrVal - port, err := strconv.Atoi(portName) - if err == nil { - return port, nil - } - for _, portSpec := range container.Ports { - if portSpec.Name == portName { - return int(portSpec.ContainerPort), nil - } - } - return -1, fmt.Errorf("couldn't find port: %v in %v", portReference, container) -} - func (hr *handlerRunner) runSleepHandler(ctx context.Context, seconds int64) error { if !utilfeature.DefaultFeatureGate.Enabled(features.PodLifecycleSleepAction) { return nil diff --git a/pkg/kubelet/lifecycle/handlers_test.go b/pkg/kubelet/lifecycle/handlers_test.go index 3e48303889c..8d6c9220c13 100644 --- a/pkg/kubelet/lifecycle/handlers_test.go +++ b/pkg/kubelet/lifecycle/handlers_test.go @@ -41,47 +41,6 @@ import ( "k8s.io/kubernetes/test/utils/ktesting" ) -func TestResolvePort(t *testing.T) { - for _, testCase := range []struct { - container *v1.Container - stringPort string - expected int - }{ - { - stringPort: "foo", - container: &v1.Container{ - Ports: []v1.ContainerPort{{Name: "foo", ContainerPort: int32(80)}}, - }, - expected: 80, - }, - { - container: &v1.Container{}, - stringPort: "80", - expected: 80, - }, - { - container: &v1.Container{ - Ports: []v1.ContainerPort{ - {Name: "bar", ContainerPort: int32(80)}, - }, - }, - stringPort: "foo", - expected: -1, - }, - } { - port, err := resolvePort(intstr.FromString(testCase.stringPort), testCase.container) - if testCase.expected != -1 && err != nil { - t.Fatalf("unexpected error while resolving port: %s", err) - } - if testCase.expected == -1 && err == nil { - t.Errorf("expected error when a port fails to resolve") - } - if testCase.expected != port { - t.Errorf("failed to resolve port, expected %d, got %d", testCase.expected, port) - } - } -} - type fakeContainerCommandRunner struct { Cmd []string ID kubecontainer.ContainerID