kubernetes/kops · error
error parsing file %q: %v
Error message
error parsing file %q: %v
What it means
After splitting the file into YAML/JSON sections, kops decodes each via kopscodecs.Decode. This error means a section is not valid YAML/JSON, is not a registered kops API type, or fails schema validation for the target group-version.
Source
Thrown at cmd/kops/replace.go:112
for _, f := range c.Filenames {
var contents []byte
if f == "-" {
contents, err = ConsumeStdin()
if err != nil {
return err
}
} else {
contents, err = vfsContext.ReadFile(f)
if err != nil {
return fmt.Errorf("error reading file %q: %v", f, err)
}
}
sections := text.SplitContentToSections(contents)
for _, section := range sections {
o, gvk, err := kopscodecs.Decode(section, nil)
if err != nil {
return fmt.Errorf("error parsing file %q: %v", f, err)
}
switch v := o.(type) {
case *kopsapi.Cluster:
{
// Retrieve the current status of the cluster. This will eventually be part of the cluster object.
cloud, err := cloudup.BuildCloud(v)
if err != nil {
return err
}
status, err := cloud.FindClusterStatus(v)
if err != nil {
return err
}
// Check if the cluster exists already
clusterName := v.Name
cluster, err := clientset.GetCluster(ctx, clusterName)View on GitHub (pinned to 4c8573c808)
Solutions
- Validate the YAML/JSON syntax (yamllint or `kubectl apply --dry-run=client` style parsing)
- Ensure kind and apiVersion are supported kops types for your kops binary version
- Run `kops replace --help` / check docs for the accepted schema; upgrade or regenerate the manifest with `kops get -o yaml`
- Fix indentation (spaces, never tabs) and document separators (---)
- Decode incrementally: split the file and parse each section to find the offending document
Example fix
// before (invalid) kind: Cluster metadata: name: mycluster // after kind: Cluster metadata: name: mycluster
Defensive patterns
Strategy: validation
Validate before calling
var raw interface{}
if err := yaml.Unmarshal(contents, &raw); err != nil {
return fmt.Errorf("invalid YAML: %w", err)
}
// Optionally assert kind is a supported kops type before calling replace.
kind, _ := raw.(map[string]interface{})["kind"].(string)
supported := map[string]bool{"Cluster": true, "InstanceGroup": true}
if !supported[kind] {
return fmt.Errorf("unsupported kind for kops replace: %s", kind)
} Type guard
func isKopsManifestKind(kind string) bool {
switch kind {
case "Cluster", "InstanceGroup":
return true
}
return false
} Try / catch
if err != nil {
if strings.Contains(err.Error(), "no kind") || strings.Contains(err.Error(), "cannot unmarshal") {
// schema/parse problem: log file + line, fix manifest
}
return err
} Prevention
- Lint manifests with yamllint before running kops
- Use spaces, never tabs, in YAML
- Generate manifests with `kops get -o yaml` to guarantee valid schema
- Pin kops CLI and manifest apiVersion to compatible versions
When it happens
Trigger: Manifest with invalid YAML syntax (bad indentation, tabs); YAML that parses but whose kind/apiVersion is not a kops type (e.g. a Deployment); missing required fields; documents separated with a separator kops does not recognize; an apiVersion from an incompatible kops version.
Common situations: Hand-edited cluster.yaml with a syntax error; exporting a k8s workload manifest and passing it to `kops replace`; upgrading kops so an old apiVersion (e.g. kops/v1alpha2) is no longer accepted; copy-pasting YAML with tabs.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- error parsing addons: %v
- error parsing addons or manifest: %v
- failed to parse objects: %w
- failed to parse objects: %w
- error parsing file %q: %v
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/9d42896761e5c671.
Report an issue: GitHub.