kubernetes/kops · warning
creating IMDS request: %w
Error message
creating IMDS request: %w
What it means
queryIMDS failed to construct the *http.Request via http.NewRequestWithContext for the IMDS URL. The error is wrapped as "creating IMDS request". In practice this is nearly impossible here since the URL is the constant imdsBaseURL plus a static path, so it usually signals an internal/programming problem rather than an environment issue.
Source
Thrown at upup/pkg/fi/cloudup/azure/azuremetadata/imds.go:69
// InstanceMetadata contains compute instance metadata from the Azure IMDS.
type InstanceMetadata struct {
SubscriptionID string `json:"subscriptionId"`
ResourceGroupName string `json:"resourceGroupName"`
ResourceID string `json:"resourceId"`
VMID string `json:"vmId"`
}
// attestedDocument is the JSON response from the IMDS attested/document endpoint.
type attestedDocument struct {
Signature string `json:"signature"`
}
// queryIMDS queries an Azure IMDS endpoint and unmarshals the JSON response.
// https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service
func queryIMDS(ctx context.Context, path string, params url.Values, result any) error {
req, err := http.NewRequestWithContext(ctx, "GET", imdsBaseURL+path, nil)
if err != nil {
return fmt.Errorf("creating IMDS request: %w", err)
}
req.Header.Add("Metadata", "True")
params.Set("api-version", imdsAPIVersion)
req.URL.RawQuery = params.Encode()
klog.V(4).Infof("Azure IMDS query: %q", req.URL.String())
resp, err := imdsHTTPClient.Do(req)
if err != nil {
return fmt.Errorf("querying IMDS %s: %w", path, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("querying IMDS %s: status %d", path, resp.StatusCode)
}
View on GitHub (pinned to 4c8573c808)
Solutions
- Inspect the wrapped error; it names the URL parse failure — fix the offending constant/path value
- Validate that imdsBaseURL and the caller-provided path form a valid absolute URL
- Add a unit test covering queryIMDS with all call sites' paths
Example fix
// before imdsBaseURL = "169.254.169.254" // missing scheme -> request creation fails // after imdsBaseURL = "http://169.254.169.254"
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the IMDS URL is well-formed before calling queryIMDS
if _, err := url.Parse(imdsBaseURL + "/metadata/instance/compute"); err != nil {
return fmt.Errorf("invalid IMDS base URL configured: %w", err)
} Prevention
- Keep imdsBaseURL as a compile-time constant; never build it from user input
- Add a unit test asserting http.NewRequest succeeds for every IMDS path used
- Run go vet/lint on changes touching URL constants
When it happens
Trigger: http.NewRequestWithContext returns an error for imdsBaseURL+path — e.g. a malformed URL string, which would only happen if imdsBaseURL or the path constants were changed to invalid values, or ctx is invalid.
Common situations: Code modification introducing a bad base URL or path (typos, illegal characters); misuse of the function with a path containing characters that break URL parsing; otherwise essentially never in production binaries.
Related errors
- empty subscription ID
- querying instance metadata: %w
- missing resource ID
- querying attested document: %w
- empty attested document signature
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/065facf665730744.
Report an issue: GitHub.