github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/gce/gce.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gce
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"time"
    23  
    24  	"cloud.google.com/go/compute/metadata"
    25  	"github.com/golang/glog"
    26  )
    27  
    28  const (
    29  	waitForGCEInterval = 5 * time.Second
    30  	waitForGCETimeout  = 3 * time.Minute
    31  	gcpCredentialsEnv  = "GOOGLE_APPLICATION_CREDENTIALS"
    32  	gcpProjectIdEnv    = "GOOGLE_PROJECT_ID"
    33  )
    34  
    35  func EnsureOnGCE() error {
    36  	for start := time.Now(); time.Since(start) < waitForGCETimeout; time.Sleep(waitForGCEInterval) {
    37  		glog.Infof("Waiting for GCE metadata to be available")
    38  		if metadata.OnGCE() {
    39  			return nil
    40  		}
    41  	}
    42  	return fmt.Errorf("not running on GCE")
    43  }
    44  
    45  func GetProjectId() (string, error) {
    46  	// Try the environment variable first.
    47  	if projectId, err := getProjectIdFromEnv(); err != nil {
    48  		glog.V(4).Infof("Unable to get GCP project ID from environment variable: %v", err)
    49  	} else {
    50  		return projectId, nil
    51  	}
    52  
    53  	// Try the default credentials file.
    54  	if projectId, err := getProjectIdFromFile(); err != nil {
    55  		glog.V(4).Infof("Unable to get GCP project ID from default credentials file: %v", err)
    56  	} else {
    57  		return projectId, nil
    58  	}
    59  
    60  	// Finally, fallback on the metadata service.
    61  	projectId, err := getProjectIdFromMeta()
    62  	if err != nil {
    63  		return "", fmt.Errorf("unable to get GCP project ID: %v", err)
    64  	}
    65  	return projectId, nil
    66  }
    67  
    68  func getProjectIdFromEnv() (string, error) {
    69  	projectId, set := os.LookupEnv(gcpProjectIdEnv)
    70  	if set != true {
    71  		return "", fmt.Errorf("environment variable %s not found", gcpProjectIdEnv)
    72  	}
    73  	return projectId, nil
    74  }
    75  
    76  func getProjectIdFromFile() (string, error) {
    77  	file, set := os.LookupEnv(gcpCredentialsEnv)
    78  	if set != true {
    79  		return "", fmt.Errorf("environment variable %s not found", gcpCredentialsEnv)
    80  	}
    81  	conf, err := ioutil.ReadFile(file)
    82  	if err != nil {
    83  		return "", err
    84  	}
    85  	var gcpConfig struct {
    86  		ProjectId *string `json:"project_id"`
    87  	}
    88  	err = json.Unmarshal(conf, &gcpConfig)
    89  	if err != nil {
    90  		return "", err
    91  	}
    92  	if gcpConfig.ProjectId == nil {
    93  		return "", fmt.Errorf("field project_id not found")
    94  	}
    95  	return *gcpConfig.ProjectId, nil
    96  }
    97  
    98  func getProjectIdFromMeta() (string, error) {
    99  	if err := EnsureOnGCE(); err != nil {
   100  		return "", err
   101  	}
   102  	projectId, err := metadata.ProjectID()
   103  	if err != nil {
   104  		return "", err
   105  	}
   106  	return projectId, nil
   107  }