{"record":{"id":"cdaacd4a80e50363","repo":"derailed/k9s","slug":"no-exposed-ports","errorCode":null,"errorMessage":"no exposed ports","messagePattern":"no exposed ports","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/port/ann.go","lineNumber":14,"sourceCode":"// SPDX-License-Identifier: Apache-2.0\n// Copyright Authors of K9s\n\npackage port\n\nimport (\n\t\"errors\"\n)\n\ntype Annotations map[string]string\n\nfunc (a Annotations) PreferredPorts(specs ContainerPortSpecs) (PFAnns, error) {\n\tif len(specs) == 0 {\n\t\treturn nil, errors.New(\"no exposed ports\")\n\t}\n\n\tvalue, ok := a[K9sPortForwardsKey]\n\tif !ok {\n\t\treturn PFAnns{specs[0].ToPFAnn()}, nil\n\t}\n\n\treturn specs.MatchAnnotations(value), nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/port/ann.go#L1-L24","documentation":"Annotations.PreferredPorts decides which container port to port-forward: with no k9s.io/portforward annotation it defaults to the first declared container port. If the container spec declares zero ports (specs is empty) there is nothing to forward and k9s refuses rather than guessing. Only an explicit annotation with an explicit port can override this.","triggerScenarios":"Launching a port-forward on a pod whose container spec has no ports[] entries and no k9s port-forward annotation; containers that receive traffic only via Service port targeting or env-configured listeners.","commonSituations":"Port-forwarding helper/sidecar containers (e.g. log shippers) with no declared ports; apps where the Dockerfile EXPOSE was not translated into containerPort; Istio-injected proxies in the list.","solutions":["Annotate the pod to declare the port explicitly: kubectl annotate pod <p> k9s.io/portforward=\"container::localPort:containerPort\" so PreferredPorts uses MatchAnnotations instead of the empty spec list.","Or add containerPort entries to the workload spec if you own it.","Port-forward via the Service instead, where targetPort mapping is known."],"exampleFix":"# before: container has no ports -> \"no exposed ports\"\nspec:\n  containers:\n    - name: app\n      image: app:dev\n# after: declare the listening port\nspec:\n  containers:\n    - name: app\n      image: app:dev\n      ports:\n        - containerPort: 8080","handlingStrategy":"validation","validationCode":"if len(specs) == 0 {\n    // cannot pick a default port; require an explicit annotation\n    if ann, ok := podAnns[port.K9sPortForwardsKey]; ok {\n        _ = ann // use annotation path\n    } else {\n        return errors.New(\"container exposes no ports; annotate or add containerPort\")\n    }\n}","typeGuard":"func hasExposedPorts(specs port.ContainerPortSpecs) bool { return len(specs) > 0 }","tryCatchPattern":null,"preventionTips":["Declare containerPort for anything you intend to port-forward.","Annotate pods with k9s.io/portforward when ports are dynamic.","Port-forward via the Service when the pod exposes nothing."],"tags":["go","kubernetes","port-forward","annotation","container-ports"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}