mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Use local metadata server, if available, for GCE compute API invocations.
Signed-off-by: Vishnu kannan <vishnuk@google.com>
This commit is contained in:
parent
661bfd964f
commit
85efe33c16
@ -153,6 +153,19 @@ func getCurrentExternalIDViaMetadata() (string, error) {
|
|||||||
return externalID, nil
|
return externalID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCurrentMachineTypeViaMetadata() (string, error) {
|
||||||
|
mType, err := metadata.Get("instance/machine-type")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("couldn't get machine type: %v", err)
|
||||||
|
}
|
||||||
|
parts := strings.Split(mType, "/")
|
||||||
|
if len(parts) != 4 {
|
||||||
|
return "", fmt.Errorf("unexpected response for machine type: %s", mType)
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts[3], nil
|
||||||
|
}
|
||||||
|
|
||||||
func getNetworkNameViaMetadata() (string, error) {
|
func getNetworkNameViaMetadata() (string, error) {
|
||||||
result, err := metadata.Get("instance/network-interfaces/0/network")
|
result, err := metadata.Get("instance/network-interfaces/0/network")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1825,6 +1838,15 @@ func (gce *GCECloud) ExternalID(instance string) (string, error) {
|
|||||||
|
|
||||||
// InstanceID returns the cloud provider ID of the specified instance.
|
// InstanceID returns the cloud provider ID of the specified instance.
|
||||||
func (gce *GCECloud) InstanceID(instanceName string) (string, error) {
|
func (gce *GCECloud) InstanceID(instanceName string) (string, error) {
|
||||||
|
if gce.useMetadataServer {
|
||||||
|
// Use metadata, if possible, to fetch ID. See issue #12000
|
||||||
|
if gce.isCurrentInstance(instanceName) {
|
||||||
|
projectID, zone, err := getProjectAndZone()
|
||||||
|
if err == nil {
|
||||||
|
return projectID + "/" + zone + "/" + canonicalizeInstanceName(instanceName), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
instance, err := gce.getInstanceByName(instanceName)
|
instance, err := gce.getInstanceByName(instanceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -1834,6 +1856,15 @@ func (gce *GCECloud) InstanceID(instanceName string) (string, error) {
|
|||||||
|
|
||||||
// InstanceType returns the type of the specified instance.
|
// InstanceType returns the type of the specified instance.
|
||||||
func (gce *GCECloud) InstanceType(instanceName string) (string, error) {
|
func (gce *GCECloud) InstanceType(instanceName string) (string, error) {
|
||||||
|
if gce.useMetadataServer {
|
||||||
|
// Use metadata, if possible, to fetch ID. See issue #12000
|
||||||
|
if gce.isCurrentInstance(instanceName) {
|
||||||
|
mType, err := getCurrentMachineTypeViaMetadata()
|
||||||
|
if err == nil {
|
||||||
|
return mType, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
instance, err := gce.getInstanceByName(instanceName)
|
instance, err := gce.getInstanceByName(instanceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
Loading…
Reference in New Issue
Block a user