mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #33123 from kokhang/node-ip-cloud-provider
Automatic merge from submit-queue Node-ip is not used when cloud provider is used Currently --node-ip in kubelet is not being used when kubelet is configured with a cloud provider. With this fix, kubelet will get a list of IPs from the provider and parse it to return the one that matches node-ip. This fixes #23568
This commit is contained in:
commit
906cb1ce70
@ -462,12 +462,6 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub
|
||||
klet.flannelHelper = NewFlannelHelper()
|
||||
glog.Infof("Flannel is in charge of podCIDR and overlay networking.")
|
||||
}
|
||||
if klet.nodeIP != nil {
|
||||
if err := klet.validateNodeIP(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
glog.Infof("Using node IP: %q", klet.nodeIP.String())
|
||||
}
|
||||
|
||||
if mode, err := effectiveHairpinMode(componentconfig.HairpinMode(kubeCfg.HairpinMode), kubeCfg.ContainerRuntime, kubeCfg.ConfigureCBR0, kubeCfg.NetworkPluginName); err != nil {
|
||||
// This is a non-recoverable error. Returning it up the callstack will just
|
||||
|
@ -81,39 +81,6 @@ func effectiveHairpinMode(hairpinMode componentconfig.HairpinMode, containerRunt
|
||||
return hairpinMode, nil
|
||||
}
|
||||
|
||||
// Validate given node IP belongs to the current host
|
||||
func (kl *Kubelet) validateNodeIP() error {
|
||||
if kl.nodeIP == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Honor IP limitations set in setNodeStatus()
|
||||
if kl.nodeIP.IsLoopback() {
|
||||
return fmt.Errorf("nodeIP can't be loopback address")
|
||||
}
|
||||
if kl.nodeIP.To4() == nil {
|
||||
return fmt.Errorf("nodeIP must be IPv4 address")
|
||||
}
|
||||
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if ip != nil && ip.Equal(kl.nodeIP) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Node IP: %q not found in the host's network interfaces", kl.nodeIP.String())
|
||||
}
|
||||
|
||||
// providerRequiresNetworkingConfiguration returns whether the cloud provider
|
||||
// requires special networking configuration.
|
||||
func (kl *Kubelet) providerRequiresNetworkingConfiguration() bool {
|
||||
|
@ -359,6 +359,14 @@ func (kl *Kubelet) recordNodeStatusEvent(eventtype, event string) {
|
||||
|
||||
// Set IP addresses for the node.
|
||||
func (kl *Kubelet) setNodeAddress(node *api.Node) error {
|
||||
|
||||
if kl.nodeIP != nil {
|
||||
if err := kl.validateNodeIP(); err != nil {
|
||||
return fmt.Errorf("failed to validate nodeIP: %v", err)
|
||||
}
|
||||
glog.V(2).Infof("Using node IP: %q", kl.nodeIP.String())
|
||||
}
|
||||
|
||||
if kl.cloud != nil {
|
||||
instances, ok := kl.cloud.Instances()
|
||||
if !ok {
|
||||
@ -372,6 +380,19 @@ func (kl *Kubelet) setNodeAddress(node *api.Node) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get node address from cloud provider: %v", err)
|
||||
}
|
||||
|
||||
if kl.nodeIP != nil {
|
||||
for _, nodeAddress := range nodeAddresses {
|
||||
if nodeAddress.Address == kl.nodeIP.String() {
|
||||
node.Status.Addresses = []api.NodeAddress{
|
||||
{Type: nodeAddress.Type, Address: nodeAddress.Address},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("failed to get node address from cloud provider that matches ip: %v", kl.nodeIP)
|
||||
}
|
||||
|
||||
node.Status.Addresses = nodeAddresses
|
||||
} else {
|
||||
var ipAddr net.IP
|
||||
@ -848,3 +869,36 @@ func SetNodeStatus(f func(*api.Node) error) Option {
|
||||
k.setNodeStatusFuncs = append(k.setNodeStatusFuncs, f)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate given node IP belongs to the current host
|
||||
func (kl *Kubelet) validateNodeIP() error {
|
||||
if kl.nodeIP == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Honor IP limitations set in setNodeStatus()
|
||||
if kl.nodeIP.IsLoopback() {
|
||||
return fmt.Errorf("nodeIP can't be loopback address")
|
||||
}
|
||||
if kl.nodeIP.To4() == nil {
|
||||
return fmt.Errorf("nodeIP must be IPv4 address")
|
||||
}
|
||||
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if ip != nil && ip.Equal(kl.nodeIP) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Node IP: %q not found in the host's network interfaces", kl.nodeIP.String())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user