Skip to content

Modify inject to warn when file is un-injectable#1603

Merged
siggy merged 1 commit intomasterfrom
siggy/inject-reports
Sep 10, 2018
Merged

Modify inject to warn when file is un-injectable#1603
siggy merged 1 commit intomasterfrom
siggy/inject-reports

Conversation

@siggy
Copy link
Member

@siggy siggy commented Sep 6, 2018

If an input file is un-injectable, existing inject behavior is to simply
output a copy of the input.

Introduce a report, printed to stderr, that communicates the end state
of the inject command. Currently this includes checking for hostNetwork
and unsupported resources.

Malformed YAML documents will continue to cause no YAML output, and return
error code 1.

example outputs...

some pods injected, none with host networking:

hostNetwork: pods do not use host networking...............................[ok]
supported: at least one resource injected..................................[ok]

Summary: 4 of 8 YAML document(s) injected
  deploy/emoji
  deploy/voting
  deploy/web
  deploy/vote-bot

some pods injected, one host networking:

hostNetwork: pods do not use host networking...............................[warn] -- deploy/vote-bot uses "hostNetwork: true"
supported: at least one resource injected..................................[ok]

Summary: 3 of 8 YAML document(s) injected
  deploy/emoji
  deploy/voting
  deploy/web

no pods injected:

hostNetwork: pods do not use host networking...............................[warn] -- deploy/emoji, deploy/voting, deploy/web, deploy/vote-bot use "hostNetwork: true"
supported: at least one resource injected..................................[warn] -- no supported objects found

Summary: 0 of 8 YAML document(s) injected

TODO: check for UDP and other init containers

Part of #1516

Signed-off-by: Andrew Seigner [email protected]

@siggy siggy added the area/cli label Sep 6, 2018
@siggy siggy self-assigned this Sep 6, 2018
@siggy siggy requested a review from klingerf September 6, 2018 19:00
}

if injected > 0 {
report.Write([]byte(fmt.Sprintf("%s %d of %d YAML document(s) injected\n", okStatus, injected, len(injectReports))))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if everything went fine, should we print this at all? another way to put it: do we want to communicate to the user the number of documents that were injected?

@siggy siggy requested a review from grampelberg September 6, 2018 19:53
@siggy siggy force-pushed the siggy/inject-reports branch from 6b0da2c to e8b91ed Compare September 6, 2018 22:22
@siggy siggy force-pushed the siggy/inject-reports branch 2 times, most recently from e58861b to cd5951d Compare September 6, 2018 22:38
@siggy siggy force-pushed the siggy/inject-reports branch from cd5951d to c805a0f Compare September 6, 2018 23:39
@siggy siggy force-pushed the siggy/inject-reports branch 2 times, most recently from a5f2a6c to b696474 Compare September 6, 2018 23:53
Copy link
Contributor

@klingerf klingerf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, and I'm excited to see this feature in action!

This change introduces a small issue with our integration tests, since the LinkerdRun helper method returns combined stdout and stderr from the commands that it runs, and the combined output from the inject command isn't valid yaml.

I think the right fix is for LinkerdRun to return two separate strings, one for stdout and another one for stderr. But unfortunately that will require updating all of the tests that call LinkerdRun to accept or discard the additional return value. On the upside, you could update the install integration test to validate that the generated report is accurate.


return InjectYAML(buf, w, injectOptions)
// throwaway the stderr buffer
return InjectYAML(buf, w, new(bytes.Buffer), injectOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am pretty sure you can use ioutil.Discard here instead of allocating a new buffer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

report := new(bytes.Buffer)

err = InjectYAML(read, output, tc.testInjectOptions)
err = InjectYAML(read, output, report, tc.testInjectOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests for this are really slick -- thanks for adding them!

if err := yaml.Unmarshal(bytes, &om); err != nil {
return nil, err
}
report.name = fmt.Sprintf("%s/%s", k8s.ShortNameFromCanonicalResourceName(strings.ToLower(meta.Kind)), om.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShortNameFromCanonicalResourceName only works for a subset of all Kubernetes object types, so if the inject yaml includes an object type that's not covered by that function, then we won't properly format the name. For instance, if we were trying to inject a Secret named "super", report.name would be "/super".

I don't think this is an issue in the current implementation, however, since we only print the names of objects that include pod specs in the report, and all objects that include pod specs are covered in ShortNameFromCanonicalResourceName. But I'm inclined to switch to using full names so that down the road we know that the name field is printable for all object types.

	report.name = fmt.Sprintf("%s/%s", strings.ToLower(meta.Kind), om.Name)

@siggy siggy force-pushed the siggy/inject-reports branch from 8876f7d to 11d5cde Compare September 7, 2018 20:47
Copy link
Contributor

@klingerf klingerf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐️ Awesome, thanks for updating the integration tests! This all looks good to me.

If an input file is un-injectable, existing inject behavior is to simply
output a copy of the input.

Introduce a report, printed to stderr, that communicates the end state
of the inject command. Currently this includes checking for hostNetwork
and unsupported resources.

Malformed YAML documents will continue to cause no YAML output, and return
error code 1.

This change also modifies integration tests to handle stdout and stderr separately.

example outputs...

some pods injected, none with host networking:

```
hostNetwork: pods do not use host networking...............................[ok]
supported: at least one resource injected..................................[ok]

Summary: 4 of 8 YAML document(s) injected
  deploy/emoji
  deploy/voting
  deploy/web
  deploy/vote-bot
```

some pods injected, one host networking:

```
hostNetwork: pods do not use host networking...............................[warn] -- deploy/vote-bot uses "hostNetwork: true"
supported: at least one resource injected..................................[ok]

Summary: 3 of 8 YAML document(s) injected
  deploy/emoji
  deploy/voting
  deploy/web
```

no pods injected:

```
hostNetwork: pods do not use host networking...............................[warn] -- deploy/emoji, deploy/voting, deploy/web, deploy/vote-bot use "hostNetwork: true"
supported: at least one resource injected..................................[warn] -- no supported objects found

Summary: 0 of 8 YAML document(s) injected
```

TODO: check for UDP and other init containers

Part of #1516

Signed-off-by: Andrew Seigner <[email protected]>
@siggy siggy force-pushed the siggy/inject-reports branch from 11d5cde to 17136cb Compare September 10, 2018 17:17
@siggy siggy merged commit c5a719d into master Sep 10, 2018
@siggy siggy deleted the siggy/inject-reports branch September 10, 2018 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants