kubernetes/kops · error

error parsing location %q - not a valid URI

Error message

error parsing location %q - not a valid URI

What it means

VFSContext.ReadFile parses a location string with net/url.Parse when it contains "://" and is not a file:// URL. If url.Parse fails, the location is not a syntactically valid URI, so kOps cannot determine the scheme and returns this error instead of reading any file.

Source

Thrown at util/pkg/vfs/context.go:132

	ctx := context.TODO()

	var opts vfsOptions
	// Exponential backoff, starting with 500 milliseconds, doubling each time, 5 steps
	opts.backoff = wait.Backoff{
		Duration: 500 * time.Millisecond,
		Factor:   2,
		Steps:    5,
	}

	for _, option := range options {
		option(&opts)
	}

	if strings.Contains(location, "://") && !strings.HasPrefix(location, "file://") {
		// Handle our special case schemas
		u, err := url.Parse(location)
		if err != nil {
			return nil, fmt.Errorf("error parsing location %q - not a valid URI", location)
		}

		switch u.Scheme {
		case "metadata":
			switch u.Host {
			case "gce":
				httpURL := "http://169.254.169.254/computeMetadata/v1/" + u.Path
				httpHeaders := make(map[string]string)
				httpHeaders["Metadata-Flavor"] = "Google"
				return c.readHTTPLocation(httpURL, httpHeaders, opts)
			case "aws":
				return c.readAWSMetadata(ctx, u.Path)
			case "digitalocean":
				httpURL := "http://169.254.169.254/metadata/v1" + u.Path
				return c.readHTTPLocation(httpURL, nil, opts)
			case "openstack":
				httpURL := "http://169.254.169.254/latest/meta-data/" + u.Path
				return c.readHTTPLocation(httpURL, nil, opts)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Print the exact location value in the error and run url.Parse on it in a scratch program to see the specific parse error
  2. Fix the malformed URI in the config/flag/environment value that produced it
  3. Validate the location with url.ParseRequestURI before passing it to ReadFile
  4. If the value is a local path that happens to contain '://', restructure it or use a file:// prefix explicitly

Example fix

// before
kops.ReadFile(cfg.BaseURL + "/channel") // BaseURL = "https://example.com "/a b"
// after
u, err := url.Parse(cfg.BaseURL + "/channel")
if err != nil {
	return fmt.Errorf("invalid channel location: %w", err)
}
data, err := kops.ReadFile(u.String())
Defensive patterns

Strategy: validation

Validate before calling

func validateVFSLocation(loc string) error {
	if !strings.Contains(loc, "://") || strings.HasPrefix(loc, "file://") {
		return nil
	}
	if _, err := url.Parse(loc); err != nil {
		return fmt.Errorf("invalid vfs location %q: %w", loc, err)
	}
	return nil
}

Type guard

func isParsableURI(loc string) bool {
	u, err := url.Parse(loc)
	return err == nil && u.Scheme != ""
}

Try / catch

data, err := vfs.Context.ReadFile(loc)
if err != nil && strings.Contains(err.Error(), "not a valid URI") {
	return fmt.Errorf("check the location value %q: %w", loc, err)
}

Prevention

When it happens

Trigger: Calling ReadFile (directly or via updateAddon, LoadAddons, LoadChannel, Run, transferFile, findHash) with a location containing "://" that url.Parse rejects - e.g. control characters, malformed percent-encoding like 'https://host/%zz', or a bad scheme such as 'ht tp://x' built by string concatenation.

Common situations: Typo in a --state store or addon/channel URL in cluster config; programmatic construction of URLs from unvalidated user input or environment variables; shell interpolation inserting spaces into a URI.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/38caa224beb4d8fc. Report an issue: GitHub.