Skip to content

Fix AdoptedResources functionality for CapacityReservations #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

fizashaikh
Copy link
Contributor

@fizashaikh fizashaikh commented May 20, 2025

Issue #, if available: aws-controllers-k8s/community#2485

Description of changes:
This PR adds the following hooks for CapacityReservations.

  1. post_set_resource_identifiers - This hook is needed to set additional fields required for successfully adopting existing reservation.
  2. sdk_read_many_post_set_output - This hook is required to correctly update instance count for reservations after they are adopted.
  3. sdk_update_post_build_request - This hook is needed to add support to update/modify capacity reservations using the additionalInfo reserved field.

The new AdoptedResource config looks like follows,

apiVersion: services.k8s.aws/v1alpha1
kind: AdoptedResource
metadata:
  name: test-adopt-existing-capacity   # name of the adopted resource
spec: 
  aws:
    nameOrID: cr-XXXXXXXXX     # ID of the actual capacity reservation in AWS
    additionalKeys:
      instanceCount: "1"              # replace with the actual instance count from your reservation
      availabilityZone: "us-west-2a"  # replace with your actual AZ
      instancePlatform: "Linux/UNIX"  # match the actual platform
      instanceType: "t2.nano"         # match the actual instance type
  kubernetes:
    group: ec2.services.k8s.aws
    kind: CapacityReservation
    metadata:
      name: test-adopt-capacity # name of the K8s capacity reservation resource created by ack-ec2-controller
      namespace: default

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

ack-prow bot commented May 20, 2025

Hi @fizashaikh. Thanks for your PR.

I'm waiting for a aws-controllers-k8s member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

ack-prow bot commented May 20, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fizashaikh
Once this PR has been reviewed and has the lgtm label, please assign knottnt for approval by writing /assign @knottnt in a comment. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-prow ack-prow bot requested a review from a-hilaly May 20, 2025 00:30
@ack-prow ack-prow bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 20, 2025
@ack-prow ack-prow bot requested a review from jlbutler May 20, 2025 00:30
build_date: "2025-05-26T04:21:07Z"
build_hash: b57010693b861c793703028023ae75209a6af343
go_version: go1.24.0
version: v0.43.2-26-gb570106-dirty
api_directory_checksum: 3df6921566512e42abdabd564c80aea5311d410a
Copy link
Member

Choose a reason for hiding this comment

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

We are currently on v 47.2 for code-gen

Copy link
Member

@rushmash91 rushmash91 May 27, 2025

Choose a reason for hiding this comment

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

Hi @fizashaikh , Thank you for your contribution!

I see that that the code-generator version you are using is v0.43.2 dirty, can you update it to the latest version and regenerate the controller.

Copy link
Member

@michaelhtm michaelhtm left a comment

Choose a reason for hiding this comment

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

Thank you @fizashaikh
left a few comments..
Also would you like to try out https://aws-controllers-k8s.github.io/community/docs/user-docs/features/#resourceadoption?

Comment on lines +2 to +8
if instanceCount, ok := identifier.AdditionalKeys["instanceCount"]; ok {
parsedInstanceCount, err := strconv.ParseInt(instanceCount, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse instanceCount: %v", err)
}
r.ko.Spec.InstanceCount = &parsedInstanceCount
}
Copy link
Member

Choose a reason for hiding this comment

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

Having an sdk_read_many_post_set_output.go would be enough instead of adding this one?

Comment on lines +10 to +24
if instancePlatform, ok := identifier.AdditionalKeys["instancePlatform"]; ok {
r.ko.Spec.InstancePlatform = &instancePlatform
}

if instanceType, ok := identifier.AdditionalKeys["instanceType"]; ok {
r.ko.Spec.InstanceType = &instanceType
}

if availabilityZone, ok := identifier.AdditionalKeys["availabilityZone"]; ok {
r.ko.Spec.AvailabilityZone = &availabilityZone
}

if availabilityZoneID, ok := identifier.AdditionalKeys["availabilityZoneID"]; ok {
r.ko.Spec.AvailabilityZoneID = &availabilityZoneID
}
Copy link
Member

Choose a reason for hiding this comment

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

these fields seem to already be set in sdkFind..why set them again?

Comment on lines +5 to +7
if ko.Status.TotalInstanceCount != nil {
ko.Spec.InstanceCount = ko.Status.TotalInstanceCount
}
Copy link
Member

Choose a reason for hiding this comment

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

👍

Comment on lines +614 to +619
if input.AdditionalInfo != nil {
input.InstanceCount = nil
input.EndDate = nil
input.EndDateType = ""
input.InstanceMatchCriteria = ""
}
Copy link
Member

Choose a reason for hiding this comment

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

with this change, AdditionalInfo may never be nil...would this be better?

Suggested change
if input.AdditionalInfo != nil {
input.InstanceCount = nil
input.EndDate = nil
input.EndDateType = ""
input.InstanceMatchCriteria = ""
}
if delta.DifferentAt("Spec.AdditionalInfo") {
input.InstanceCount = nil
input.EndDate = nil
input.EndDateType = ""
input.InstanceMatchCriteria = ""
} else {
input.AdditionalInfo = nil
}

Copy link
Member

Choose a reason for hiding this comment

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

This also depends on which field would we prioritize to update..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants