Skip to content

Commit

Permalink
Added rebase and export capabilities for all cluster snapshot impleme…
Browse files Browse the repository at this point in the history
…ntations
  • Loading branch information
Duke0404 committed Nov 6, 2024
1 parent 9c91496 commit b742e15
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cluster-autoscaler/simulator/clustersnapshot/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,26 @@ func (snapshot *BasicClusterSnapshot) Clear() {
snapshot.data = []*internalBasicSnapshotData{baseData}
}

// Export returns a shallow copy of the snapshot.
func (snapshot *BasicClusterSnapshot) Export() ClusterSnapshot {
exportedSnapshot := &BasicClusterSnapshot{}
exportedSnapshot.data = make([]*internalBasicSnapshotData, len(snapshot.data))
for i, data := range snapshot.data {
exportedSnapshot.data[i] = data.clone()
}
return exportedSnapshot
}

// Rebase rebases the snapshot to a new base snapshot.
func (snapshot *BasicClusterSnapshot) Rebase(base ClusterSnapshot) error {
baseSnapshot, ok := base.(*BasicClusterSnapshot)
if !ok {
return fmt.Errorf("cannot rebase to different type of snapshot")
}
snapshot.data = []*internalBasicSnapshotData{baseSnapshot.getInternalData().clone(), snapshot.data[len(snapshot.data)-1]}
return nil
}

// implementation of SharedLister interface

type basicClusterSnapshotNodeLister BasicClusterSnapshot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type ClusterSnapshot interface {
Commit() error
// Clear reset cluster snapshot to empty, unforked state.
Clear()
// Export returns a shallow copy of the snapshot.
Export() ClusterSnapshot
// Rebase rebases the snapshot to a new base snapshot.
Rebase(base ClusterSnapshot) error
}

// ErrNodeNotFound means that a node wasn't found in the snapshot.
Expand Down
21 changes: 21 additions & 0 deletions cluster-autoscaler/simulator/clustersnapshot/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
// fork - O(1)
// revert - O(1)
// commit - O(n)
// rebase - O(1)
// export - O(1)
// list all pods (no filtering) - O(n), cached
// list all pods (with filtering) - O(n)
// list node infos - O(n), cached
Expand Down Expand Up @@ -504,3 +506,22 @@ func (snapshot *DeltaClusterSnapshot) Commit() error {
func (snapshot *DeltaClusterSnapshot) Clear() {
snapshot.data = newInternalDeltaSnapshotData()
}

// Export returns a shallow copy of the changes made to the snapshot.
// Time: O(1) (shallow copy)
func (snapshot *DeltaClusterSnapshot) Export() ClusterSnapshot {
return &DeltaClusterSnapshot{
data: snapshot.data,
}
}

// Rebase rebases the snapshot to a new base snapshot.
// Time: O(1)
func (snapshot *DeltaClusterSnapshot) Rebase(base ClusterSnapshot) error {
snapshotBase, ok := base.(*DeltaClusterSnapshot)
if !ok {
return fmt.Errorf("cannot rebase to a different type of snapshot")
}
snapshot.data.baseData = snapshotBase.data
return nil
}

0 comments on commit b742e15

Please sign in to comment.