Skip to content
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

Merged
merged 6 commits into from
Jul 19, 2019
Merged

Conversation

StevenTobin
Copy link
Contributor

@StevenTobin StevenTobin commented Jul 16, 2019

Check cr for valid JSON before importing dashboard.

Verification steps:

  1. Install the operator from this branch
  2. Create a dashboard with invalid json and try to import it
  3. The operator should log a meaningful error and the dashboard should not get imported
  4. Check the dashboard CR: a status message with the json error should have been appended
  5. fix the json and make sure the dashboard is imported now

@StevenTobin StevenTobin marked this pull request as ready for review July 16, 2019 10:40
@StevenTobin StevenTobin requested a review from pb82 July 16, 2019 10:40
@StevenTobin StevenTobin changed the title check cr for valid json INTLY-2712 check cr for valid json Jul 16, 2019
@david-martin
Copy link
Contributor

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)
Copy link
Collaborator

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).

Copy link
Collaborator

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)

@pb82 pb82 self-assigned this Jul 18, 2019
}
if known {
r.deleteDashboard(cr)
cr.Status.Phase = common.StatusResourceUninitialized
Copy link
Collaborator

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?

Copy link
Contributor Author

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Collaborator

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)
Copy link
Collaborator

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

Copy link
Collaborator

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

Copy link
Contributor

@david-martin david-martin left a 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?

@pb82
Copy link
Collaborator

pb82 commented Jul 19, 2019

@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?

@david-martin
Copy link
Contributor

@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 grafana-dashboards configmap & UI.

StevenTobin and others added 6 commits July 19, 2019 11:42
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
@StevenTobin
Copy link
Contributor Author

Tested changes locally, all looks good. CR with invalid JSON is being ignored and an error logged in status.

  messages:
    - message: 'invalid JSON, error: invalid character ''\n'' in string literal'
      timestamp: 'Friday, 19-Jul-19 10:49:11 IST'
    - message: dashboard 'ferwa/dashboard-with-plugins.json' imported
      timestamp: 'Friday, 19-Jul-19 10:49:39 IST'
    - message: installing plugin grafana-piechart-panel@1.3.6
      timestamp: 'Friday, 19-Jul-19 10:49:44 IST'
    - message: installing plugin grafana-clock-panel@1.0.2
      timestamp: 'Friday, 19-Jul-19 10:49:44 IST'

Import message is being created after fixing the invalid JSON.

I can't approve as I'm the author of the PR but 👍

@pb82 pb82 merged commit fdf7dae into grafana:master Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants