cloudguard

package
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package cloudguard is the one gate every AWS create goes through: caps, allowlists, kill switch and mandatory tags. It never deletes anything.

Index

Constants

View Source
const (
	CodeKillSwitch      = "kill_switch"
	CodeServiceDisabled = "service_disabled"
	CodePerCreateMax    = "per_create_max"
	CodeMonthlyCap      = "monthly_cap"
	CodeSpendUnknown    = "spend_unknown"
	CodeInstanceType    = "instance_type_not_allowed"
	CodeTagsMissing     = "tags_missing"
	CodeAccountMissing  = "account_missing"
)

Refusal codes.

View Source
const (
	KindEC2LowCPU     = "ec2_low_cpu"
	KindEBSUnattached = "ebs_unattached"
	KindEIPFree       = "eip_unassociated"
	KindALBNoRequests = "alb_no_requests"
	KindRDSStopped    = "rds_stopped"
	KindRDSRestart    = "rds_cluster_restart"

	ActionStop           = "stop"
	ActionSnapshotDelete = "snapshot_delete"
	ActionRelease        = "release"
	ActionDelete         = "delete"
	ActionRestop         = "restop"
)

Idle suggestion kinds and actions.

Variables

View Source
var ErrRefused = errors.New("cloudguard: create refused")

ErrRefused wraps every guard refusal of an EC2 create.

Functions

func InjectTags

func InjectTags(cfg Config, base map[string]string, m TagMeta) map[string]string

InjectTags returns base plus the mandatory tags. Mandatory keys always win over caller values so a caller cannot drop or forge them.

func RunInstance

func RunInstance(ctx context.Context, c *awsx.Client, g *Guard, in InstanceInput) (string, error)

RunInstance creates one EC2 instance. The guard decides first and the mandatory tags go on the RunInstances call itself, so the instance is never untagged.

func TagKey

func TagKey(cfg Config, suffix string) string

TagKey returns the full key for a suffix such as "expires".

Types

type CacheSpend

type CacheSpend struct {
	Cache *cloudspend.Cache
	// Fetch builds the provider fetch for one account.
	Fetch func(account string) cloudspend.Fetch
}

CacheSpend reads month to date USD from the shared spend cache.

func (CacheSpend) MonthToDateUSD

func (c CacheSpend) MonthToDateUSD(ctx context.Context, account string) (float64, error)

MonthToDateUSD implements SpendSource.

type Config

type Config struct {
	KillSwitch bool
	// MonthlyCapUSD is the default cap per account; AccountCaps overrides it.
	MonthlyCapUSD   float64
	AccountCaps     map[string]float64
	PerCreateMaxUSD float64
	// InstanceTypes is the allowlist for creates that carry an instance type.
	InstanceTypes   []string
	ServicesEnabled map[string]bool
	// DefaultTTL feeds the expires tag; zero omits the tag.
	DefaultTTL time.Duration
	// TagPrefix defaults to the prefix of cloudspend.ProjectTagKey.
	TagPrefix string
	Now       func() time.Time
}

Config holds the live guard settings. Zero values mean no limit, except ServicesEnabled and InstanceTypes, which fail closed.

type CreateRequest

type CreateRequest struct {
	Account             string
	Service             string
	Resource            string
	InstanceType        string
	EstimatedMonthlyUSD float64
	Tags                map[string]string
}

CreateRequest describes one create before it reaches AWS.

type Decision

type Decision struct {
	Allowed bool
	Code    string
	Message string
}

Decision is the guard answer. Message is fixed text per Code.

type Expired

type Expired struct {
	ARN       string
	ExpiredAt time.Time
}

Expired is one managed resource past its expires tag.

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard evaluates creates against the live Config.

func New

func New(cfg func() Config, spend SpendSource) *Guard

New builds a guard. spend may be nil, which makes a set cap fail closed.

func (*Guard) Authorize

func (g *Guard) Authorize(ctx context.Context, in InstanceInput) (map[string]string, Decision)

Authorize runs the guard for one EC2 create and returns the full tag set to apply. A refusal carries the guard code and message so callers can answer with a clear 4xx.

func (*Guard) Check

func (g *Guard) Check(ctx context.Context, r CreateRequest) Decision

Check allows or refuses one create. The first failing rule wins.

func (*Guard) Tags

func (g *Guard) Tags(m TagMeta) map[string]string

Tags returns the mandatory tag set for a new resource, with expires from the configured TTL.

type IdleConfig

type IdleConfig struct {
	CPUPercent    float64
	Days          int
	EBSGBMonthUSD float64
	EIPMonthUSD   float64
	ALBMonthUSD   float64
	Now           func() time.Time
}

IdleConfig carries the live idle thresholds and the unit prices behind the savings.

type InstanceInput

type InstanceInput struct {
	Account, Region, AMI, Type, Name, SubnetID string
	EstimatedMonthlyUSD                        float64
	Meta                                       TagMeta
}

InstanceInput is one EC2 create request.

type Orphan

type Orphan struct {
	ID     string
	Detail string
}

Orphan is one resource that looks forgotten.

type OrphanReport

type OrphanReport struct {
	DryRun    bool
	Volumes   []Orphan
	Addresses []Orphan
	Snapshots []Orphan
}

OrphanReport lists candidates only. Nothing here is ever deleted.

func FindOrphans

func FindOrphans(ctx context.Context, c *awsx.Client, region string, maxPages int) (OrphanReport, error)

FindOrphans reads unattached volumes, unassociated Elastic IPs and snapshots whose source volume is gone. The status-available filter and the self owner filter follow the EC2 DescribeVolumes and DescribeSnapshots docs.

type Plan

type Plan struct {
	DryRun bool
	Items  []Expired
	// Unparsed lists managed ARNs whose expires tag was not a valid time.
	Unparsed []string
}

Plan is what a sweep would delete. It is always a dry run.

func PlanExpired

func PlanExpired(ctx context.Context, c *awsx.Client, cfg Config, region string, maxPages int) (Plan, error)

PlanExpired lists managed resources whose expires tag is before now. The Tagging API cannot filter by date, so the comparison happens here.

type SpendSource

type SpendSource interface {
	MonthToDateUSD(ctx context.Context, account string) (float64, error)
}

SpendSource returns month to date USD spend for an account.

type Suggestion

type Suggestion struct {
	Kind, ID, Detail, Action string
	// MonthlyUSD is the estimated saving, zero when no estimate exists.
	MonthlyUSD float64
}

Suggestion is one idle resource. The control plane only reports it; it never acts.

func FindIdle

func FindIdle(ctx context.Context, c *awsx.Client, cfg IdleConfig, region string, maxPages int) ([]Suggestion, error)

FindIdle is a dry run. Metrics are read in batches, so the AWS bill for it stays small.

type TagMeta

type TagMeta struct{ Owner, Env, Project, ID string }

TagMeta carries the values behind the mandatory tags.

Jump to

Keyboard shortcuts

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