inventory

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AWSProvider - Amazon Web Services Cloud Provider
	AWSProvider CloudProvider = "AWS"
	// AzureProvider - Microsoft Azure Cloud Provider
	AzureProvider = "Azure"
	// GCPProvider - Google Cloud Platform Cloud Provider
	GCPProvider = "GCP"
	// UnknownProvider - Unknown Platform Cloud Provider
	UnknownProvider = "UNKNOWN"
)
View Source
const (
	UnknownClusterNameCode = "UNKNOWN-CLUSTER"
	UnknownClusterIDCode   = "UNKNOWN-CLUSTER"
)
View Source
const (
	// Cluster actions
	ClusterPowerOnAction  = "PowerOn"
	ClusterPowerOffAction = "PowerOff"

	// Resource types
	ClusterResourceType  = "cluster"
	InstanceResourceType = "instance"
)
View Source
const (
	// ClusterTagKey string to identify to which cluster is the instance associated
	ClusterTagKey string = "kubernetes.io/cluster/"
)

Variables

View Source
var (
	ERR_INSTANCE_TOTAL_COST_LESS_ZERO = errors.New("TotalCost of an instance cannot be less than zero")
	ERR_INSTANCE_DAILY_COST_LESS_ZERO = errors.New("DailyCost of an instance cannot be less than zero")
	ERR_INSTANCE_AGE_LESS_ZERO        = errors.New("Cannot recalculate costs if instance's Age is 0")
)

Errors for Instances

Functions

func GenerateClusterID

func GenerateClusterID(name string, infraID string, accountName string) (string, error)

Obtain the required parameters for generate a ClusterID. If any key parameter is missing, it will return a non-nil error

func GetClusterIDFromTags added in v0.4.1

func GetClusterIDFromTags(tags []Tag) string

func GetClusterNameFromTags added in v0.4.1

func GetClusterNameFromTags(tags []Tag) string

func GetInfraIDFromTags added in v0.4.1

func GetInfraIDFromTags(tags []Tag) string

func GetInstanceNameFromTags added in v0.4.1

func GetInstanceNameFromTags(tags []Tag) string

func GetOwnerFromTags added in v0.4.1

func GetOwnerFromTags(tags []Tag) string

GetOwnerFromTags looks for a tag with the key "Owner" and returns its value

func JSONMarshal

func JSONMarshal(object interface{}) (string, error)

JSONMarshal converts an inventory object as JSON format for printing

Types

type Account

type Account struct {
	// ID is the uniq identifier for each account without considering the cloud provider
	// AWS: AccountID
	// Azure: SubscriptionID
	// GCP: ProjectID
	ID string `db:"id" json:"id"`

	// Account's name. It's considered as an uniq key. Two accounts with same
	// name can't belong to same Inventory
	Name string `db:"name" json:"name"`

	// Infrastructure provider identifier.
	Provider CloudProvider `db:"provider" json:"provider"`

	// ClusterCount
	ClusterCount int `db:"cluster_count" json:"clusterCount"`

	// List of clusters deployed on this account indexed by Cluster's name
	Clusters map[string]*Cluster `json:"-"`

	// Last scan timestamp of the account
	LastScanTimestamp time.Time `db:"last_scan_timestamp" json:"lastScanTimestamp"`

	// Total cost (US Dollars)
	TotalCost float64 `db:"total_cost" json:"totalCost"`

	// Cost Last 15d
	Last15DaysCost float64 `db:"last_15_days_cost" json:"last15DaysCost"`

	// Last month cost
	LastMonthCost float64 `db:"last_month_cost" json:"lastMonthCost"`

	// Current month so far cost
	CurrentMonthSoFarCost float64 `db:"current_month_so_far_cost" json:"currentMonthSoFarCost"`
	// contains filtered or unexported fields
}

Account defines an infrastructure provider account

func NewAccount

func NewAccount(id string, name string, provider CloudProvider, user string, password string) *Account

NewAccount create a new Could Provider account to store its instances

func (*Account) AddCluster

func (a *Account) AddCluster(cluster *Cluster) error

AddCluster adds a cluster to the stock

func (*Account) DisableBilling

func (a *Account) DisableBilling()

DisableBilling disables the billing information scanner for this account

func (*Account) EnableBilling

func (a *Account) EnableBilling()

EnableBilling enables the billing information scanner for this account

func (Account) GetPassword

func (a Account) GetPassword() string

GetPassword returns the password value

func (Account) GetUser

func (a Account) GetUser() string

GetUser returns the username value

func (Account) IsBillingEnabled

func (a Account) IsBillingEnabled() bool

IsBillingEnabled returns a boolean value based on if the billing module is enabled or not

func (Account) IsClusterOnAccount

func (a Account) IsClusterOnAccount(id string) bool

IsClusterOnAccount checks if a cluster is already in the Stock

func (Account) PrintAccount

func (a Account) PrintAccount()

PrintAccount prints account info and every cluster on it by stdout

type CloudProvider

type CloudProvider string

CloudProvider defines the cloud provider of the instance

func GetCloudProvider added in v0.4.2

func GetCloudProvider(provider string) CloudProvider

GetCloudProvider checks a incoming string and returns the corresponding inventory.CloudProvider value

type Cluster

type Cluster struct {
	// ID is the unique key to identify every cluster independently of which account it belongs
	// Its built as "name+infra_id+account"
	ID string `db:"id" json:"id"`

	// Cluster's Name. Must be unique per Account
	Name string `db:"name" json:"name"`

	// InfraID is the infrastructure ID generated by openshift-installer during the installation.Cluster (Could be undefined)
	InfraID string `db:"infra_id" json:"infra_id"`

	// Infrastructure provider identifier.
	Provider CloudProvider `db:"provider" json:"provider"`

	// Defines the status of the cluster if its infrastructure is running or not or it was removed
	Status InstanceStatus `db:"status" json:"status"`

	// The region of the infrastructure provider in which the cluster is deployed
	Region string `db:"region" json:"region"`

	// Account name which this cluster belongs to
	AccountName string `db:"account_name" json:"accountName"`

	// Openshift Console URL. Might not be accesible if its protected
	ConsoleLink string `db:"console_link" json:"consoleLink"`

	// Instances count
	InstanceCount int `db:"instance_count" json:"instanceCount"`

	// Last scan timestamp of the cluster
	LastScanTimestamp time.Time `db:"last_scan_timestamp" json:"lastScanTimestamp"`

	// Timestamp when the cluster was created
	CreationTimestamp time.Time `db:"creation_timestamp" json:"creationTimestamp"`

	// Amount of days since the cluster was created
	Age int `db:"age" json:"age"`

	// Cluster's owner
	Owner string `db:"owner" json:"owner"`

	// Total cost (US Dollars)
	TotalCost float64 `db:"total_cost" json:"totalCost"`

	// Cost Last 15d
	Last15DaysCost float64 `db:"last_15_days_cost" json:"last15DaysCost"`

	// Last month cost
	LastMonthCost float64 `db:"last_month_cost" json:"lastMonthCost"`

	// Current month so far cost
	CurrentMonthSoFarCost float64 `db:"current_month_so_far_cost" json:"currentMonthSoFarCost"`

	// Cluster's instance (nodes) lists
	Instances []Instance
}

Cluster is the object to store Openshift Clusters and its properties

func NewCluster

func NewCluster(name string, infraID string, provider CloudProvider, region string, accountName string, consoleLink string, owner string) *Cluster

NewCluster creates a new cluster instance

func (*Cluster) AddInstance

func (c *Cluster) AddInstance(instance Instance) error

AddInstance add a new instance to a cluster

func (Cluster) IsClusterRunning added in v0.4.2

func (c Cluster) IsClusterRunning() bool

IsClusterRunning checks if the Cluster is Running

func (Cluster) IsClusterStopped added in v0.4.2

func (c Cluster) IsClusterStopped() bool

IsClusterStopped checks if the Cluster is Stopped

func (Cluster) PrintCluster

func (c Cluster) PrintCluster()

PrintCluster prints cluster info

func (*Cluster) Update

func (c *Cluster) Update() error

UpdateClusterInfo as a update function wrapper

func (*Cluster) UpdateAge

func (c *Cluster) UpdateAge() error

UpdateAge updates cluster age based on current and creation timestamps

func (*Cluster) UpdateCosts

func (c *Cluster) UpdateCosts() error

UpdateCosts takes every cluster's instance costs and estimates the total cost for the cluster

func (*Cluster) UpdateStatus

func (c *Cluster) UpdateStatus()

UpdateStatus evaluate the status of the cluster checking how many of the nodes are in Running, Stopped or Terminated status. Logic rules: - Empty cluster (no instances) -> Terminated - Any instance Running -> Running (early return) - All instances Terminated -> Terminated - Otherwise (mix of Stopped/Terminated or all Stopped) -> Stopped

type Expense

type Expense struct {
	// InstanceID references the instance of the expense
	InstanceID string `db:"instance_id" json:"instanceID"`

	// Ammount represents the cost in USDollars
	Amount float64 `db:"amount" json:"amount"`

	// Date (Year, month, day)
	Date time.Time `db:"date" json:"date"`
}

Expense defines the expenses applied to an instance

func NewExpense

func NewExpense(instanceID string, amount float64, date time.Time) *Expense

NewExpense create a expense for an instance

type Instance

type Instance struct {
	// Uniq Identifier of the instance
	ID string `db:"id" json:"id"`

	// Instance Name. In some Cloud Providers, the name is managed as a Tag
	Name string `db:"name" json:"name"`

	// Instance provider (public/private cloud provider)
	Provider CloudProvider `db:"provider" json:"provider"`

	// Instance type/size/flavour
	InstanceType string `db:"instance_type" json:"instanceType"`

	// Availability Zone in which the instance is running on
	AvailabilityZone string `db:"availability_zone" json:"availabilityZone"`

	// Instance Status
	Status InstanceStatus `db:"status" json:"status"`

	// ClusterID
	ClusterID string `db:"cluster_id" json:"clusterID"`

	// Last scan timestamp of the instance
	LastScanTimestamp time.Time `db:"last_scan_timestamp" json:"lastScanTimestamp"`

	// Timestamp when the instance was created
	CreationTimestamp time.Time `db:"creation_timestamp" json:"creationTimestamp"`

	// Ammount of days since the instance was created
	Age int `db:"age" json:"age"`

	// Daily cost (US Dollars) estimated based on total cost and age of the instance
	DailyCost float64 `db:"daily_cost" json:"dailyCost"`

	// Total cost (US Dollars) accumulated since ClusterIQ is scanning
	TotalCost float64 `db:"total_cost" json:"totalCost"`

	// Instance Tags as key-value array
	Tags []Tag `json:"tags"`

	// Expenses list associated to the instance
	Expenses []Expense `json:"expenses"`
}

Instance model a cloud provider instance

func NewInstance

func NewInstance(id string, name string, provider CloudProvider, instanceType string, availabilityZone string, status InstanceStatus, clusterID string, tags []Tag, creationTimestamp time.Time) *Instance

NewInstance returns a new Instance object

func (*Instance) AddTag

func (i *Instance) AddTag(tag Tag)

AddTag adds a tag to an instance

func (Instance) PrintInstance

func (i Instance) PrintInstance()

PrintInstance prints Instance details

func (Instance) String

func (i Instance) String() string

String as ToString func

func (*Instance) UpdateCosts

func (i *Instance) UpdateCosts() error

UpdateCosts updates the totalCost of the instance using the instance age and the DailyCost

type InstanceStatus

type InstanceStatus string

InstanceStatus defines the status of the instance

const (
	// Running Instance status
	Running InstanceStatus = "Running"
	// Stopped Instance status
	Stopped InstanceStatus = "Stopped"
	// Terminated Instance status
	Terminated InstanceStatus = "Terminated"
)

func AsInstanceStatus

func AsInstanceStatus(status string) InstanceStatus

AsInstanceStatus converts the incoming argument into a InstanceStatus type

type Inventory

type Inventory struct {
	// Accounts map indexed by Account's Name
	Accounts map[string]*Account `db:"accounts" json:"accounts"`

	// Date of Inventory creation/update
	CreationTimestamp time.Time `db:"creationTimestamp" json:"creationTimestamp"`
}

Inventory object to store inventored resources

func NewInventory

func NewInventory() *Inventory

NewInventory creates a new Inventory variable

func (*Inventory) AddAccount

func (s *Inventory) AddAccount(account *Account) error

AddAccount adds a new account into the Inventory

func (Inventory) IsAccountOnInventory

func (s Inventory) IsAccountOnInventory(name string) bool

IsAccountOnInventory checks if a cluster is already in the Inventory

func (Inventory) PrintInventory

func (s Inventory) PrintInventory()

PrintInventory prints the entire Inventory content

type Tag

type Tag struct {
	// Tag's key
	Key string `db:"key" json:"key"`

	// Tag's Value
	Value string `db:"value" json:"value"`

	// InstanceID reference
	InstanceID string `db:"instance_id" json:"instance_id"`
}

Tag model generic tags as a Key-Value object

func LookForTagByKey added in v0.4.1

func LookForTagByKey(key string, tags []Tag) *Tag

lookForTagByKey looks for a Tag based on its Key and returns a pointer to it

func NewTag

func NewTag(key string, value string, instanceID string) *Tag

NewTag returns a new generic tag struct

Jump to

Keyboard shortcuts

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