discovery

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 2 Imported by: 0

README

discovery

Go Report Card Documentation license

Border0 service discovery framework and library.

Example: Discover EC2, ECS, and RDS Resources

Assume that the following variables are defined as follows:

ctx := context.Background()

cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
	// handle error
}

Then,

// initialize a new one off engine
engine := engines.NewOneOffEngine(
	engines.OneOffEngineOptionWithDiscoverers(
		discoverers.NewAwsEc2Discoverer(cfg),
		discoverers.NewAwsEcsDiscoverer(cfg),
		discoverers.NewAwsRdsDiscoverer(cfg),
		// ... LAN, docker, k8s, gcp compute, azure vms, etc ...
	),
)

// create channels for discovery results
results := make(chan *discovery.Result, 10)

// run engine
go engine.Run(ctx, results)

// process results as they come in
for result := range results {
	// ... do something ...
}
Example: Continuously Discover EC2, ECS, and RDS Resources

Assume that the following variables are defined as follows:

Assume that ctx (type context.Context) is defined by some upstream code

cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
	// handle error
}

Then,

// initialize a new continuous engine
engine := engines.NewContinuousEngine(
	engines.ContinuousEngineOptionWithDiscoverer(
		discoverers.NewAwsEc2Discoverer(cfg),
		time.Minute*1,
	),
	engines.ContinuousEngineOptionWithDiscoverer(
		discoverers.NewAwsEcsDiscoverer(cfg),
		time.Minute*10,
	),
	engines.ContinuousEngineOptionWithDiscoverer(
		discoverers.NewAwsRdsDiscoverer(cfg),
		time.Minute*15,
	),
)

// create channels for discovery results
results := make(chan *discovery.Result, 10)

// run engine
go engine.Run(ctx, results)

// process results as they come in
for result := range results {
	// ... do something ...
}
Example: Discover EC2 Instances In Multiple AWS Regions

Assume that the following variables are defined as follows:

awsRegions := []string{"us-east-1", "us-east-2", "us-west-2", "eu-west-1"}

ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
	// handle error
}

Then,

// define an ec2 discoverer for each region
ds := []discovery.Discoverer{}
for _, region := range regions {
	cfg.Region = region

	ds = append(ds, discoverers.NewAwsEc2Discoverer(cfg))
}

// initialize a new one off engine with the discoverers
engine := engines.NewOneOffEngine(
	engines.OneOffEngineOptionWithDiscoverers(ds...),
)

// create channels for discovery results
results := make(chan *discovery.Result, 10)

// run engine
go engine.Run(ctx, results)

// process results as they come in
for result := range results {
	// ... do something ...
}

Documentation

Index

Constants

View Source
const (
	// ResourceTypeAwsEc2Instance is the resource type for AWS EC2 instances.
	ResourceTypeAwsEc2Instance = "aws_ec2_instance"

	// ResourceTypeAwsEcsCluster is the resource type for AWS ECS clusters.
	ResourceTypeAwsEcsCluster = "aws_ecs_cluster"

	// ResourceTypeAwsRdsInstnace is the resource type for AWS RDS instances.
	ResourceTypeAwsRdsInstance = "aws_rds_instance"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AwsBaseDetails

type AwsBaseDetails struct {
	AwsAccountId string `json:"aws_account_id"`
	AwsRegion    string `json:"aws_region"`
	AwsArn       string `json:"aws_arn"`
}

AwsBaseDetails represents the details of a discovered generic AWS resource.

type AwsEc2InstanceDetails

type AwsEc2InstanceDetails struct {
	AwsBaseDetails // extends

	Tags map[string]string `json:"tags"`

	InstanceId       string `json:"instance_id"`
	ImageId          string `json:"ami_id"`
	VpcId            string `json:"vpc_id"`
	SubnetId         string `json:"subnet_id"`
	PrivateDnsName   string `json:"private_dns_name"`
	PrivateIpAddress string `json:"private_ip_address"`
	PublicDnsName    string `json:"public_dns_name"`
	PublicIpAddress  string `json:"public_ip_address"`
	InstanceType     string `json:"instance_type"`
}

AwsEc2InstanceDetails represents the details of a discovered AWS EC2 instance.

type AwsEcsClusterDetails

type AwsEcsClusterDetails struct {
	AwsBaseDetails // extends

	Tags map[string]string `json:"tags"`

	ClusterName string   `json:"cluster_name"`
	Services    []string `json:"services"`
	Tasks       []string `json:"tasks"`
	Containers  []string `json:"containers"`
}

AwsEcsClusterDetails represents the details of a discovered AWS ECS cluster.

type AwsRdsInstanceDetails

type AwsRdsInstanceDetails struct {
	AwsBaseDetails // extends

	Tags map[string]string `json:"tags"`

	DBInstanceIdentifier string `json:"db_instance_identifier"`
	Engine               string `json:"engine"`
	EngineVersion        string `json:"engine_version"`
	VpcId                string `json:"vpc_id"`
	DBSubnetGroupName    string `json:"db_subnet_group_name"`
	EndpointAddress      string `json:"endpoint_address"`
	EndpointPort         int32  `json:"endpoint_port"`
}

AwsRdsInstanceDetails represents the details of a discovered AWS RDS instance.

type Discoverer

type Discoverer interface {
	Discover(context.Context, chan<- *Result)
}

Discoverer represents an entity capable of discovering resources.

An Discoverer has three responsibilities: - Write zero or more results to the channel - Close the channel as soon as they are done with it - Exit gracefully upon the context being cancelled/done

type Engine added in v0.1.0

type Engine interface {
	Run(context.Context, chan<- *Result)
}

Engine represents an entity capable of managing discovery jobs.

An Engine has three responsibilities: - Write zero or more results to the channel - Close the channel as soon as they are done with it - Exit gracefully upon the context being done

type Metrics added in v0.1.0

type Metrics struct {
	StartedAt time.Time `json:"started_at"`
	EndedAt   time.Time `json:"ended_at"`
}

Metrics represents stats/metrics for a

type Resource

type Resource struct {
	ResourceType string `json:"resource_type"`

	AwsEc2InstanceDetails *AwsEc2InstanceDetails `json:"aws_ec2_instance_details,omitempty"`
	AwsEcsClusterDetails  *AwsEcsClusterDetails  `json:"aws_ecs_cluster_details,omitempty"`
	AwsRdsInstanceDetails *AwsRdsInstanceDetails `json:"aws_rds_instance_details,omitempty"`
}

Resource represents a generic discovered resource.

type Result added in v0.1.0

type Result struct {
	Resources []Resource `json:"resources"`
	Errors    []string   `json:"errors"`
	Metrics   Metrics    `json:"metrics"`
}

Result represents the result of a discoverer

func NewResult added in v0.1.0

func NewResult() *Result

NewResult returns a new Result object with the StartedAt time set to the current time.

func (*Result) Done added in v0.1.0

func (r *Result) Done()

Done sets the EndedAt time in a Result to the current time.

Directories

Path Synopsis
__examples__
aws_continuous command
aws_multiregion command
aws_oneoff command

Jump to

Keyboard shortcuts

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