kubernetes/kops · error
failed to marshal token data: %w
Error message
failed to marshal token data: %w
What it means
After building the AuthTokenData struct (audience, request hash), CreateToken serializes it with json.Marshal. This error wraps any failure of that marshal step. In practice it is near-impossible with this plain struct, so it indicates a programming error or an exotic json.Marshaler bug rather than an environment problem.
Source
Thrown at upup/pkg/fi/cloudup/gce/tpm/gcetpmsigner/tpmauthenticator.go:98
}
defer key.Close()
klog.V(2).Infof("attestation key is %v", debugToPEM(key.PublicKey()))
klog.Infof("TPM initialization took %v", time.Since(tpmStart))
data := gcetpm.AuthTokenData{
GCPProjectID: a.projectID,
Zone: a.zone,
Instance: a.instance,
Timestamp: time.Now().Unix(),
Audience: gcetpm.AudienceNodeAuthentication,
RequestHash: requestHash[:],
}
payload, err := json.Marshal(&data)
if err != nil {
return "", fmt.Errorf("failed to marshal token data: %w", err)
}
signature, err := tpmSign(key, payload)
if err != nil {
return "", fmt.Errorf("failed to sign token data: %w", err)
}
token := &gcetpm.AuthToken{
Data: payload,
Signature: signature,
}
b, err := json.Marshal(token)
if err != nil {
return "", fmt.Errorf("failed to marshal token: %w", err)
}
return gcetpm.GCETPMAuthenticationTokenPrefix + base64.StdEncoding.EncodeToString(b), nil
}
View on GitHub (pinned to 4c8573c808)
Solutions
- Inspect the wrapped error for the offending field/type
- Remove or fix any custom MarshalJSON implementation on gcetpm.AuthTokenData
- Ensure all struct fields are JSON-serializable types
- Rebuild with the version of kops/gcetpm package that matches your vendored types
Example fix
null
Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := json.Marshal(gcetpm.AuthTokenData{}); err != nil {
// detect serialization problem in struct definition early (tests)
panic(err)
} Try / catch
token, err := authenticator.CreateToken(ctx, request)
if err != nil {
var me *json.MarshalTypeError
if errors.As(err, &me) { log.Fatalf("bad field %s", me.Field) }
return err
} Prevention
- Add a unit test marshalling AuthTokenData with representative data
- Keep AuthTokenData fields to JSON-native types
- Avoid custom MarshalJSON unless necessary and well-tested
When it happens
Trigger: json.Marshal(&data) returns an error — only possible if AuthTokenData (or a field of it) implements MarshalJSON and fails, or an unsupported type is present.
Common situations: Custom MarshalJSON on AuthTokenData that panics/returns error after a refactor; adding an unsupported field type (chan, func) to the struct.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- failed to marshal token: %w
- unmarshalling authorization token: %w
- unmarshalling authorization token data: %w
- error building annotation patch: %v
- building node patch: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/e5f868d375342aca.
Report an issue: GitHub.