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
- Variables
- type Client
- func (c *Client) Get(ctx context.Context, path string) ([]byte, error)
- func (c *Client) GetJSON(ctx context.Context, path string, out any) error
- func (c *Client) GetWithHeaders(ctx context.Context, path string, headers map[string]string) ([]byte, error)
- func (c *Client) Put(ctx context.Context, path string, headers map[string]string) ([]byte, error)
- func (c *Client) RawGet(ctx context.Context, path string) ([]byte, int, error)
- type Option
Constants ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.