-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
737 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module sigs.k8s.io/karpenter | ||
|
||
go 1.23.6 | ||
go 1.24.0 | ||
|
||
require ( | ||
github.com/Pallinder/go-randomdata v1.2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package fake | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/awslabs/operatorpkg/option" | ||
"github.com/samber/lo" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"sigs.k8s.io/karpenter/pkg/cloudprovider" | ||
) | ||
|
||
type ReservationManagerProvider struct { | ||
snapshots map[types.UID]*snapshot | ||
capacity map[string]int // map[offering name]total capacity | ||
} | ||
|
||
type snapshot struct { | ||
reservations map[string]sets.Set[string] // map[reservation id]set[offering name] | ||
capacity map[string]int | ||
} | ||
|
||
func NewReservationManagerProvider() *ReservationManagerProvider { | ||
return &ReservationManagerProvider{ | ||
snapshots: map[types.UID]*snapshot{}, | ||
capacity: map[string]int{}, | ||
} | ||
} | ||
|
||
// SetCapacity sets the total number of instances available for a given reservationID. This value will be decremented | ||
// internally each time an instance is launched for the given reservationID. | ||
func (p *ReservationManagerProvider) SetCapacity(reservationID string, capacity int) { | ||
p.capacity[reservationID] = capacity | ||
} | ||
|
||
// Capacity returns the total number of instances | ||
func (p *ReservationManagerProvider) Capacity(reservationID string) int { | ||
return p.capacity[reservationID] | ||
} | ||
|
||
// create decrements the availability for the given reservationID by one. | ||
func (p *ReservationManagerProvider) create(reservationID string) { | ||
lo.Must0(p.capacity[reservationID] > 0, "created an instance with an offering with no availability") | ||
p.capacity[reservationID] -= 1 | ||
if p.capacity[reservationID] == 0 { | ||
|
||
} | ||
} | ||
|
||
// getSnapshot returns an existing snapshot, if one exists for the given UUID, or creates a new one | ||
func (p *ReservationManagerProvider) getSnapshot(uuid *types.UID) *snapshot { | ||
if uuid != nil { | ||
if snapshot, ok := p.snapshots[*uuid]; ok { | ||
return snapshot | ||
} | ||
} | ||
snapshot := &snapshot{ | ||
reservations: map[string]sets.Set[string]{}, | ||
capacity: map[string]int{}, | ||
} | ||
maps.Copy(snapshot.capacity, p.capacity) | ||
if uuid != nil { | ||
p.snapshots[*uuid] = snapshot | ||
} | ||
return snapshot | ||
} | ||
|
||
func (p *ReservationManagerProvider) Reset() { | ||
*p = *NewReservationManagerProvider() | ||
} | ||
|
||
func (p *ReservationManagerProvider) ReservationManager(reservationID string, opts ...option.Function[cloudprovider.GetInstanceTypeOptions]) cloudprovider.ReservationManager { | ||
return snapshotAdapter{ | ||
snapshot: p.getSnapshot(option.Resolve(opts...).AvailabilitySnapshotUUID), | ||
reservationID: reservationID, | ||
} | ||
} | ||
|
||
type snapshotAdapter struct { | ||
*snapshot | ||
reservationID string | ||
} | ||
|
||
func (a snapshotAdapter) Reserve(reservationID string) bool { | ||
if reservations, ok := a.reservations[reservationID]; ok && reservations.Has(a.reservationID) { | ||
return true | ||
} | ||
if a.capacity[a.reservationID] > 0 { | ||
reservations, ok := a.reservations[reservationID] | ||
if !ok { | ||
reservations = sets.New[string]() | ||
a.reservations[reservationID] = reservations | ||
} | ||
reservations.Insert(a.reservationID) | ||
a.capacity[a.reservationID] -= 1 | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (a snapshotAdapter) Release(reservationID string) { | ||
if reservations, ok := a.reservations[reservationID]; ok && reservations.Has(a.reservationID) { | ||
reservations.Delete(a.reservationID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.