Skip to content

feat: [FEP-0001] add placement resource snapshot manager (1/) - #844

Open
Chen Yu (michaelawyu) wants to merge 4 commits into
kubefleet-dev:mainfrom
michaelawyu:feat/placement-resource-snapshot-manager
Open

feat: [FEP-0001] add placement resource snapshot manager (1/)#844
Chen Yu (michaelawyu) wants to merge 4 commits into
kubefleet-dev:mainfrom
michaelawyu:feat/placement-resource-snapshot-manager

Conversation

@michaelawyu

Copy link
Copy Markdown
Member

Description of your changes

This pull request introduces a new Placement Resource Snapshot Manager for KubeFleet, providing a robust foundation for managing placement resource snapshots. It also adds new accessor interfaces for placement-related resources, constants for snapshot labeling, and improvements to the informer manager utility. The changes enhance modularity, maintainability, and clarity in handling placement resource snapshots and their indices.

The most important changes are:

Placement Resource Snapshot Manager:

  • Added a new Manager in pkg/v1/managers/placementresourcesnapshot/manager.go responsible for managing placement resource snapshots, including setup of custom field indices for efficient querying and per-placement-policy locking for concurrency control.
  • Added snapshot creation helpers (primaryPlacementResourceSnapshot and secondaryPlacementResourceSnapshot) in pkg/v1/managers/placementresourcesnapshot/snapshots.go for constructing and labeling snapshot resources with proper controller references.

Placement Resource Snapshot Labeling and Accessors:

  • Introduced new constants in apis/kubefleet.dev/placement/v1alpha1/placementresourcesnapshot_types.go for standardized snapshot labeling and annotation, enabling consistent identification and management of placement resource snapshots and their indices/sub-indices.
  • Added accessor interfaces and implementations in apis/kubefleet.dev/placement/v1alpha1/interface.go to provide unified access to the spec and status of placement policies, resource snapshots, and bindings for both namespace-scoped and cluster-scoped resources.

Informer Manager Utility Improvements:

  • Enhanced the informer manager in pkg/utils/informer/informermanager.go by documenting side effects of implicit informer creation, adding an IsInformerSet method to check if an informer is set up for a resource, and implementing the method for safer informer usage. [1] [2] [3]

Related to #787

I have:

  • Associated this change with a known KubeFleet Issue (Bug, Feature, etc).
  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

For size control reasons, tests have been skipped in this PR. Will add in subsequent PRs.

Special notes for your reviewer

Signed-off-by: michaelawyu <chenyu1@microsoft.com>
Signed-off-by: michaelawyu <chenyu1@microsoft.com>
Signed-off-by: michaelawyu <chenyu1@microsoft.com>
@michaelawyu
Chen Yu (michaelawyu) requested a review from a team as a code owner August 20, 2026 21:21
@michaelawyu

Copy link
Copy Markdown
Member Author

Important:

  • The PR depends on the accessor interface PR to be merged (feat: [FEP-0001] add accessor interface for placement policy and related APIs #827); will rebase when that PR is merged.
  • Tests are omitted from this PR due to size control reasons; they will be submitted separately.
  • Certain logic (block reserved namespaces/resources, strip fields from specific resource types) is omitted form this PR; will submit them separately.

Signed-off-by: michaelawyu <chenyu1@microsoft.com>
@michaelawyu
Chen Yu (michaelawyu) requested a lite review from Copilot August 20, 2026 21:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces the v1 Placement Resource Snapshot Manager for KubeFleet, adding snapshot creation/retrieval logic (including cache indexing and per-placement locking) and extending the dynamic informer manager with an explicit “is informer registered” check to avoid unintended informer creation.

Changes:

  • Added a new placementresourcesnapshot manager (setup, locking, resource retrieval/hashing, snapshot ops, and name generation helpers).
  • Standardized labeling/annotation keys for placement resource snapshots and added accessor interfaces for placement policy/snapshot/binding types (namespaced + cluster-scoped).
  • Enhanced the dynamic informer manager with IsInformerSet and documented implicit informer-creation side effects.

Reviewed changes

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

Show a summary per file
File Description
test/utils/informer/manager.go Extends FakeManager to satisfy the updated informer Manager interface (IsInformerSet).
pkg/v1/managers/placementresourcesnapshot/uniquename.go Adds snapshot unique-name generation with truncation + hash behavior.
pkg/v1/managers/placementresourcesnapshot/snapshots.go Adds helpers to construct primary/secondary snapshot objects with labels/annotations and controller refs.
pkg/v1/managers/placementresourcesnapshot/resources.go Implements selected-resource retrieval (cache-first) and deterministic hashing, plus snapshot-size grouping.
pkg/v1/managers/placementresourcesnapshot/ops.go Implements latest-snapshot retrieval, up-to-date checks (dry-run), and snapshot creation/orphan cleanup.
pkg/v1/managers/placementresourcesnapshot/manager.go Adds manager wiring, cache field indices, and per-placement sharded locking.
pkg/utils/informer/informermanager.go Documents implicit informer creation and adds IsInformerSet implementation.
apis/kubefleet.dev/placement/v1alpha1/placementresourcesnapshot_types.go Introduces labeling/annotation constants for placement resource snapshots.
apis/kubefleet.dev/placement/v1alpha1/interface.go Adds accessor interfaces + implementations for placement policy/snapshot/binding resources.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +214 to +225
latestPrimarySnapshot := snapshots[len(snapshots)-1]
// Check if there are snapshots with the same index.
subIndexedSnapshotCntStr := latestPrimarySnapshot.GetLabels()[placementv1alpha1.SubIndexedPlacementResourceSnapshotCountLabelKey]
if subIndexedSnapshotCntStr == "1" {
// The primary placement resource snapshot is the only snapshot with the latest index; return it.
return []placementv1alpha1.PlacementResourceSnapshotAccessor{latestPrimarySnapshot}, nil
}
subIndexedSnapshotCnt, err := strconv.Atoi(subIndexedSnapshotCntStr)
if err != nil {
return nil, errors.NewUnexpectedError(err, "failed to convert sub-indexed placement resource snapshot count label to integer",
"placementResourceSnapshot", klog.KObj(latestPrimarySnapshot))
}
Comment on lines +62 to +65
// `[PLACEMENT-POLICY-NAME-TRUNCATED]-resource-snapshot-[INDEX-TRUNCATED]-[HASH]`,
//
// where `[HASH]` is the first few characters of the hash of the value
// `[PLACEMENT-POLICY-NAME-TRUNCATED]-resource-snapshot-[SNAPSHOT-INDEX]-[SNAPSHOT-SUB-INDEX]`.
Comment on lines +53 to +55
// IsInformerSet returns if an informer has been set up for the given resource.
IsInformerSet(gvk schema.GroupVersionKind) bool

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.

2 participants