gcpenv

package module
v0.0.0-...-bed434f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2020 License: Apache-2.0 Imports: 22 Imported by: 1

README

GCPEnv

This is a small library for making it easy to figure out what GCP product you are running on.

  • App Engine
  • Compute Engine
  • Kubernetes Engine
  • Cloud Functions
  • Cloud Run

It uses a combo of the GCP metadata apis and environmental variables to accomplish what it needs to do.

One editorial decision, I haven't figured out a way to suss out running the difference between running Kubernetes on Compute Engine manually, from being on Kubernetes Engine. But if you are that much of a masocist I am not really here to help you.

Endpoints

Additionally, I try and get the "endpoint" for each environment. By endpoint, I mean the URI where the service answers. For products like AppEngine, Cloud Run, and Cloud Functions this is pretty straight forward.

For Compute Engine and Kubernetes, this is a little more complicated. For both I do the best guess of the external IP at which the service is available. I'm open to changing it and accepting PRs. This works for me, on the project I needed it.

This is not an official Google product.

Documentation

Overview

Package gcpenv allows processes on GCP to do some introspection about what service they are running on.

Index

Constants

View Source
const (
	CodeNot  = "not_gcp"
	CodeGCE  = "compute_engine"
	CodeGCF  = "cloud_functions"
	CodeRun  = "cloud_run"
	CodeGAE  = "app_engine"
	CodeGKE  = "kubernetes_engine"
	LabelNot = "Not GCP"
	LabelGCE = "Compute Engine"
	LabelGCF = "Cloud Functions"
	LabelRun = "Cloud Run"
	LabelGAE = "App Engine"
	LabelGKE = "Kubernetes Engine"
)

Codes and strings for comparison in consuming apps

Variables

This section is empty.

Functions

func GetVM

func GetVM(project, instance string) (*compute.Instance, error)

GetVM gets a Instance from the GCP SDK.

func IsAppEngine

func IsAppEngine() bool

IsAppEngine checks whether or not this process is running on App Engine

func IsCloudFunction

func IsCloudFunction() bool

IsCloudFunction checks whether or not this process is running on Cloud Functions

func IsCloudRun

func IsCloudRun() bool

IsCloudRun checks whether or not this process is running on Cloud Run

func IsComputeEngine

func IsComputeEngine() bool

IsComputeEngine checks whether or not this process is running on Compute Engine

func IsDocker

func IsDocker() bool

IsDocker checks whether or not this process is running on Docker

func IsKubernetes

func IsKubernetes() bool

IsKubernetes checks whether or not this process is running on Kubernetes

Types

type Report

type Report struct {
	Project struct {
		ID     string `json:"id"`
		Zone   string `json:"zone"`
		Region string `json:"region"`
	} `json:"project"`

	Environment struct {
		Code  string `json:"code"`
		Label string `json:"label"`
	} `json:"environment"`
	Products struct {
		AppEngine      bool `json:"appengine"`
		ComputeEngine  bool `json:"computeengine"`
		CloudRun       bool `json:"cloudrun"`
		CloudFunctions bool `json:"cloudfunctions"`
	} `json:"products"`
	Environments struct {
		Kubernetes bool `json:"kubernetes"`
		Docker     bool `json:"docker"`
	} `json:"environments"`
	Endpoint string `json:"endpoint"`
}

Report is a set of products and environments for a given environment in GCP.

func NewGCPEnv

func NewGCPEnv(j []byte) (Report, error)

NewGCPEnv returns an environment report that contains a lot of helpful info.

func (*Report) Do

func (r *Report) Do() error

Do queries the environment and populates the report to answer the question what GCP environment is this process running in.

func (Report) JSON

func (r Report) JSON() (string, error)

JSON Returns the given report struct as a JSON string

type VM

type VM struct {
	Name                  string
	Region                string
	Zone                  string
	ManagedGroup          string
	IPAddress             string
	ManagedGroupIPAddress string
}

VM is a collection of information that helps us know about the environment

func NewVM

func NewVM(in *compute.Instance) (VM, error)

NewVM takes a GCP SDK VM instance and returns a VM for this code's purposes

func (*VM) Managed

func (v *VM) Managed() bool

Managed is a shortcut to knowing if the VM is in a managed group.

Directories

Path Synopsis
examples
gcf Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL