mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 10:20:51 +00:00
Move GetHostname() from k/k/pkg/util/node to k/component-helpers/node/util
In fact, this actually uses pkg/util/node's GetHostname() but takes the unit tests from cmd/kubeadm/app/util's private fork of that function since they were more extensive. (Of course the fact that kubeadm had a private fork of this function is a strong argument for moving it to component-helpers.)
This commit is contained in:
parent
d29e3bd7aa
commit
f994ae2521
@ -46,6 +46,7 @@ import (
|
|||||||
toolswatch "k8s.io/client-go/tools/watch"
|
toolswatch "k8s.io/client-go/tools/watch"
|
||||||
"k8s.io/component-base/configz"
|
"k8s.io/component-base/configz"
|
||||||
"k8s.io/component-base/metrics"
|
"k8s.io/component-base/metrics"
|
||||||
|
nodeutil "k8s.io/component-helpers/node/util"
|
||||||
utilsysctl "k8s.io/component-helpers/node/util/sysctl"
|
utilsysctl "k8s.io/component-helpers/node/util/sysctl"
|
||||||
"k8s.io/kubernetes/pkg/proxy"
|
"k8s.io/kubernetes/pkg/proxy"
|
||||||
proxyconfigapi "k8s.io/kubernetes/pkg/proxy/apis/config"
|
proxyconfigapi "k8s.io/kubernetes/pkg/proxy/apis/config"
|
||||||
@ -59,7 +60,6 @@ import (
|
|||||||
utilipset "k8s.io/kubernetes/pkg/util/ipset"
|
utilipset "k8s.io/kubernetes/pkg/util/ipset"
|
||||||
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
|
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
|
||||||
utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
|
utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
|
||||||
utilnode "k8s.io/kubernetes/pkg/util/node"
|
|
||||||
"k8s.io/utils/exec"
|
"k8s.io/utils/exec"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ func newProxyServer(
|
|||||||
metrics.SetShowHidden()
|
metrics.SetShowHidden()
|
||||||
}
|
}
|
||||||
|
|
||||||
hostname, err := utilnode.GetHostname(config.HostnameOverride)
|
hostname, err := nodeutil.GetHostname(config.HostnameOverride)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,13 @@ import (
|
|||||||
"k8s.io/client-go/tools/events"
|
"k8s.io/client-go/tools/events"
|
||||||
"k8s.io/component-base/configz"
|
"k8s.io/component-base/configz"
|
||||||
"k8s.io/component-base/metrics"
|
"k8s.io/component-base/metrics"
|
||||||
|
nodeutil "k8s.io/component-helpers/node/util"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
"k8s.io/kubernetes/pkg/proxy"
|
"k8s.io/kubernetes/pkg/proxy"
|
||||||
proxyconfigapi "k8s.io/kubernetes/pkg/proxy/apis/config"
|
proxyconfigapi "k8s.io/kubernetes/pkg/proxy/apis/config"
|
||||||
proxyconfigscheme "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
|
proxyconfigscheme "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
|
||||||
"k8s.io/kubernetes/pkg/proxy/healthcheck"
|
"k8s.io/kubernetes/pkg/proxy/healthcheck"
|
||||||
"k8s.io/kubernetes/pkg/proxy/winkernel"
|
"k8s.io/kubernetes/pkg/proxy/winkernel"
|
||||||
utilnode "k8s.io/kubernetes/pkg/util/node"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewProxyServer returns a new ProxyServer.
|
// NewProxyServer returns a new ProxyServer.
|
||||||
@ -71,7 +71,7 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, master string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create event recorder
|
// Create event recorder
|
||||||
hostname, err := utilnode.GetHostname(config.HostnameOverride)
|
hostname, err := nodeutil.GetHostname(config.HostnameOverride)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import (
|
|||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
|
nodeutil "k8s.io/component-helpers/node/util"
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/images"
|
"k8s.io/kubernetes/cmd/kubeadm/app/images"
|
||||||
@ -45,7 +46,7 @@ type kubeletFlagsOpts struct {
|
|||||||
// - "hostname-override" flag in NodeRegistrationOptions.KubeletExtraArgs
|
// - "hostname-override" flag in NodeRegistrationOptions.KubeletExtraArgs
|
||||||
// It also returns the hostname or an error if getting the hostname failed.
|
// It also returns the hostname or an error if getting the hostname failed.
|
||||||
func GetNodeNameAndHostname(cfg *kubeadmapi.NodeRegistrationOptions) (string, string, error) {
|
func GetNodeNameAndHostname(cfg *kubeadmapi.NodeRegistrationOptions) (string, string, error) {
|
||||||
hostname, err := kubeadmutil.GetHostname("")
|
hostname, err := nodeutil.GetHostname("")
|
||||||
nodeName := hostname
|
nodeName := hostname
|
||||||
if cfg.Name != "" {
|
if cfg.Name != "" {
|
||||||
nodeName = cfg.Name
|
nodeName = cfg.Name
|
||||||
|
@ -30,6 +30,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
netutil "k8s.io/apimachinery/pkg/util/net"
|
netutil "k8s.io/apimachinery/pkg/util/net"
|
||||||
bootstraputil "k8s.io/cluster-bootstrap/token/util"
|
bootstraputil "k8s.io/cluster-bootstrap/token/util"
|
||||||
|
nodeutil "k8s.io/component-helpers/node/util"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
|
|
||||||
@ -98,7 +99,7 @@ func SetBootstrapTokensDynamicDefaults(cfg *[]bootstraptokenv1.BootstrapToken) e
|
|||||||
// SetNodeRegistrationDynamicDefaults checks and sets configuration values for the NodeRegistration object
|
// SetNodeRegistrationDynamicDefaults checks and sets configuration values for the NodeRegistration object
|
||||||
func SetNodeRegistrationDynamicDefaults(cfg *kubeadmapi.NodeRegistrationOptions, controlPlaneTaint bool) error {
|
func SetNodeRegistrationDynamicDefaults(cfg *kubeadmapi.NodeRegistrationOptions, controlPlaneTaint bool) error {
|
||||||
var err error
|
var err error
|
||||||
cfg.Name, err = kubeadmutil.GetHostname(cfg.Name)
|
cfg.Name, err = nodeutil.GetHostname(cfg.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,7 @@ import (
|
|||||||
tracing "k8s.io/component-base/tracing"
|
tracing "k8s.io/component-base/tracing"
|
||||||
"k8s.io/component-base/version"
|
"k8s.io/component-base/version"
|
||||||
"k8s.io/component-base/version/verflag"
|
"k8s.io/component-base/version/verflag"
|
||||||
|
nodeutil "k8s.io/component-helpers/node/util"
|
||||||
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
|
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
|
||||||
"k8s.io/kubernetes/cmd/kubelet/app/options"
|
"k8s.io/kubernetes/cmd/kubelet/app/options"
|
||||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||||
@ -102,7 +103,6 @@ import (
|
|||||||
kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
|
kubeletutil "k8s.io/kubernetes/pkg/kubelet/util"
|
||||||
utilfs "k8s.io/kubernetes/pkg/util/filesystem"
|
utilfs "k8s.io/kubernetes/pkg/util/filesystem"
|
||||||
"k8s.io/kubernetes/pkg/util/flock"
|
"k8s.io/kubernetes/pkg/util/flock"
|
||||||
nodeutil "k8s.io/kubernetes/pkg/util/node"
|
|
||||||
"k8s.io/kubernetes/pkg/util/oom"
|
"k8s.io/kubernetes/pkg/util/oom"
|
||||||
"k8s.io/kubernetes/pkg/util/rlimit"
|
"k8s.io/kubernetes/pkg/util/rlimit"
|
||||||
"k8s.io/kubernetes/pkg/volume/util/hostutil"
|
"k8s.io/kubernetes/pkg/volume/util/hostutil"
|
||||||
|
@ -20,8 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
@ -42,26 +40,6 @@ const (
|
|||||||
NodeUnreachablePodMessage = "Node %v which was running pod %v is unresponsive"
|
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.
|
// NoMatchError is a typed implementation of the error interface. It indicates a failure to get a matching Node.
|
||||||
type NoMatchError struct {
|
type NoMatchError struct {
|
||||||
addresses []v1.NodeAddress
|
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) {
|
func TestIsNodeReady(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
@ -42,7 +42,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/errors"
|
"k8s.io/apimachinery/pkg/util/errors"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
volumehelpers "k8s.io/cloud-provider/volume/helpers"
|
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"
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
volutil "k8s.io/kubernetes/pkg/volume/util"
|
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.
|
// Construct lock id using host name and a magic prefix.
|
||||||
hostName, err := node.GetHostname("")
|
hostName, err := nodeutil.GetHostname("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -17,20 +17,20 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetHostname returns OS's hostname if 'hostnameOverride' is empty; otherwise, return 'hostnameOverride'
|
// GetHostname returns OS's hostname if 'hostnameOverride' is empty; otherwise, it returns
|
||||||
// NOTE: This function copied from pkg/util/node package to avoid external kubeadm dependency
|
// 'hostnameOverride'. In either case, the value is canonicalized (trimmed and
|
||||||
|
// lowercased).
|
||||||
func GetHostname(hostnameOverride string) (string, error) {
|
func GetHostname(hostnameOverride string) (string, error) {
|
||||||
hostName := hostnameOverride
|
hostName := hostnameOverride
|
||||||
if len(hostName) == 0 {
|
if len(hostName) == 0 {
|
||||||
nodeName, err := os.Hostname()
|
nodeName, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "couldn't determine hostname")
|
return "", fmt.Errorf("couldn't determine hostname: %w", err)
|
||||||
}
|
}
|
||||||
hostName = nodeName
|
hostName = nodeName
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func GetHostname(hostnameOverride string) (string, error) {
|
|||||||
// For linux, the hostname is read from file /proc/sys/kernel/hostname directly
|
// For linux, the hostname is read from file /proc/sys/kernel/hostname directly
|
||||||
hostName = strings.TrimSpace(hostName)
|
hostName = strings.TrimSpace(hostName)
|
||||||
if len(hostName) == 0 {
|
if len(hostName) == 0 {
|
||||||
return "", errors.New("empty hostname is invalid")
|
return "", fmt.Errorf("empty hostname is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToLower(hostName), nil
|
return strings.ToLower(hostName), nil
|
@ -17,11 +17,10 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetHostname(t *testing.T) {
|
func TestGetHostname(t *testing.T) {
|
||||||
@ -55,7 +54,7 @@ func TestGetHostname(t *testing.T) {
|
|||||||
desc: "hostname contains only spaces",
|
desc: "hostname contains only spaces",
|
||||||
hostname: " ",
|
hostname: " ",
|
||||||
result: "",
|
result: "",
|
||||||
expectedErr: errors.New("empty hostname is invalid"),
|
expectedErr: fmt.Errorf("empty hostname is invalid"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "empty parameter",
|
desc: "empty parameter",
|
Loading…
Reference in New Issue
Block a user