cloudmetadata

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cloudmetadata is the shared HTTP client every cloud-provider collector (ec2, gce, azure, digital_ocean, openstack, oci, linode, alibaba, scaleway) uses to talk to its provider's link-local metadata endpoint. Handles short-timeout GETs, per-provider authentication headers (Azure's `Metadata: true`, OCI's `Authorization: Bearer Oracle`), and a typed ErrNotAvailable signal so collectors can short-circuit to a nil result when the host isn't on that provider.

Tests don't stub this package — they point a Client at an httptest.Server URL and exercise the real HTTP round-trip against canned responses.

Index

Constants

View Source
const DefaultUserAgent = "gohai"

DefaultUserAgent is the User-Agent header applied to every request unless the caller overrides it via WithHeader. Matches Ohai's chef-ohai/<version> pattern — metadata services occasionally rate- limit or filter by UA, and a stable identifier makes gohai traffic distinguishable in provider logs.

View Source
const MaxBody = 1 << 20

MaxBody is the most that one metadata response may hold. A metadata document, such as Azure's compute instance, is some kilobytes. A longer body is from an endpoint that is not the provider's, or a broken one, and fails.

Variables

View Source
var ErrNotAvailable = errors.New("cloud metadata endpoint not available")

ErrNotAvailable signals the provider's metadata endpoint couldn't be reached (connection refused, timeout, DNS failure, HTTP non-2xx). Collectors unwrap this via errors.Is and return (nil, nil) so the typed field drops from Facts without surfacing a misleading error.

Functions

This section is empty.

Types

type Client

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

Client is a thin HTTP wrapper scoped to one provider's metadata base URL. Construct via New; swap the base URL for tests by pointing it at an httptest.Server.

func New

func New(
	baseURL string,
	opts ...Option,
) *Client

New returns a Client rooted at baseURL. Default timeout is 2s; override with WithTimeout. Each Client shares one transport, with proxy lookup disabled (metadata IPs are link-local — honoring HTTP_PROXY would route cloud probes through a proxy and almost certainly fail). The User-Agent header defaults to DefaultUserAgent; override with WithHeader("User-Agent", "...") if needed.

func (*Client) Get

func (c *Client) Get(
	ctx context.Context,
	path string,
) ([]byte, error)

Get fetches baseURL + path and returns the response body on 2xx. Wraps transport failures and non-2xx responses in ErrNotAvailable so callers can branch with errors.Is.

func (*Client) GetJSON

func (c *Client) GetJSON(
	ctx context.Context,
	path string,
	out any,
) error

GetJSON fetches path and JSON-decodes into out. Transport-level failures propagate as ErrNotAvailable (same as Get); JSON decode errors propagate as-is.

func (*Client) GetWithHeaders

func (c *Client) GetWithHeaders(
	ctx context.Context,
	path string,
	headers map[string]string,
) ([]byte, error)

GetWithHeaders is Get with per-request headers merged onto the Client's default headers. Used by EC2's IMDSv2 flow where the token header must accompany every read but isn't known until Collect runs.

func (*Client) Put

func (c *Client) Put(
	ctx context.Context,
	path string,
	headers map[string]string,
) ([]byte, error)

Put issues a PUT against baseURL + path with per-request headers merged onto the Client's default headers. Body may be nil. Used by EC2's IMDSv2 token flow — that's currently the only provider that writes rather than reads the metadata endpoint.

func (*Client) RawGet

func (c *Client) RawGet(
	ctx context.Context,
	path string,
) ([]byte, int, error)

RawGet issues a GET and returns the response body + status code, without treating non-2xx as ErrNotAvailable. Used by Azure's api-version negotiation which relies on reading the HTTP 400 body Azure emits when no api-version is supplied. Transport failures still wrap ErrNotAvailable.

type Option

type Option func(*Client)

Option configures a Client at construction time.

func WithHTTPClient

func WithHTTPClient(
	h *http.Client,
) Option

WithHTTPClient swaps the underlying http.Client wholesale. Tests rarely need this — pointing baseURL at an httptest.Server exercises the full HTTP round-trip via the default client. Reserved for advanced cases (custom Transport for TLS-on-metadata, retry wrappers, etc.).

func WithHeader

func WithHeader(
	k, v string,
) Option

WithHeader adds a header sent with every request. Providers use this for auth / content negotiation (Azure's "Metadata: true", OCI's "Authorization: Bearer Oracle"). Multiple calls accumulate.

func WithTimeout

func WithTimeout(
	d time.Duration,
) Option

WithTimeout overrides the default 2s per-request timeout.

func WithTransport added in v1.16.0

func WithTransport(
	rt http.RoundTripper,
) Option

WithTransport makes rt the transport of the Client's requests. A nil rt keeps the shared transport. A caller's source.Sources.HTTP comes here, so a fake host answers the metadata reads.

Jump to

Keyboard shortcuts

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