|
| 1 | +/* |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +package resourcetypes |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 22 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 23 | + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 24 | + "github.com/samber/lo" |
| 25 | + "go.uber.org/multierr" |
| 26 | +) |
| 27 | + |
| 28 | +type CapacityReservation struct { |
| 29 | + ec2Client *ec2.Client |
| 30 | +} |
| 31 | + |
| 32 | +func NewCapacityReservation(ec2Client *ec2.Client) *CapacityReservation { |
| 33 | + return &CapacityReservation{ec2Client: ec2Client} |
| 34 | +} |
| 35 | + |
| 36 | +func (cr *CapacityReservation) String() string { |
| 37 | + return "CapacityReservations" |
| 38 | +} |
| 39 | + |
| 40 | +func (cr *CapacityReservation) Global() bool { |
| 41 | + return false |
| 42 | +} |
| 43 | + |
| 44 | +func (cr *CapacityReservation) GetExpired(ctx context.Context, expirationTime time.Time, excludedClusters []string) (ids []string, err error) { |
| 45 | + reservations, err := cr.getAllCapacityReservations(ctx, &ec2.DescribeCapacityReservationsInput{ |
| 46 | + Filters: []ec2types.Filter{ |
| 47 | + { |
| 48 | + Name: lo.ToPtr("state"), |
| 49 | + Values: []string{string(ec2types.CapacityReservationStateActive)}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return ids, err |
| 55 | + } |
| 56 | + |
| 57 | + reservations = lo.Filter(reservations, func(reservation ec2types.CapacityReservation, _ int) bool { |
| 58 | + return lo.FromPtr(reservation.CreateDate).Before(expirationTime) |
| 59 | + }) |
| 60 | + ids = lo.Map(reservations, func(reservation ec2types.CapacityReservation, _ int) string { |
| 61 | + return lo.FromPtr(reservation.CapacityReservationId) |
| 62 | + }) |
| 63 | + |
| 64 | + return ids, err |
| 65 | +} |
| 66 | + |
| 67 | +func (cr *CapacityReservation) CountAll(ctx context.Context) (count int, err error) { |
| 68 | + reservations, err := cr.getAllCapacityReservations(ctx, &ec2.DescribeCapacityReservationsInput{ |
| 69 | + Filters: []ec2types.Filter{ |
| 70 | + { |
| 71 | + Name: lo.ToPtr("state"), |
| 72 | + Values: []string{ |
| 73 | + string(ec2types.CapacityReservationStateActive), |
| 74 | + string(ec2types.CapacityReservationStatePending), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return count, err |
| 81 | + } |
| 82 | + |
| 83 | + return len(reservations), err |
| 84 | +} |
| 85 | + |
| 86 | +func (cr *CapacityReservation) Get(ctx context.Context, clusterName string) (ids []string, err error) { |
| 87 | + return ids, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Cleanup cancels capacity reservations. For IODCRs, it sets the allocation to 0. |
| 91 | +// For ODCRs the reservations are cancelled. |
| 92 | +func (cr *CapacityReservation) Cleanup(ctx context.Context, ids []string) ([]string, error) { |
| 93 | + if len(ids) == 0 { |
| 94 | + return nil, nil |
| 95 | + } |
| 96 | + |
| 97 | + out, err := cr.ec2Client.DescribeCapacityReservations(ctx, &ec2.DescribeCapacityReservationsInput{ |
| 98 | + CapacityReservationIds: ids, |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + reservationMap := lo.SliceToMap(out.CapacityReservations, func(reservation ec2types.CapacityReservation) (string, ec2types.CapacityReservation) { |
| 105 | + return lo.FromPtr(reservation.CapacityReservationId), reservation |
| 106 | + }) |
| 107 | + |
| 108 | + var iodcrIDs []string |
| 109 | + var odcrIDs []string |
| 110 | + for _, id := range ids { |
| 111 | + if reservation, exists := reservationMap[id]; exists { |
| 112 | + if reservation.Interruptible != nil && lo.FromPtr(reservation.Interruptible) { |
| 113 | + iodcrIDs = append(iodcrIDs, id) |
| 114 | + } else { |
| 115 | + odcrIDs = append(odcrIDs, id) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + cleaned := make([]string, 0, len(ids)) |
| 121 | + var errs []error |
| 122 | + |
| 123 | + for _, id := range iodcrIDs { |
| 124 | + _, err := cr.ec2Client.UpdateInterruptibleCapacityReservationAllocation(ctx, &ec2.UpdateInterruptibleCapacityReservationAllocationInput{ |
| 125 | + CapacityReservationId: aws.String(id), |
| 126 | + TargetInstanceCount: aws.Int32(0), |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + errs = append(errs, err) |
| 130 | + continue |
| 131 | + } |
| 132 | + cleaned = append(cleaned, id) |
| 133 | + } |
| 134 | + |
| 135 | + for _, id := range odcrIDs { |
| 136 | + _, err := cr.ec2Client.CancelCapacityReservation(ctx, &ec2.CancelCapacityReservationInput{ |
| 137 | + CapacityReservationId: aws.String(id), |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + errs = append(errs, err) |
| 141 | + continue |
| 142 | + } |
| 143 | + cleaned = append(cleaned, id) |
| 144 | + } |
| 145 | + |
| 146 | + return cleaned, multierr.Combine(errs...) |
| 147 | +} |
| 148 | + |
| 149 | +func (cr *CapacityReservation) getAllCapacityReservations(ctx context.Context, params *ec2.DescribeCapacityReservationsInput) (reservations []ec2types.CapacityReservation, err error) { |
| 150 | + paginator := ec2.NewDescribeCapacityReservationsPaginator(cr.ec2Client, params) |
| 151 | + |
| 152 | + for paginator.HasMorePages() { |
| 153 | + page, err := paginator.NextPage(ctx) |
| 154 | + if err != nil { |
| 155 | + return reservations, err |
| 156 | + } |
| 157 | + |
| 158 | + reservations = append(reservations, page.CapacityReservations...) |
| 159 | + } |
| 160 | + |
| 161 | + return reservations, nil |
| 162 | +} |
0 commit comments