GoogleContainerTools/skaffold · error

cannot parse the namespace template on user defined port…

Error message

cannot parse the namespace template on user defined port forwarder: %w

What it means

applyWithTemplate expands environment-variable templates in user-defined port-forward resources. If the namespace field contains an invalid template (e.g. unbalanced braces or a missing env var), ExpandEnvTemplateOrFail fails and the error is wrapped with "cannot parse the namespace template on user defined port forwarder".

Solutions

  1. Fix the template syntax in the portForward namespace field (balanced {{ }} and valid field names)
  2. Define the referenced environment variable in the shell/CI before running skaffold
  3. Use a literal namespace string instead of a template if templating is unnecessary
  4. Validate the config with skaffold config or skaffold dev --dry-run style checks before deploy

Example fix

# before (skaffold.yaml)
portForward:
  - resourceType: pod
    resourceName: web
    namespace: {{.MISSING_NS}}
# after
export NAMESPACE=my-ns  # or fix the template
portForward:
  - resourceType: pod
    resourceName: web
    namespace: {{.NAMESPACE}}
Defensive patterns

Strategy: validation

Validate before calling

func validNamespaceTemplate(ns string) error {
    if ns == "" {
        return nil
    }
    if strings.Count(ns, "{{") != strings.Count(ns, "}}") {
        return fmt.Errorf("unbalanced template braces in namespace %q", ns)
    }
    return nil
}

Try / catch

if err := applyWithTemplate(res); err != nil {
    if strings.Contains(err.Error(), "namespace template") {
        return fmt.Errorf("fix portForward.namespace in skaffold.yaml: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: PortForwardResource.Namespace is non-empty and util.ExpandEnvTemplateOrFail(resource.Namespace, nil) errors during Start's resource preparation — caused by malformed Go template syntax or an undefined environment variable in skaffold.yaml's portForward.namespace.

Common situations: skaffold.yaml has portForward namespace like {{.MISSING_VAR}} or {{NAMESPACE} (unbalanced brace); CI environment lacks a variable set locally; copy-pasted template using wrong delimiters.

Related errors


AI-assisted analysis of GoogleContainerTools/skaffold@a1189de023 (2026-09-05). Data as JSON: /api/errors/00a7ee459c29e89d. Report an issue: GitHub.

Appendix: source

Thrown at pkg/skaffold/kubernetes/portforward/resource_forwarder.go:115

	}

	var serviceResources []*latest.PortForwardResource
	if p.services {
		found, err := retrieveServices(ctx, p.label, namespaces, p.kubeContext)
		if err != nil {
			return fmt.Errorf("retrieving services for automatic port forwarding: %w", err)
		}
		serviceResources = found
	}
	p.portForwardResources(ctx, append(p.userDefinedResources, serviceResources...))
	return nil
}

func applyWithTemplate(resource *latest.PortForwardResource) error {
	if resource.Namespace != "" {
		namespace, err := util.ExpandEnvTemplateOrFail(resource.Namespace, nil)
		if err != nil {
			return fmt.Errorf("cannot parse the namespace template on user defined port forwarder: %w", err)
		}
		resource.Namespace = namespace
	}
	name, err := util.ExpandEnvTemplateOrFail(resource.Name, nil)
	if err != nil {
		return fmt.Errorf("cannot parse the name template on user defined port forwarder: %w", err)
	}
	resource.Name = name
	return nil
}

func (p *ResourceForwarder) Stop() {
	p.entryManager.Stop()
}

// Port forward each resource individually in a goroutine
func (p *ResourceForwarder) portForwardResources(ctx context.Context, resources []*latest.PortForwardResource) {
	var wg sync.WaitGroup

View on GitHub (pinned to a1189de023)