{"record":{"id":"065facf665730744","repo":"kubernetes/kops","slug":"creating-imds-request-w","errorCode":null,"errorMessage":"creating IMDS request: %w","messagePattern":"creating IMDS request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"upup/pkg/fi/cloudup/azure/azuremetadata/imds.go","lineNumber":69,"sourceCode":"// InstanceMetadata contains compute instance metadata from the Azure IMDS.\ntype InstanceMetadata struct {\n\tSubscriptionID    string `json:\"subscriptionId\"`\n\tResourceGroupName string `json:\"resourceGroupName\"`\n\tResourceID        string `json:\"resourceId\"`\n\tVMID              string `json:\"vmId\"`\n}\n\n// attestedDocument is the JSON response from the IMDS attested/document endpoint.\ntype attestedDocument struct {\n\tSignature string `json:\"signature\"`\n}\n\n// queryIMDS queries an Azure IMDS endpoint and unmarshals the JSON response.\n// https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service\nfunc queryIMDS(ctx context.Context, path string, params url.Values, result any) error {\n\treq, err := http.NewRequestWithContext(ctx, \"GET\", imdsBaseURL+path, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"creating IMDS request: %w\", err)\n\t}\n\treq.Header.Add(\"Metadata\", \"True\")\n\n\tparams.Set(\"api-version\", imdsAPIVersion)\n\treq.URL.RawQuery = params.Encode()\n\n\tklog.V(4).Infof(\"Azure IMDS query: %q\", req.URL.String())\n\n\tresp, err := imdsHTTPClient.Do(req)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"querying IMDS %s: %w\", path, err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"querying IMDS %s: status %d\", path, resp.StatusCode)\n\t}\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/azure/azuremetadata/imds.go#L51-L87","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nimdsBaseURL = \"169.254.169.254\" // missing scheme -> request creation fails\n// after\nimdsBaseURL = \"http://169.254.169.254\"","handlingStrategy":"validation","validationCode":"// Ensure the IMDS URL is well-formed before calling queryIMDS\nif _, err := url.Parse(imdsBaseURL + \"/metadata/instance/compute\"); err != nil {\n    return fmt.Errorf(\"invalid IMDS base URL configured: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["azure","imds","http-request"],"backgroundTag":"malformed-url","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}