-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INTLY-2712 check cr for valid json #25
Conversation
closes #18 |
@@ -118,7 +104,8 @@ func (h *PluginsHelperImpl) FilterPlugins(cr *integreatly.Grafana, requested int | |||
// Don't allow to install multiple versions of the same plugin | |||
if filteredPlugins.HasSomeVersionOf(&plugin) == true { | |||
installedVersion := filteredPlugins.GetInstalledVersionOf(&plugin) | |||
h.AppendMessage(fmt.Sprintf("Not installing version %s of %s because %s is already installed", plugin.Version, plugin.Name, installedVersion.Version), plugin.Origin) | |||
msg := fmt.Sprintf("Not installing version %s of %s because %s is already installed", plugin.Version, plugin.Name, installedVersion.Version) | |||
common.KubeHelperImpl{}.AppendMessage(msg, plugin.Origin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is creating a new instance of KubeHelper for every iteration of this line. Would be better to not make AppendMessage
part of the KubeHelperImpl
struct (by removing the (h KubeHelperImpl) part of the function definition).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or by using a shared instance of KubeHelper (could also be passed in or as a property of PluginHelper)
} | ||
if known { | ||
r.deleteDashboard(cr) | ||
cr.Status.Phase = common.StatusResourceUninitialized |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this lead to the dashboard being reimported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because we return reconcile.Result after it and the json check won't allow it to get imported the next time.
This is to catch where the json was valid, so the dashboard is in the configmap, but now the json has changed and is invalid.
return reconcile.Result{}, nil | ||
} | ||
if known { | ||
r.deleteDashboard(cr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could lead to a valid dashboard to be removed from the configmap when a change is made that breaks the json and the dashboard is reconciled a second time (e.g. by adding a label).
I'm unsure if we should remove a dashboard from the configmap, it shouldn't be possible for it to be imported in the first place when the json was invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah just typing out an answer to your last comment I realise we should leave the valid dashboard in place if the json is now invalid.
func isJSON(s string) (bool, error) { | ||
var js map[string]interface{} | ||
err := json.Unmarshal([]byte(s), &js) | ||
return json.Unmarshal([]byte(s), &js) == nil, err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be return err == nil, err
, to avoid doing the check twice?
// Dashboard is in cm and JSON is invalid | ||
if jsonTest == false && changed { | ||
jsonError := err.Error() | ||
msg := fmt.Sprintf("Invalid JSON, Error: %s", jsonError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per convention, all golang log messages should start with lowercase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah sorry, this is what's going into the status message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StevenTobin @pb82 Logs after creating a dashboard with invalid json
{"level":"info","ts":1563525778.167916,"logger":"controller_grafanadashboard","caller":"grafanadashboard/dashboard_controller.go:193","msg":"dashboard prerequisite check failed"}
{"level":"info","ts":1563525778.168719,"logger":"controller_grafanadashboard","caller":"grafanadashboard/dashboard_controller.go:159","msg":"dashboard reconciled but no changes"}
{"level":"info","ts":1563525778.168788,"logger":"controller_grafanadashboard","caller":"grafanadashboard/dashboard_controller.go:171","msg":"dashboard reconciled but json still invalid"}
{"level":"info","ts":1563525778.168808,"logger":"controller_grafanadashboard","caller":"grafanadashboard/dashboard_controller.go:193","msg":"dashboard prerequisite check failed"}
status of CR
status:
lastConfig: a6bc7800395833e792c7be2240ecbd0d
messages:
- message: >-
invalid JSON, error: invalid character ',' looking for beginning of
object key string
timestamp: 'Friday, 19-Jul-19 09:42:58 IST'
phase: 2
Fixed the invalid json and the dashboard was added to Grafana.
However, the status still shows:
status:
lastConfig: ac51e61e86cc26f00717240114511787
messages:
- message: >-
invalid JSON, error: invalid character ',' looking for beginning of
object key string
timestamp: 'Friday, 19-Jul-19 09:42:58 IST'
phase: 2
Is that expected?
@david-martin yes, at the moment we don't remove the error message. No i'm thinking, maybe it would be a good idea to do so? |
@pb82 The reason I mention it is because it made me think there was still a problem with the dashboard. However, that wasn't the case as the Dashboard was added OK to Grafana when I checked the |
newline at EOF add JSON error to status fix for code check add error checking for phases, JSON error changing and fixing error fix for code check remove debug print use hash to control when to run
Tested changes locally, all looks good. CR with invalid JSON is being ignored and an error logged in status.
Import message is being created after fixing the invalid JSON. I can't approve as I'm the author of the PR but 👍 |
Check cr for valid JSON before importing dashboard.
Verification steps: