mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Change NodeAddresses function to work if it runs outside kubelet
This commit is contained in:
parent
3fc7831cd8
commit
dd1f029cd9
@ -83,7 +83,16 @@ func (g *Cloud) ToInstanceReferences(zone string, instanceNames []string) (refs
|
||||
}
|
||||
|
||||
// NodeAddresses is an implementation of Instances.NodeAddresses.
|
||||
func (g *Cloud) NodeAddresses(_ context.Context, _ types.NodeName) ([]v1.NodeAddress, error) {
|
||||
func (g *Cloud) NodeAddresses(ctx context.Context, nodeName types.NodeName) ([]v1.NodeAddress, error) {
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
|
||||
defer cancel()
|
||||
|
||||
instanceName := string(nodeName)
|
||||
|
||||
if g.useMetadataServer {
|
||||
// Use metadata server if possible
|
||||
if g.isCurrentInstance(instanceName) {
|
||||
|
||||
internalIP, err := metadata.Get("instance/network-interfaces/0/ip")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't get internal IP: %v", err)
|
||||
@ -107,6 +116,21 @@ func (g *Cloud) NodeAddresses(_ context.Context, _ types.NodeName) ([]v1.NodeAdd
|
||||
}
|
||||
return addresses, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Use GCE API
|
||||
instanceObj, err := g.getInstanceByName(instanceName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't get instance details: %v", err)
|
||||
}
|
||||
|
||||
instance, err := g.c.Instances().Get(timeoutCtx, meta.ZonalKey(canonicalizeInstanceName(instanceObj.Name), instanceObj.Zone))
|
||||
if err != nil {
|
||||
return []v1.NodeAddress{}, fmt.Errorf("error while querying for instance: %v", err)
|
||||
}
|
||||
|
||||
return nodeAddressesFromInstance(instance)
|
||||
}
|
||||
|
||||
// NodeAddressesByProviderID will not be called from the node that is requesting this ID.
|
||||
// i.e. metadata service and other local methods cannot be used here
|
||||
|
Loading…
Reference in New Issue
Block a user