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

Implement the sync state calculation for the kubernetes livestate plugin #5676

Merged
merged 8 commits into from
Mar 19, 2025

Conversation

Warashi
Copy link
Contributor

@Warashi Warashi commented Mar 17, 2025

What this PR does:

as title

Why we need it:

We want to support DriftDetection feature with k8s plugin

Which issue(s) this PR fixes:

Part of #5363

Does this PR introduce a user-facing change?: No

  • How are users affected by this change:
  • Is this breaking change:
  • How to migrate (if breaking change):

Copy link

codecov bot commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 41.55844% with 45 lines in your changes missing coverage. Please review.

Project coverage is 25.68%. Comparing base (317a7c9) to head (3278eba).
Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
.../app/pipedv1/plugin/kubernetes/livestate/plugin.go 41.55% 44 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5676      +/-   ##
==========================================
- Coverage   25.73%   25.68%   -0.06%     
==========================================
  Files         478      476       -2     
  Lines       51400    50973     -427     
==========================================
- Hits        13230    13092     -138     
+ Misses      37166    36890     -276     
+ Partials     1004      991      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Warashi added 3 commits March 17, 2025 17:02
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
}, nil
}

func calculateSyncState(diffResult *provider.DiffListResult, commit string) sdk.ApplicationSyncState {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reference implementation is here

func makeSyncState(r *provider.DiffListResult, commit string) model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
ShortReason: "",
Reason: "",
Timestamp: time.Now().Unix(),
}
}
total := len(r.Adds) + len(r.Deletes) + len(r.Changes)
shortReason := fmt.Sprintf("There are %d manifests not synced (%d adds, %d deletes, %d changes)", total, len(r.Adds), len(r.Deletes), len(r.Changes))
if len(commit) >= 7 {
commit = commit[:7]
}
var b strings.Builder
b.WriteString(fmt.Sprintf("Diff between the defined state in Git at commit %s and actual state in cluster:\n\n", commit))
b.WriteString("--- Actual (LiveState)\n+++ Expected (Git)\n\n")
details := r.Render(provider.DiffRenderOptions{
MaskSecret: true,
MaskConfigMap: true,
MaxChangedManifests: 3,
// Currently, we do not use the diff command to render the result
// because Kubernetes adds a large number of default values to the
// running manifest that causes a wrong diff text.
UseDiffCommand: false,
})
b.WriteString(details)
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Timestamp: time.Now().Unix(),
}
}

@Warashi Warashi marked this pull request as ready for review March 18, 2025 08:34
},
},
{
name: "service and ingress changes",
Copy link
Member

Choose a reason for hiding this comment

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

[ask] Does the logic for calculateSyncState depend on each resource kind?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ConfigMap and Secrets are masked. The other kinds are treated as the same.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Then, we can fix the test case name to clarify the spec for the function.
For example "changed two resources". WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, it's a good name to show the spec.
Also, I'll change the second test case, deployment spec changes, to changed one resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed on this commit
9a4b84a

}
}

func TestCalculateSyncState(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

[nit] It would be nice to add some testcases to cover the spec.

  • The maximum count of the changes is three.
  • ConfigMap and Secret are masked on the text

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!
Added at this commit 2306bc9

Warashi added 2 commits March 19, 2025 15:33
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
Copy link
Member

@ffjlabo ffjlabo left a comment

Choose a reason for hiding this comment

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

Sorry, just only one more nit 🙏

Co-authored-by: Yoshiki Fujikane <[email protected]>
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
@Warashi Warashi requested a review from ffjlabo March 19, 2025 06:44
Copy link
Member

@ffjlabo ffjlabo left a comment

Choose a reason for hiding this comment

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

LGTM

@Warashi Warashi enabled auto-merge (squash) March 19, 2025 06:46
@Warashi Warashi merged commit 5079ca7 into master Mar 19, 2025
17 of 18 checks passed
@Warashi Warashi deleted the k8s-plugin-livestate-api-drifts branch March 19, 2025 06:51
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.

3 participants