Merge pull request #115338 from danwinship/nodeutil.gethostname
Move GetHostname() from k/k/pkg/util/node to k/component-helpers/node/util
This commit is contained in:
@@ -20,8 +20,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
@@ -42,26 +40,6 @@ const (
|
||||
NodeUnreachablePodMessage = "Node %v which was running pod %v is unresponsive"
|
||||
)
|
||||
|
||||
// GetHostname returns OS's hostname if 'hostnameOverride' is empty; otherwise, return 'hostnameOverride'.
|
||||
func GetHostname(hostnameOverride string) (string, error) {
|
||||
hostName := hostnameOverride
|
||||
if len(hostName) == 0 {
|
||||
nodeName, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("couldn't determine hostname: %v", err)
|
||||
}
|
||||
hostName = nodeName
|
||||
}
|
||||
|
||||
// Trim whitespaces first to avoid getting an empty hostname
|
||||
// For linux, the hostname is read from file /proc/sys/kernel/hostname directly
|
||||
hostName = strings.TrimSpace(hostName)
|
||||
if len(hostName) == 0 {
|
||||
return "", fmt.Errorf("empty hostname is invalid")
|
||||
}
|
||||
return strings.ToLower(hostName), nil
|
||||
}
|
||||
|
||||
// NoMatchError is a typed implementation of the error interface. It indicates a failure to get a matching Node.
|
||||
type NoMatchError struct {
|
||||
addresses []v1.NodeAddress
|
||||
|
@@ -198,38 +198,6 @@ func TestGetNodeHostIPs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHostname(t *testing.T) {
|
||||
testCases := []struct {
|
||||
hostName string
|
||||
expectedHostName string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
hostName: " ",
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
hostName: " abc ",
|
||||
expectedHostName: "abc",
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for idx, test := range testCases {
|
||||
hostName, err := GetHostname(test.hostName)
|
||||
if err != nil && !test.expectError {
|
||||
t.Errorf("[%d]: unexpected error: %s", idx, err)
|
||||
}
|
||||
if err == nil && test.expectError {
|
||||
t.Errorf("[%d]: expected error, got none", idx)
|
||||
}
|
||||
if test.expectedHostName != hostName {
|
||||
t.Errorf("[%d]: expected output %q, got %q", idx, test.expectedHostName, hostName)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNodeReady(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
@@ -42,7 +42,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
volumehelpers "k8s.io/cloud-provider/volume/helpers"
|
||||
"k8s.io/kubernetes/pkg/util/node"
|
||||
nodeutil "k8s.io/component-helpers/node/util"
|
||||
"k8s.io/kubernetes/pkg/volume"
|
||||
volutil "k8s.io/kubernetes/pkg/volume/util"
|
||||
)
|
||||
@@ -327,7 +327,7 @@ func (util *rbdUtil) rbdUnlock(b rbdMounter) error {
|
||||
}
|
||||
|
||||
// Construct lock id using host name and a magic prefix.
|
||||
hostName, err := node.GetHostname("")
|
||||
hostName, err := nodeutil.GetHostname("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user