common

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package common contains common components for the whole application such as context, logger, options, plugins, and meta client for cloud platforms.

Index

Constants

View Source
const (
	// UNKNOWN is a kind of "I don't know" from the perspective of a specific
	// driver. For example, GCE meta client can answer with UNKNOWN if they
	// could not detect as they are on GCE.
	UNKNOWN = "Unknown"
	// NOWHERE has different meaning. For example, if this keyword is used,
	// it should indicate that the environment is explicitly not supported.
	NOWHERE = "Nowhere"
)
View Source
const GOOGLE = "Google"

GOOGLE is the name of the platform.

Variables

View Source
var (
	// ErrInvalidePluginOption indicates that the given option name is unknown
	// from the perspective of the plugin. It could also indicate a typos.
	ErrInvalidePluginOption = errors.New("invalid plugin option")
	// ErrInvalidOptionValue indicates that the given option value is invalid.
	// It could used for the situation that numeric conversion is not possible.
	ErrInvalidOptionValue = errors.New("invalid option value")
)
View Source
var ErrNotOnGCE = errors.New("not on the Google Compute Engine")

ErrNotOnGCE indicates that the application is not running on a GCE instance.

Functions

func BuildPluginOptions

func BuildPluginOptions(s string) (map[string]PluginOptions, error)

BuildPluginOptions converts given string s into a map of PluginOptions. The string contains all options of all plugins at once. Option entries are separated by ';' and Each option entry consists of three parts with delimiter ':'. The first part is the name of plugin, the second is the name of the option, and the third is the value of the option. The value part is a comma or space separated list of values even if there is only one value (the plugins may understand them):

"plugin:opt1_key:opt1_value1,opt1_value2;plugin:opt2_key:opt2_value"

func Contains

func Contains(list []string, item string) bool

Contains checks if the given list has the given string item.

func StringValues

func StringValues(s string) []string

StringValues creates and returns a list of strings from the given string s. If the string contains any comma, commas will be used as delimiter and all leading/trailing spaces of each substring will be removed. When there is no comma on the string, (merged) white-spaces are used as a delimiter.

    "hey, bulldog " --> ["hey", "dog"]
	" oh  darling " --> ["oh", "darling"]

Types

type Context

type Context interface {
	context.Context
	Cancel()
	Channel() chan interface{}
	WG() *sync.WaitGroup
	Logger() Logger
	Meta() MetaClient
}

Context is the main application context for whole components This context will be used to control go routines.

func NewDefaultContext

func NewDefaultContext(opts *Options) (Context, context.CancelFunc)

NewDefaultContext creates a new DefaultContext with cancel function then returns it as Context.

type GCEClient

type GCEClient struct {
	*metadata.Client
	// contains filtered or unexported fields
}

GCEClient is a struct for handling GCE metadata client.

func (*GCEClient) AttributeCSV

func (m *GCEClient) AttributeCSV(s string) []string

AttributeCSV returns the metadata as an array of strings. The raw value will be treated as comma separated values. So if the raw metadata is "oh, little darling", the returned array will be consist of "oh" and "little darling".

func (*GCEClient) AttributeSSV

func (m *GCEClient) AttributeSSV(s string) []string

AttributeSSV returns the metadata as an array of strings. The raw value will be treated as space separated values. So if the raw metadata is "oh, little darling", the returned array will be consist of "oh,", "little", and "darling".

func (*GCEClient) AttributeValue

func (m *GCEClient) AttributeValue(key string) string

AttributeValue returns the raw metadata stored for the instance. It returns empty string if the metadata with the key exists but the value is empty. When the instance has no the metadata defined, it will returns the project's metadata.

func (*GCEClient) AttributeValues

func (m *GCEClient) AttributeValues(s string) []string

AttributeValues returns the metadata as an array of strings. The raw value will be treated as both comma and space separated values. So if the raw metadata is "oh, little darling", the returned array will be consist of "oh", "little", and "darling".

func (*GCEClient) ExternalIP

func (m *GCEClient) ExternalIP() string

ExternalIP implements MetaClient.

func (*GCEClient) InstanceName

func (m *GCEClient) InstanceName() string

InstanceName implements MetaClient.

func (*GCEClient) WhereAmI

func (m *GCEClient) WhereAmI() string

WhereAmI implements MetaClient.

func (*GCEClient) Zone

func (m *GCEClient) Zone() string

Zone implements MetaClient.

type Logger

type Logger interface {
	// basic logging functions
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
	Debug(...interface{})
	Info(...interface{})
	Warn(...interface{})
	Error(...interface{})
	Fatal(...interface{})
	Panic(...interface{})
	// extended functions
	WithField(string, interface{}) Logger
	WithFields(map[string]interface{}) Logger
}

Logger is an interface which supports logging with fields.

func NewDefaultLogger

func NewDefaultLogger(level string) Logger

NewDefaultLogger returns a new logrus based default logger.

type MetaClient

type MetaClient interface {
	// WhereAmI returns the name of CSP.
	WhereAmI() string
	// InstanceName returns the current VM's instance name string.
	InstanceName() string
	// ExternalIP returns the instance's primary external (public) IP address.
	ExternalIP() string
	// Zone returns the current VM's zone, such as "asia-northeast3-a".
	Zone() string
	// AttributeValues gets comma or space separated metadata values
	// It is a wrapper of AttributeValue(string)
	AttributeValues(key string) []string
	// AttributeValue gets bare string for the given metadata key. It will
	// check the metadata for the instance first and will return the value
	// if the metadata with the same key exists, otherwise it will check
	// the project's metadata with the same logic.
	AttributeValue(key string) string
	// AttributeSSV gets space separated values for the given metadata key
	// It is a wrapper of AttributeValue(string)
	AttributeSSV(key string) []string
	// AttributeCSV gets comma separated values for the given metadata key
	// It is a wrapper of AttributeValue(string)
	AttributeCSV(key string) []string
}

MetaClient is an interface to be implemented for each cloud provider.

func NewGCEMetaClient

func NewGCEMetaClient(c Context) MetaClient

NewGCEMetaClient tests if the application is running on a GCE instance, then returns the `GCEClient` as `MetaClient`.

type Options

type Options struct {
	IsDebug  bool
	LogLevel string
	// Checkers constains selected checkers. All available checkers will be
	// selected if this field is empty.
	Checkers []string
	// Exporters contains selected exporters. All available exporters will be
	// selected if this field is empty.
	Exporters []string

	// Address is a listen address for embedded webserver
	Address string

	CheckerOptions  map[string]PluginOptions
	ExporterOptions map[string]PluginOptions
}

Options is a struct to store command line options.

func DefaultOptions added in v0.3.2

func DefaultOptions() Options

DefaultOptions returns Option structure with the default values.

type Plugin added in v0.3.2

type Plugin interface {
	// Name returns the name of the plugin.
	Name() string
	// Run starts the plugin's runner in a goroutine and returns the status of
	// execution. The runner will run forever until the given context is
	// canceled.
	Run(Context, PluginOptions, chan interface{}) error
}

Plugin is an interface for all bogo plugins.

func Plugins added in v0.3.2

func Plugins(t reflect.Type) []Plugin

Plugins returns the plugin list for the given type. All register function of the type which is prefixed with "Register" will executed during the registration.

type PluginOptions

type PluginOptions map[string][]string

PluginOptions is used for CheckerOptions and ExporterOptions.

func (*PluginOptions) GetIntegerOr added in v0.3.2

func (o *PluginOptions) GetIntegerOr(key string, def int) (int, error)

GetIntegerOr returns the option value for the given key from the options as int. If no option value found, it will returns the def value with nil error.

Note that if the found value is not properly converted to integer, it will returns the default value with error.

func (*PluginOptions) GetValueOr added in v0.3.2

func (o *PluginOptions) GetValueOr(key, def string) string

GetValueOr returns the option value for the given key from the options as string. If no option value found, it will returns the def string.

Note that it just returns the first value if the option values are more than one.

func (*PluginOptions) GetValuesOr added in v0.3.2

func (o *PluginOptions) GetValuesOr(key string, def []string) []string

GetValuesOr returns the option values for the given key from the options. If no option values found, it will returns the def value.

type Runner

type Runner func(Context, PluginOptions, chan interface{}) error

Runner is a function type for plugable checkers and exporters.

Jump to

Keyboard shortcuts

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