Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions otdfctl/migrations/namespacedpolicy/prune_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,11 @@ func (p *PruneSubjectMappingPlan) pruneDetails(styled bool, styles *migrations.D
}

func (p *PruneRegisteredResourcePlan) pruneDetails(styled bool, styles *migrations.DisplayStyles) []string {
details := []string{
return []string{
formatPruneDetail("source_id", pruneIDValue(styled, styles, p.Source.GetId())),
registeredResourceSourcePruneDetail("source", p.Source, styled, styles),
targetRefPruneDetail("found_migrated_target", p.MigratedTarget, styled, styles),
}
if !styled && p.Reason.Type == PruneStatusReasonTypeRegisteredResourceSourceMismatch {
details = append(details, formatPruneDetail("full_source", plainRegisteredResourceSourceSummary(p.FullSource)))
}
return details
}

func (p *PruneObligationTriggerPlan) pruneDetails(styled bool, styles *migrations.DisplayStyles) []string {
Expand Down
14 changes: 0 additions & 14 deletions otdfctl/migrations/namespacedpolicy/prune_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,10 @@ func mixedPrunePlan(scope Scope) *PrunePlan {
RegisteredResources: []*PruneRegisteredResourcePlan{
{
Source: &policy.RegisteredResource{Id: "resource-delete-1"},
FullSource: &policy.RegisteredResource{
Id: "resource-delete-1",
Values: []*policy.RegisteredResourceValue{
{Id: "value-1"},
{Id: "value-2"},
},
},
Status: PruneStatusDelete,
},
{
Source: &policy.RegisteredResource{Id: "resource-delete-2"},
FullSource: &policy.RegisteredResource{
Id: "resource-delete-2",
Values: []*policy.RegisteredResourceValue{
{Id: "value-3"},
{Id: "value-4"},
},
},
Status: PruneStatusDelete,
},
{
Expand Down
40 changes: 19 additions & 21 deletions otdfctl/migrations/namespacedpolicy/prune_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ const (
type PruneStatusReasonType string

const (
PruneStatusReasonTypeMigratedTargetNotFound PruneStatusReasonType = "MigratedTargetNotFound"
PruneStatusReasonTypeNoMatchingLabelsFound PruneStatusReasonType = "NoMatchingLabelsFound"
PruneStatusReasonTypeMismatchedMigrationLabel PruneStatusReasonType = "MismatchedMigrationLabel"
PruneStatusReasonTypeMissingMigrationLabel PruneStatusReasonType = "MissingMigrationLabel"
PruneStatusReasonTypeInUse PruneStatusReasonType = "InUse"
PruneStatusReasonTypeNeedsMigration PruneStatusReasonType = "NeedsMigration"
PruneStatusReasonTypeRegisteredResourceSourceMismatch PruneStatusReasonType = "RegisteredResourceSourceMismatch"
PruneStatusReasonTypeSkippedByUser PruneStatusReasonType = "SkippedByUser"

pruneStatusReasonMessageMigratedTargetNotFound = "no migrated target was found for this source"
pruneStatusReasonMessageInUse = "source object is still referenced by legacy policy"
pruneStatusReasonMessageNoMatchingLabelsFound = "canonical migrated targets were found, but none carry migrated_from for this source"
pruneStatusReasonMessageMismatchedMigrationLabel = "migrated target carries migrated_from metadata for a different source"
pruneStatusReasonMessageMissingMigrationLabel = "migrated target is missing migrated_from metadata for this source"
pruneStatusReasonMessageNeedsMigration = "source object does not have a migrated target yet"
pruneStatusReasonMessageSkippedByUser = "skipped by user"
pruneStatusReasonMessageRegisteredResourceSourceMismatchFmt = "resolved registered resource view does not match the full source object for target namespace %q; source contains values outside the resolved migration view"
PruneStatusReasonTypeMigratedTargetNotFound PruneStatusReasonType = "MigratedTargetNotFound"
PruneStatusReasonTypeNoMatchingLabelsFound PruneStatusReasonType = "NoMatchingLabelsFound"
PruneStatusReasonTypeMismatchedMigrationLabel PruneStatusReasonType = "MismatchedMigrationLabel"
PruneStatusReasonTypeMissingMigrationLabel PruneStatusReasonType = "MissingMigrationLabel"
PruneStatusReasonTypeInUse PruneStatusReasonType = "InUse"
PruneStatusReasonTypeNeedsMigration PruneStatusReasonType = "NeedsMigration"
PruneStatusReasonTypeMultiNamespaceManualDelete PruneStatusReasonType = "MultiNamespaceManualDelete"
PruneStatusReasonTypeSkippedByUser PruneStatusReasonType = "SkippedByUser"

pruneStatusReasonMessageMigratedTargetNotFound = "no migrated target was found for this source"
pruneStatusReasonMessageInUse = "source object is still referenced by legacy policy"
pruneStatusReasonMessageNoMatchingLabelsFound = "canonical migrated targets were found, but none carry migrated_from for this source"
pruneStatusReasonMessageMismatchedMigrationLabel = "migrated target carries migrated_from metadata for a different source"
pruneStatusReasonMessageMissingMigrationLabel = "migrated target is missing migrated_from metadata for this source"
pruneStatusReasonMessageNeedsMigration = "source object does not have a migrated target yet"
pruneStatusReasonMessageMultiNamespaceManualDelete = "registered resource spans multiple target namespaces and was not migrated; must be deleted manually"
pruneStatusReasonMessageSkippedByUser = "skipped by user"
)

type PruneStatusReason struct {
Expand Down Expand Up @@ -310,10 +310,8 @@ func (p *PruneSubjectMappingPlan) setExecution(execution *ExecutionResult) {
// for deletion and the single migrated target RR matched to it by migration
// metadata.
type PruneRegisteredResourcePlan struct {
// Source is the resolved RR source from planning and may be filtered by interactive review.
Source *policy.RegisteredResource `json:"source"`
// FullSource is the authoritative RR source reloaded from the global namespace for prune verification.
FullSource *policy.RegisteredResource `json:"full_source,omitempty"`
// Source is the legacy RR source being considered for deletion.
Source *policy.RegisteredResource `json:"source"`
Status PruneStatus `json:"status"`
MigratedTarget TargetRef `json:"migrated_target,omitzero"`
Reason PruneStatusReason `json:"reason,omitzero"`
Expand Down
124 changes: 28 additions & 96 deletions otdfctl/migrations/namespacedpolicy/prune_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
var (
ErrMultiplePruneScopes = errors.New("prune planner accepts exactly one scope")
ErrInvalidPruneResolvedTarget = errors.New("invalid prune resolved target")
ErrInvalidPruneResolvedSource = errors.New("invalid prune resolved source")
)

// PrunePlanner classifies whether legacy policy objects can be deleted after
Expand All @@ -35,7 +34,6 @@ type PrunePlanner struct {

type prunePlannerConfig struct {
pageSize int32
reviewer InteractiveReviewer
}

type PruneOption func(*prunePlannerConfig)
Expand All @@ -55,9 +53,9 @@ type pruneMigratedObject interface {
}

// NewPrunePlanner constructs a single-scope prune planner on top of the shared
// migration planner infrastructure. Interactive review is still supported for
// the resolved-object scopes, and direct-prune scopes reuse the same retriever
// and namespace discovery logic.
// migration planner infrastructure. Direct-prune scopes reuse the same
// retriever and namespace discovery logic, while resolved-object scopes reuse
// the resolver state without planner-time interactive review.
func NewPrunePlanner(handler PolicyClient, scopeCSV string, opts ...PruneOption) (*PrunePlanner, error) {
if handler == nil {
return nil, ErrNilPlannerHandler
Expand All @@ -84,12 +82,7 @@ func NewPrunePlanner(handler PolicyClient, scopeCSV string, opts ...PruneOption)
config.pageSize = defaultPlannerPageSize
}

plannerOpts := []Option{WithPageSize(config.pageSize)}
if config.reviewer != nil {
plannerOpts = append(plannerOpts, WithInteractiveReviewer(config.reviewer))
}

planner, err := NewPlanner(handler, scopeCSV, plannerOpts...)
planner, err := NewPlanner(handler, scopeCSV, WithPageSize(config.pageSize))
if err != nil {
return nil, err
}
Expand All @@ -106,12 +99,6 @@ func WithPrunePageSize(pageSize int32) PruneOption {
}
}

func WithPruneInteractiveReviewer(reviewer InteractiveReviewer) PruneOption {
return func(config *prunePlannerConfig) {
config.reviewer = reviewer
}
}

// Plan produces a prune plan for the configured scope.
//
// Actions and subject condition sets bypass resolved migration output and are
Expand Down Expand Up @@ -141,12 +128,7 @@ func (p *PrunePlanner) Plan(ctx context.Context) (*PrunePlan, error) {
return nil, ErrNilResolvedTargets
}

sourceRegisteredResources, err := p.sourceRegisteredResources(ctx)
if err != nil {
return nil, err
}

return buildPrunePlanFromResolved(p.scopes, resolved, sourceRegisteredResources)
return buildPrunePlanFromResolved(p.scopes, resolved)
}

func (p *PrunePlanner) planActions(ctx context.Context) (*PrunePlan, error) {
Expand Down Expand Up @@ -252,19 +234,6 @@ func (p *PrunePlanner) planSubjectConditionSets(ctx context.Context) (*PrunePlan
return plan, nil
}

func (p *PrunePlanner) sourceRegisteredResources(ctx context.Context) (map[string]*policy.RegisteredResource, error) {
if !p.scopes.has(ScopeRegisteredResources) {
return map[string]*policy.RegisteredResource{}, nil
}

resources, err := p.planner.retriever.retrieveRegisteredResources(ctx)
if err != nil {
return nil, err
}

return sourceRegisteredResourcesByID(resources), nil
}

func (p *PrunePlanner) usedLegacyActionsByID(ctx context.Context, sourceIDs map[string]struct{}) (map[string]struct{}, error) {
used := make(map[string]struct{}, len(sourceIDs))
if len(sourceIDs) == 0 {
Expand Down Expand Up @@ -351,26 +320,24 @@ func (p *PrunePlanner) usedLegacySubjectConditionSetsByID(ctx context.Context, s
return used, nil
}

func buildPrunePlanFromResolved(scopes scopeSet, resolved *ResolvedTargets, sourceRegisteredResources map[string]*policy.RegisteredResource) (*PrunePlan, error) {
func buildPrunePlanFromResolved(scopes scopeSet, resolved *ResolvedTargets) (*PrunePlan, error) {
if resolved == nil {
return &PrunePlan{}, nil
}

builder := newPrunePlanBuilder(scopes, resolved, sourceRegisteredResources)
builder := newPrunePlanBuilder(scopes, resolved)
return builder.build()
}

type prunePlanBuilder struct {
scopes scopeSet
resolved *ResolvedTargets
sourceRegisteredResources map[string]*policy.RegisteredResource
scopes scopeSet
resolved *ResolvedTargets
}

func newPrunePlanBuilder(scopes scopeSet, resolved *ResolvedTargets, sourceRegisteredResources map[string]*policy.RegisteredResource) *prunePlanBuilder {
func newPrunePlanBuilder(scopes scopeSet, resolved *ResolvedTargets) *prunePlanBuilder {
return &prunePlanBuilder{
scopes: scopes,
resolved: resolved,
sourceRegisteredResources: sourceRegisteredResources,
scopes: scopes,
resolved: resolved,
}
}

Expand Down Expand Up @@ -425,11 +392,10 @@ func (b *prunePlanBuilder) subjectMappings() ([]*PruneSubjectMappingPlan, error)
return plans, nil
}

// registeredResources verifies prune safety against the authoritative source RR.
// It first reloads the full source RR and marks the plan unresolved if the
// planner's resolved source is only a filtered view. For a full-source match, it
// then classifies the RR based on whether a migrated target exists and whether
// that target carries the expected migration metadata for the source RR.
// registeredResources classifies each resolved RR prune decision directly from
// the resolved migration state. Multi-namespace legacy RRs are blocked when no
// migrated target exists because the migration planner cannot determine a single
// target namespace for them and prune cannot safely auto-delete them.
func (b *prunePlanBuilder) registeredResources() ([]*PruneRegisteredResourcePlan, error) {
plans := make([]*PruneRegisteredResourcePlan, 0, len(b.resolved.RegisteredResources))

Expand All @@ -441,33 +407,26 @@ func (b *prunePlanBuilder) registeredResources() ([]*PruneRegisteredResourcePlan
continue
}

fullSource, err := b.registeredResourceSource(resource)
if err != nil {
return nil, err
}

if !registeredResourceCanonicalEqual(resource.Source, fullSource) {
if pruneManualDeleteRequired(resource) {
plans = append(plans, &PruneRegisteredResourcePlan{
Source: resource.Source,
FullSource: fullSource,
Status: PruneStatusUnresolved,
Status: PruneStatusBlocked,
MigratedTarget: migratedTarget(resource.AlreadyMigrated, resource.Namespace),
Reason: newPruneReasonf(
PruneStatusReasonTypeRegisteredResourceSourceMismatch,
pruneStatusReasonMessageRegisteredResourceSourceMismatchFmt,
namespaceLabel(resource.Namespace)),
Reason: newPruneReason(
PruneStatusReasonTypeMultiNamespaceManualDelete,
pruneStatusReasonMessageMultiNamespaceManualDelete,
),
})
continue
}

status, reason, err := pruneStatusForRegisteredResource(fullSource, resource.AlreadyMigrated)
status, reason, err := pruneStatusForRegisteredResource(resource.Source, resource.AlreadyMigrated)
if err != nil {
return nil, fmt.Errorf("registered resource %q: %w", resource.Source.GetId(), err)
}

plans = append(plans, &PruneRegisteredResourcePlan{
Source: resource.Source,
FullSource: fullSource,
Status: status,
MigratedTarget: migratedTarget(resource.AlreadyMigrated, resource.Namespace),
Reason: reason,
Expand All @@ -477,23 +436,11 @@ func (b *prunePlanBuilder) registeredResources() ([]*PruneRegisteredResourcePlan
return plans, nil
}

func (b *prunePlanBuilder) registeredResourceSource(resource *ResolvedRegisteredResource) (*policy.RegisteredResource, error) {
source := resource.Source
if b.sourceRegisteredResources == nil {
return nil, fmt.Errorf("%w: source registered resource verifier was not loaded", ErrInvalidPruneResolvedSource)
}

sourceID := source.GetId()
if sourceID == "" {
return nil, fmt.Errorf("%w: registered resource source has empty id", ErrInvalidPruneResolvedSource)
}

fullSource := b.sourceRegisteredResources[sourceID]
if fullSource == nil {
return nil, fmt.Errorf("%w: registered resource source %q not found during prune verification", ErrInvalidPruneResolvedSource, sourceID)
}

return fullSource, nil
func pruneManualDeleteRequired(resource *ResolvedRegisteredResource) bool {
return resource != nil &&
resource.AlreadyMigrated == nil &&
resource.Unresolved != nil &&
resource.Unresolved.Reason == UnresolvedReasonRegisteredResourceConflictingNamespaces
}

func (b *prunePlanBuilder) obligationTriggers() ([]*PruneObligationTriggerPlan, error) {
Expand All @@ -519,17 +466,6 @@ func (b *prunePlanBuilder) obligationTriggers() ([]*PruneObligationTriggerPlan,
return plans, nil
}

func sourceRegisteredResourcesByID(resources []*policy.RegisteredResource) map[string]*policy.RegisteredResource {
byID := make(map[string]*policy.RegisteredResource, len(resources))
for _, resource := range resources {
if resource == nil || resource.GetId() == "" {
continue
}
byID[resource.GetId()] = resource
}
return byID
}

func customLegacyActions(actions []*policy.Action) []*policy.Action {
custom := make([]*policy.Action, 0, len(actions))
for _, action := range actions {
Expand Down Expand Up @@ -699,10 +635,6 @@ func newPruneReason(reasonType PruneStatusReasonType, message string) PruneStatu
}
}

func newPruneReasonf(reasonType PruneStatusReasonType, format string, args ...any) PruneStatusReason {
return newPruneReason(reasonType, fmt.Sprintf(format, args...))
}

func pruneStatusReasonForMigrationLabel(target pruneObject) PruneStatusReason {
if migratedFromID(target) == "" {
return newPruneReason(PruneStatusReasonTypeMissingMigrationLabel, pruneStatusReasonMessageMissingMigrationLabel)
Expand Down
Loading
Loading