instanceinfo

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package instanceinfo provides cloud instance metadata provider interface and implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCloudInitInstanceID

func GetCloudInitInstanceID(providerName string) (string, error)

GetCloudInitInstanceID gets the instance ID from cloud-init cache for the given provider.

Types

type Client

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

Client provides access to cloud instance metadata.

func New

func New() (*Client, error)

New creates a new instance metadata client. It auto-detects the cloud provider.

func NewWithProvider

func NewWithProvider(provider Provider) *Client

NewWithProvider creates a client with an explicit provider.

func (*Client) GetInstanceID

func (c *Client) GetInstanceID(ctx context.Context) (string, error)

GetInstanceID returns the provider-specific instance ID.

func (*Client) GetTerminationNotice

func (c *Client) GetTerminationNotice(ctx context.Context) (*health.TerminationNotice, error)

GetTerminationNotice checks for spot termination notices.

func (*Client) IsSpot

func (c *Client) IsSpot(ctx context.Context) (bool, error)

IsSpot returns true if running on a spot/preemptible instance.

func (*Client) Provider

func (c *Client) Provider() string

Provider returns the detected provider kind.

type CloudInitDS

type CloudInitDS struct {
	MetaData CloudInitProviderMeta `json:"meta_data"`
}

CloudInitDS contains provider-specific data source metadata.

type CloudInitData

type CloudInitData struct {
	V1       CloudInitV1    `json:"v1"`
	DS       CloudInitDS    `json:"ds"`
	Metadata map[string]any `json:"meta_data"`
}

CloudInitData represents the cloud-init instance data structure.

func ReadCloudInit

func ReadCloudInit() (*CloudInitData, error)

ReadCloudInit reads and parses the cloud-init instance data file. Returns nil if the file doesn't exist (not running with cloud-init).

type CloudInitProviderMeta

type CloudInitProviderMeta struct {
	// AWS fields
	InstanceID string `json:"instance-id"`

	// Azure fields
	Compute *struct {
		Name              string `json:"name"`
		VMId              string `json:"vmId"`
		ResourceGroupName string `json:"resourceGroupName"`
		SubscriptionId    string `json:"subscriptionId"`
	} `json:"compute"`

	// GCP fields
	Name      string `json:"name"`
	Zone      string `json:"zone"`
	ProjectID string `json:"project-id"`
}

CloudInitProviderMeta contains provider-specific metadata fields.

type CloudInitV1

type CloudInitV1 struct {
	InstanceID string `json:"instance_id"`
	Region     string `json:"region"`
	Zone       string `json:"availability_zone"`
}

CloudInitV1 contains standardized instance data.

type Provider

type Provider interface {
	// Kind returns the provider kind (aws, azure, gcp).
	Kind() string

	// GetInstanceID returns the provider-specific instance ID.
	// For AWS: instance-id (e.g., i-abc123)
	// For Azure: VM name
	// For GCP: instance name
	GetInstanceID(ctx context.Context) (string, error)

	// IsSpot returns true if running on a spot/preemptible instance.
	IsSpot(ctx context.Context) (bool, error)

	// GetTerminationNotice checks for spot termination notices.
	// Returns nil if no termination notice is present.
	GetTerminationNotice(ctx context.Context) (*health.TerminationNotice, error)
}

Provider defines the interface for querying cloud instance metadata.

func NewMockProvider

func NewMockProvider(serverID string) Provider

NewMockProvider creates a Provider that returns the given serverID as the instance identity.

type SpotMonitor

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

SpotMonitor monitors for spot instance termination notices.

func NewSpotMonitor

func NewSpotMonitor(cfg SpotMonitorConfig) (*SpotMonitor, error)

NewSpotMonitor creates a new termination notice monitor. It auto-detects the provider and spot status.

func (*SpotMonitor) GetTerminationNotice

func (m *SpotMonitor) GetTerminationNotice() *health.TerminationNotice

GetTerminationNotice returns the most recent termination notice.

func (*SpotMonitor) Start

func (m *SpotMonitor) Start(ctx context.Context)

Start begins monitoring for termination notices.

type SpotMonitorConfig

type SpotMonitorConfig struct {
	PollInterval time.Duration
	Logger       *slog.Logger
}

SpotMonitorConfig configures the termination notice monitor.

Jump to

Keyboard shortcuts

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