Skip to content

feat(migration): enhance skipMigration logic and update README for clarity#326

Open
anushkaaaaaaaa wants to merge 1 commit into
kyverno:mainfrom
anushkaaaaaaaa:fix/auto-skip-migration-323
Open

feat(migration): enhance skipMigration logic and update README for clarity#326
anushkaaaaaaaa wants to merge 1 commit into
kyverno:mainfrom
anushkaaaaaaaa:fix/auto-skip-migration-323

Conversation

@anushkaaaaaaaa
Copy link
Copy Markdown

Fixes

Added automatic detection to check if reports already exist in the database with the reports-server annotation. When detected, migration is skipped without requiring user intervention.

The server now queries the database on startup and looks for the kyverno.reports-server.io/served-by: reports-server annotation on existing reports. If found, it means a previous instance already migrated the data so we skip it. Keeps the --skipmigration flag for manual override if needed.

Changes

  • Added shouldSkipMigration() helper that checks database for reports with the annotation
  • Modified migration logic to use automatic detection instead of only checking the flag
  • Updated docs to mention automatic detection behavior

Copilot AI review requested due to automatic review settings January 27, 2026 18:36
…arity

Signed-off-by: Anushka Sharan <anushkasharan05@gmail.com>
@anushkaaaaaaaa anushkaaaaaaaa force-pushed the fix/auto-skip-migration-323 branch from 05bf78a to ada4ff3 Compare January 27, 2026 18:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds automatic detection of previously migrated reports to prevent redundant migrations when the reports-server is upgraded or restarted. The server now checks the database on startup for reports containing the kyverno.reports-server.io/served-by: reports-server annotation and skips migration if found, while maintaining the --skipmigration flag for manual override.

Changes:

  • Added shouldSkipMigration() function to automatically detect if reports with the reports-server annotation already exist in the database
  • Updated migration logic to use automatic detection in addition to the manual flag
  • Fixed spelling errors in migration error messages (changed "mirgrate" to "migrate")
  • Updated documentation to reflect the new automatic detection behavior

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.

File Description
pkg/server/migration.go Adds automatic migration skip detection logic and fixes spelling errors in error messages
pkg/app/opts/options.go Updates flag description to mention automatic detection
charts/reports-server/README.md Updates configuration documentation to mention automatic detection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/server/migration.go
Comment on lines +32 to +64
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}

polrs, err := c.Store.PolicyReports().List(ctx, "")
if err == nil && len(polrs) > 0 {
for _, r := range polrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}
}

if c.APIServices.StoreEphemeralReports {
cephrs, err := c.Store.ClusterEphemeralReports().List(ctx)
if err == nil && len(cephrs) > 0 {
for _, r := range cephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}

ephrs, err := c.Store.EphemeralReports().List(ctx, "")
if err == nil && len(ephrs) > 0 {
for _, r := range ephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The same log message "found existing reports in database with reports-server annotation, skipping migration" is printed on lines 32, 42, 54, and 64. This will result in duplicate log entries if reports exist in multiple categories. Consider logging once at the end of the function or using different messages for each report type to make debugging easier.

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
cpolrs, err := c.Store.ClusterPolicyReports().List(ctx)
if err == nil && len(cpolrs) > 0 {
for _, r := range cpolrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The code accesses r.Annotations without checking if it's nil first. This could cause a nil pointer dereference if a report exists without annotations. The pattern used elsewhere in the codebase (e.g., watch.go lines 38, 74, 110, 146) includes a nil check before accessing the Annotations map. Add a nil check: if r.Annotations != nil && r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
polrs, err := c.Store.PolicyReports().List(ctx, "")
if err == nil && len(polrs) > 0 {
for _, r := range polrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The code accesses r.Annotations without checking if it's nil first. This could cause a nil pointer dereference if a report exists without annotations. The pattern used elsewhere in the codebase (e.g., watch.go lines 38, 74, 110, 146) includes a nil check before accessing the Annotations map. Add a nil check: if r.Annotations != nil && r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
Comment on lines +38 to +46
polrs, err := c.Store.PolicyReports().List(ctx, "")
if err == nil && len(polrs) > 0 {
for _, r := range polrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

Errors from database List operations are silently ignored. If the database is temporarily unavailable or there's a connection issue, the function will return false and proceed with migration, potentially causing duplicate data or migration failures. Consider logging the error or returning an error to the caller to allow proper error handling. Other database operations in the migration function return errors (e.g., line 90, 120).

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
cephrs, err := c.Store.ClusterEphemeralReports().List(ctx)
if err == nil && len(cephrs) > 0 {
for _, r := range cephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The code accesses r.Annotations without checking if it's nil first. This could cause a nil pointer dereference if a report exists without annotations. The pattern used elsewhere in the codebase (e.g., watch.go lines 38, 74, 110, 146) includes a nil check before accessing the Annotations map. Add a nil check: if r.Annotations != nil && r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
ephrs, err := c.Store.EphemeralReports().List(ctx, "")
if err == nil && len(ephrs) > 0 {
for _, r := range ephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The code accesses r.Annotations without checking if it's nil first. This could cause a nil pointer dereference if a report exists without annotations. The pattern used elsewhere in the codebase (e.g., watch.go lines 38, 74, 110, 146) includes a nil check before accessing the Annotations map. Add a nil check: if r.Annotations != nil && r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
Comment on lines +28 to +36
cpolrs, err := c.Store.ClusterPolicyReports().List(ctx)
if err == nil && len(cpolrs) > 0 {
for _, r := range cpolrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

Errors from database List operations are silently ignored. If the database is temporarily unavailable or there's a connection issue, the function will return false and proceed with migration, potentially causing duplicate data or migration failures. Consider logging the error or returning an error to the caller to allow proper error handling. Other database operations in the migration function return errors (e.g., line 90, 120).

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
Comment on lines +50 to +58
cephrs, err := c.Store.ClusterEphemeralReports().List(ctx)
if err == nil && len(cephrs) > 0 {
for _, r := range cephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

Errors from database List operations are silently ignored. If the database is temporarily unavailable or there's a connection issue, the function will return false and proceed with migration, potentially causing duplicate data or migration failures. Consider logging the error or returning an error to the caller to allow proper error handling. Other database operations in the migration function return errors (e.g., line 153, 183).

Copilot uses AI. Check for mistakes.
Comment thread pkg/server/migration.go
Comment on lines +60 to +68
ephrs, err := c.Store.EphemeralReports().List(ctx, "")
if err == nil && len(ephrs) > 0 {
for _, r := range ephrs {
if r.Annotations[api.ServedByReportsServerAnnotation] == api.ServedByReportsServerValue {
klog.Info("found existing reports in database with reports-server annotation, skipping migration")
return true
}
}
}
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

Errors from database List operations are silently ignored. If the database is temporarily unavailable or there's a connection issue, the function will return false and proceed with migration, potentially causing duplicate data or migration failures. Consider logging the error or returning an error to the caller to allow proper error handling. Other database operations in the migration function return errors (e.g., line 153, 183).

Copilot uses AI. Check for mistakes.
@aerosouund
Copy link
Copy Markdown
Member

@anushkaaaaaaaa
thanks for submitting this, can you please resolve copilot's comments ? and also it would be great if you add conditionals on whether an api is enabled in the reports server before listing it and add openreports apis to the logic.

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.

4 participants