linki / chaoskube

chaoskube periodically kills random pods in your Kubernetes cluster.
MIT License
1.8k stars 120 forks source link

Log output error: json: error calling MarshalJSON for type time.Time #592

Closed johnhowlett closed 2 months ago

johnhowlett commented 8 months ago

If I start chaoskube with the --log-format=json in the log output there is a error:

./chaoskube --log-format=json --excluded-times-of-day="22:00-06:00" --excluded-weekdays=Sat,Sun --excluded-days-of-year=Apr1,Dec24 --labels="foo,bar" --kinds='!DaemonSet' --annotations='a.foo,a.bar'
{"dryRun":true,"interval":600000000000,"level":"info","maxRuntime":-1000000000,"msg":"starting up","time":"2024-02-18T11:15:37+01:00","version":"v0.31.0"}
{"level":"info","master":"https://127.0.0.1:6443","msg":"connected to cluster","serverVersion":{"major":"1","minor":"28","gitVersion":"v1.28.1+k3s1","gitCommit":"0d23cfe038ef22d7ca899764e9aaeea8a39d4874","gitTreeState":"clean","buildDate":"2023-09-08T17:15:00Z","goVersion":"go1.20.7","compiler":"gc","platform":"linux/arm64"},"time":"2024-02-18T11:15:37+01:00"}
{"annotations":[{},{}],"excludedPodNames":null,"includedPodNames":null,"kinds":[{}],"labels":[{},{}],"level":"info","maxKill":1,"minimumAge":0,"msg":"setting pod filter","namespaceLabels":null,"namespaces":null,"time":"2024-02-18T11:15:37+01:00"}
Failed to obtain reader, failed to marshal fields to JSON, json: error calling MarshalJSON for type time.Time: Time.MarshalJSON: year outside of range [0,9999]
{"level":"info","location":{},"msg":"setting timezone","name":"UTC","offset":0,"time":"2024-02-18T11:15:37+01:00"}

Also the parameter of the kinds, annotations, labels, ... are not shown in the log output. The program is running fine, it is only the log output that has a error and thas not show all of the input parameters

With adding the String() method to the:

-               "labels":           labelSelector,
-               "annotations":      annotations,
-               "kinds":            kinds,
-               "namespaces":       namespaces,
-               "namespaceLabels":  namespaceLabels,
+               "labels":           labelSelector.String(),
+               "annotations":      annotations.String(),
+               "kinds":            kinds.String(),
+               "namespaces":       namespaces.String(),
+               "namespaceLabels":  namespaceLabels.String(),

and renaming the log field:

-               "timesOfDay": parsedTimesOfDay,
+               "timesOfDay": excludedTimesOfDay, 

I was able to solve the issue, but I'm not sure if this approach is in the spirit of the inventor.

linki commented 5 months ago

@johnhowlett Sounds good to me! Feel free to open a pull request with your changes.

johnhowlett commented 5 months ago

made pull request: https://github.com/linki/chaoskube/pull/602

linki commented 2 months ago

Merged in https://github.com/linki/chaoskube/pull/602