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

fix: Detect unknown fields in invalid specs as OutOfSync #154

Merged
merged 3 commits into from
Oct 14, 2020

Conversation

jgwest
Copy link
Member

@jgwest jgwest commented Oct 3, 2020

There are a couple of spots in diff.go that are unmarshalling JSON resource data into k8s structs, but in doing so they are silently tossing (invalid) JSON fields that are not present in that struct. In general, this is due to the use of json.Unmarshal(...) (whose default implementation silently ignores extra fields) in conjunction with k8s structs that do not contain those extra fields.

The fix is:

  • Use decoder := json.NewDecoder(strings.NewReader(string(data))) and decoder.DisallowUnknownFields() to detect unknown fields when remarshalling
  • When applyPatching against a Kubernetes resource, indirectly apply the result of the scheme default functions to the target JSON, rather than directly marshalling the scheme function results to the k8s resource (which would cause those extra fields to be discarded)

See code comments for details.

Fixes argoproj/argo-cd#1787

@codecov-commenter
Copy link

Codecov Report

Merging #154 into master will increase coverage by 0.24%.
The diff coverage is 54.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #154      +/-   ##
==========================================
+ Coverage   53.79%   54.03%   +0.24%     
==========================================
  Files          25       25              
  Lines        2400     2439      +39     
==========================================
+ Hits         1291     1318      +27     
- Misses        974      976       +2     
- Partials      135      145      +10     
Impacted Files Coverage Δ
pkg/diff/diff.go 63.06% <54.28%> (-0.43%) ⬇️
pkg/health/health.go 55.40% <0.00%> (-0.60%) ⬇️
pkg/sync/sync_context.go 70.36% <0.00%> (+0.24%) ⬆️
pkg/cache/cluster.go 49.87% <0.00%> (+1.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d05efd...9bc80c7. Read the comment docs.

@sonarcloud
Copy link

sonarcloud bot commented Oct 14, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 1 Code Smell

No Coverage information No Coverage information
24.1% 24.1% Duplication

@codecov-io
Copy link

Codecov Report

Merging #154 into master will increase coverage by 0.10%.
The diff coverage is 54.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #154      +/-   ##
==========================================
+ Coverage   48.14%   48.25%   +0.10%     
==========================================
  Files          36       36              
  Lines        2858     2887      +29     
==========================================
+ Hits         1376     1393      +17     
- Misses       1339     1341       +2     
- Partials      143      153      +10     
Impacted Files Coverage Δ
pkg/diff/diff.go 61.45% <54.28%> (-0.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a1dc4c5...7d2fbb1. Read the comment docs.

Copy link
Contributor

@alexmt alexmt left a comment

Choose a reason for hiding this comment

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

Awesome solution @jgwest ! I did not think it is even possible to support unknown fields. Thank you!

LGTM

@alexmt alexmt merged commit 872c470 into argoproj:master Oct 14, 2020
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.

Argo CD should detect unknown fields in invalid specs as OutOfSync
4 participants